Skip to contentSkip to conversation

various fixes for next PR (testing, vhost, guest_base fixes)

PATCH In review Alex Bennée 5 participants 21 messages Jun 5, 2020 – Jun 8, 2020

lore
Share & tools
lore
#0 lore raw

cover various fixes for next PR (testing, vhost, guest_base fixes)

Hi,

These are all the patches I've currently got which are ready for a pull request next week. I've included some patches which are destined to go in via other trees so I can keep the testing green on the CI.

In summary:

Some simple plugin cleanups (the reset remain in plugins/next) Reliability fixes for travis/shippable
iotest 194 fix (going in via block tree?)
docker updates (ubuntu and tricore fix)
vhost-user and TCG fix
more linux-user guest_base fixes

I'll certainly include the testing stuff in my PR but if others are happy for me to include bits touching their areas then shout and I'll include them in the PR.

The following need review:

- linux-user: detect overflow of MAP_FIXED mmap
- linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit
- linux-user: provide fallback pgd_find_hole for bare chroots
- tests/docker: fix pre-requisite for debian-tricore-cross
- hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
- .shippable: temporaily disable some cross builds
- exec: flush the whole TLB if a watchpoint crosses a page boundary

Alex Bennée (10):
tests/plugin: correctly honour io_count
exec: flush the whole TLB if a watchpoint crosses a page boundary .travis.yml: allow failure for unreliable hosts .shippable: temporaily disable some cross builds tests/docker: fix pre-requisite for debian-tricore-cross hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
linux-user: provide fallback pgd_find_hole for bare chroots
linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit tests/tcg: add simple commpage test case
linux-user: detect overflow of MAP_FIXED mmap

Emilio G. Cota (1):
qemu-plugin.h: add missing include <stddef.h> to define size_t

Paolo Bonzini (1):
docker: update Ubuntu to 20.04

Philippe Mathieu-Daudé (1):
scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header

Vladimir Sementsov-Ogievskiy (1):
iotests: 194: wait migration completion on target too

 include/qemu/qemu-plugin.h             |  1 +
 exec.c                                 |  8 ++-
 hw/virtio/vhost.c                      | 57 +++++++++++++++------
 linux-user/elfload.c                   | 71 ++++++++++++++++++++++----
 linux-user/mmap.c                      |  2 +-
 tests/plugin/mem.c                     |  2 +-
 tests/tcg/arm/commpage.c               | 61 ++++++++++++++++++++++
 .shippable.yml                         | 12 ++---
 .travis.yml                            |  5 ++
 hw/virtio/trace-events                 |  3 +-
 scripts/clean-includes                 |  1 +
 tests/docker/Makefile.include          |  2 +-
 tests/docker/dockerfiles/ubuntu.docker |  2 +-
 tests/qemu-iotests/194                 | 10 ++++
 tests/qemu-iotests/194.out             |  5 ++
 tests/tcg/arm/Makefile.target          |  2 +
 16 files changed, 206 insertions(+), 38 deletions(-)
 create mode 100644 tests/tcg/arm/commpage.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/14 qemu-plugin.h: add missing include <stddef.h> to define size_t

From: "Emilio G. Cota" <[email protected]>

Message-Id: <[email protected]>

---
 include/qemu/qemu-plugin.h | 1 +
 1 file changed, 1 insertion(+)
include/qemu/qemu-plugin.h +1 -0
index 89ed579f559..bab8b0d4b3a 100644
@@ -12,6 +12,7 @@
 
 #include <inttypes.h>
 #include <stdbool.h>
+#include <stddef.h>
 
 /*
  * For best performance, build the plugin with -fvisibility=hidden so that
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/14 scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header

In reply to Alex Bennée

From: Philippe Mathieu-Daudé <[email protected]>

"qemu/qemu-plugin.h" isn't meant to be include by QEMU codebase, but by 3rd party plugins that QEMU can use. These plugins can be built out of QEMU and don't include "qemu/osdep.h". Mark "qemu/qemu-plugin.h" as a special header that doesn't need to be cleaned for "qemu/osdep.h".

Message-Id: <[email protected]>

---
 scripts/clean-includes | 1 +
 1 file changed, 1 insertion(+)
scripts/clean-includes +1 -0
index dd938daa3ec..795b3bea318 100755
@@ -123,6 +123,7 @@ for f in "$@"; do
       ;;
     *include/qemu/osdep.h | \
     *include/qemu/compiler.h | \
+    *include/qemu/qemu-plugin.h | \
     *include/glib-compat.h | \
     *include/sysemu/os-posix.h | \
     *include/sysemu/os-win32.h | \
Signature · 1 line
2.20.1
#3 lore raw

PATCH 3/14 tests/plugin: correctly honour io_count

In reply to Alex Bennée
---
 tests/plugin/mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/plugin/mem.c +1 -1
index 878abf09d19..4725bd851d8 100644
@@ -28,7 +28,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
 
     g_string_printf(out, "mem accesses: %" PRIu64 "\n", mem_count);
     if (do_haddr) {
-        g_string_append_printf(out, "io accesses: %" PRIu64 "\n", mem_count);
+        g_string_append_printf(out, "io accesses: %" PRIu64 "\n", io_count);
     }
     qemu_plugin_outs(out->str);
 }
Signature · 1 line
2.20.1
#4 lore raw

PATCH 4/14 exec: flush the whole TLB if a watchpoint crosses a page boundary

In reply to Alex Bennée

There is no particular reason why you can't have a watchpoint in TCG that covers a large chunk of the address space. We could be clever about it but these cases are pretty rare and we can assume the user will expect a little performance degradation.

NB: In my testing gdb will silently squash a watchpoint like:

watch (char[0x7fffffffff]) *0x0

to a 4 byte watchpoint. Practically it will limit the maximum size based on max-value-size. However given enough of a tweak the sky is the limit.

---
v2
- use cleaner in_page = -(addr | TARGET_PAGE_MASK) logic per rth

---
 exec.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
exec.c +7 -1
index 3d4c94a9dc3..7cd45e94fce 100644
@@ -1036,6 +1036,7 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
                           int flags, CPUWatchpoint **watchpoint)
 {
     CPUWatchpoint *wp;
+    vaddr in_page;
 
     /* forbid ranges which are empty or run off the end of the address space */
     if (len == 0 || (addr + len - 1) < addr) {
@@ -1056,7 +1057,12 @@ int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
         QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
     }
 
