Skip to contentSkip to conversation

linux-user: Fix shmat emulation by honoring host SHMLBA

PATCH In review Richard Henderson 2 participants 3 messages Jul 9, 2018 – Jul 10, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Fix shmat emulation by honoring host SHMLBA

For those hosts with SHMLBA > getpagesize, we don't automatically select a guest address that is compatible with the host. We can achieve this by boosting the alignment of guest_base and by adding an extra alignment argument to mmap_find_vma.

---

Found by running check-tcg on an arm32 host, which defines (host) SHMLBA as 16K, executing the "native" or "non-cross-compiled" linux-test. Pushing that back to x86 snuffled out more fixes required for mips, which defines (guest) SHMLBA as 256k.

While this is newly exposed by check-tcg, the problem has been latent for quite a while. It probably doesn't warrent rushing in for 3.0.

r~

---
 linux-user/qemu.h    |  2 +-
 linux-user/elfload.c | 17 +++++-----
 linux-user/mmap.c    | 74 +++++++++++++++++++++++---------------------
 linux-user/syscall.c |  3 +-
 4 files changed, 52 insertions(+), 44 deletions(-)
linux-user/qemu.h +1 -1
index bb85c81aa4..b79ac8bd01 100644
@@ -436,7 +436,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
                        abi_ulong new_addr);
 extern unsigned long last_brk;
 extern abi_ulong mmap_next_start;
-abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
+abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
 void mmap_fork_start(void);
 void mmap_fork_end(int child);
 
linux-user/elfload.c +10 -7
index 942a1b661f..3467e3512e 100644
@@ -3,6 +3,7 @@
 #include <sys/param.h>
 
 #include <sys/resource.h>
+#include <sys/shm.h>
 
 #include "qemu.h"
 #include "disas/disas.h"
@@ -1929,6 +1930,8 @@ unsigned long init_guest_space(unsigned long host_start,
                                unsigned long guest_start,
                                bool fixed)
 {
+    /* In order to use host shmat, we must be able to honor SHMLBA.  */
+    unsigned long align = MAX(SHMLBA, qemu_host_page_size);
     unsigned long current_start, aligned_start;
     int flags;
 
@@ -1946,7 +1949,7 @@ unsigned long init_guest_space(unsigned long host_start,
     }
 
     /* Setup the initial flags and start address.  */
-    current_start = host_start & qemu_host_page_mask;
+    current_start = host_start & -align;
     flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
     if (fixed) {
         flags |= MAP_FIXED;
@@ -1982,8 +1985,8 @@ unsigned long init_guest_space(unsigned long host_start,
             return (unsigned long)-1;
         }
         munmap((void *)real_start, host_full_size);
-        if (real_start & ~qemu_host_page_mask) {
-            /* The same thing again, but with an extra qemu_host_page_size
+        if (real_start & (align - 1)) {
+            /* The same thing again, but with extra
              * so that we can shift around alignment.
              */
             unsigned long real_size = host_full_size + qemu_host_page_size;
@@ -1996,7 +1999,7 @@ unsigned long init_guest_space(unsigned long host_start,
                 return (unsigned long)-1;
             }
             munmap((void *)real_start, real_size);
-            real_start = HOST_PAGE_ALIGN(real_start);
+            real_start = ROUND_UP(real_start, align);
         }
         current_start = real_start;
     }
@@ -2023,7 +2026,7 @@ unsigned long init_guest_space(unsigned long host_start,
         }
 
         /* Ensure the address is properly aligned.  */
-        if (real_start & ~qemu_host_page_mask) {
+        if (real_start & (align - 1)) {
             /* Ideally, we adjust like
              *
              *    pages: [  ][  ][  ][  ][  ]
@@ -2051,7 +2054,7 @@ unsigned long init_guest_space(unsigned long host_start,
             if (real_start == (unsigned long)-1) {
                 return (unsigned long)-1;
             }
-            aligned_start = HOST_PAGE_ALIGN(real_start);
+            aligned_start = ROUND_UP(real_start, align);
         } else {
             aligned_start = real_start;
         }
@@ -2088,7 +2091,7 @@ unsigned long init_guest_space(unsigned long host_start,
          * because of trouble with ARM commpage setup.
          */
         munmap((void *)real_start, real_size);
-        current_start += qemu_host_page_size;
+        current_start += align;
         if (host_start == current_start) {
             /* Theoretically possible if host doesn't have any suitably
              * aligned areas.  Normally the first mmap will fail.
linux-user/mmap.c +39 -35

139 lines not on this page · raw message

linux-user/syscall.c +2 -1
index 5822e03e28..b6c37cbb97 100644
@@ -5000,7 +5000,8 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_env,
     else {
         abi_ulong mmap_start;
 
-        mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
+        /* In order to use the host shmat, we need to honor host SHMLBA.  */
+        mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
 
         if (mmap_start == -1) {
             errno = ENOMEM;
Signature · 1 line
2.17.1
#1 lore raw

Le 09/07/2018 à 21:15, Richard Henderson a écrit :

Quoted 7 lines

For those hosts with SHMLBA > getpagesize, we don't automatically select a guest address that is compatible with the host. We can achieve this by boosting the alignment of guest_base and by adding an extra alignment argument to mmap_find_vma.

---

...

linux-user/mmap.c abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) · line 306

Quoted 6 lines
linux-user/mmap.c +1 -1
@@ -299,7 +303,7 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
         if (h2g_valid(ptr + size - 1)) {
             addr = h2g(ptr);
 
-            if ((addr & ~TARGET_PAGE_MASK) == 0) {
+            if ((addr & (align - 1)) == 0) {

so ~TARGET_PAGE_MASK is (align - 1)...

Quoted 5 lines
linux-user/mmap.c +0 -0
                 /* Success.  */
                 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
                     mmap_next_start = addr + size;
@@ -313,12 +317,12 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
                 /* Assume the result that the kernel gave us is the

linux-user/mmap.c abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) · line 325

Quoted 10 lines
linux-user/mmap.c +2 -2
                    first with enough free space, so start again at the
                    next higher target page.  */
-                addr = TARGET_PAGE_ALIGN(addr);
+                addr = ROUND_UP(addr, align);
                 break;
             case 1:
                 /* Sometimes the kernel decides to perform the allocation
                    at the top end of memory instead.  */
-                addr &= TARGET_PAGE_MASK;
+                addr &= align - 1;

and here TARGET_PAGE_MASK is also (align - 1)...

So I think it should be "addr &= -align".

Thanks,
Laurent

#2 lore raw
Quoted 6 lines · Laurent Vivier

linux-user/mmap.c abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) · line 325

Quoted 2 lines
linux-user/mmap.c +1 -1
-                addr &= TARGET_PAGE_MASK;
+                addr &= align - 1;

and here TARGET_PAGE_MASK is also (align - 1)...

So I think it should be "addr &= -align".

Whoops, yes indeed.

r~