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;