-    tlb_flush_page(cpu, addr);
+    in_page = -(addr | TARGET_PAGE_MASK);
+    if (len <= in_page) {
+        tlb_flush_page(cpu, addr);
+    } else {
+        tlb_flush(cpu);
+    }
 
     if (watchpoint)
         *watchpoint = wp;
Signature · 1 line
2.20.1
#5 lore raw

PATCH 5/14 .travis.yml: allow failure for unreliable hosts

In reply to Alex Bennée

They will still run but they won't get in the way of the result.

---
 .travis.yml | 5 +++++
 1 file changed, 5 insertions(+)
.travis.yml +5 -0
index 564be50a3c1..ec6367af1f0 100644
@@ -429,6 +429,7 @@ jobs:
       env:
         - TEST_CMD="make check check-tcg V=1"
         - CONFIG="--disable-containers --target-list=${MAIN_SOFTMMU_TARGETS}"
+        - UNRELIABLE=true
 
     - name: "[ppc64] GCC check-tcg"
       arch: ppc64le
@@ -493,6 +494,7 @@ jobs:
       env:
         - TEST_CMD="make check check-tcg V=1"
         - CONFIG="--disable-containers --target-list=${MAIN_SOFTMMU_TARGETS},s390x-linux-user"
+        - UNRELIABLE=true
       script:
         - ( cd ${SRC_DIR} ; git submodule update --init roms/SLOF )
         - BUILD_RC=0 && make -j${JOBS} || BUILD_RC=$?
@@ -535,6 +537,7 @@ jobs:
         - TEST_CMD="make check-unit"
         - CONFIG="--disable-containers --disable-tcg --enable-kvm
                   --disable-tools --host-cc=clang --cxx=clang++"
+        - UNRELIABLE=true
 
     # Release builds
     # The make-release script expect a QEMU version, so our tag must start with a 'v'.
@@ -556,3 +559,5 @@ jobs:
         - mkdir -p release-build && cd release-build
         - ../configure ${BASE_CONFIG} ${CONFIG} || { cat config.log && exit 1; }
         - make install
+  allow_failures:
+    - env: UNRELIABLE=true
Signature · 1 line
2.20.1
#6 lore raw

PATCH 6/14 .shippable: temporaily disable some cross builds

In reply to Alex Bennée

These currently fail due to Debian bug #960271 as the linux-libc-library has a user-space build breaking symbol in it.

---
 .shippable.yml | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
.shippable.yml +6 -6
index 2cce7b56890..10cf219bff4 100644
@@ -5,8 +5,8 @@ env:
   global:
     - LC_ALL=C
   matrix:
-    - IMAGE=debian-amd64
-      TARGET_LIST=x86_64-softmmu,x86_64-linux-user
+    # - IMAGE=debian-amd64
+    #   TARGET_LIST=x86_64-softmmu,x86_64-linux-user
     - IMAGE=debian-win32-cross
       TARGET_LIST=arm-softmmu,i386-softmmu,lm32-softmmu
     - IMAGE=debian-win64-cross
@@ -19,10 +19,10 @@ env:
       TARGET_LIST=aarch64-softmmu,aarch64-linux-user
     - IMAGE=debian-s390x-cross
       TARGET_LIST=s390x-softmmu,s390x-linux-user
-    - IMAGE=debian-mips-cross
-      TARGET_LIST=mips-softmmu,mipsel-linux-user
-    - IMAGE=debian-mips64el-cross
-      TARGET_LIST=mips64el-softmmu,mips64el-linux-user
+    # - IMAGE=debian-mips-cross
+    #   TARGET_LIST=mips-softmmu,mipsel-linux-user
+    # - IMAGE=debian-mips64el-cross
+    #   TARGET_LIST=mips64el-softmmu,mips64el-linux-user
     - IMAGE=debian-ppc64el-cross
       TARGET_LIST=ppc64-softmmu,ppc64-linux-user,ppc64abi32-linux-user
 build:
Signature · 1 line
2.20.1
Reviewed-by Tested-by on v1 6/14
#7 lore raw

Re: [PATCH v1 06/14] .shippable: temporaily disable some cross builds

Quoted 29 lines · Alex Bennée

These currently fail due to Debian bug #960271 as the linux-libc-library has a user-space build breaking symbol in it.

---
 .shippable.yml | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
