From 5cc94e59eca97fcc30f7484a7d1abc354f7af3cd Mon Sep 17 00:00:00 2001 From: James Ring Date: Mon, 2 Sep 2024 20:03:24 -0700 Subject: [PATCH] Improve posix mmap retry logic (#3714) - Only retry on EAGAIN, ENOMEM or EINTR. - On EINTR, don't count it against the retry budget, just keep retrying. EINTR can happen in bursts. - Log the errno on failure, and don't conditionalize that logging on BH_ENABLE_TRACE_MMAP. In other parts of the code, error logging is not conditional on that define, while turning on that tracing define makes things overly verbose. --- .../shared/platform/common/posix/posix_memmap.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index c76abf13..1d972f5f 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -138,18 +138,25 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) /* memory hasn't been mapped or was mapped failed previously */ if (addr == MAP_FAILED) { - /* try 5 times */ - for (i = 0; i < 5; i++) { + /* try 5 times on EAGAIN or ENOMEM, and keep retrying on EINTR */ + i = 0; + while (i < 5) { addr = mmap(hint, request_size, map_prot, map_flags, file, 0); if (addr != MAP_FAILED) break; + if (errno == EINTR) + continue; + if (errno != EAGAIN && errno != ENOMEM) { + break; + } + i++; } } if (addr == MAP_FAILED) { -#if BH_ENABLE_TRACE_MMAP != 0 - os_printf("mmap failed\n"); -#endif + os_printf("mmap failed with errno: %d, hint: %p, size: %" PRIu64 + ", prot: %d, flags: %d", + errno, hint, request_size, map_prot, map_flags); return NULL; }