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.
This commit is contained in:
James Ring
2024-09-02 20:03:24 -07:00
committed by GitHub
parent 0b62cc8921
commit 5cc94e59ec

View File

@ -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 */ /* memory hasn't been mapped or was mapped failed previously */
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
/* try 5 times */ /* try 5 times on EAGAIN or ENOMEM, and keep retrying on EINTR */
for (i = 0; i < 5; i++) { i = 0;
while (i < 5) {
addr = mmap(hint, request_size, map_prot, map_flags, file, 0); addr = mmap(hint, request_size, map_prot, map_flags, file, 0);
if (addr != MAP_FAILED) if (addr != MAP_FAILED)
break; break;
if (errno == EINTR)
continue;
if (errno != EAGAIN && errno != ENOMEM) {
break;
}
i++;
} }
} }
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
#if BH_ENABLE_TRACE_MMAP != 0 os_printf("mmap failed with errno: %d, hint: %p, size: %" PRIu64
os_printf("mmap failed\n"); ", prot: %d, flags: %d",
#endif errno, hint, request_size, map_prot, map_flags);
return NULL; return NULL;
} }