[compiler-rt] [scudo] Add support for multi-regions. (PR #214076)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 10 14:55:10 PDT 2026
================
@@ -445,129 +704,186 @@ void SizeClassAllocator64<Config>::initRegion(RegionInfo *Region, uptr ClassId,
template <typename Config> void SizeClassAllocator64<Config>::unmapTestOnly() {
for (uptr I = 0; I < NumClasses; I++) {
- RegionInfo *Region = getRegionInfo(I);
- {
- ScopedLock ML(Region->MMLock);
- MemMapT MemMap = Region->MemMapInfo.MemMap;
+ auto RegionInfoIter = RegionInfoManager.getRegionInfoIter(I);
+ do {
+ ScopedLock ML(RegionInfoIter->MMLock);
+ MemMapT MemMap = RegionInfoIter->MemMapInfo.MemMap;
if (MemMap.isAllocated())
- MemMap.unmap();
- }
- *Region = {};
+ MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
+ RegionInfo *OldRegion = RegionInfoIter.get();
+ ++RegionInfoIter;
+ *OldRegion = {};
+ } while (!RegionInfoIter.end());
}
}
template <typename Config>
void SizeClassAllocator64<Config>::verifyAllBlocksAreReleasedTestOnly() {
+ uptr NumRegionInfo = 0;
+ // TODO: Verify all pointers belong to the correct region.
// `BatchGroup` and `Batch` also use the blocks from BatchClass.
uptr BatchClassUsedInFreeLists = 0;
for (uptr I = 0; I < NumClasses; I++) {
// We have to count BatchClassUsedInFreeLists in other regions first.
if (I == SizeClassMap::BatchClassId)
continue;
- RegionInfo *Region = getRegionInfo(I);
- ScopedLock ML(Region->MMLock);
- ScopedFLLock FL(Region->FLLock, Region);
- const uptr BlockSize = getSizeByClassId(I);
- uptr TotalBlocks = 0;
- for (BatchGroupT &BG : Region->FreeListInfo.BlockList) {
- // `BG::Batches` are `Batches`. +1 for `BatchGroup`.
- BatchClassUsedInFreeLists += BG.Batches.size() + 1;
- for (const auto &It : BG.Batches)
- TotalBlocks += It.getCount();
+ auto RegionInfoIter = RegionInfoManager.getRegionInfoIter(I);
+ do {
+ ++NumRegionInfo;
+
+ ScopedLock ML(RegionInfoIter->MMLock);
+ ScopedFLLock FL(RegionInfoIter->FLLock, RegionInfoIter.get());
+ const uptr BlockSize = getSizeByClassId(I);
+ uptr TotalBlocks = 0;
+ for (BatchGroupT &BG : RegionInfoIter->FreeListInfo.BlockList) {
+ // `BG::Batches` are `Batches`. +1 for `BatchGroup`.
+ BatchClassUsedInFreeLists += BG.Batches.size() + 1;
+ for (const auto &It : BG.Batches)
+ TotalBlocks += It.getCount();
+ }
+
+ DCHECK_EQ(TotalBlocks,
+ RegionInfoIter->MemMapInfo.AllocatedUser / BlockSize);
+ DCHECK_EQ(RegionInfoIter->FreeListInfo.PushedBlocks,
+ RegionInfoIter->FreeListInfo.PoppedBlocks);
+
+ ++RegionInfoIter;
+ } while (!RegionInfoIter.end());
+ }
+
+ // Verify batch class blocks are correct across all regions.
+ auto RegionInfoIter =
+ RegionInfoManager.getRegionInfoIter(SizeClassMap::BatchClassId);
+ uptr TotalBatchBlocks = 0;
+ uptr AllocatedBatchBlocks = 0;
+ uptr BatchBlocksInUse = 0;
+ do {
+ ++NumRegionInfo;
+
+ ScopedLock ML(RegionInfoIter->MMLock);
+ ScopedFLLock FL(RegionInfoIter->FLLock, RegionInfoIter.get());
+ const uptr BlockSize = getSizeByClassId(SizeClassMap::BatchClassId);
+ for (BatchGroupT &BG : RegionInfoIter->FreeListInfo.BlockList) {
+ if (LIKELY(!BG.Batches.empty())) {
+ for (const auto &It : BG.Batches)
+ TotalBatchBlocks += It.getCount();
+ } else {
+ // `BatchGroup` with empty freelist doesn't have `Batch`
+ // record itself.
+ ++TotalBatchBlocks;
+ }
}
+ DCHECK_GE(RegionInfoIter->FreeListInfo.PoppedBlocks,
+ RegionInfoIter->FreeListInfo.PushedBlocks);
- DCHECK_EQ(TotalBlocks, Region->MemMapInfo.AllocatedUser / BlockSize);
- DCHECK_EQ(Region->FreeListInfo.PushedBlocks,
- Region->FreeListInfo.PoppedBlocks);
- }
+ AllocatedBatchBlocks +=
+ RegionInfoIter->MemMapInfo.AllocatedUser / BlockSize;
+ BatchBlocksInUse += RegionInfoIter->FreeListInfo.PoppedBlocks -
+ RegionInfoIter->FreeListInfo.PushedBlocks;
+ ++RegionInfoIter;
+ } while (!RegionInfoIter.end());
- RegionInfo *Region = getRegionInfo(SizeClassMap::BatchClassId);
- ScopedLock ML(Region->MMLock);
- ScopedFLLock FL(Region->FLLock, Region);
- const uptr BlockSize = getSizeByClassId(SizeClassMap::BatchClassId);
- uptr TotalBlocks = 0;
- for (BatchGroupT &BG : Region->FreeListInfo.BlockList) {
- if (LIKELY(!BG.Batches.empty())) {
- for (const auto &It : BG.Batches)
- TotalBlocks += It.getCount();
- } else {
- // `BatchGroup` with empty freelist doesn't have `Batch` record
- // itself.
- ++TotalBlocks;
- }
- }
- DCHECK_EQ(TotalBlocks + BatchClassUsedInFreeLists,
- Region->MemMapInfo.AllocatedUser / BlockSize);
- DCHECK_GE(Region->FreeListInfo.PoppedBlocks,
- Region->FreeListInfo.PushedBlocks);
- const uptr BlocksInUse =
- Region->FreeListInfo.PoppedBlocks - Region->FreeListInfo.PushedBlocks;
- DCHECK_EQ(BlocksInUse, BatchClassUsedInFreeLists);
+ DCHECK_EQ(TotalBatchBlocks + BatchClassUsedInFreeLists, AllocatedBatchBlocks);
+ DCHECK_EQ(BatchBlocksInUse, BatchClassUsedInFreeLists);
+
+ RegionInfoAllocator.verifyTheNumberOfAllocatedRegionInfo(NumRegionInfo);
}
template <typename Config>
u16 SizeClassAllocator64<Config>::popBlocks(
SizeClassAllocatorT *SizeClassAllocator, uptr ClassId, CompactPtrT *ToArray,
const u16 MaxBlockCount) {
DCHECK_LT(ClassId, NumClasses);
- RegionInfo *Region = getRegionInfo(ClassId);
- u16 PopCount = 0;
{
- ScopedFLLock FL(Region->FLLock, Region);
- PopCount = popBlocksImpl(SizeClassAllocator, ClassId, Region, ToArray,
- MaxBlockCount);
- if (PopCount != 0U)
- return PopCount;
- }
+ // Scan through all of the regions looking for regions with free blocks.
+ auto RegionInfoIter = RegionInfoManager.getRegionInfoIter(ClassId);
+ do {
+ {
+ ScopedFLLock FL(RegionInfoIter->FLLock, RegionInfoIter.get());
+ u16 PopCount =
+ popBlocksImpl(SizeClassAllocator, ClassId, RegionInfoIter.get(),
+ ToArray, MaxBlockCount);
+ if (PopCount != 0U)
+ return PopCount;
+ }
- bool ReportRegionExhausted = false;
+ ++RegionInfoIter;
+ } while (!RegionInfoIter.end());
+ }
- if constexpr (conditionVariableEnabled()) {
- PopCount = popBlocksWithCV(SizeClassAllocator, ClassId, Region, ToArray,
- MaxBlockCount, ReportRegionExhausted);
- } else {
- while (true) {
+ // Scan through regions trying to populate more allocations.
+ auto RegionInfoIter = RegionInfoManager.getRegionInfoIter(ClassId);
+ do {
+ if constexpr (conditionVariableEnabled()) {
+ bool RegionExhausted = false;
+ u16 PopCount =
+ popBlocksWithCV(SizeClassAllocator, ClassId, RegionInfoIter.get(),
+ ToArray, MaxBlockCount, &RegionExhausted);
+ if (PopCount != 0)
+ return PopCount;
+
+ if (RegionExhausted && RegionInfoIter.last()) {
+ if (Config::getEnableMultiRegions()) {
+ ScopedLock ML(RegionInfoIter->MMLock);
+ RegionInfo *Region =
+ reinterpret_cast<RegionInfo *>(populateNewRegion(ClassId));
+ if (Region == nullptr)
+ return 0;
+ // There should be no free blocks, so create new blocks.
+ return popBlocksWithCV(SizeClassAllocator, ClassId, Region, ToArray,
+ MaxBlockCount, &RegionExhausted);
+ } else {
+ return 0;
+ }
+ }
+ } else {
// When two threads compete for `Region->MMLock`, we only want one of
// them to call populateFreeListAndPopBlocks(). To avoid both of them
// doing that, always check the freelist before mapping new pages.
- ScopedLock ML(Region->MMLock);
+ ScopedLock ML(RegionInfoIter->MMLock);
----------------
ChiaHungDuan wrote:
We implicitly rely on the lowest address Region as the lock for allocating a new region. I'm wondering if using CurRegionInfo will be better so that we will be maintaining the idea of "always holding the lock of newest allocated Region to create a new region"
The approach here works as well since as long as we make the behavior consistent. I was also thinking if we should reply on other specific lock (like the one in RegionInfoManager)
https://github.com/llvm/llvm-project/pull/214076
More information about the llvm-commits
mailing list