From 9bd9b7dd205e79577f5cd1d9488aabe0f213ce35 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 3 Mar 2014 11:54:21 -0800 Subject: [PATCH] If the kernel doesn't have MADV_MERGEABLE, stop asking for it. Note that a dynamically-linked binary will still probably see two attempts --- one by the dynamic linker (which will set its copy of the flag so it won't try again) and then one by the executable itself (which gets a new uninitialized copy of the flag). Change-Id: Id6b7e47780f0f24d2ca0384a75373f4824fa8f12 --- libc/bionic/mmap.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libc/bionic/mmap.cpp b/libc/bionic/mmap.cpp index 28a47cc9d..8f25a8941 100644 --- a/libc/bionic/mmap.cpp +++ b/libc/bionic/mmap.cpp @@ -37,16 +37,23 @@ extern "C" void* __mmap2(void*, size_t, int, int, int, size_t); #define MMAP2_SHIFT 12 // 2**12 == 4096 +static bool kernel_has_MADV_MERGEABLE = true; + void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off64_t offset) { if (offset < 0 || (offset & ((1UL << MMAP2_SHIFT)-1)) != 0) { errno = EINVAL; return MAP_FAILED; } + bool is_private_anonymous = (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) != 0; void* result = __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT); - if (result != MAP_FAILED && (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) != 0) { + + if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE && is_private_anonymous) { ErrnoRestorer errno_restorer; - madvise(result, size, MADV_MERGEABLE); + int rc = madvise(result, size, MADV_MERGEABLE); + if (rc == -1 && errno == EINVAL) { + kernel_has_MADV_MERGEABLE = false; + } } return result;