[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
================
@@ -233,13 +267,212 @@ template <typename Config> class SizeClassAllocator64 {
using ScopedFLLock = ScopedFLLockBase<conditionVariableEnabled()>;
- RegionInfo *getRegionInfo(uptr ClassId) {
- DCHECK_LT(ClassId, NumClasses);
- return &RegionInfoArray[ClassId];
- }
+ // gcc cannot compile a template <> value, so add a Dummy type.
+ template <bool IsMultiRegions = false, typename Dummy = void>
+ struct RegionInfoAlloc {
+ RegionInfo *allocate() {
+ UNREACHABLE("RegionInfo is statically allocated");
+ }
+
+ void verifyTheNumberOfAllocatedRegionInfo(uptr NumRegionInfo) {
+ DCHECK_EQ(NumRegionInfo, NumClasses);
+ }
+ };
+
+ // gcc cannot compile a template <> value, so add a Dummy type.
+ template <typename Dummy>
+ struct RegionInfoAlloc</*isMultiRegions=*/true, Dummy> {
+ RegionInfo *allocate() {
+ ScopedLock L(M);
+ return S.pop();
+ }
+
+ void verifyTheNumberOfAllocatedRegionInfo(uptr NumRegionInfo) {
+ ScopedLock L(M);
+ DCHECK_EQ(NumRegionInfo, S.Size);
+ }
+
+ HybridMutex M;
+ // According to the following,
+ // DR1351: If the brace-or-equal-initializer of a non-static data
+ // member invokes a defaulted default constructor of its class or of an
+ // enclosing class in a potentially evaluated subexpression, the program
+ // is ill-formed.
+ // So we have to `outline` the `Size`/`Array` into another struct `Storage`.
+ struct Storage {
+ RegionInfo *pop() {
+ if (Size == NumEntries)
+ return nullptr;
+ return &Array[Size++];
+ }
+ // The amount of memory used by this allocator is about (NumEntries *
+ // RegionSize). For example, a 256 KB region will have 2GB space
+ // available.
+ // TODO(cferris): Make this configurablge.
+ static constexpr uptr NumEntries = 1UL << 13;
+ uptr Size = 0;
+ alignas(SCUDO_CACHE_LINE_SIZE) RegionInfo Array[NumEntries];
+ } S GUARDED_BY(M);
+ };
+
+ // gcc cannot compile a template <> value, so add a Dummy type.
+ template <bool IsMultiRegions = false, typename Dummy = void>
+ struct RegionInfoInterface {
+ struct RegionInfoIter {
+ RegionInfoIter(RegionInfo *Region) : CurRegionInfo(Region) {}
+ RegionInfo *operator->() { return CurRegionInfo; }
+ RegionInfoIter &operator++() {
+ CurRegionInfo = nullptr;
+ return *this;
+ }
+ bool last() { return true; }
+ RegionInfo *get() { return CurRegionInfo; }
+ bool end() { return CurRegionInfo == nullptr; }
+ RegionInfo *CurRegionInfo = nullptr;
+ };
+
+ void init(UNUSED RegionInfoAlloc<IsMultiRegions> &Allocator) {
+ // The RegionInfo storage is statically initialized.
+ }
+
+ ALWAYS_INLINE RegionInfo *getCurRegionInfo(uptr ClassId) {
+ DCHECK_LT(ClassId, NumClasses);
+ return &RegionInfoArray[ClassId];
+ }
+ ALWAYS_INLINE RegionInfoIter getRegionInfoIter(uptr ClassId) {
+ return RegionInfoIter(getCurRegionInfo(ClassId));
+ }
+
+ void pushRegionInfo(UNUSED RegionInfo *Region, UNUSED uptr ClassId) {
+ UNREACHABLE("Only MultiRegions supports this operation\n");
+ }
+ void shuffle(u32 *Seed) {
+ scudo::shuffle(RegionInfoArray, NumClasses, Seed);
+ }
+
+ alignas(SCUDO_CACHE_LINE_SIZE) RegionInfo RegionInfoArray[NumClasses];
+ };
+
+ // gcc cannot compile a template <> value, so add a Dummy type.
+ template <typename Dummy>
+ struct RegionInfoInterface</*isMultiRegions=*/true, Dummy> {
+ struct RegionInfoIter {
+ RegionInfoIter(RegionInfo *Region, HybridMutex &RegionInfoListLock)
+ : CurRegionInfo(Region), M(RegionInfoListLock) {}
+ RegionInfo *operator->() { return CurRegionInfo; }
+ RegionInfoIter &operator++() {
+ ScopedLock L(M);
+ CurRegionInfo = CurRegionInfo->Next;
+ return *this;
+ }
+ bool last() {
+ ScopedLock L(M);
+ return CurRegionInfo->Next == nullptr;
+ }
+ RegionInfo *get() { return CurRegionInfo; }
+ bool end() { return CurRegionInfo == nullptr; }
+ RegionInfo *CurRegionInfo = nullptr;
+ HybridMutex &M;
+ };
+
+ void init(RegionInfoAlloc</*isMultiRegions=*/true> &Allocator) {
+ for (uptr I = 0; I < NumClasses; I++) {
+ RegionInfo *Region = Allocator.allocate();
+ LowestAddrRegionInfo[I].P = Region;
+ CurrentRegionInfo[I].P = Region;
+ }
+ }
+
+ // Return the last pushed RegionInfo. For one size class, the current
+ // RegionInfo is responsible for the page mapping and the other RegionInfos
+ // will have been exhausted already.
+ ALWAYS_INLINE RegionInfo *getCurRegionInfo(uptr ClassId) {
+ DCHECK_LT(ClassId, NumClasses);
+ return CurrentRegionInfo[ClassId].P;
+ }
+
+ ALWAYS_INLINE RegionInfoIter getRegionInfoIter(uptr ClassId) {
+ return RegionInfoIter(LowestAddrRegionInfo[ClassId].P,
+ RegionInfoLock[ClassId]);
+ }
+
+ // RegionInfos for the same size class will be ordered by base address.
+ // Every RegionInfo will start from lowest address and aligns with how
+ // pointer grouping works.
+ void pushRegionInfo(RegionInfo *Region, uptr ClassId)
+ REQUIRES(Region->MMLock) {
+ DCHECK_LT(ClassId, NumClasses);
+ DCHECK(Region->MemMapInfo.MemMap.isAllocated());
+
+ RegionInfo *RegionCursor = LowestAddrRegionInfo[ClassId].P;
+ DCHECK_NE(RegionCursor, nullptr);
+
+ ScopedLock L(RegionInfoLock[ClassId]);
----------------
ChiaHungDuan wrote:
I'm thinking if we want to use tryLock and assert it should always success. In our case, I think we want to make sure only one thread will allocate a new region and the others will wait on some other `Region->MMLock`
https://github.com/llvm/llvm-project/pull/214076
More information about the llvm-commits
mailing list