From b94221f3b29c00321e4fe15fcdf499b7275a389c Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Fri, 8 May 2020 22:57:07 +0200 Subject: [PATCH] mmap: Use MAP_NORESERVE to request overcommit, not PROT_NONE. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts hrev54120 and instead adds the commonly supported MAP_NORESERVE flag to request overcommit. Using PROT_NONE for overcommit is problematic as the protection of individual pages can still be changed via mprotect to make them accessible, but that won't change the commitment. An application using such a pattern may then unexpectedly run into out of memory conditions on random writes into the address space. With MAP_NORESERVE the overcommit can explicitly be requested by applications that want to reserve address space without producing memory pressure. Change-Id: Id213d2245c5e23103e8e0857f7902e0cd8a2c65d Reviewed-on: https://review.haiku-os.org/c/haiku/+/2611 Reviewed-by: waddlesplash Reviewed-by: Jérôme Duval --- headers/posix/sys/mman.h | 1 + src/system/libroot/posix/sys/mman.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/headers/posix/sys/mman.h b/headers/posix/sys/mman.h index a69991ac05..40c49c7b5d 100644 --- a/headers/posix/sys/mman.h +++ b/headers/posix/sys/mman.h @@ -23,6 +23,7 @@ #define MAP_FIXED 0x04 /* require mapping to specified addr */ #define MAP_ANONYMOUS 0x08 /* no underlying object */ #define MAP_ANON MAP_ANONYMOUS +#define MAP_NORESERVE 0x10 /* don't commit memory */ /* mmap() error return code */ #define MAP_FAILED ((void*)-1) diff --git a/src/system/libroot/posix/sys/mman.cpp b/src/system/libroot/posix/sys/mman.cpp index 8de57a3dd7..3944538ef9 100644 --- a/src/system/libroot/posix/sys/mman.cpp +++ b/src/system/libroot/posix/sys/mman.cpp @@ -131,8 +131,9 @@ mmap(void* address, size_t length, int protection, int flags, int fd, areaProtection |= B_WRITE_AREA; if ((protection & PROT_EXEC) != 0) areaProtection |= B_EXECUTE_AREA; - if (protection == PROT_NONE) - areaProtection = B_OVERCOMMITTING_AREA; + + if ((flags & MAP_NORESERVE) != 0) + areaProtection |= B_OVERCOMMITTING_AREA; // create a name for this area based on calling image void* addr = __builtin_return_address(0);