This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer] 64-bit allocator's PopulateFreeArray partial refactor
ClosedPublic

Authored by cryptoad on Dec 1 2017, 2:19 PM.

Details

Summary

This is an attempt at making PopulateFreeArray less obscure, more consistent,
and a tiny bit faster in some circumstances:

  • use more consistent variable names, that work both for the user & the metadata portions of the code; the purpose of the code is mostly the same for both regions, so it makes sense that the code should be mostly similar as well;
  • replace the while sum loops with a single RoundUpTo;
  • mask most of the metadata computations behind kMetadataSize, allowing some blocks to be completely optimized out if not use metadata;
  • const the constant variables;
  • add a LIKELY as the branch it applies to will almost always be taken.

Event Timeline

cryptoad created this revision.Dec 1 2017, 2:19 PM
cryptoad edited the summary of this revision. (Show Details)Dec 1 2017, 2:21 PM
alekseyshl added inline comments.Dec 1 2017, 2:56 PM
lib/sanitizer_common/sanitizer_allocator_primary64.h
732

I'd suggest to wrap the entire metadata allocating block (from // Calculate the required space for metadata) with:

if (kMetadataSize) {
  ...
}
cryptoad added inline comments.Dec 1 2017, 3:00 PM
lib/sanitizer_common/sanitizer_allocator_primary64.h
732

So I thought about it, but I was wondering about something: currently the check for exhaustion will be done whether or not we use metadata.
I wanted to leave it like that at first, because it sort of made sense.
But then I figure that if we are not using metadata, and are exhausted, then the initial mmap would fail anyway since we'd hit the free array.
It seems to be safe to place all of this within if (kMetadataSize), but I wanted to raise this to your attention and get your feedback.

alekseyshl added inline comments.Dec 1 2017, 3:33 PM
lib/sanitizer_common/sanitizer_allocator_primary64.h
732

Yep, now we will return false due to MapWithCallback failure and the OOM error handling will kick in. I think, it's just what we want.

cryptoad added inline comments.Dec 2 2017, 3:01 PM
lib/sanitizer_common/sanitizer_allocator_primary64.h
732

Tried out wrapping everything within a if (kMetadataSize).
The SizeClassAllocator64PopulateFreeListOOM test fails.
If we don't keep the exhaustion check out of the if, the map is successful, and we "overflow" into the free array space (it's empty and mappable).
So I think the current way is the way to go.

alekseyshl accepted this revision.Dec 4 2017, 9:18 AM
alekseyshl added inline comments.
lib/sanitizer_common/sanitizer_allocator_primary64.h
732

That means, when kMetadataSize == 0 and exhaustion check is true, we already stepped into the free array zone, right? We have not used any of it yet, but still, sloppy. Ok, let's take care of it in a separate patch.

This revision is now accepted and ready to land.Dec 4 2017, 9:18 AM
cryptoad updated this revision to Diff 125364.Dec 4 2017, 10:11 AM

Introduce IsRegionExhausted that checks if the region has enough room prior
to calling Mmap for the user & metadata portions of memory.
Fold all the metadata work in a if (kMetadataSize).

cryptoad requested review of this revision.Dec 4 2017, 10:11 AM
cryptoad edited edge metadata.
cryptoad marked 5 inline comments as done.
alekseyshl accepted this revision.Dec 4 2017, 10:45 AM

Great, thanks!

This revision is now accepted and ready to land.Dec 4 2017, 10:45 AM
cryptoad closed this revision.Dec 4 2017, 10:57 AM