Skip to contentSkip to conversation

linux-user: add support for MADV_DONTNEED

PATCH Superseded Simon Hausmann 1 participant 1 message Aug 24, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: add support for MADV_DONTNEED

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.

---
 linux-user/mmap.c    | 18 ++++++++++++++++++
 linux-user/qemu.h    |  1 +
 linux-user/syscall.c |  6 +-----
 3 files changed, 20 insertions(+), 5 deletions(-)
linux-user/mmap.c +18 -0
index 41e0983ce8..3adb7a0f0a 100644
@@ -762,3 +762,21 @@ 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)
+{
+    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;
+}
linux-user/qemu.h +1 -0
index b4959e41c6..4b68019904 100644
@@ -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);
linux-user/syscall.c +1 -5
index 202aa777ad..023874ac8c 100644
@@ -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
Signature · 1 line
2.17.1