diff mbox

[v7,27/42] postcopy: ram_enable_notify to switch on userfault

Message ID 1434450415-11339-28-git-send-email-dgilbert@redhat.com
State New
Headers show

Commit Message

Dr. David Alan Gilbert June 16, 2015, 10:26 a.m. UTC
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

Mark the area of RAM as 'userfault'
Start up a fault-thread to handle any userfaults we might receive
from it (to be filled in later)

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
 include/migration/migration.h    |  3 ++
 include/migration/postcopy-ram.h |  6 ++++
 migration/postcopy-ram.c         | 69 +++++++++++++++++++++++++++++++++++++++-
 migration/savevm.c               |  9 ++++++
 4 files changed, 86 insertions(+), 1 deletion(-)

Comments

Juan Quintela July 13, 2015, 12:10 p.m. UTC | #1
"Dr. David Alan Gilbert (git)" <dgilbert@redhat.com> wrote:
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
>
> Mark the area of RAM as 'userfault'
> Start up a fault-thread to handle any userfaults we might receive
> from it (to be filled in later)
>
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  include/migration/migration.h    |  3 ++
>  include/migration/postcopy-ram.h |  6 ++++
>  migration/postcopy-ram.c         | 69 +++++++++++++++++++++++++++++++++++++++-
>  migration/savevm.c               |  9 ++++++
>  4 files changed, 86 insertions(+), 1 deletion(-)
>
> diff --git a/include/migration/migration.h b/include/migration/migration.h
> index 98e2568..e6585c5 100644
> --- a/include/migration/migration.h
> +++ b/include/migration/migration.h
> @@ -69,6 +69,9 @@ struct MigrationIncomingState {
>       */
>      QemuEvent      main_thread_load_event;
>  
> +    QemuThread     fault_thread;

Best name ever?  Well, it could be "wrong_thread" O:-)
> +static void *postcopy_ram_fault_thread(void *opaque)
> +{
> +    MigrationIncomingState *mis = (MigrationIncomingState *)opaque;

Uneeded cast.

> +
> +    fprintf(stderr, "postcopy_ram_fault_thread\n");
> +    /* TODO: In later patch */
> +    qemu_sem_post(&mis->fault_thread_sem);
> +    while (1) {
> +        /* TODO: In later patch */
> +    }
> +
> +    return NULL;
> +}
> +
> +int postcopy_ram_enable_notify(MigrationIncomingState *mis)
> +{
> +    /* Create the fault handler thread and wait for it to be ready */
> +    qemu_sem_init(&mis->fault_thread_sem, 0);
> +    qemu_thread_create(&mis->fault_thread, "postcopy/fault",
> +                       postcopy_ram_fault_thread, mis, QEMU_THREAD_JOINABLE);
> +    qemu_sem_wait(&mis->fault_thread_sem);
> +    qemu_sem_destroy(&mis->fault_thread_sem);
> +
> +    /* Mark so that we get notified of accesses to unwritten areas */
> +    if (qemu_ram_foreach_block(ram_block_enable_notify, mis)) {
> +        return -1;
> +    }
> +
> +    return 0;
> +}
> +
>  #else
>  /* No target OS support, stubs just fail */
> -

This belongs in a different patch O:-)

If you have to resend, just change them.

Reviewed-by: Juan Quintela <quintela@redhat.com>
Dr. David Alan Gilbert July 13, 2015, 5:36 p.m. UTC | #2
* Juan Quintela (quintela@redhat.com) wrote:
> "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com> wrote:
> > From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
> >
> > Mark the area of RAM as 'userfault'
> > Start up a fault-thread to handle any userfaults we might receive
> > from it (to be filled in later)
> >
> > Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  include/migration/migration.h    |  3 ++
> >  include/migration/postcopy-ram.h |  6 ++++
> >  migration/postcopy-ram.c         | 69 +++++++++++++++++++++++++++++++++++++++-
> >  migration/savevm.c               |  9 ++++++
> >  4 files changed, 86 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/migration/migration.h b/include/migration/migration.h
> > index 98e2568..e6585c5 100644
> > --- a/include/migration/migration.h
> > +++ b/include/migration/migration.h
> > @@ -69,6 +69,9 @@ struct MigrationIncomingState {
> >       */
> >      QemuEvent      main_thread_load_event;
> >  
> > +    QemuThread     fault_thread;
> 
> Best name ever?  Well, it could be "wrong_thread" O:-)

Well they are 'user faults'!   Not that we blame the user for
them personally of course.

> > +static void *postcopy_ram_fault_thread(void *opaque)
> > +{
> > +    MigrationIncomingState *mis = (MigrationIncomingState *)opaque;
> 
> Uneeded cast.

Thanks, gone.

> > +
> > +    fprintf(stderr, "postcopy_ram_fault_thread\n");
> > +    /* TODO: In later patch */
> > +    qemu_sem_post(&mis->fault_thread_sem);
> > +    while (1) {
> > +        /* TODO: In later patch */
> > +    }
> > +
> > +    return NULL;
> > +}
> > +
> > +int postcopy_ram_enable_notify(MigrationIncomingState *mis)
> > +{
> > +    /* Create the fault handler thread and wait for it to be ready */
> > +    qemu_sem_init(&mis->fault_thread_sem, 0);
> > +    qemu_thread_create(&mis->fault_thread, "postcopy/fault",
> > +                       postcopy_ram_fault_thread, mis, QEMU_THREAD_JOINABLE);
> > +    qemu_sem_wait(&mis->fault_thread_sem);
> > +    qemu_sem_destroy(&mis->fault_thread_sem);
> > +
> > +    /* Mark so that we get notified of accesses to unwritten areas */
> > +    if (qemu_ram_foreach_block(ram_block_enable_notify, mis)) {
> > +        return -1;
> > +    }
> > +
> > +    return 0;
> > +}
> > +
> >  #else
> >  /* No target OS support, stubs just fail */
> > -
> 
> This belongs in a different patch O:-)

