qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
To: qemu-devel@nongnu.org, mst@redhat.com,
	maxime.coquelin@redhat.com, marcandre.lureau@redhat.com,
	peterx@redhat.com, quintela@redhat.com
Cc: aarcange@redhat.com
Subject: [Qemu-devel] [PATCH v5 23/29] libvhost-user: mprotect & madvises for postcopy
Date: Mon, 12 Mar 2018 17:21:18 +0000	[thread overview]
Message-ID: <20180312172124.56461-24-dgilbert@redhat.com> (raw)
In-Reply-To: <20180312172124.56461-1-dgilbert@redhat.com>

From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

Clear the area and turn off THP.
PROT_NONE the area until after we've userfault advised it
to catch any unexpected changes.

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 contrib/libvhost-user/libvhost-user.c | 47 +++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 5 deletions(-)

diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c
index 6314549b65..5feed52098 100644
--- a/contrib/libvhost-user/libvhost-user.c
+++ b/contrib/libvhost-user/libvhost-user.c
@@ -454,7 +454,7 @@ vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
     int i;
     VhostUserMemory *memory = &vmsg->payload.memory;
     dev->nregions = memory->nregions;
-    /* TODO: Postcopy specific code */
+
     DPRINT("Nregions: %d\n", memory->nregions);
     for (i = 0; i < dev->nregions; i++) {
         void *mmap_addr;
@@ -478,9 +478,12 @@ vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
 
         /* We don't use offset argument of mmap() since the
          * mapped address has to be page aligned, and we use huge
-         * pages.  */
+         * pages.
+         * In postcopy we're using PROT_NONE here to catch anyone
+         * accessing it before we userfault
+         */
         mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
-                         PROT_READ | PROT_WRITE, MAP_SHARED,
+                         PROT_NONE, MAP_SHARED,
                          vmsg->fds[i], 0);
 
         if (mmap_addr == MAP_FAILED) {
@@ -519,12 +522,38 @@ vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
     /* OK, now we can go and register the memory and generate faults */
     for (i = 0; i < dev->nregions; i++) {
         VuDevRegion *dev_region = &dev->regions[i];
+        int ret;
 #ifdef UFFDIO_REGISTER
         /* We should already have an open ufd. Mark each memory
          * range as ufd.
-         * Note: Do we need any madvises? Well it's not been accessed
-         * yet, still probably need no THP to be safe, discard to be safe?
+         * Discard any mapping we have here; note I can't use MADV_REMOVE
+         * or fallocate to make the hole since I don't want to lose
+         * data that's already arrived in the shared process.
+         * TODO: How to do hugepage
          */
+        ret = madvise((void *)dev_region->mmap_addr,
+                      dev_region->size + dev_region->mmap_offset,
+                      MADV_DONTNEED);
+        if (ret) {
+            fprintf(stderr,
+                    "%s: Failed to madvise(DONTNEED) region %d: %s\n",
+                    __func__, i, strerror(errno));
+        }
+        /* Turn off transparent hugepages so we dont get lose wakeups
+         * in neighbouring pages.
+         * TODO: Turn this backon later.
+         */
+        ret = madvise((void *)dev_region->mmap_addr,
+                      dev_region->size + dev_region->mmap_offset,
+                      MADV_NOHUGEPAGE);
+        if (ret) {
+            /* Note: This can happen legally on kernels that are configured
+             * without madvise'able hugepages
+             */
+            fprintf(stderr,
+                    "%s: Failed to madvise(NOHUGEPAGE) region %d: %s\n",
+                    __func__, i, strerror(errno));
+        }
         struct uffdio_register reg_struct;
         reg_struct.range.start = (uintptr_t)dev_region->mmap_addr;
         reg_struct.range.len = dev_region->size + dev_region->mmap_offset;
@@ -546,6 +575,14 @@ vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
         }
         DPRINT("%s: region %d: Registered userfault for %llx + %llx\n",
                 __func__, i, reg_struct.range.start, reg_struct.range.len);
+        /* Now it's registered we can let the client at it */
+        if (mprotect((void *)dev_region->mmap_addr,
+                     dev_region->size + dev_region->mmap_offset,
+                     PROT_READ | PROT_WRITE)) {
+            vu_panic(dev, "failed to mprotect region %d for postcopy (%s)",
+                     i, strerror(errno));
+            return false;
+        }
         /* TODO: Stash 'zero' support flags somewhere */
 #endif
     }
