Skip to content

Commit

Permalink
stop implicitly marking mappings as mergeable
Browse files Browse the repository at this point in the history
Signed-off-by: anupritaisno1 <www.anuprita804@gmail.com>
  • Loading branch information
thestinger committed Jun 7, 2021
1 parent 5323b39 commit 6a91d9d
Showing 1 changed file with 1 addition and 18 deletions.
19 changes: 1 addition & 18 deletions libc/bionic/mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ 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;
Expand All @@ -54,22 +52,7 @@ void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off64_t offse
return MAP_FAILED;
}

bool is_private_anonymous =
(flags & (MAP_PRIVATE | MAP_ANONYMOUS)) == (MAP_PRIVATE | MAP_ANONYMOUS);
bool is_stack_or_grows_down = (flags & (MAP_STACK | MAP_GROWSDOWN)) != 0;

void* result = __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);

if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE &&
is_private_anonymous && !is_stack_or_grows_down) {
ErrnoRestorer errno_restorer;
int rc = madvise(result, size, MADV_MERGEABLE);
if (rc == -1 && errno == EINVAL) {
kernel_has_MADV_MERGEABLE = false;
}
}

return result;
return __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);
}

void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
Expand Down

0 comments on commit 6a91d9d

Please sign in to comment.