Commit a457ee76 authored by Samuel Groß's avatar Samuel Groß Committed by V8 LUCI CQ

[platform] Use MADV_DONTNEED in OS::DiscardSystemPages on Linux

This is in line with PartitionAlloc's DiscardSystemPagesInternal.

When the sandbox is enabled, OS::DiscardSystemPages is used instead of
PA's version. As such, these two implementations should ideally be
mostly identical. Using MADV_FREE instead of MADV_DONTNEED as was
previously done appears to cause some memory regressions.

Bug: chromium:1276887
Change-Id: Ied92b106e9894d428e599801d753ab4c8cffd874
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3364090Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78465}
parent 97751649
......@@ -473,6 +473,8 @@ bool OS::SetPermissions(void* address, size_t size, MemoryPermission access) {
// static
bool OS::DiscardSystemPages(void* address, size_t size) {
// Roughly based on PartitionAlloc's DiscardSystemPagesInternal
// (base/allocator/partition_allocator/page_allocator_internals_posix.h)
DCHECK_EQ(0, reinterpret_cast<uintptr_t>(address) % CommitPageSize());
DCHECK_EQ(0, size % CommitPageSize());
#if defined(V8_OS_MACOSX)
......@@ -480,23 +482,19 @@ bool OS::DiscardSystemPages(void* address, size_t size) {
// marks the pages with the reusable bit, which allows both Activity Monitor
// and memory-infra to correctly track the pages.
int ret = madvise(address, size, MADV_FREE_REUSABLE);
if (ret) {
// MADV_FREE_REUSABLE sometimes fails, so fall back to MADV_DONTNEED.
ret = madvise(address, size, MADV_DONTNEED);
}
#elif defined(_AIX) || defined(V8_OS_SOLARIS)
int ret = madvise(reinterpret_cast<caddr_t>(address), size, MADV_FREE);
#else
int ret = madvise(address, size, MADV_FREE);
#endif
if (ret != 0 && errno == ENOSYS)
return true; // madvise is not available on all systems.
if (ret != 0 && errno == EINVAL) {
// MADV_FREE only works on Linux 4.5+ . If request failed, retry with older
// MADV_DONTNEED . Note that MADV_FREE being defined at compile time doesn't
// imply runtime support.
#if defined(_AIX) || defined(V8_OS_SOLARIS)
if (ret != 0 && errno == EINVAL)
ret = madvise(reinterpret_cast<caddr_t>(address), size, MADV_DONTNEED);
#else
ret = madvise(address, size, MADV_DONTNEED);
int ret = madvise(address, size, MADV_DONTNEED);
#endif
}
return ret == 0;
}
......
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