-- 
2.14.3

  parent reply	other threads:[~2018-03-12 17:22 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-12 17:20 [Qemu-devel] [PATCH v5 00/29] postcopy+vhost-user/shared ram Dr. David Alan Gilbert (git)
2018-03-12 17:20 ` [Qemu-devel] [PATCH v5 01/29] migrate: Update ram_block_discard_range for shared Dr. David Alan Gilbert (git)
2018-03-12 17:20 ` [Qemu-devel] [PATCH v5 02/29] qemu_ram_block_host_offset Dr. David Alan Gilbert (git)
2018-03-12 17:20 ` [Qemu-devel] [PATCH v5 03/29] postcopy: use UFFDIO_ZEROPAGE only when available Dr. David Alan Gilbert (git)
2018-03-12 17:20 ` [Qemu-devel] [PATCH v5 04/29] postcopy: Add notifier chain Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 05/29] postcopy: Add vhost-user flag for postcopy and check it Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 06/29] vhost-user: Add 'VHOST_USER_POSTCOPY_ADVISE' message Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 07/29] libvhost-user: Support sending fds back to qemu Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 08/29] libvhost-user: Open userfaultfd Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 09/29] postcopy: Allow registering of fd handler Dr. David Alan Gilbert (git)
2018-03-13 20:34   ` Michael S. Tsirkin
2018-03-14 10:05     ` Dr. David Alan Gilbert
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 10/29] vhost+postcopy: Register shared ufd with postcopy Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 11/29] vhost+postcopy: Transmit 'listen' to slave Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 12/29] postcopy+vhost-user: Split set_mem_table for postcopy Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 13/29] migration/ram: ramblock_recv_bitmap_test_byte_offset Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 14/29] libvhost-user+postcopy: Register new regions with the ufd Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 15/29] vhost+postcopy: Send address back to qemu Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 16/29] vhost+postcopy: Stash RAMBlock and offset Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 17/29] vhost+postcopy: Helper to send requests to source for shared pages Dr. David Alan Gilbert (git)
2018-03-20 14:54   ` Michael S. Tsirkin
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 18/29] vhost+postcopy: Resolve client address Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 19/29] postcopy: helper for waking shared Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 20/29] postcopy: postcopy_notify_shared_wake Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 21/29] vhost+postcopy: Add vhost waker Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 22/29] vhost+postcopy: Call wakeups Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` Dr. David Alan Gilbert (git) [this message]
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 24/29] vhost-user: Add VHOST_USER_POSTCOPY_END message Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 25/29] vhost+postcopy: Wire up POSTCOPY_END notify Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 26/29] vhost: Huge page align and merge Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 27/29] postcopy: Allow shared memory Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 28/29] libvhost-user: Claim support for postcopy Dr. David Alan Gilbert (git)
2018-03-12 17:21 ` [Qemu-devel] [PATCH v5 29/29] postcopy shared docs Dr. David Alan Gilbert (git)
2018-03-13 21:14 ` [Qemu-devel] [PATCH v5 00/29] postcopy+vhost-user/shared ram Michael S. Tsirkin
2018-03-14 11:49   ` Dr. David Alan Gilbert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180312172124.56461-24-dgilbert@redhat.com \
    --to=dgilbert@redhat.com \
    --cc=aarcange@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=maxime.coquelin@redhat.com \
    --cc=mst@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).