Skip to contentSkip to conversation

linux-user: Fix shmat by honoring SHMLBA

PATCH New Richard Henderson 1 participant 4 messages Oct 17, 2018

lore
Share & tools
lore
#0 lore raw

cover linux-user: Fix shmat by honoring SHMLBA

These three patches were posted independently back in July. The second patch has been modified for feedback from Laurent.

C.f.
http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg01999.html
http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg02180.html
http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg02652.html

r~

Richard Henderson (3):
tests/tcg/multiarch/linux-test: Fix error check for shmat
linux-user: Fix shmat emulation by honoring host SHMLBA
linux-user: Align mmap_find_vma to host page size

 linux-user/qemu.h                |  2 +-
 linux-user/elfload.c             | 17 ++++---
 linux-user/mmap.c                | 76 +++++++++++++++++---------------
 linux-user/syscall.c             |  3 +-
 tests/tcg/multiarch/linux-test.c |  3 +-
 5 files changed, 56 insertions(+), 45 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH 1/3 tests/tcg/multiarch/linux-test: Fix error check for shmat

The error indicator for this syscall is -1, not 0.

---
 tests/tcg/multiarch/linux-test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/tcg/multiarch/linux-test.c +2 -1
index e80eccc0ce..6d2c02c5c0 100644
@@ -503,8 +503,9 @@ static void test_shm(void)
 
     shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
     ptr = shmat(shmid, NULL, 0);
-    if (!ptr)
+    if (ptr == (void *)-1) {
         error("shmat");
+    }
 
     memset(ptr, 0, SHM_SIZE);
 
Signature · 1 line
2.17.2
#2 lore raw

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

In reply to Richard Henderson

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/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 b4959e41c6..67a4f3c020 100644
@@ -439,7 +439,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 10bca65b99..ec200a39ad 100644
@@ -3,6 +3,7 @@
 #include <sys/param.h>
 
 #include <sys/resource.h>
+#include <sys/shm.h>
 
 #include "qemu.h"
 #include "disas/disas.h"
@@ -1942,6 +1943,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;
 
@@ -1959,7 +1962,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;
@@ -1995,8 +1998,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;
@@ -2009,7 +2012,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;
     }
@@ -2036,7 +2039,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: [  ][  ][  ][  ][  ]
@@ -2064,7 +2067,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;
         }
@@ -2101,7 +2104,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 ae3c0dfef7..7a69855344 100644
@@ -3815,7 +3815,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.2
#3 lore raw

PATCH 3/3 linux-user: Align mmap_find_vma to host page size

In reply to Richard Henderson

This can avoid stack allocation failures for i386 guest on ppc64 (64k page) host.

---
 linux-user/mmap.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/mmap.c +2 -0
index 47950ee9d7..735bc928fe 100644
@@ -262,6 +262,8 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align)
     abi_ulong addr;
     int wrapped, repeat;
 
+    align = MAX(align, qemu_host_page_size);
+
     /* If 'start' == 0, then a default start address is used. */
     if (start == 0) {
         start = mmap_next_start;
Signature · 1 line
2.17.2