.shippable.yml +2 -3
index 2cce7b56890..10cf219bff4 100644
@@ -5,8 +5,8 @@ env:
   global:
     - LC_ALL=C
   matrix:
-    - IMAGE=debian-amd64
-      TARGET_LIST=x86_64-softmmu,x86_64-linux-user
+    # - IMAGE=debian-amd64
+    #   TARGET_LIST=x86_64-softmmu,x86_64-linux-user
     - IMAGE=debian-win32-cross
       TARGET_LIST=arm-softmmu,i386-softmmu,lm32-softmmu
     - IMAGE=debian-win64-cross
@@ -19,10 +19,10 @@ env:
       TARGET_LIST=aarch64-softmmu,aarch64-linux-user
     - IMAGE=debian-s390x-cross
       TARGET_LIST=s390x-softmmu,s390x-linux-user
-    - IMAGE=debian-mips-cross

.shippable.yml env: · line 28

Quoted 10 lines
.shippable.yml +4 -3
-      TARGET_LIST=mips-softmmu,mipsel-linux-user
-    - IMAGE=debian-mips64el-cross
-      TARGET_LIST=mips64el-softmmu,mips64el-linux-user
+    # - IMAGE=debian-mips-cross
+    #   TARGET_LIST=mips-softmmu,mipsel-linux-user
+    # - IMAGE=debian-mips64el-cross
+    #   TARGET_LIST=mips64el-softmmu,mips64el-linux-user
     - IMAGE=debian-ppc64el-cross
       TARGET_LIST=ppc64-softmmu,ppc64-linux-user,ppc64abi32-linux-user
 build:
#8 lore raw

PATCH 7/14 iotests: 194: wait migration completion on target too

In reply to Alex Bennée

From: Vladimir Sementsov-Ogievskiy <[email protected]>

It is possible, that shutdown on target occurs earlier than migration finish. In this case we crash in bdrv_release_dirty_bitmap_locked() on assertion "assert(!bdrv_dirty_bitmap_busy(bitmap));" as we do have busy bitmap, as bitmap migration is ongoing.

We'll fix bitmap migration to gracefully cancel on early shutdown soon. Now let's fix iotest 194 to wait migration completion before shutdown.

Note that in this test dest_vm.shutdown() is called implicitly, as vms used as context-providers, see __exit__() method of QEMUMachine class.

Actually, not waiting migration finish is a wrong thing, but the test started to crash after commit ae00aa239847682
"iotests: 194: test also migration of dirty bitmap", which added dirty bitmaps here. So, Fixes: tag won't hurt.

Message-Id: <[email protected]>

---
 tests/qemu-iotests/194     | 10 ++++++++++
 tests/qemu-iotests/194.out |  5 +++++
 2 files changed, 15 insertions(+)
tests/qemu-iotests/194 +10 -0
index 3fad7c6c1ab..6dc2bc94d7e 100755
@@ -87,4 +87,14 @@ with iotests.FilePath('source.img') as source_img_path, \
             iotests.log(dest_vm.qmp('nbd-server-stop'))
             break
 
+    iotests.log('Wait migration completion on target...')
+    migr_events = (('MIGRATION', {'data': {'status': 'completed'}}),
+                   ('MIGRATION', {'data': {'status': 'failed'}}))
+    event = dest_vm.events_wait(migr_events)
+    iotests.log(event, filters=[iotests.filter_qmp_event])
+
+    iotests.log('Check bitmaps on source:')
     iotests.log(source_vm.qmp('query-block')['return'][0]['dirty-bitmaps'])
+
+    iotests.log('Check bitmaps on target:')
+    iotests.log(dest_vm.qmp('query-block')['return'][0]['dirty-bitmaps'])
tests/qemu-iotests/194.out +5 -0
index dd60dcc14f1..f70cf7610e0 100644
@@ -21,4 +21,9 @@ Gracefully ending the `drive-mirror` job on source...
 {"data": {"device": "mirror-job0", "len": 1073741824, "offset": 1073741824, "speed": 0, "type": "mirror"}, "event": "BLOCK_JOB_COMPLETED", "timestamp": {"microseconds": "USECS", "seconds": "SECS"}}
 Stopping the NBD server on destination...
 {"return": {}}
+Wait migration completion on target...
+{"data": {"status": "completed"}, "event": "MIGRATION", "timestamp": {"microseconds": "USECS", "seconds": "SECS"}}
+Check bitmaps on source:
+[{"busy": false, "count": 0, "granularity": 65536, "name": "bitmap0", "persistent": false, "recording": true, "status": "active"}]
+Check bitmaps on target:
 [{"busy": false, "count": 0, "granularity": 65536, "name": "bitmap0", "persistent": false, "recording": true, "status": "active"}]
Signature · 1 line
2.20.1
#9 lore raw

PATCH 8/14 tests/docker: fix pre-requisite for debian-tricore-cross

In reply to Alex Bennée
---
 tests/docker/Makefile.include | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/docker/Makefile.include +1 -1
index ed46bd98eb5..981b7fcf2a5 100644
@@ -130,7 +130,7 @@ docker-image-debian-sparc64-cross: docker-image-debian10
 docker-image-travis: NOUSER=1
 
 # Specialist build images, sometimes very limited tools
-docker-image-tricore-cross: docker-image-debian9
+docker-image-debian-tricore-cross: docker-image-debian9
 docker-image-debian-arm64-test-cross: docker-image-debian11
 
 # These images may be good enough for building tests but not for test builds
