Commit d08eb736 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Use CommitPageSize where appropriate

{CommitPageSize()} can be smaller than {AllocatePageSize()} (on win64,
it's 4kb vs 64kb), thus use the commit size where appropriate.

R=titzer@chromium.org

Change-Id: Ic9a009158d788aa0c53e15790ea089f01ade0d0a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1605940Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61450}
parent 31712717
......@@ -447,9 +447,9 @@ Vector<byte> WasmCodeAllocator::AllocateForCode(NativeModule* native_module,
code_space = free_code_space_.Allocate(size);
DCHECK(!code_space.is_empty());
}
const Address page_size = page_allocator->AllocatePageSize();
Address commit_start = RoundUp(code_space.begin(), page_size);
Address commit_end = RoundUp(code_space.end(), page_size);
const Address commit_page_size = page_allocator->CommitPageSize();
Address commit_start = RoundUp(code_space.begin(), commit_page_size);
Address commit_end = RoundUp(code_space.end(), commit_page_size);
// {commit_start} will be either code_space.start or the start of the next
// page. {commit_end} will be the start of the page after the one in which
// the allocation ends.
......@@ -531,11 +531,11 @@ bool WasmCodeAllocator::SetExecutable(bool executable) {
return true;
}
#endif
size_t commit_page_size = page_allocator->CommitPageSize();
for (auto& region : allocated_code_space_.regions()) {
// allocated_code_space_ is fine-grained, so we need to
// page-align it.
size_t region_size =
RoundUp(region.size(), page_allocator->AllocatePageSize());
size_t region_size = RoundUp(region.size(), commit_page_size);
if (!SetPermissions(page_allocator, region.begin(), region_size,
permission)) {
return false;
......@@ -566,13 +566,15 @@ void WasmCodeAllocator::FreeCode(Vector<WasmCode* const> codes) {
// Merge {freed_regions} into {freed_code_space_} and discard full pages.
base::MutexGuard guard(&mutex_);
PageAllocator* allocator = GetPlatformPageAllocator();
size_t page_size = allocator->AllocatePageSize();
size_t commit_page_size = allocator->CommitPageSize();
for (auto region : freed_regions.regions()) {
auto merged_region = freed_code_space_.Merge(region);
Address discard_start = std::max(RoundUp(merged_region.begin(), page_size),
RoundDown(region.begin(), page_size));
Address discard_end = std::min(RoundDown(merged_region.end(), page_size),
RoundUp(region.end(), page_size));
Address discard_start =
std::max(RoundUp(merged_region.begin(), commit_page_size),
RoundDown(region.begin(), commit_page_size));
Address discard_end =
std::min(RoundDown(merged_region.end(), commit_page_size),
RoundUp(region.end(), commit_page_size));
if (discard_start >= discard_end) continue;
// TODO(clemensh): Reenable after fixing https://crbug.com/960707.
// allocator->DiscardSystemPages(reinterpret_cast<void*>(discard_start),
......@@ -1153,8 +1155,8 @@ bool WasmCodeManager::CanRegisterUnwindInfoForNonABICompliantCodeRange() const {
bool WasmCodeManager::Commit(Address start, size_t size) {
// TODO(v8:8462) Remove eager commit once perf supports remapping.
if (FLAG_perf_prof) return true;
DCHECK(IsAligned(start, AllocatePageSize()));
DCHECK(IsAligned(size, AllocatePageSize()));
DCHECK(IsAligned(start, CommitPageSize()));
DCHECK(IsAligned(size, CommitPageSize()));
// Reserve the size. Use CAS loop to avoid overflow on
// {total_committed_code_space_}.
size_t old_value = total_committed_code_space_.load();
......@@ -1193,12 +1195,12 @@ void WasmCodeManager::AssignRanges(Address start, Address end,
VirtualMemory WasmCodeManager::TryAllocate(size_t size, void* hint) {
v8::PageAllocator* page_allocator = GetPlatformPageAllocator();
DCHECK_GT(size, 0);
size = RoundUp(size, page_allocator->AllocatePageSize());
size_t allocate_page_size = page_allocator->AllocatePageSize();
size = RoundUp(size, allocate_page_size);
if (!memory_tracker_->ReserveAddressSpace(size)) return {};
if (hint == nullptr) hint = page_allocator->GetRandomMmapAddr();
VirtualMemory mem(page_allocator, size, hint,
page_allocator->AllocatePageSize());
VirtualMemory mem(page_allocator, size, hint, allocate_page_size);
if (!mem.IsReserved()) {
memory_tracker_->ReleaseReservation(size);
return {};
......@@ -1429,7 +1431,7 @@ void WasmCodeManager::FreeNativeModule(Vector<VirtualMemory> owned_code_space,
DCHECK(!code_space.IsReserved());
}
DCHECK(IsAligned(committed_size, AllocatePageSize()));
DCHECK(IsAligned(committed_size, CommitPageSize()));
size_t old_committed = total_committed_code_space_.fetch_sub(committed_size);
DCHECK_LE(committed_size, old_committed);
USE(old_committed);
......
......@@ -158,11 +158,17 @@ class WasmCodeManagerTest : public TestWithContext,
static constexpr uint32_t kNumFunctions = 10;
static constexpr uint32_t kJumpTableSize = RoundUp<kCodeAlignment>(
JumpTableAssembler::SizeForNumberOfSlots(kNumFunctions));
static size_t page_size;
static size_t allocate_page_size;
static size_t commit_page_size;
WasmCodeManagerTest() {
if (page_size == 0) page_size = AllocatePageSize();
DCHECK_NE(0, page_size);
CHECK_EQ(allocate_page_size == 0, commit_page_size == 0);
if (allocate_page_size == 0) {
allocate_page_size = AllocatePageSize();
commit_page_size = CommitPageSize();
}
CHECK_NE(0, allocate_page_size);
CHECK_NE(0, commit_page_size);
}
using NativeModulePtr = std::shared_ptr<NativeModule>;
......@@ -202,7 +208,8 @@ class WasmCodeManagerTest : public TestWithContext,
};
// static
size_t WasmCodeManagerTest::page_size = 0;
size_t WasmCodeManagerTest::allocate_page_size = 0;
size_t WasmCodeManagerTest::commit_page_size = 0;
INSTANTIATE_TEST_SUITE_P(Parameterized, WasmCodeManagerTest,
::testing::Values(Fixed, Growable),
......@@ -212,32 +219,32 @@ TEST_P(WasmCodeManagerTest, EmptyCase) {
SetMaxCommittedMemory(0);
CHECK_EQ(0, manager()->committed_code_space());
ASSERT_DEATH_IF_SUPPORTED(AllocModule(page_size, GetParam()),
ASSERT_DEATH_IF_SUPPORTED(AllocModule(allocate_page_size, GetParam()),
"OOM in wasm code commit");
}
TEST_P(WasmCodeManagerTest, AllocateAndGoOverLimit) {
SetMaxCommittedMemory(page_size);
SetMaxCommittedMemory(allocate_page_size);
DisableWin64UnwindInfoForTesting();
CHECK_EQ(0, manager()->committed_code_space());
NativeModulePtr native_module = AllocModule(page_size, GetParam());
NativeModulePtr native_module = AllocModule(allocate_page_size, GetParam());
CHECK(native_module);
CHECK_EQ(page_size, manager()->committed_code_space());
CHECK_EQ(commit_page_size, manager()->committed_code_space());
WasmCodeRefScope code_ref_scope;
uint32_t index = 0;
WasmCode* code = AddCode(native_module.get(), index++, 1 * kCodeAlignment);
CHECK_NOT_NULL(code);
CHECK_EQ(page_size, manager()->committed_code_space());
CHECK_EQ(commit_page_size, manager()->committed_code_space());
code = AddCode(native_module.get(), index++, 3 * kCodeAlignment);
CHECK_NOT_NULL(code);
CHECK_EQ(page_size, manager()->committed_code_space());
CHECK_EQ(commit_page_size, manager()->committed_code_space());
code = AddCode(native_module.get(), index++,
page_size - 4 * kCodeAlignment - kJumpTableSize);
allocate_page_size - 4 * kCodeAlignment - kJumpTableSize);
CHECK_NOT_NULL(code);
CHECK_EQ(page_size, manager()->committed_code_space());
CHECK_EQ(allocate_page_size, manager()->committed_code_space());
// This fails in "reservation" if we cannot extend the code space, or in
// "commit" it we can (since we hit the allocation limit in the
......@@ -248,27 +255,29 @@ TEST_P(WasmCodeManagerTest, AllocateAndGoOverLimit) {
}
TEST_P(WasmCodeManagerTest, TotalLimitIrrespectiveOfModuleCount) {
SetMaxCommittedMemory(3 * page_size);
SetMaxCommittedMemory(3 * allocate_page_size);
DisableWin64UnwindInfoForTesting();
NativeModulePtr nm1 = AllocModule(2 * page_size, GetParam());
NativeModulePtr nm2 = AllocModule(2 * page_size, GetParam());
NativeModulePtr nm1 = AllocModule(2 * allocate_page_size, GetParam());
NativeModulePtr nm2 = AllocModule(2 * allocate_page_size, GetParam());
CHECK(nm1);
CHECK(nm2);
WasmCodeRefScope code_ref_scope;
WasmCode* code = AddCode(nm1.get(), 0, 2 * page_size - kJumpTableSize);
WasmCode* code =
AddCode(nm1.get(), 0, 2 * allocate_page_size - kJumpTableSize);
CHECK_NOT_NULL(code);
ASSERT_DEATH_IF_SUPPORTED(
AddCode(nm2.get(), 0, 2 * page_size - kJumpTableSize),
AddCode(nm2.get(), 0, 2 * allocate_page_size - kJumpTableSize),
"OOM in wasm code commit");
}
TEST_P(WasmCodeManagerTest, GrowingVsFixedModule) {
SetMaxCommittedMemory(3 * page_size);
SetMaxCommittedMemory(3 * allocate_page_size);
DisableWin64UnwindInfoForTesting();
NativeModulePtr nm = AllocModule(page_size, GetParam());
size_t module_size = GetParam() == Fixed ? kMaxWasmCodeMemory : page_size;
NativeModulePtr nm = AllocModule(allocate_page_size, GetParam());
size_t module_size =
GetParam() == Fixed ? kMaxWasmCodeMemory : allocate_page_size;
size_t remaining_space_in_module = module_size - kJumpTableSize;
if (GetParam() == Fixed) {
// Requesting more than the remaining space fails because the module cannot
......@@ -281,33 +290,36 @@ TEST_P(WasmCodeManagerTest, GrowingVsFixedModule) {
WasmCodeRefScope code_ref_scope;
CHECK_NOT_NULL(
AddCode(nm.get(), 0, remaining_space_in_module + kCodeAlignment));
CHECK_EQ(2 * page_size, manager()->committed_code_space());
CHECK_EQ(commit_page_size + allocate_page_size,
manager()->committed_code_space());
}
}
TEST_P(WasmCodeManagerTest, CommitIncrements) {
SetMaxCommittedMemory(10 * page_size);
SetMaxCommittedMemory(10 * allocate_page_size);
DisableWin64UnwindInfoForTesting();
NativeModulePtr nm = AllocModule(3 * page_size, GetParam());
NativeModulePtr nm = AllocModule(3 * allocate_page_size, GetParam());
WasmCodeRefScope code_ref_scope;
WasmCode* code = AddCode(nm.get(), 0, kCodeAlignment);
CHECK_NOT_NULL(code);
CHECK_EQ(page_size, manager()->committed_code_space());
code = AddCode(nm.get(), 1, 2 * page_size);
CHECK_EQ(commit_page_size, manager()->committed_code_space());
code = AddCode(nm.get(), 1, 2 * allocate_page_size);
CHECK_NOT_NULL(code);
CHECK_EQ(3 * page_size, manager()->committed_code_space());
code = AddCode(nm.get(), 2, page_size - kCodeAlignment - kJumpTableSize);
CHECK_EQ(commit_page_size + 2 * allocate_page_size,
manager()->committed_code_space());
code = AddCode(nm.get(), 2,
allocate_page_size - kCodeAlignment - kJumpTableSize);
CHECK_NOT_NULL(code);
CHECK_EQ(3 * page_size, manager()->committed_code_space());
CHECK_EQ(3 * allocate_page_size, manager()->committed_code_space());
}
TEST_P(WasmCodeManagerTest, Lookup) {
SetMaxCommittedMemory(2 * page_size);
SetMaxCommittedMemory(2 * allocate_page_size);
DisableWin64UnwindInfoForTesting();
NativeModulePtr nm1 = AllocModule(page_size, GetParam());
NativeModulePtr nm2 = AllocModule(page_size, GetParam());
NativeModulePtr nm1 = AllocModule(allocate_page_size, GetParam());
NativeModulePtr nm2 = AllocModule(allocate_page_size, GetParam());
Address mid_code1_1;
{
// The {WasmCodeRefScope} needs to die before {nm1} dies.
......@@ -349,10 +361,10 @@ TEST_P(WasmCodeManagerTest, Lookup) {
}
TEST_P(WasmCodeManagerTest, LookupWorksAfterRewrite) {
SetMaxCommittedMemory(2 * page_size);
SetMaxCommittedMemory(2 * allocate_page_size);
DisableWin64UnwindInfoForTesting();
NativeModulePtr nm1 = AllocModule(page_size, GetParam());
NativeModulePtr nm1 = AllocModule(allocate_page_size, GetParam());
WasmCodeRefScope code_ref_scope;
WasmCode* code0 = AddCode(nm1.get(), 0, kCodeAlignment);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment