Skip to contentSkip to conversation

linux-user: add support for MADV_DONTNEED

PATCH In review Simon Hausmann 2 participants 4 messages Aug 27, 2018 – Sep 6, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v3 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. The file backed mappings this is tricky - hence the original comment - but for anonymous mappings it is safe to forward. We just need to remember which those mappings are, which is now stored in the flags.

Cc Riku Voipio <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Sami Nurmenniemi <[email protected]>

--
v2:
- align start and len on host page size
v3:
- align start and len to target page size as it may be bigger than host
- use immediate return in do_syscall
- forward MADV_DONTNEED advice only for anonymously mapped pages
- remember which mappings are anonymous in the page flags and preserve those bits across mprotect calls.

---
 accel/tcg/translate-all.c |  8 +++++--
 bsd-user/mmap.c           |  8 +++----
 include/exec/cpu-all.h    | 11 ++++++++-
 linux-user/elfload.c      |  3 ++-
 linux-user/mmap.c         | 49 +++++++++++++++++++++++++++++++++++----
 linux-user/qemu.h         |  1 +
 linux-user/syscall.c      | 12 ++++------
 7 files changed, 72 insertions(+), 20 deletions(-)
accel/tcg/translate-all.c +6 -2
index 898c3bb3d1..467fbd9aeb 100644
@@ -2481,7 +2481,8 @@ int page_get_flags(target_ulong address)
 /* Modify the flags of a page and invalidate the code if necessary.
    The flag PAGE_WRITE_ORG is positioned automatically depending
    on PAGE_WRITE.  The mmap_lock should already be held.  */
-void page_set_flags(target_ulong start, target_ulong end, int flags)
+void page_set_flags(target_ulong start, target_ulong end, int flags,
+                    enum page_set_flags_mode mode)
 {
     target_ulong addr, len;
 
@@ -2513,7 +2514,10 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
             p->first_tb) {
             tb_invalidate_phys_page(addr, 0);
         }
-        p->flags = flags;
+        if (mode == PAGE_SET_ALL_FLAGS)
+            p->flags = flags;
+        else /* PAGE_SET_PROTECTION */
+            p->flags |= (p->flags & ~(PAGE_BITS - 1)) | (flags & PAGE_BITS);
     }
 }
 
bsd-user/mmap.c +4 -4
index 17f4cd80aa..4bfac81af4 100644
@@ -125,7 +125,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
         if (ret != 0)
             goto error;
     }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);
     mmap_unlock();
     return 0;
 error:
@@ -214,7 +214,7 @@ static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
            to mmap.  */
         page_set_flags(last_brk & TARGET_PAGE_MASK,
                        TARGET_PAGE_ALIGN(new_brk),
-                       PAGE_RESERVED);
+                       PAGE_RESERVED, PAGE_SET_ALL_FLAGS);
     }
     last_brk = new_brk;
 
@@ -397,7 +397,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
         }
     }
  the_end1:
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot | PAGE_VALID, PAGE_SET_ALL_FLAGS);
  the_end:
 #ifdef DEBUG_MMAP
     printf("ret=0x" TARGET_FMT_lx "\n", start);
@@ -460,7 +460,7 @@ int target_munmap(abi_ulong start, abi_ulong len)
     }
 
     if (ret == 0)
-        page_set_flags(start, start + len, 0);
+        page_set_flags(start, start + len, 0, PAGE_SET_ALL_FLAGS);
     mmap_unlock();
     return ret;
 }
include/exec/cpu-all.h +10 -1
index 117d2fbbca..100388dcd4 100644
@@ -249,6 +249,9 @@ extern intptr_t qemu_host_page_mask;
 /* FIXME: Code that sets/uses this is broken and needs to go away.  */
 #define PAGE_RESERVED  0x0020
 #endif
+#if defined(CONFIG_LINUX) && defined(CONFIG_USER_ONLY)
+#define PAGE_MAP_ANONYMOUS 0x0080
+#endif
 
 #if defined(CONFIG_USER_ONLY)
 void page_dump(FILE *f);
@@ -257,8 +260,14 @@ typedef int (*walk_memory_regions_fn)(void *, target_ulong,
                                       target_ulong, unsigned long);
 int walk_memory_regions(void *, walk_memory_regions_fn);
 
+enum page_set_flags_mode {
+    PAGE_SET_ALL_FLAGS,
+    PAGE_SET_PROTECTION
+};
+
 int page_get_flags(target_ulong address);
-void page_set_flags(target_ulong start, target_ulong end, int flags);
+void page_set_flags(target_ulong start, target_ulong end, int flags,
+                    enum page_set_flags_mode);
 int page_check_range(target_ulong start, target_ulong len, int flags);
 #endif
 
linux-user/elfload.c +2 -1
index 8638612aec..c59cf4359c 100644
@@ -1702,7 +1702,8 @@ static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
 
     /* Ensure that the bss page(s) are valid */
     if ((page_get_flags(last_bss-1) & prot) != prot) {
-        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
+        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot,
+                       PAGE_SET_PROTECTION);
     }
 
     if (host_start < host_map_start) {
linux-user/mmap.c +44 -5

81 lines not on this page · raw message

linux-user/qemu.h +1 -0
index b4959e41c6..e5ac5e9c6a 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 advice);
 extern unsigned long last_brk;
 extern abi_ulong mmap_next_start;
 abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
linux-user/syscall.c +5 -7
index 850b72a0c7..4478de5fc8 100644
@@ -5124,7 +5124,8 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_env,
 
     page_set_flags(raddr, raddr + shm_info.shm_segsz,
                    PAGE_VALID | PAGE_READ |
-                   ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
+                   ((shmflg & SHM_RDONLY) ? 0 : PAGE_WRITE),
+                   PAGE_SET_ALL_FLAGS);
 
     for (i = 0; i < N_SHM_REGIONS; i++) {
         if (!shm_regions[i].in_use) {
@@ -5150,7 +5151,8 @@ static inline abi_long do_shmdt(abi_ulong shmaddr)
     for (i = 0; i < N_SHM_REGIONS; ++i) {
         if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
             shm_regions[i].in_use = false;
-            page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
+            page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0,
+                           PAGE_SET_ALL_FLAGS);
             break;
         }
     }
@@ -11559,11 +11561,7 @@ static abi_long do_syscall1(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.  */
-        return 0;
+        return get_errno(target_madvise(arg1, arg2, arg3));
 #endif
 #if TARGET_ABI_BITS == 32
     case TARGET_NR_fcntl64:
Signature · 1 line
2.17.1
#1 lore raw

Ping :)

Quoted 282 lines · Simon Hausmann

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. The file backed mappings this is tricky - hence the original comment - but for anonymous mappings it is safe to forward. We just need to remember which those mappings are, which is now stored in the flags.

Cc Riku Voipio <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Sami Nurmenniemi <[email protected]>

--
v2:
- align start and len on host page size
v3:
- align start and len to target page size as it may be bigger than host
- use immediate return in do_syscall
- forward MADV_DONTNEED advice only for anonymously mapped pages
- remember which mappings are anonymous in the page flags and preserve those bits across mprotect calls.

---
  accel/tcg/translate-all.c |  8 +++++--
  bsd-user/mmap.c           |  8 +++----
  include/exec/cpu-all.h    | 11 ++++++++-
  linux-user/elfload.c      |  3 ++-
  linux-user/mmap.c         | 49 +++++++++++++++++++++++++++++++++++----
  linux-user/qemu.h         |  1 +
  linux-user/syscall.c      | 12 ++++------
  7 files changed, 72 insertions(+), 20 deletions(-)
accel/tcg/translate-all.c +6 -2
index 898c3bb3d1..467fbd9aeb 100644
@@ -2481,7 +2481,8 @@ int page_get_flags(target_ulong address)
  /* Modify the flags of a page and invalidate the code if necessary.
     The flag PAGE_WRITE_ORG is positioned automatically depending
     on PAGE_WRITE.  The mmap_lock should already be held.  */
-void page_set_flags(target_ulong start, target_ulong end, int flags)
+void page_set_flags(target_ulong start, target_ulong end, int flags,
+                    enum page_set_flags_mode mode)
  {
      target_ulong addr, len;
 
@@ -2513,7 +2514,10 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
              p->first_tb) {
              tb_invalidate_phys_page(addr, 0);
          }
-        p->flags = flags;
+        if (mode == PAGE_SET_ALL_FLAGS)
+            p->flags = flags;
+        else /* PAGE_SET_PROTECTION */
+            p->flags |= (p->flags & ~(PAGE_BITS - 1)) | (flags & PAGE_BITS);
      }
  }
 
bsd-user/mmap.c +4 -4
index 17f4cd80aa..4bfac81af4 100644
@@ -125,7 +125,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
          if (ret != 0)
              goto error;
      }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);
      mmap_unlock();
      return 0;
  error:
@@ -214,7 +214,7 @@ static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
             to mmap.  */
          page_set_flags(last_brk & TARGET_PAGE_MASK,
                         TARGET_PAGE_ALIGN(new_brk),
-                       PAGE_RESERVED);
+                       PAGE_RESERVED, PAGE_SET_ALL_FLAGS);
      }
      last_brk = new_brk;
 
@@ -397,7 +397,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
          }
      }
   the_end1:
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot | PAGE_VALID, PAGE_SET_ALL_FLAGS);
   the_end:
  #ifdef DEBUG_MMAP
      printf("ret=0x" TARGET_FMT_lx "\n", start);
@@ -460,7 +460,7 @@ int target_munmap(abi_ulong start, abi_ulong len)
      }
 
      if (ret == 0)
-        page_set_flags(start, start + len, 0);
+        page_set_flags(start, start + len, 0, PAGE_SET_ALL_FLAGS);
      mmap_unlock();
      return ret;
  }
include/exec/cpu-all.h +10 -1
index 117d2fbbca..100388dcd4 100644
@@ -249,6 +249,9 @@ extern intptr_t qemu_host_page_mask;
  /* FIXME: Code that sets/uses this is broken and needs to go away.  */
  #define PAGE_RESERVED  0x0020
  #endif
+#if defined(CONFIG_LINUX) && defined(CONFIG_USER_ONLY)
+#define PAGE_MAP_ANONYMOUS 0x0080
+#endif
 
  #if defined(CONFIG_USER_ONLY)
  void page_dump(FILE *f);
@@ -257,8 +260,14 @@ typedef int (*walk_memory_regions_fn)(void *, target_ulong,
                                        target_ulong, unsigned long);
  int walk_memory_regions(void *, walk_memory_regions_fn);
 
+enum page_set_flags_mode {
+    PAGE_SET_ALL_FLAGS,
+    PAGE_SET_PROTECTION
+};
+
  int page_get_flags(target_ulong address);
-void page_set_flags(target_ulong start, target_ulong end, int flags);
+void page_set_flags(target_ulong start, target_ulong end, int flags,
+                    enum page_set_flags_mode);
  int page_check_range(target_ulong start, target_ulong len, int flags);
  #endif
 
linux-user/elfload.c +2 -1
index 8638612aec..c59cf4359c 100644
@@ -1702,7 +1702,8 @@ static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
 
      /* Ensure that the bss page(s) are valid */
      if ((page_get_flags(last_bss-1) & prot) != prot) {
-        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
+        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot,
+                       PAGE_SET_PROTECTION);
      }
 
      if (host_start < host_map_start) {
linux-user/mmap.c +44 -5
index 41e0983ce8..fff29ee04b 100644
@@ -124,7 +124,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
          if (ret != 0)
              goto error;
      }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);
      mmap_unlock();
      return 0;
  error:
@@ -362,6 +362,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
                       int flags, int fd, abi_ulong offset)
  {
      abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
+    int page_flags;
 
      mmap_lock();
  #ifdef DEBUG_MMAP
@@ -562,7 +563,11 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
          }
      }
   the_end1:
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_flags = prot | PAGE_VALID;
+    if (flags & MAP_ANONYMOUS) {
+        page_flags |= PAGE_MAP_ANONYMOUS;
+    }
+    page_set_flags(start, start + len, page_flags, PAGE_SET_ALL_FLAGS);
   the_end:
  #ifdef DEBUG_MMAP
      printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
@@ -675,7 +680,7 @@ int target_munmap(abi_ulong start, abi_ulong len)
      }
 
      if (ret == 0) {
-        page_set_flags(start, start + len, 0);
+        page_set_flags(start, start + len, 0, PAGE_SET_ALL_FLAGS);
          tb_invalidate_phys_range(start, start + len);
      }
      mmap_unlock();
@@ -755,10 +760,44 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
      } else {
          new_addr = h2g(host_addr);
          prot = page_get_flags(old_addr);
-        page_set_flags(old_addr, old_addr + old_size, 0);
-        page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
+        page_set_flags(old_addr, old_addr + old_size, 0,
+                       PAGE_SET_ALL_FLAGS);
+        page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID,
+                       PAGE_SET_ALL_FLAGS);
      }
      tb_invalidate_phys_range(new_addr, new_addr + new_size);
      mmap_unlock();
      return new_addr;
  }
+
+int target_madvise(abi_ulong start, abi_ulong len, int advice)
+{
+    abi_ulong end, addr;
+    int ret;
+
+    len = TARGET_PAGE_ALIGN(len);
+    start &= TARGET_PAGE_MASK;
+
+    if (!guest_range_valid(start, len)) {
+        errno = 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 so we ignore them.
+       One exception is made for MADV_DONTNEED on anonymous mappings,
+       that applications may rely on to zero out pages. */
+    if (advice & MADV_DONTNEED) {
+        end = start + len;
+        for (addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
+            if (page_get_flags(addr) & PAGE_MAP_ANONYMOUS) {
+                ret = madvise(g2h(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
+                if (ret != 0) {
+                    return ret;
+                }
+            }
+        }
+    }
+    return 0;
+}
linux-user/qemu.h +1 -0
index b4959e41c6..e5ac5e9c6a 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 advice);
  extern unsigned long last_brk;
  extern abi_ulong mmap_next_start;
  abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
linux-user/syscall.c +5 -7
index 850b72a0c7..4478de5fc8 100644
@@ -5124,7 +5124,8 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_env,
 
      page_set_flags(raddr, raddr + shm_info.shm_segsz,
                     PAGE_VALID | PAGE_READ |
-                   ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
+                   ((shmflg & SHM_RDONLY) ? 0 : PAGE_WRITE),
+                   PAGE_SET_ALL_FLAGS);
 
      for (i = 0; i < N_SHM_REGIONS; i++) {
          if (!shm_regions[i].in_use) {
@@ -5150,7 +5151,8 @@ static inline abi_long do_shmdt(abi_ulong shmaddr)
      for (i = 0; i < N_SHM_REGIONS; ++i) {
          if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
              shm_regions[i].in_use = false;
-            page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
+            page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0,
+                           PAGE_SET_ALL_FLAGS);
              break;
          }
      }
@@ -11559,11 +11561,7 @@ static abi_long do_syscall1(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.  */
-        return 0;
+        return get_errno(target_madvise(arg1, arg2, arg3));
  #endif
  #if TARGET_ABI_BITS == 32
      case TARGET_NR_fcntl64:
#2 lore raw
In reply to Simon Hausmann

Le 27/08/2018 à 10:40, Simon Hausmann a écrit :

Quoted 38 lines

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. The file backed mappings this is tricky - hence the original comment - but for anonymous mappings it is safe to forward. We just need to remember which those mappings are, which is now stored in the flags.

Cc Riku Voipio <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Sami Nurmenniemi <[email protected]>

--
v2:
- align start and len on host page size
v3:
- align start and len to target page size as it may be bigger than host
- use immediate return in do_syscall
- forward MADV_DONTNEED advice only for anonymously mapped pages
- remember which mappings are anonymous in the page flags and preserve those bits across mprotect calls.

---
 accel/tcg/translate-all.c |  8 +++++--
 bsd-user/mmap.c           |  8 +++----
 include/exec/cpu-all.h    | 11 ++++++++-
 linux-user/elfload.c      |  3 ++-
 linux-user/mmap.c         | 49 +++++++++++++++++++++++++++++++++++----
 linux-user/qemu.h         |  1 +
 linux-user/syscall.c      | 12 ++++------
 7 files changed, 72 insertions(+), 20 deletions(-)
accel/tcg/translate-all.c +0 -0
index 898c3bb3d1..467fbd9aeb 100644

accel/tcg/translate-all.c int page_get_flags(target_ulong address) · line 2485

Quoted 6 lines
accel/tcg/translate-all.c +2 -1
@@ -2481,7 +2481,8 @@ int page_get_flags(target_ulong address)
 /* Modify the flags of a page and invalidate the code if necessary.
    The flag PAGE_WRITE_ORG is positioned automatically depending
    on PAGE_WRITE.  The mmap_lock should already be held.  */
-void page_set_flags(target_ulong start, target_ulong end, int flags)
+void page_set_flags(target_ulong start, target_ulong end, int flags,
+                    enum page_set_flags_mode mode)

Is it possible to not add the mode, and only use "flags" to set the new flag? Using page_get_flags() to keep flags that needed to be kept?

Quoted 20 lines
accel/tcg/translate-all.c +4 -1
 {
     target_ulong addr, len;
 
@@ -2513,7 +2514,10 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
             p->first_tb) {
             tb_invalidate_phys_page(addr, 0);
         }
-        p->flags = flags;
+        if (mode == PAGE_SET_ALL_FLAGS)
+            p->flags = flags;
+        else /* PAGE_SET_PROTECTION */
+            p->flags |= (p->flags & ~(PAGE_BITS - 1)) | (flags & PAGE_BITS);
     }
 }
 
bsd-user/mmap.c +0 -0
index 17f4cd80aa..4bfac81af4 100644

bsd-user/mmap.c int target_mprotect(abi_ulong start, abi_ulong len, int prot) · line 128

Quoted 5 lines
bsd-user/mmap.c +1 -1
@@ -125,7 +125,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
         if (ret != 0)
             goto error;
     }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);

Why do you remove the PAGE_VALID flag?

...

linux-user/elfload.c static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot) · line 1707

Quoted 12 lines
linux-user/elfload.c +2 -1
index 8638612aec..c59cf4359c 100644
@@ -1702,7 +1702,8 @@ static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
 
     /* Ensure that the bss page(s) are valid */
     if ((page_get_flags(last_bss-1) & prot) != prot) {
-        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
+        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot,
+                       PAGE_SET_PROTECTION);
     }

PAGE_VALID?

linux-user/mmap.c int target_mprotect(abi_ulong start, abi_ulong len, int prot) · line 128

Quoted 13 lines
linux-user/elfload.c +0 -0
     if (host_start < host_map_start) {
linux-user/mmap.c +1 -1
index 41e0983ce8..fff29ee04b 100644
@@ -124,7 +124,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
         if (ret != 0)
             goto error;
     }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);
     mmap_unlock();

PAGE_VALID?

...

Quoted 13 lines
linux-user/mmap.c +13 -0
+int target_madvise(abi_ulong start, abi_ulong len, int advice)
+{
+    abi_ulong end, addr;
+    int ret;
+
+    len = TARGET_PAGE_ALIGN(len);
+    start &= TARGET_PAGE_MASK;
+
+    if (!guest_range_valid(start, len)) {
+        errno = EINVAL;
+        return -1;
+    }
+

linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 795

Quoted 10 lines
linux-user/mmap.c +10 -0
+    /* A straight passthrough may not be safe because qemu sometimes
+       turns private file-backed mappings into anonymous mappings.
+       Most flags are hints so we ignore them.
+       One exception is made for MADV_DONTNEED on anonymous mappings,
+       that applications may rely on to zero out pages. */
+    if (advice & MADV_DONTNEED) {
+        end = start + len;
+        for (addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
+            if (page_get_flags(addr) & PAGE_MAP_ANONYMOUS) {
+                ret = madvise(g2h(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);

these values must be aligned on host page size.

Thanks,
Laurent

#3 lore raw
Quoted 34 lines · Laurent Vivier

Le 27/08/2018 à 10:40, Simon Hausmann a écrit :

Quoted 33 lines

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. The file backed mappings this is tricky - hence the original comment - but for anonymous mappings it is safe to forward. We just need to remember which those mappings are, which is now stored in the flags.

Cc Riku Voipio <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Sami Nurmenniemi <[email protected]>

--
v2:
- align start and len on host page size
v3:
- align start and len to target page size as it may be bigger than host
- use immediate return in do_syscall
- forward MADV_DONTNEED advice only for anonymously mapped pages
- remember which mappings are anonymous in the page flags and preserve those bits across mprotect calls.

---
  accel/tcg/translate-all.c |  8 +++++--
  bsd-user/mmap.c           |  8 +++----
  include/exec/cpu-all.h    | 11 ++++++++-
  linux-user/elfload.c      |  3 ++-
  linux-user/mmap.c         | 49 +++++++++++++++++++++++++++++++++++----
  linux-user/qemu.h         |  1 +
  linux-user/syscall.c      | 12 ++++------
  7 files changed, 72 insertions(+), 20 deletions(-)
Quoted 13 lines

accel/tcg/translate-all.c int page_get_flags(target_ulong address) · line 2485

Quoted 11 lines
accel/tcg/translate-all.c +2 -1
index 898c3bb3d1..467fbd9aeb 100644
@@ -2481,7 +2481,8 @@ int page_get_flags(target_ulong address)
  /* Modify the flags of a page and invalidate the code if necessary.
     The flag PAGE_WRITE_ORG is positioned automatically depending
     on PAGE_WRITE.  The mmap_lock should already be held.  */
-void page_set_flags(target_ulong start, target_ulong end, int flags)
+void page_set_flags(target_ulong start, target_ulong end, int flags,
+                    enum page_set_flags_mode mode)

Is it possible to not add the mode, and only use "flags" to set the new flag? Using page_get_flags() to keep flags that needed to be kept?

Hm, the reason why I didn't do that is because page_set_flags operates

on a range while page_get_flags() gets the flags for only one page.

If you prefer not to have the mode, then I can see only two other options:

    (1) Assume that the flags are typically homogeneous and use something like

page_set_flags(start, end, page_get_flags(start) | whatever)

    (2) Change the call sites to not use the range setting ability of page_set_flags but

loop itself.

I don't like either, so I went for the mode. But I can change to any way you prefer.

Quoted 15 lines
Quoted 15 lines
accel/tcg/translate-all.c +4 -1
  {
      target_ulong addr, len;
 
@@ -2513,7 +2514,10 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
              p->first_tb) {
              tb_invalidate_phys_page(addr, 0);
          }
-        p->flags = flags;
+        if (mode == PAGE_SET_ALL_FLAGS)
+            p->flags = flags;
+        else /* PAGE_SET_PROTECTION */
+            p->flags |= (p->flags & ~(PAGE_BITS - 1)) | (flags & PAGE_BITS);
      }
  }
 
Quoted 11 lines

bsd-user/mmap.c int target_mprotect(abi_ulong start, abi_ulong len, int prot) · line 128

Quoted 10 lines
bsd-user/mmap.c +1 -1
index 17f4cd80aa..4bfac81af4 100644
@@ -125,7 +125,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
          if (ret != 0)
              goto error;
      }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);

Why do you remove the PAGE_VALID flag?

As the mode set PAGE_SET_PROTECTION, the page_set_flags call only affects the protection

bits and the existing PAGE_VALID is preserved. I believe PAGE_VALID must be set for that range.

If that assumption is wrong, then I could change page_set_flags to implicitly set PAGE_VALID if the

mode is to apply new protection flags. Would that be ok?

Quoted 7 lines

...

Quoted 6 lines
linux-user/elfload.c +0 -0
index 8638612aec..c59cf4359c 100644
@@ -1702,7 +1702,8 @@ static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
 
Quoted 7 lines

linux-user/elfload.c static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot) · line 1707

Quoted 6 lines
linux-user/elfload.c +2 -1
      /* Ensure that the bss page(s) are valid */
      if ((page_get_flags(last_bss-1) & prot) != prot) {
-        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
+        page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot,
+                       PAGE_SET_PROTECTION);
      }

PAGE_VALID?

Oh! You're right, here I mistakenly thought the pages are already marked valid, but

it appears that this is a new mapping, so I'll fix that to set PAGE_VALID.

Quoted 15 lines

linux-user/mmap.c int target_mprotect(abi_ulong start, abi_ulong len, int prot) · line 128

Quoted 13 lines
linux-user/elfload.c +0 -0
      if (host_start < host_map_start) {
linux-user/mmap.c +1 -1
index 41e0983ce8..fff29ee04b 100644
@@ -124,7 +124,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
          if (ret != 0)
              goto error;
      }
-    page_set_flags(start, start + len, prot | PAGE_VALID);
+    page_set_flags(start, start + len, prot, PAGE_SET_PROTECTION);
      mmap_unlock();

PAGE_VALID?

Quoted 1 line

...

Same - potentially wrong - reasoning as with the other mprotect() wrapper.

Quoted 16 lines
Quoted 16 lines
linux-user/mmap.c +16 -0
+int target_madvise(abi_ulong start, abi_ulong len, int advice)
+{
+    abi_ulong end, addr;
+    int ret;
+
+    len = TARGET_PAGE_ALIGN(len);
+    start &= TARGET_PAGE_MASK;
+
+    if (!guest_range_valid(start, len)) {
+        errno = 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 so we ignore them.
Quoted 9 lines

linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 795

Quoted 7 lines
linux-user/mmap.c +7 -0
+       One exception is made for MADV_DONTNEED on anonymous mappings,
+       that applications may rely on to zero out pages. */
+    if (advice & MADV_DONTNEED) {
+        end = start + len;
+        for (addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
+            if (page_get_flags(addr) & PAGE_MAP_ANONYMOUS) {
+                ret = madvise(g2h(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);

these values must be aligned on host page size.

Sorry, I think I assumed from the earlier statement that the target page size may be bigger than the

host and that that would be sufficient for host alignment. I didn't consider that the reverse may also apply.

Would the following be correct?

    (1) Use target aligned start/len for guest_range_valid call

    (2) Use host aligned start/end/increment for the loop (including the page_get_flags call parameters).

Thanks for the feedback :)

Simon