Thanks, gone.

> If you have to resend, just change them.
> 
> Reviewed-by: Juan Quintela <quintela@redhat.com>

Dave

--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Amit Shah July 23, 2015, 5:22 a.m. UTC | #3
On (Tue) 16 Jun 2015 [11:26:40], Dr. David Alan Gilbert (git) wrote:
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
> 
> Mark the area of RAM as 'userfault'
> Start up a fault-thread to handle any userfaults we might receive
> from it (to be filled in later)
> 
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

Reviewed-by: Amit Shah <amit.shah@redhat.com>

		Amit
diff mbox

Patch

diff --git a/include/migration/migration.h b/include/migration/migration.h
index 98e2568..e6585c5 100644
--- a/include/migration/migration.h
+++ b/include/migration/migration.h
@@ -69,6 +69,9 @@  struct MigrationIncomingState {
      */
     QemuEvent      main_thread_load_event;
 
+    QemuThread     fault_thread;
+    QemuSemaphore  fault_thread_sem;
+
     /* For the kernel to send us notifications */
     int            userfault_fd;
     QEMUFile *return_path;
diff --git a/include/migration/postcopy-ram.h b/include/migration/postcopy-ram.h
index 4192108..8a8616b 100644
--- a/include/migration/postcopy-ram.h
+++ b/include/migration/postcopy-ram.h
@@ -17,6 +17,12 @@ 
 bool postcopy_ram_supported_by_host(void);
 
 /*
+ * Make all of RAM sensitive to accesses to areas that haven't yet been written
+ * and wire up anything necessary to deal with it.
+ */
+int postcopy_ram_enable_notify(MigrationIncomingState *mis);
+
+/*
  * Initialise postcopy-ram, setting the RAM to a state where we can go into
  * postcopy later; must be called prior to any precopy.
  * called from ram.c's similarly named ram_postcopy_incoming_init
diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index 35c87b4..7158d08 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -282,9 +282,71 @@  int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
     return 0;
 }
 
+/*
+ * Mark the given area of RAM as requiring notification to unwritten areas
+ * Used as a  callback on qemu_ram_foreach_block.
+ *   host_addr: Base of area to mark
+ *   offset: Offset in the whole ram arena
+ *   length: Length of the section
+ *   opaque: MigrationIncomingState pointer
+ * Returns 0 on success
+ */
+static int ram_block_enable_notify(const char *block_name, void *host_addr,
+                                   ram_addr_t offset, ram_addr_t length,
+                                   void *opaque)
+{
+    MigrationIncomingState *mis = opaque;
+    struct uffdio_register reg_struct;
+
+    reg_struct.range.start = (uintptr_t)host_addr;
+    reg_struct.range.len = length;
+    reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
+
+    /* Now tell our userfault_fd that it's responsible for this area */
+    if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, &reg_struct)) {
+        error_report("%s userfault register: %s", __func__, strerror(errno));
+        return -1;
+    }
+
+    return 0;
+}
+
+/*
+ * Handle faults detected by the USERFAULT markings
+ */
+static void *postcopy_ram_fault_thread(void *opaque)
+{
+    MigrationIncomingState *mis = (MigrationIncomingState *)opaque;
+
+    fprintf(stderr, "postcopy_ram_fault_thread\n");
+    /* TODO: In later patch */
+    qemu_sem_post(&mis->fault_thread_sem);
+    while (1) {
+        /* TODO: In later patch */
+    }
+
+    return NULL;
+}
+
+int postcopy_ram_enable_notify(MigrationIncomingState *mis)
+{
+    /* Create the fault handler thread and wait for it to be ready */
+    qemu_sem_init(&mis->fault_thread_sem, 0);
+    qemu_thread_create(&mis->fault_thread, "postcopy/fault",
+                       postcopy_ram_fault_thread, mis, QEMU_THREAD_JOINABLE);
+    qemu_sem_wait(&mis->fault_thread_sem);
+    qemu_sem_destroy(&mis->fault_thread_sem);
+
+    /* Mark so that we get notified of accesses to unwritten areas */
+    if (qemu_ram_foreach_block(ram_block_enable_notify, mis)) {
+        return -1;
+    }
+
+    return 0;
+}
+
 #else
 /* No target OS support, stubs just fail */
-
 bool postcopy_ram_supported_by_host(void)
 {
     error_report("%s: No OS support", __func__);
@@ -307,6 +369,11 @@  int postcopy_ram_discard_range(MigrationIncomingState *mis, uint8_t *start,
 {
     assert(0);
 }
+
+int postcopy_ram_enable_notify(MigrationIncomingState *mis)
+{
+    assert(0);
+}
 #endif
 
 /* ------------------------------------------------------------------------- */
diff --git a/migration/savevm.c b/migration/savevm.c
index f4de52d..b87238a 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -1324,6 +1324,15 @@  static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
         return -1;
     }
 
+    /*
+     * Sensitise RAM - can now generate requests for blocks that don't exist
+     * However, at this point the CPU shouldn't be running, and the IO
+     * shouldn't be doing anything yet so don't actually expect requests
+     */
+    if (postcopy_ram_enable_notify(mis)) {
+        return -1;
+    }
+
     /* TODO start up the postcopy listening thread */
     return 0;
 }