Most flags to madvise() are just hints, so typically ignoring the syscall and returning okay is fine. However applications exist that do rely on MADV_DONTNEED behavior to guarantee that upon subsequent access the mapping is refreshed from the backing file or zero for anonymous mappings.
Signed-off-by: Simon Hausmann <simon.hausm...@qt.io> --- linux-user/mmap.c | 21 +++++++++++++++++++++ linux-user/qemu.h | 1 + linux-user/syscall.c | 6 +----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 41e0983ce8..6b308d4e13 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -762,3 +762,24 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, mmap_unlock(); return new_addr; } + +int target_madvise(abi_ulong start, abi_ulong len, int flags) +{ + len = HOST_PAGE_ALIGN(len); + start &= qemu_host_page_mask; + + if (!guest_range_valid(start, len)) { + errno = TARGET_EINVAL; + return -1; + } + + /* A straight passthrough may not be safe because qemu sometimes + turns private file-backed mappings into anonymous mappings. + Most flags are hints, except for MADV_DONTNEED that applications + may rely on to zero out pages, so we pass that through. + Otherwise returning success is ok. */ + if (flags & MADV_DONTNEED) { + return madvise(g2h(start), len, MADV_DONTNEED); + } + return 0; +} diff --git a/linux-user/qemu.h b/linux-user/qemu.h index b4959e41c6..4b68019904 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -437,6 +437,7 @@ int target_munmap(abi_ulong start, abi_ulong len); abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, abi_ulong new_size, unsigned long flags, abi_ulong new_addr); +int target_madvise(abi_ulong start, abi_ulong len, int flags); extern unsigned long last_brk; extern abi_ulong mmap_next_start; abi_ulong mmap_find_vma(abi_ulong, abi_ulong); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 202aa777ad..023874ac8c 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -11874,11 +11874,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, #ifdef TARGET_NR_madvise case TARGET_NR_madvise: - /* A straight passthrough may not be safe because qemu sometimes - turns private file-backed mappings into anonymous mappings. - This will break MADV_DONTNEED. - This is a hint, so ignoring and returning success is ok. */ - ret = get_errno(0); + ret = get_errno(target_madvise(arg1, arg2, arg3)); break; #endif #if TARGET_ABI_BITS == 32 -- 2.17.1