Signature · 1 line
2.20.1
#10 lore raw

PATCH 9/14 docker: update Ubuntu to 20.04

In reply to Alex Bennée

From: Paolo Bonzini <[email protected]>

Message-Id: <[email protected]>

---
 tests/docker/dockerfiles/ubuntu.docker | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/docker/dockerfiles/ubuntu.docker +1 -1
index eeb3b22bf20..43872417dec 100644
@@ -9,7 +9,7 @@
 # system won't pick up that it has changed.
 #
 
-FROM ubuntu:19.04
+FROM ubuntu:20.04
 ENV PACKAGES flex bison \
     ccache \
     clang \
Signature · 1 line
2.20.1
#11 lore raw
In reply to Alex Bennée
Quoted 11 lines · Alex Bennée

Hi,

These are all the patches I've currently got which are ready for a pull request next week. I've included some patches which are destined to go in via other trees so I can keep the testing green on the CI.

In summary:

Some simple plugin cleanups (the reset remain in plugins/next) Reliability fixes for travis/shippable
iotest 194 fix (going in via block tree?)

I've got that one queued for a PR Monday with some typo fixes included.

Quoted 18 lines

docker updates (ubuntu and tricore fix)
vhost-user and TCG fix
more linux-user guest_base fixes

I'll certainly include the testing stuff in my PR but if others are happy for me to include bits touching their areas then shout and I'll include them in the PR.

The following need review:

- linux-user: detect overflow of MAP_FIXED mmap
- linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit
- linux-user: provide fallback pgd_find_hole for bare chroots
- tests/docker: fix pre-requisite for debian-tricore-cross
- hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
- .shippable: temporaily disable some cross builds
- exec: flush the whole TLB if a watchpoint crosses a page boundary

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#12 lore raw

PATCH 10/14 hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE

In reply to Alex Bennée

The purpose of vhost_section is to identify RAM regions that need to be made available to a vhost client. However when running under TCG all RAM sections have DIRTY_MEMORY_CODE set which leads to problems down the line.

Re-factor the code so:

- steps are clearer to follow
- reason for rejection is recorded in the trace point
- we allow DIRTY_MEMORY_CODE

We expand the comment to explain that kernel based vhost has specific support for migration tracking.

Cc Michael S. Tsirkin <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
Cc Stefan Hajnoczi <[email protected]>

Message-Id: <[email protected]>

---
v3
- more tweaks to comments
- merge the flag setting into one line

---
 hw/virtio/vhost.c      | 57 ++++++++++++++++++++++++++++++------------
 hw/virtio/trace-events |  3 ++-
 2 files changed, 43 insertions(+), 17 deletions(-)
hw/virtio/vhost.c +41 -16
index aff98a0ede5..e3e21812905 100644
@@ -27,6 +27,7 @@
 #include "migration/blocker.h"
 #include "migration/qemu-file-types.h"
 #include "sysemu/dma.h"
+#include "sysemu/tcg.h"
 #include "trace.h"
 
 /* enabled until disconnected backend stabilizes */
@@ -403,26 +404,50 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,
     return r;
 }
 
+/*
+ * vhost_section: identify sections needed for vhost access
+ *
+ * We only care about RAM sections here (where virtqueue and guest
+ * internals accessed by virtio might live). If we find one we still
+ * allow the backend to potentially filter it out of our list.
+ */
 static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section)
 {
-    bool result;
-    bool log_dirty = memory_region_get_dirty_log_mask(section->mr) &
-                     ~(1 << DIRTY_MEMORY_MIGRATION);
-    result = memory_region_is_ram(section->mr) &&
-        !memory_region_is_rom(section->mr);
-
-    /* Vhost doesn't handle any block which is doing dirty-tracking other
-     * than migration; this typically fires on VGA areas.
-     */
-    result &= !log_dirty;
+    MemoryRegion *mr = section->mr;
+
+    if (memory_region_is_ram(mr) && !memory_region_is_rom(mr)) {
+        uint8_t dirty_mask = memory_region_get_dirty_log_mask(mr);
+        uint8_t handled_dirty;
+
+        /*
+         * Kernel based vhost doesn't handle any block which is doing
+         * dirty-tracking other than migration for which it has
+         * specific logging support. However for TCG the kernel never
+         * gets involved anyway so we can also ignore it's
+         * self-modiying code detection flags. However a vhost-user
+         * client could still confuse a TCG guest if it re-writes
+         * executable memory that has already been translated.
+         */
+        handled_dirty = (1 << DIRTY_MEMORY_MIGRATION) |
+            (1 << DIRTY_MEMORY_CODE);
 
-    if (result && dev->vhost_ops->vhost_backend_mem_section_filter) {
-        result &=
-            dev->vhost_ops->vhost_backend_mem_section_filter(dev, section);
-    }
+        if (dirty_mask & ~handled_dirty) {
+            trace_vhost_reject_section(mr->name, 1);
+            return false;
+        }
+
+        if (dev->vhost_ops->vhost_backend_mem_section_filter &&
+            !dev->vhost_ops->vhost_backend_mem_section_filter(dev, section)) {
+            trace_vhost_reject_section(mr->name, 2);
+            return false;
+        }
 
-    trace_vhost_section(section->mr->name, result);
-    return result;
+        trace_vhost_section(mr->name);
+        return true;
+    } else {
+        trace_vhost_reject_section(mr->name, 3);
+        return false;
+    }
 }
 
 static void vhost_begin(MemoryListener *listener)
