release.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. //===-- release.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_RELEASE_H_
  9. #define SCUDO_RELEASE_H_
  10. #include "common.h"
  11. #include "list.h"
  12. #include "mutex.h"
  13. namespace scudo {
  14. class ReleaseRecorder {
  15. public:
  16. ReleaseRecorder(uptr Base, MapPlatformData *Data = nullptr)
  17. : Base(Base), Data(Data) {}
  18. uptr getReleasedRangesCount() const { return ReleasedRangesCount; }
  19. uptr getReleasedBytes() const { return ReleasedBytes; }
  20. uptr getBase() const { return Base; }
  21. // Releases [From, To) range of pages back to OS.
  22. void releasePageRangeToOS(uptr From, uptr To) {
  23. const uptr Size = To - From;
  24. releasePagesToOS(Base, From, Size, Data);
  25. ReleasedRangesCount++;
  26. ReleasedBytes += Size;
  27. }
  28. private:
  29. uptr ReleasedRangesCount = 0;
  30. uptr ReleasedBytes = 0;
  31. uptr Base = 0;
  32. MapPlatformData *Data = nullptr;
  33. };
  34. // A Region page map is used to record the usage of pages in the regions. It
  35. // implements a packed array of Counters. Each counter occupies 2^N bits, enough
  36. // to store counter's MaxValue. Ctor will try to use a static buffer first, and
  37. // if that fails (the buffer is too small or already locked), will allocate the
  38. // required Buffer via map(). The caller is expected to check whether the
  39. // initialization was successful by checking isAllocated() result. For
  40. // performance sake, none of the accessors check the validity of the arguments,
  41. // It is assumed that Index is always in [0, N) range and the value is not
  42. // incremented past MaxValue.
  43. class RegionPageMap {
  44. public:
  45. RegionPageMap()
  46. : Regions(0),
  47. NumCounters(0),
  48. CounterSizeBitsLog(0),
  49. CounterMask(0),
  50. PackingRatioLog(0),
  51. BitOffsetMask(0),
  52. SizePerRegion(0),
  53. BufferSize(0),
  54. Buffer(nullptr) {}
  55. RegionPageMap(uptr NumberOfRegions, uptr CountersPerRegion, uptr MaxValue) {
  56. reset(NumberOfRegions, CountersPerRegion, MaxValue);
  57. }
  58. ~RegionPageMap() {
  59. if (!isAllocated())
  60. return;
  61. if (Buffer == &StaticBuffer[0])
  62. Mutex.unlock();
  63. else
  64. unmap(reinterpret_cast<void *>(Buffer),
  65. roundUpTo(BufferSize, getPageSizeCached()));
  66. Buffer = nullptr;
  67. }
  68. void reset(uptr NumberOfRegion, uptr CountersPerRegion, uptr MaxValue) {
  69. DCHECK_GT(NumberOfRegion, 0);
  70. DCHECK_GT(CountersPerRegion, 0);
  71. DCHECK_GT(MaxValue, 0);
  72. Regions = NumberOfRegion;
  73. NumCounters = CountersPerRegion;
  74. constexpr uptr MaxCounterBits = sizeof(*Buffer) * 8UL;
  75. // Rounding counter storage size up to the power of two allows for using
  76. // bit shifts calculating particular counter's Index and offset.
  77. const uptr CounterSizeBits =
  78. roundUpToPowerOfTwo(getMostSignificantSetBitIndex(MaxValue) + 1);
  79. DCHECK_LE(CounterSizeBits, MaxCounterBits);
  80. CounterSizeBitsLog = getLog2(CounterSizeBits);
  81. CounterMask = ~(static_cast<uptr>(0)) >> (MaxCounterBits - CounterSizeBits);
  82. const uptr PackingRatio = MaxCounterBits >> CounterSizeBitsLog;
  83. DCHECK_GT(PackingRatio, 0);
  84. PackingRatioLog = getLog2(PackingRatio);
  85. BitOffsetMask = PackingRatio - 1;
  86. SizePerRegion =
  87. roundUpTo(NumCounters, static_cast<uptr>(1U) << PackingRatioLog) >>
  88. PackingRatioLog;
  89. BufferSize = SizePerRegion * sizeof(*Buffer) * Regions;
  90. if (BufferSize <= (StaticBufferCount * sizeof(Buffer[0])) &&
  91. Mutex.tryLock()) {
  92. Buffer = &StaticBuffer[0];
  93. memset(Buffer, 0, BufferSize);
  94. } else {
  95. // When using a heap-based buffer, precommit the pages backing the
  96. // Vmar by passing |MAP_PRECOMMIT| flag. This allows an optimization
  97. // where page fault exceptions are skipped as the allocated memory
  98. // is accessed.
  99. const uptr MmapFlags =
  100. MAP_ALLOWNOMEM | (SCUDO_FUCHSIA ? MAP_PRECOMMIT : 0);
  101. Buffer = reinterpret_cast<uptr *>(
  102. map(nullptr, roundUpTo(BufferSize, getPageSizeCached()),
  103. "scudo:counters", MmapFlags, &MapData));
  104. }
  105. }
  106. bool isAllocated() const { return !!Buffer; }
  107. uptr getCount() const { return NumCounters; }
  108. uptr get(uptr Region, uptr I) const {
  109. DCHECK_LT(Region, Regions);
  110. DCHECK_LT(I, NumCounters);
  111. const uptr Index = I >> PackingRatioLog;
  112. const uptr BitOffset = (I & BitOffsetMask) << CounterSizeBitsLog;
  113. return (Buffer[Region * SizePerRegion + Index] >> BitOffset) & CounterMask;
  114. }
  115. void inc(uptr Region, uptr I) const {
  116. DCHECK_LT(get(Region, I), CounterMask);
  117. const uptr Index = I >> PackingRatioLog;
  118. const uptr BitOffset = (I & BitOffsetMask) << CounterSizeBitsLog;
  119. DCHECK_LT(BitOffset, SCUDO_WORDSIZE);
  120. DCHECK_EQ(isAllCounted(Region, I), false);
  121. Buffer[Region * SizePerRegion + Index] += static_cast<uptr>(1U)
  122. << BitOffset;
  123. }
  124. void incRange(uptr Region, uptr From, uptr To) const {
  125. DCHECK_LE(From, To);
  126. const uptr Top = Min(To + 1, NumCounters);
  127. for (uptr I = From; I < Top; I++)
  128. inc(Region, I);
  129. }
  130. // Set the counter to the max value. Note that the max number of blocks in a
  131. // page may vary. To provide an easier way to tell if all the blocks are
  132. // counted for different pages, set to the same max value to denote the
  133. // all-counted status.
  134. void setAsAllCounted(uptr Region, uptr I) const {
  135. DCHECK_LE(get(Region, I), CounterMask);
  136. const uptr Index = I >> PackingRatioLog;
  137. const uptr BitOffset = (I & BitOffsetMask) << CounterSizeBitsLog;
  138. DCHECK_LT(BitOffset, SCUDO_WORDSIZE);
  139. Buffer[Region * SizePerRegion + Index] |= CounterMask << BitOffset;
  140. }
  141. bool isAllCounted(uptr Region, uptr I) const {
  142. return get(Region, I) == CounterMask;
  143. }
  144. uptr getBufferSize() const { return BufferSize; }
  145. static const uptr StaticBufferCount = 2048U;
  146. private:
  147. uptr Regions;
  148. uptr NumCounters;
  149. uptr CounterSizeBitsLog;
  150. uptr CounterMask;
  151. uptr PackingRatioLog;
  152. uptr BitOffsetMask;
  153. uptr SizePerRegion;
  154. uptr BufferSize;
  155. uptr *Buffer;
  156. [[no_unique_address]] MapPlatformData MapData = {};
  157. static HybridMutex Mutex;
  158. static uptr StaticBuffer[StaticBufferCount];
  159. };
  160. template <class ReleaseRecorderT> class FreePagesRangeTracker {
  161. public:
  162. explicit FreePagesRangeTracker(ReleaseRecorderT &Recorder)
  163. : Recorder(Recorder), PageSizeLog(getLog2(getPageSizeCached())) {}
  164. void processNextPage(bool Released) {
  165. if (Released) {
  166. if (!InRange) {
  167. CurrentRangeStatePage = CurrentPage;
  168. InRange = true;
  169. }
  170. } else {
  171. closeOpenedRange();
  172. }
  173. CurrentPage++;
  174. }
  175. void skipPages(uptr N) {
  176. closeOpenedRange();
  177. CurrentPage += N;
  178. }
  179. void finish() { closeOpenedRange(); }
  180. private:
  181. void closeOpenedRange() {
  182. if (InRange) {
  183. Recorder.releasePageRangeToOS((CurrentRangeStatePage << PageSizeLog),
  184. (CurrentPage << PageSizeLog));
  185. InRange = false;
  186. }
  187. }
  188. ReleaseRecorderT &Recorder;
  189. const uptr PageSizeLog;
  190. bool InRange = false;
  191. uptr CurrentPage = 0;
  192. uptr CurrentRangeStatePage = 0;
  193. };
  194. struct PageReleaseContext {
  195. PageReleaseContext(uptr BlockSize, uptr RegionSize, uptr NumberOfRegions) :
  196. BlockSize(BlockSize),
  197. RegionSize(RegionSize),
  198. NumberOfRegions(NumberOfRegions) {
  199. PageSize = getPageSizeCached();
  200. if (BlockSize <= PageSize) {
  201. if (PageSize % BlockSize == 0) {
  202. // Same number of chunks per page, no cross overs.
  203. FullPagesBlockCountMax = PageSize / BlockSize;
  204. SameBlockCountPerPage = true;
  205. } else if (BlockSize % (PageSize % BlockSize) == 0) {
  206. // Some chunks are crossing page boundaries, which means that the page
  207. // contains one or two partial chunks, but all pages contain the same
  208. // number of chunks.
  209. FullPagesBlockCountMax = PageSize / BlockSize + 1;
  210. SameBlockCountPerPage = true;
  211. } else {
  212. // Some chunks are crossing page boundaries, which means that the page
  213. // contains one or two partial chunks.
  214. FullPagesBlockCountMax = PageSize / BlockSize + 2;
  215. SameBlockCountPerPage = false;
  216. }
  217. } else {
  218. if (BlockSize % PageSize == 0) {
  219. // One chunk covers multiple pages, no cross overs.
  220. FullPagesBlockCountMax = 1;
  221. SameBlockCountPerPage = true;
  222. } else {
  223. // One chunk covers multiple pages, Some chunks are crossing page
  224. // boundaries. Some pages contain one chunk, some contain two.
  225. FullPagesBlockCountMax = 2;
  226. SameBlockCountPerPage = false;
  227. }
  228. }
  229. PagesCount = roundUpTo(RegionSize, PageSize) / PageSize;
  230. PageSizeLog = getLog2(PageSize);
  231. RoundedRegionSize = PagesCount << PageSizeLog;
  232. RoundedSize = NumberOfRegions * RoundedRegionSize;
  233. }
  234. // PageMap is lazily allocated when markFreeBlocks() is invoked.
  235. bool hasBlockMarked() const {
  236. return PageMap.isAllocated();
  237. }
  238. void ensurePageMapAllocated() {
  239. if (PageMap.isAllocated())
  240. return;
  241. PageMap.reset(NumberOfRegions, PagesCount, FullPagesBlockCountMax);
  242. DCHECK(PageMap.isAllocated());
  243. }
  244. template<class TransferBatchT, typename DecompactPtrT>
  245. void markFreeBlocks(const IntrusiveList<TransferBatchT> &FreeList,
  246. DecompactPtrT DecompactPtr, uptr Base) {
  247. ensurePageMapAllocated();
  248. // Iterate over free chunks and count how many free chunks affect each
  249. // allocated page.
  250. if (BlockSize <= PageSize && PageSize % BlockSize == 0) {
  251. // Each chunk affects one page only.
  252. for (const auto &It : FreeList) {
  253. for (u16 I = 0; I < It.getCount(); I++) {
  254. const uptr P = DecompactPtr(It.get(I)) - Base;
  255. if (P >= RoundedSize)
  256. continue;
  257. const uptr RegionIndex = NumberOfRegions == 1U ? 0 : P / RegionSize;
  258. const uptr PInRegion = P - RegionIndex * RegionSize;
  259. PageMap.inc(RegionIndex, PInRegion >> PageSizeLog);
  260. }
  261. }
  262. } else {
  263. // In all other cases chunks might affect more than one page.
  264. DCHECK_GE(RegionSize, BlockSize);
  265. const uptr LastBlockInRegion =
  266. ((RegionSize / BlockSize) - 1U) * BlockSize;
  267. for (const auto &It : FreeList) {
  268. for (u16 I = 0; I < It.getCount(); I++) {
  269. const uptr P = DecompactPtr(It.get(I)) - Base;
  270. if (P >= RoundedSize)
  271. continue;
  272. const uptr RegionIndex = NumberOfRegions == 1U ? 0 : P / RegionSize;
  273. uptr PInRegion = P - RegionIndex * RegionSize;
  274. PageMap.incRange(RegionIndex, PInRegion >> PageSizeLog,
  275. (PInRegion + BlockSize - 1) >> PageSizeLog);
  276. // The last block in a region might straddle a page, so if it's
  277. // free, we mark the following "pretend" memory block(s) as free.
  278. if (PInRegion == LastBlockInRegion) {
  279. PInRegion += BlockSize;
  280. while (PInRegion < RoundedRegionSize) {
  281. PageMap.incRange(RegionIndex, PInRegion >> PageSizeLog,
  282. (PInRegion + BlockSize - 1) >> PageSizeLog);
  283. PInRegion += BlockSize;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. uptr BlockSize;
  291. uptr RegionSize;
  292. uptr NumberOfRegions;
  293. uptr PageSize;
  294. uptr PagesCount;
  295. uptr PageSizeLog;
  296. uptr RoundedRegionSize;
  297. uptr RoundedSize;
  298. uptr FullPagesBlockCountMax;
  299. bool SameBlockCountPerPage;
  300. RegionPageMap PageMap;
  301. };
  302. // Try to release the page which doesn't have any in-used block, i.e., they are
  303. // all free blocks. The `PageMap` will record the number of free blocks in each
  304. // page.
  305. template <class ReleaseRecorderT, typename SkipRegionT>
  306. NOINLINE void
  307. releaseFreeMemoryToOS(PageReleaseContext &Context,
  308. ReleaseRecorderT &Recorder, SkipRegionT SkipRegion) {
  309. const uptr PageSize = Context.PageSize;
  310. const uptr BlockSize = Context.BlockSize;
  311. const uptr PagesCount = Context.PagesCount;
  312. const uptr NumberOfRegions = Context.NumberOfRegions;
  313. const uptr FullPagesBlockCountMax = Context.FullPagesBlockCountMax;
  314. const bool SameBlockCountPerPage = Context.SameBlockCountPerPage;
  315. RegionPageMap &PageMap = Context.PageMap;
  316. // Iterate over pages detecting ranges of pages with chunk Counters equal
  317. // to the expected number of chunks for the particular page.
  318. FreePagesRangeTracker<ReleaseRecorderT> RangeTracker(Recorder);
  319. if (SameBlockCountPerPage) {
  320. // Fast path, every page has the same number of chunks affecting it.
  321. for (uptr I = 0; I < NumberOfRegions; I++) {
  322. if (SkipRegion(I)) {
  323. RangeTracker.skipPages(PagesCount);
  324. continue;
  325. }
  326. for (uptr J = 0; J < PagesCount; J++) {
  327. const bool CanRelease = PageMap.get(I, J) == FullPagesBlockCountMax;
  328. if (CanRelease)
  329. PageMap.setAsAllCounted(I, J);
  330. RangeTracker.processNextPage(CanRelease);
  331. }
  332. }
  333. } else {
  334. // Slow path, go through the pages keeping count how many chunks affect
  335. // each page.
  336. const uptr Pn = BlockSize < PageSize ? PageSize / BlockSize : 1;
  337. const uptr Pnc = Pn * BlockSize;
  338. // The idea is to increment the current page pointer by the first chunk
  339. // size, middle portion size (the portion of the page covered by chunks
  340. // except the first and the last one) and then the last chunk size, adding
  341. // up the number of chunks on the current page and checking on every step
  342. // whether the page boundary was crossed.
  343. for (uptr I = 0; I < NumberOfRegions; I++) {
  344. if (SkipRegion(I)) {
  345. RangeTracker.skipPages(PagesCount);
  346. continue;
  347. }
  348. uptr PrevPageBoundary = 0;
  349. uptr CurrentBoundary = 0;
  350. for (uptr J = 0; J < PagesCount; J++) {
  351. const uptr PageBoundary = PrevPageBoundary + PageSize;
  352. uptr BlocksPerPage = Pn;
  353. if (CurrentBoundary < PageBoundary) {
  354. if (CurrentBoundary > PrevPageBoundary)
  355. BlocksPerPage++;
  356. CurrentBoundary += Pnc;
  357. if (CurrentBoundary < PageBoundary) {
  358. BlocksPerPage++;
  359. CurrentBoundary += BlockSize;
  360. }
  361. }
  362. PrevPageBoundary = PageBoundary;
  363. const bool CanRelease = PageMap.get(I, J) == BlocksPerPage;
  364. if (CanRelease)
  365. PageMap.setAsAllCounted(I, J);
  366. RangeTracker.processNextPage(CanRelease);
  367. }
  368. }
  369. }
  370. RangeTracker.finish();
  371. }
  372. // An overload releaseFreeMemoryToOS which doesn't require the page usage
  373. // information after releasing.
  374. template <class TransferBatchT, class ReleaseRecorderT, typename DecompactPtrT,
  375. typename SkipRegionT>
  376. NOINLINE void
  377. releaseFreeMemoryToOS(const IntrusiveList<TransferBatchT> &FreeList,
  378. uptr RegionSize, uptr NumberOfRegions, uptr BlockSize,
  379. ReleaseRecorderT &Recorder, DecompactPtrT DecompactPtr,
  380. SkipRegionT SkipRegion) {
  381. PageReleaseContext Context(BlockSize, RegionSize, NumberOfRegions);
  382. Context.markFreeBlocks(FreeList, DecompactPtr, Recorder.getBase());
  383. releaseFreeMemoryToOS(Context, Recorder, SkipRegion);
  384. }
  385. } // namespace scudo
  386. #endif // SCUDO_RELEASE_H_