Reland "[heap] Tie process-wide CodeRange lifetime to any remaining Heaps"

This is a reland of 1532f8ff92

Changes since revert:
 - Fix race in initialization

Original change's description:
> [heap] Tie process-wide CodeRange lifetime to any remaining Heaps
>
> Currently the process-wide CodeRange, once created, lives until process
> shutdown. This CL changes it to be alive as long as there is a Heap,
> when the last Heap is gone it gets destroyed and will be recreated the
> next time a Heap is created. This behavior is shared with
> SingleCopyReadOnlyArtifacts.
>
> Bug: v8:11929
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2989103
> Reviewed-by: Dominik Inführ <dinfuehr@chromium.org>
> Commit-Queue: Shu-yu Guo <syg@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#75522}

Bug: v8:11929
Change-Id: If250d8901044bcba1f7d7f797b398c29cc2c5a61
Cq-Include-Trybots: luci.v8.try:v8_linux64_tsan_rel_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3003910
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Dominik Inführ <dinfuehr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#75616}
This commit is contained in:
Shu-yu Guo 2021-07-02 17:33:17 -07:00 committed by V8 LUCI CQ
parent dfa9fbc599
commit aca0c208e6
3 changed files with 24 additions and 18 deletions

View File

@ -14,8 +14,15 @@ namespace internal {
namespace {
DEFINE_LAZY_LEAKY_OBJECT_GETTER(std::shared_ptr<CodeRange>,
GetProcessWideCodeRangeCage)
// Mutex for creating process_wide_code_range_.
base::LazyMutex process_wide_code_range_creation_mutex_ =
LAZY_MUTEX_INITIALIZER;
// Weak pointer holding the process-wide CodeRange, if one has been created. All
// Heaps hold a std::shared_ptr to this, so this is destroyed when no Heaps
// remain.
base::LazyInstance<std::weak_ptr<CodeRange>>::type process_wide_code_range_ =
LAZY_INSTANCE_INITIALIZER;
DEFINE_LAZY_LEAKY_OBJECT_GETTER(CodeRangeAddressHint, GetCodeRangeAddressHint)
@ -153,19 +160,24 @@ uint8_t* CodeRange::RemapEmbeddedBuiltins(Isolate* isolate,
}
// static
void CodeRange::InitializeProcessWideCodeRangeOnce(
std::shared_ptr<CodeRange> CodeRange::EnsureProcessWideCodeRange(
v8::PageAllocator* page_allocator, size_t requested_size) {
*GetProcessWideCodeRangeCage() = std::make_shared<CodeRange>();
if (!GetProcessWideCodeRange()->InitReservation(page_allocator,
requested_size)) {
V8::FatalProcessOutOfMemory(
nullptr, "Failed to reserve virtual memory for CodeRange");
base::MutexGuard guard(process_wide_code_range_creation_mutex_.Pointer());
std::shared_ptr<CodeRange> code_range = process_wide_code_range_.Get().lock();
if (!code_range) {
code_range = std::make_shared<CodeRange>();
if (!code_range->InitReservation(page_allocator, requested_size)) {
V8::FatalProcessOutOfMemory(
nullptr, "Failed to reserve virtual memory for CodeRange");
}
*process_wide_code_range_.Pointer() = code_range;
}
return code_range;
}
// static
std::shared_ptr<CodeRange> CodeRange::GetProcessWideCodeRange() {
return *GetProcessWideCodeRangeCage();
return process_wide_code_range_.Get().lock();
}
} // namespace internal

View File

@ -120,7 +120,7 @@ class CodeRange final : public VirtualMemoryCage {
const uint8_t* embedded_blob_code,
size_t embedded_blob_code_size);
static void InitializeProcessWideCodeRangeOnce(
static std::shared_ptr<CodeRange> EnsureProcessWideCodeRange(
v8::PageAllocator* page_allocator, size_t requested_size);
// If InitializeProcessWideCodeRangeOnce has been called, returns the

View File

@ -5429,10 +5429,6 @@ HeapObject Heap::AllocateRawWithRetryOrFailSlowPath(
FatalProcessOutOfMemory("CALL_AND_RETRY_LAST");
}
namespace {
V8_DECLARE_ONCE(initialize_shared_code_range_once);
} // namespace
void Heap::SetUp() {
#ifdef V8_ENABLE_ALLOCATION_TIMEOUT
allocation_timeout_ = NextAllocationTimeout();
@ -5465,10 +5461,8 @@ void Heap::SetUp() {
// When sharing a pointer cage among Isolates, also share the
// CodeRange. isolate_->page_allocator() is the process-wide pointer
// compression cage's PageAllocator.
base::CallOnce(&initialize_shared_code_range_once,
&CodeRange::InitializeProcessWideCodeRangeOnce,
isolate_->page_allocator(), requested_size);
code_range_ = CodeRange::GetProcessWideCodeRange();
code_range_ = CodeRange::EnsureProcessWideCodeRange(
isolate_->page_allocator(), requested_size);
} else {
code_range_ = std::make_shared<CodeRange>();
if (!code_range_->InitReservation(isolate_->page_allocator(),