hw/virtio/trace-events +2 -1
index e83500bee92..6427a0047df 100644
@@ -5,7 +5,8 @@ vhost_commit(bool started, bool changed) "Started: %d Changed: %d"
 vhost_region_add_section(const char *name, uint64_t gpa, uint64_t size, uint64_t host) "%s: 0x%"PRIx64"+0x%"PRIx64" @ 0x%"PRIx64
 vhost_region_add_section_merge(const char *name, uint64_t new_size, uint64_t gpa, uint64_t owr) "%s: size: 0x%"PRIx64 " gpa: 0x%"PRIx64 " owr: 0x%"PRIx64
 vhost_region_add_section_aligned(const char *name, uint64_t gpa, uint64_t size, uint64_t host) "%s: 0x%"PRIx64"+0x%"PRIx64" @ 0x%"PRIx64
-vhost_section(const char *name, int r) "%s:%d"
+vhost_section(const char *name) "%s"
+vhost_reject_section(const char *name, int d) "%s:%d"
 vhost_iotlb_miss(void *dev, int step) "%p step %d"
 
 # vhost-user.c
Signature · 1 line
2.20.1
#13 lore raw

PATCH 11/14 linux-user: provide fallback pgd_find_hole for bare chroots

In reply to Alex Bennée

When running QEMU out of a chroot environment we may not have access to /proc/self/maps. As there is no other "official" way to introspect our memory map we need to fall back to the original technique of repeatedly trying to mmap an address range until we find one that works.

Fortunately it's not quite as ugly as the original code given we already re-factored the complications of dealing with the ARM_COMMPAGE. We do make an attempt to skip over brk() which is about the only concrete piece of information we have about the address map at this moment.

Message-Id: <[email protected]>

---
v3
- skip to brk if we get there
- round up early

---
 linux-user/elfload.c | 48 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
linux-user/elfload.c +48 -0
index ebc663ea0b3..475d243f3bd 100644
@@ -2101,6 +2101,50 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
     }
 }
 
+/**
+ * pgd_find_hole_fallback: potential mmap address
+ * @guest_size: size of available space
+ * @brk: location of break
+ * @align: memory alignment
+ *
+ * This is a fallback method for finding a hole in the host address
+ * space if we don't have the benefit of being able to access
+ * /proc/self/map. It can potentially take a very long time as we can
+ * only dumbly iterate up the host address space seeing if the
+ * allocation would work.
+ */
+static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, long align)
+{
+    uintptr_t base;
+
+    /* Start (aligned) at the bottom and work our way up */
+    base = ROUND_UP(mmap_min_addr, align);
+
+    while (true) {
+        uintptr_t align_start, end;
+        align_start = ROUND_UP(base, align);
+        end = align_start + guest_size;
+
+        /* if brk is anywhere in the range give ourselves some room to grow. */
+        if (align_start <= brk && brk < end) {
+            base = brk + (16 * MiB);
+            continue;
+        } else if (align_start + guest_size < align_start) {
+            /* we have run out of space */
+            return -1;
+        } else {
+            int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE | MAP_FIXED;
+            void * mmap_start = mmap((void *) align_start, guest_size,
+                                     PROT_NONE, flags, -1, 0);
+            if (mmap_start != MAP_FAILED) {
+                munmap((void *) align_start, guest_size);
+                return (uintptr_t) mmap_start;
+            }
+            base += qemu_host_page_size;
+        }
+    }
+}
+
 /* Return value for guest_base, or -1 if no hole found. */
 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
                                long align)
@@ -2116,6 +2160,10 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
     /* Read brk after we've read the maps, which will malloc. */
     brk = (uintptr_t)sbrk(0);
 
+    if (!maps) {
+        return pgd_find_hole_fallback(guest_size, brk, align);
+    }
+
     /* The first hole is before the first map entry. */
     this_start = mmap_min_addr;
 
Signature · 1 line
2.20.1
#14 lore raw

PATCH 12/14 linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit

In reply to Alex Bennée

We rely on the pointer to wrap when accessing the high address of the COMMPAGE so it lands somewhere reasonable. However on 32 bit hosts we cannot afford just to map the entire 4gb address range. The old mmap trial and error code handled this by just checking we could map both the guest_base and the computed COMMPAGE address.

We can't just manipulate loadaddr to get what we want so we introduce an offset which pgb_find_hole can apply when looking for a gap for guest_base that ensures there is space left to map the COMMPAGE afterwards.

This is arguably a little inefficient for the one 32 bit value (kuser_helper_version) we need to keep there given all the actual code entries are picked up during the translation phase.

Message-Id: <[email protected]>

---
v3
- base offset on ARM_COMMPAGE
- ensure we MAP_FIXED the commpage
- support offset in pgd_find_hole_fallback

---
 linux-user/elfload.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
linux-user/elfload.c +17 -14
index 475d243f3bd..b5cb21384a1 100644
@@ -389,7 +389,7 @@ static bool init_guest_commpage(void)
 {
     void *want = g2h(ARM_COMMPAGE & -qemu_host_page_size);
     void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
-                      MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+                      MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
 
     if (addr == MAP_FAILED) {
         perror("Allocating guest commpage");
@@ -2113,7 +2113,8 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
  * only dumbly iterate up the host address space seeing if the
  * allocation would work.
  */
-static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, long align)
+static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
+                                        long align, uintptr_t offset)
 {
     uintptr_t base;
 
@@ -2123,7 +2124,7 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, lon
     while (true) {
         uintptr_t align_start, end;
         align_start = ROUND_UP(base, align);
-        end = align_start + guest_size;
+        end = align_start + guest_size + offset;
 
         /* if brk is anywhere in the range give ourselves some room to grow. */
         if (align_start <= brk && brk < end) {
@@ -2138,7 +2139,7 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, lon
                                      PROT_NONE, flags, -1, 0);
             if (mmap_start != MAP_FAILED) {
                 munmap((void *) align_start, guest_size);
-                return (uintptr_t) mmap_start;
+                return (uintptr_t) mmap_start + offset;
             }
             base += qemu_host_page_size;
         }
@@ -2147,7 +2148,7 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, lon
 
 /* Return value for guest_base, or -1 if no hole found. */
 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
-                               long align)
+                               long align, uintptr_t offset)
 {
     GSList *maps, *iter;
     uintptr_t this_start, this_end, next_start, brk;
@@ -2161,7 +2162,7 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
     brk = (uintptr_t)sbrk(0);
 
     if (!maps) {
-        return pgd_find_hole_fallback(guest_size, brk, align);
+        return pgd_find_hole_fallback(guest_size, brk, align, offset);
     }
 
     /* The first hole is before the first map entry. */
@@ -2173,7 +2174,7 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
 
         this_end = ((MapInfo *)iter->data)->start;
         next_start = ((MapInfo *)iter->data)->end;
-        align_start = ROUND_UP(this_start, align);
+        align_start = ROUND_UP(this_start + offset, align);
 
         /* Skip holes that are too small. */
         if (align_start >= this_end) {
@@ -2223,6 +2224,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
 {
     uintptr_t loaddr = orig_loaddr;
     uintptr_t hiaddr = orig_hiaddr;
+    uintptr_t offset = 0;
     uintptr_t addr;
 
     if (hiaddr != orig_hiaddr) {
@@ -2236,18 +2238,19 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
     if (ARM_COMMPAGE) {
         /*
          * Extend the allocation to include the commpage.
-         * For a 64-bit host, this is just 4GiB; for a 32-bit host,
-         * the address arithmetic will wrap around, but the difference
-         * will produce the correct allocation size.
+         * For a 64-bit host, this is just 4GiB; for a 32-bit host we
+         * need to ensure there is space bellow the guest_base so we
+         * can map the commpage in the place needed when the address
+         * arithmetic wraps around.
          */
         if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
-            hiaddr = (uintptr_t)4 << 30;
+            hiaddr = (uintptr_t) 4 << 30;
         } else {
-            loaddr = ARM_COMMPAGE & -align;
+            offset = -(ARM_COMMPAGE & -align);
         }
     }
 
-    addr = pgb_find_hole(loaddr, hiaddr - loaddr, align);
+    addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
     if (addr == -1) {
         /*
          * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
@@ -2282,7 +2285,7 @@ static void pgb_dynamic(const char *image_name, long align)
          * just above that, and maximises the positive guest addresses.
          */
         commpage = ARM_COMMPAGE & -align;
-        addr = pgb_find_hole(commpage, -commpage, align);
+        addr = pgb_find_hole(commpage, -commpage, align, 0);
         assert(addr != -1);
         guest_base = addr;
     }
Signature · 1 line
2.20.1
#15 lore raw

PATCH 13/14 tests/tcg: add simple commpage test case

In reply to Alex Bennée

The COMMPAGE are a number of kernel provided user-space routines for 32 bit ARM systems. Add a basic series of smoke tests to ensure it is working as it should.

Message-Id: <[email protected]>

---
 tests/tcg/arm/commpage.c      | 61 +++++++++++++++++++++++++++++++++++
 tests/tcg/arm/Makefile.target |  2 ++
 2 files changed, 63 insertions(+)
 create mode 100644 tests/tcg/arm/commpage.c
tests/tcg/arm/commpage.c new file +61 -0
new file mode 100644
index 00000000000..c76e70cb8bd
@@ -0,0 +1,61 @@
+/*
+ * Verify the COMMPAGE emulation
+ *
+ * The ARM commpage is a set of user space helper functions provided
+ * by the kernel in an effort to ease portability of user space code
+ * between different CPUs with potentially different capabilities. It
+ * is a 32 bit invention and similar to the vdso segment in many ways.
+ *
+ * The ABI is documented in the Linux kernel:
+ *     Documentation/arm/kernel_userspace_helpers.rst
+ *
+ * Copyright (c) 2020 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+
+#define ARM_COMMPAGE      (0xffff0f00u)
+#define ARM_KUSER_VERSION (*(int32_t *)(ARM_COMMPAGE + 0xfc))
+typedef void * (get_tls_fn)(void);
+#define ARM_KUSER_GET_TLS (*(get_tls_fn *)(ARM_COMMPAGE + 0xe0))
+typedef int (cmpxchg_fn)(int oldval, int newval, volatile int *ptr);
+#define ARM_KUSER_CMPXCHG (*(cmpxchg_fn *)(ARM_COMMPAGE + 0xc0))
+typedef void (dmb_fn)(void);
+#define ARM_KUSER_DMB (*(dmb_fn *)(ARM_COMMPAGE + 0xa0))
+typedef int (cmpxchg64_fn)(const int64_t *oldval,
+                           const int64_t *newval,
+                           volatile int64_t *ptr);
+#define ARM_KUSER_CMPXCHG64 (*(cmpxchg64_fn *)(ARM_COMMPAGE + 0x60))
+
+#define fail_unless(x)                                                  \
+    do {                                                                \
+        if (!(x)) {                                                     \
+            fprintf(stderr, "FAILED at %s:%d\n", __FILE__, __LINE__);   \
+            exit(EXIT_FAILURE);                                         \
+        }                                                               \
+    } while (0)
+
+
+int main(int argc, char *argv[argc])
+{
+    void *kuser_tls;
+    int val = 1;
+    const int64_t oldval = 1, newval = 2;
+    int64_t val64 = 1;
+
+    fail_unless(ARM_KUSER_VERSION == 0x5);
+    kuser_tls = ARM_KUSER_GET_TLS();
+    printf("TLS = %p\n", kuser_tls);
+    fail_unless(kuser_tls != 0);
+    fail_unless(ARM_KUSER_CMPXCHG(1, 2, &val) == 0);
+    printf("val = %d\n", val);
+    /* this is a crash test, not checking an actual barrier occurs */
+    ARM_KUSER_DMB();
+    fail_unless(ARM_KUSER_CMPXCHG64(&oldval, &newval, &val64) == 0);
+    printf("val64 = %lld\n", val64);
+    return 0;
+}
tests/tcg/arm/Makefile.target +2 -0
index 11c39c601ea..3da09a38be7 100644
@@ -68,6 +68,8 @@ run-semiconsole-arm: semiconsole-arm
 run-plugin-semiconsole-arm-with-%:
 	$(call skip-test, $<, "MANUAL ONLY")
 
+ARM_TESTS += commpage
+
 TESTS += $(ARM_TESTS)
 
 # On ARM Linux only supports 4k pages
Signature · 1 line
2.20.1
#16 lore raw

PATCH 14/14 linux-user: detect overflow of MAP_FIXED mmap

In reply to Alex Bennée

Relaxing the restrictions on 64 bit guests leads to the user being able to attempt to map right at the edge of addressable memory. This in turn lead to address overflow tripping the assert in page_set_flags when the end address wrapped around.

Detect the wrap earlier and correctly -ENOMEM the guest (in the reported case LTP mmap15).

Fixes 7d8cbbabcb
---
 linux-user/mmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/mmap.c +1 -1
index e3780337974..2e05bd499e6 100644
@@ -467,7 +467,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
          * It can fail only on 64-bit host with 32-bit target.
          * On any other target/host host mmap() handles this error correctly.
          */
-        if (!guest_range_valid(start, len)) {
+        if (end < start || !guest_range_valid(start, len)) {
             errno = ENOMEM;
             goto fail;
         }
Signature · 1 line
2.20.1
#17 lore raw

Re: [PATCH v1 14/14] linux-user: detect overflow of MAP_FIXED mmap

Quoted 9 lines · Alex Bennée

Relaxing the restrictions on 64 bit guests leads to the user being able to attempt to map right at the edge of addressable memory. This in turn lead to address overflow tripping the assert in page_set_flags when the end address wrapped around.

Detect the wrap earlier and correctly -ENOMEM the guest (in the reported case LTP mmap15).

Fixes 7d8cbbabcb
Quoted 19 lines
---
 linux-user/mmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/mmap.c +1 -1
index e3780337974..2e05bd499e6 100644
@@ -467,7 +467,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
          * It can fail only on 64-bit host with 32-bit target.
          * On any other target/host host mmap() handles this error correctly.
          */
-        if (!guest_range_valid(start, len)) {
+        if (end < start || !guest_range_valid(start, len)) {
             errno = ENOMEM;
             goto fail;
         }
#18 lore raw
In reply to Alex Bennée
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Subject: [PATCH v1 00/14] various fixes for next PR (testing, vhost, guest_base fixes)
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Auto packing the repository for optimum performance. You may also run "git gc" manually. See "git help gc" for more information. Switched to a new branch 'test'
8c192c8 linux-user: detect overflow of MAP_FIXED mmap
343b114 tests/tcg: add simple commpage test case
3ea9b6b linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit bd10df6 linux-user: provide fallback pgd_find_hole for bare chroots 140a4b0 hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE c3fdeb9 docker: update Ubuntu to 20.04
3827a17 tests/docker: fix pre-requisite for debian-tricore-cross 00ba0df iotests: 194: wait migration completion on target too 8252e14 .shippable: temporaily disable some cross builds
1027a95 .travis.yml: allow failure for unreliable hosts
d545513 exec: flush the whole TLB if a watchpoint crosses a page boundary c286730 tests/plugin: correctly honour io_count
573745f scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header 592de76 qemu-plugin.h: add missing include <stddef.h> to define size_t

=== OUTPUT BEGIN ===
1/14 Checking commit 592de76606a7 (qemu-plugin.h: add missing include <stddef.h> to define size_t) 2/14 Checking commit 573745fa77cd (scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header) 3/14 Checking commit c286730e2e6a (tests/plugin: correctly honour io_count)
4/14 Checking commit d545513f07b5 (exec: flush the whole TLB if a watchpoint crosses a page boundary) 5/14 Checking commit 1027a95a2300 (.travis.yml: allow failure for unreliable hosts)
6/14 Checking commit 8252e1457d91 (.shippable: temporaily disable some cross builds) 7/14 Checking commit 00ba0dfa3911 (iotests: 194: wait migration completion on target too) 8/14 Checking commit 3827a17199bf (tests/docker: fix pre-requisite for debian-tricore-cross) 9/14 Checking commit c3fdeb9b0a44 (docker: update Ubuntu to 20.04)
10/14 Checking commit 140a4b0c679f (hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE) 11/14 Checking commit bd10df6e7aff (linux-user: provide fallback pgd_find_hole for bare chroots)
WARNING: line over 80 characters
#46: FILE: linux-user/elfload.c:2116:
+static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, long align)

total: 0 errors, 1 warnings, 60 lines checked

Patch 11/14 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
12/14 Checking commit 3ea9b6bac8e8 (linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit) 13/14 Checking commit 343b11418044 (tests/tcg: add simple commpage test case)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#30:
new file mode 100644

ERROR: Use of volatile is usually wrong, please add a comment #59: FILE: tests/tcg/arm/commpage.c:25:
+typedef int (cmpxchg_fn)(int oldval, int newval, volatile int *ptr);

ERROR: Use of volatile is usually wrong, please add a comment #65: FILE: tests/tcg/arm/commpage.c:31:
+ volatile int64_t *ptr);

total: 2 errors, 1 warnings, 69 lines checked

Patch 13/14 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

14/14 Checking commit 8c192c8751c3 (linux-user: detect overflow of MAP_FIXED mmap) === OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#19 lore raw
In reply to Alex Bennée
Quoted 41 lines · Alex Bennée

Hi,

These are all the patches I've currently got which are ready for a pull request next week. I've included some patches which are destined to go in via other trees so I can keep the testing green on the CI.

In summary:

Some simple plugin cleanups (the reset remain in plugins/next) Reliability fixes for travis/shippable
iotest 194 fix (going in via block tree?)
docker updates (ubuntu and tricore fix)
vhost-user and TCG fix
more linux-user guest_base fixes

I'll certainly include the testing stuff in my PR but if others are happy for me to include bits touching their areas then shout and I'll include them in the PR.

The following need review:

- linux-user: detect overflow of MAP_FIXED mmap
- linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit
- linux-user: provide fallback pgd_find_hole for bare chroots
- tests/docker: fix pre-requisite for debian-tricore-cross
- hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
- .shippable: temporaily disable some cross builds
- exec: flush the whole TLB if a watchpoint crosses a page boundary

Alex Bennée (10):
tests/plugin: correctly honour io_count
exec: flush the whole TLB if a watchpoint crosses a page boundary .travis.yml: allow failure for unreliable hosts .shippable: temporaily disable some cross builds tests/docker: fix pre-requisite for debian-tricore-cross hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
linux-user: provide fallback pgd_find_hole for bare chroots
linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit tests/tcg: add simple commpage test case
linux-user: detect overflow of MAP_FIXED mmap

Quoted 8 lines

Emilio G. Cota (1):
qemu-plugin.h: add missing include <stddef.h> to define size_t

Paolo Bonzini (1):
docker: update Ubuntu to 20.04

Philippe Mathieu-Daudé (1):
scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header

What about Philippe's "tests: Remove unused bison/flex packages" v2 patch series from May 15th? I think you could include it here, too.

Thomas

#20 lore raw
Quoted 45 lines · Thomas Huth
Quoted 44 lines · Alex Bennée

Hi,

These are all the patches I've currently got which are ready for a pull request next week. I've included some patches which are destined to go in via other trees so I can keep the testing green on the CI.

In summary:

Some simple plugin cleanups (the reset remain in plugins/next) Reliability fixes for travis/shippable
iotest 194 fix (going in via block tree?)
docker updates (ubuntu and tricore fix)
vhost-user and TCG fix
more linux-user guest_base fixes

I'll certainly include the testing stuff in my PR but if others are happy for me to include bits touching their areas then shout and I'll include them in the PR.

The following need review:

- linux-user: detect overflow of MAP_FIXED mmap
- linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit
- linux-user: provide fallback pgd_find_hole for bare chroots
- tests/docker: fix pre-requisite for debian-tricore-cross
- hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
- .shippable: temporaily disable some cross builds
- exec: flush the whole TLB if a watchpoint crosses a page boundary

Alex Bennée (10):
tests/plugin: correctly honour io_count
exec: flush the whole TLB if a watchpoint crosses a page boundary .travis.yml: allow failure for unreliable hosts .shippable: temporaily disable some cross builds tests/docker: fix pre-requisite for debian-tricore-cross hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE
linux-user: provide fallback pgd_find_hole for bare chroots
linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit tests/tcg: add simple commpage test case
linux-user: detect overflow of MAP_FIXED mmap

Emilio G. Cota (1):
qemu-plugin.h: add missing include <stddef.h> to define size_t

Quoted 8 lines
Quoted 5 lines

Paolo Bonzini (1):
docker: update Ubuntu to 20.04

Philippe Mathieu-Daudé (1):
scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header

What about Philippe's "tests: Remove unused bison/flex packages" v2 patch series from May 15th? I think you could include it here, too.

Sure - grabbed.

Signature · 1 line
Alex Bennée