Skip to contentSkip to conversation

Convert live migration to memory API

PATCH In review Avi Kivity 2 participants 14 messages Dec 21, 2011

lore
Share & tools
lore
author
#6 lore raw

PATCH 4/8 Remove support for version 3 ram_load

In reply to Avi Kivity

Version 3 ram_load depends on ram_addrs, which are not stable. Version 4 was introduced in 0.13 (and RHEL 6), so this means live migration from 0.12 and earlier to 1.1 or later will not work.

---
 arch_init.c |   18 ++++--------------
 1 files changed, 4 insertions(+), 14 deletions(-)
arch_init.c +4 -14
index 8a3f052..9b8a1f3 100644
@@ -366,7 +366,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
     int flags;
     int error;
 
-    if (version_id < 3 || version_id > 4) {
+    if (version_id < 4 || version_id > 4) {
         return -EINVAL;
     }
 
@@ -377,11 +377,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
         addr &= TARGET_PAGE_MASK;
 
         if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
-            if (version_id == 3) {
-                if (addr != ram_bytes_total()) {
-                    return -EINVAL;
-                }
-            } else {
+            if (version_id == 4) {
                 /* Synchronize RAM block list */
                 char id[256];
                 ram_addr_t length;
@@ -419,10 +415,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
             void *host;
             uint8_t ch;
 
-            if (version_id == 3)
-                host = qemu_get_ram_ptr(addr);
-            else
-                host = host_from_stream_offset(f, addr, flags);
+            host = host_from_stream_offset(f, addr, flags);
             if (!host) {
                 return -EINVAL;
             }
@@ -438,10 +431,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
         } else if (flags & RAM_SAVE_FLAG_PAGE) {
             void *host;
 
-            if (version_id == 3)
-                host = qemu_get_ram_ptr(addr);
-            else
-                host = host_from_stream_offset(f, addr, flags);
+            host = host_from_stream_offset(f, addr, flags);
 
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
         }
Signature · 1 line
1.7.7.1

Avi Kivity

author
#0 lore raw

cover Convert live migration to memory API

These patches, on top of "vmstate, memory: decouple vmstate from memory API", convert live migration to use the memory API.

Patch 4 is an ABI change, please review carefully.

Avi Kivity (8):
Store MemoryRegion in RAMBlock
Switch ram_save to the memory API
Sort RAMBlocks by ID for migration, not by ram_addr Remove support for version 3 ram_load
Convert ram_load() to the memory API
memory: obsolete cpu_physical_memory_[gs]et_dirty_tracking()
xen: convert framebuffer dirty tracking to memory API
memory: obsolete more dirty memory related functions

 arch_init.c     |   71 +++++++++++++++++++-----------------------------------
 cpu-all.h       |   54 +-----------------------------------------
 exec-obsolete.h |   51 +++++++++++++++++++++++++++++++++++++++
 exec.c          |   11 +-------
 memory.c        |    2 +
 xen-all.c       |    5 +--
 6 files changed, 82 insertions(+), 112 deletions(-)
Signature · 1 line
1.7.7.1
author
#11 lore raw

PATCH 7/8 xen: convert framebuffer dirty tracking to memory API

---
 xen-all.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
xen-all.c +2 -3
index df70592..8f9db95 100644
@@ -408,7 +408,6 @@ static int xen_sync_dirty_bitmap(XenIOState *state,
                                  ram_addr_t size)
 {
     target_phys_addr_t npages = size >> TARGET_PAGE_BITS;
-    target_phys_addr_t vram_offset = 0;
     const int width = sizeof(unsigned long) * 8;
     unsigned long bitmap[(npages + width - 1) / width];
     int rc, i, j;
@@ -425,7 +424,6 @@ static int xen_sync_dirty_bitmap(XenIOState *state,
     } else if (state->log_for_dirtybit != physmap) {
         return -1;
     }
-    vram_offset = physmap->phys_offset;
 
     rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
                                  start_addr >> TARGET_PAGE_BITS, npages,
@@ -439,7 +437,8 @@ static int xen_sync_dirty_bitmap(XenIOState *state,
         while (map != 0) {
             j = ffsl(map) - 1;
             map &= ~(1ul << j);
-            cpu_physical_memory_set_dirty(vram_offset + (i * width + j) * TARGET_PAGE_SIZE);
+            memory_region_set_dirty(framebuffer,
+                                    (i * width + j) * TARGET_PAGE_SIZE);
         };
     }
 
Signature · 1 line
1.7.7.1
author
#1 lore raw

PATCH 1/8 Store MemoryRegion in RAMBlock

In reply to Avi Kivity

As a step in moving live migration from RAMBlocks to MemoryRegions, store the MemoryRegion in a RAMBlock.

---
 cpu-all.h |    1 +
 exec.c    |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
cpu-all.h +1 -0
index 734833a..4acaa8b 100644
@@ -476,6 +476,7 @@ extern ram_addr_t ram_size;
 #define RAM_PREALLOC_MASK   (1 << 0)
 
 typedef struct RAMBlock {
+    struct MemoryRegion *mr;
     uint8_t *host;
     ram_addr_t offset;
     ram_addr_t length;
exec.c +1 -0
index dffceb9..a4116d9 100644
@@ -2793,6 +2793,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
     size = TARGET_PAGE_ALIGN(size);
     new_block = g_malloc0(sizeof(*new_block));
 
+    new_block->mr = mr;
     new_block->offset = find_ram_offset(size);
     if (host) {
         new_block->host = host;
Signature · 1 line
1.7.7.1
author
#3 lore raw

PATCH 3/8 Sort RAMBlocks by ID for migration, not by ram_addr

In reply to Avi Kivity

ram_addr is (a) unstable (b) going away. Sort by idstr instead.

---
 arch_init.c |    8 ++------
 1 files changed, 2 insertions(+), 6 deletions(-)
arch_init.c +2 -6
index 2743bfd..8a3f052 100644
@@ -217,12 +217,8 @@ static int block_compar(const void *a, const void *b)
 {
     RAMBlock * const *ablock = a;
     RAMBlock * const *bblock = b;
-    if ((*ablock)->offset < (*bblock)->offset) {
-        return -1;
-    } else if ((*ablock)->offset > (*bblock)->offset) {
-        return 1;
-    }
-    return 0;
+
+    return strcmp((*ablock)->idstr, (*bblock)->idstr);
 }
 
 static void sort_ram_list(void)
Signature · 1 line
1.7.7.1
author
#10 lore raw

PATCH 6/8 memory: obsolete cpu_physical_memory_[gs]et_dirty_tracking()

In reply to Avi Kivity

The getter is no longer used, so it is completely removed.

---
 arch_init.c     |    7 +++----
 cpu-all.h       |    4 ----
 exec-obsolete.h |    2 ++
 exec.c          |   10 ----------
 memory.c        |    2 ++
 5 files changed, 7 insertions(+), 18 deletions(-)
arch_init.c +3 -4
index 381c055..d3adacf 100644
@@ -251,7 +251,7 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
     int ret;
 
     if (stage < 0) {
-        cpu_physical_memory_set_dirty_tracking(0);
+        memory_global_dirty_log_stop();
         return 0;
     }
 
@@ -274,8 +274,7 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
             }
         }
 
-        /* Enable dirty memory tracking */
-        cpu_physical_memory_set_dirty_tracking(1);
+        memory_global_dirty_log_start();
 
         qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
 
@@ -320,7 +319,7 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
         while ((bytes_sent = ram_save_block(f)) != 0) {
             bytes_transferred += bytes_sent;
         }
-        cpu_physical_memory_set_dirty_tracking(0);
+        memory_global_dirty_log_stop();
     }
 
     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
cpu-all.h +0 -4
index 4acaa8b..2fdb856 100644
@@ -566,10 +566,6 @@ void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
                                      int dirty_flags);
 void cpu_tlb_update_dirty(CPUState *env);
 
-int cpu_physical_memory_set_dirty_tracking(int enable);
-
-int cpu_physical_memory_get_dirty_tracking(void);
-
 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
 #endif /* !CONFIG_USER_ONLY */
 
exec-obsolete.h +2 -0
index 3a2faae..880105e 100644
@@ -61,6 +61,8 @@ static inline void cpu_register_physical_memory(target_phys_addr_t start_addr,
 void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size);
 void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size);
 
+int cpu_physical_memory_set_dirty_tracking(int enable);
+
 #endif
 
 #endif
exec.c +0 -10
index a4116d9..28c057c 100644
@@ -2008,19 +2008,9 @@ int cpu_physical_memory_set_dirty_tracking(int enable)
 {
     int ret = 0;
     in_migration = enable;
-    if (enable) {
-        memory_global_dirty_log_start();
-    } else {
-        memory_global_dirty_log_stop();
-    }
     return ret;
 }
 
-int cpu_physical_memory_get_dirty_tracking(void)
-{
-    return in_migration;
-}
-
 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
 {
     ram_addr_t ram_addr;
memory.c +2 -0
index f7b3d50..868ffd0 100644
@@ -1493,6 +1493,7 @@ void memory_global_dirty_log_start(void)
 {
     MemoryListener *listener;
 
+    cpu_physical_memory_set_dirty_tracking(1);
     global_dirty_log = true;
     QLIST_FOREACH(listener, &memory_listeners, link) {
         listener->log_global_start(listener);
@@ -1507,6 +1508,7 @@ void memory_global_dirty_log_stop(void)
     QLIST_FOREACH(listener, &memory_listeners, link) {
         listener->log_global_stop(listener);
     }
+    cpu_physical_memory_set_dirty_tracking(0);
 }
 
 static void listener_add_address_space(MemoryListener *listener,
Signature · 1 line
1.7.7.1
author
#2 lore raw

PATCH 2/8 Switch ram_save to the memory API

In reply to Avi Kivity

Avoid using ram_addr_t, instead use (MemoryRegion *, offset) pairs.

---
 arch_init.c |   34 ++++++++++++++--------------------
 1 files changed, 14 insertions(+), 20 deletions(-)
arch_init.c +14 -20
index ceef26e..2743bfd 100644
@@ -117,24 +117,22 @@ static int ram_save_block(QEMUFile *f)
 {
     RAMBlock *block = last_block;
     ram_addr_t offset = last_offset;
-    ram_addr_t current_addr;
     int bytes_sent = 0;
+    MemoryRegion *mr;
 
     if (!block)
         block = QLIST_FIRST(&ram_list.blocks);
 
-    current_addr = block->offset + offset;
-
     do {
-        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
+        mr = block->mr;
+        if (memory_region_get_dirty(mr, offset, DIRTY_MEMORY_MIGRATION)) {
             uint8_t *p;
             int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
 
-            cpu_physical_memory_reset_dirty(current_addr,
-                                            current_addr + TARGET_PAGE_SIZE,
-                                            MIGRATION_DIRTY_FLAG);
+            memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
+                                      DIRTY_MEMORY_MIGRATION);
 
-            p = block->host + offset;
+            p = memory_region_get_ram_ptr(mr) + offset;
 
             if (is_dup_page(p, *p)) {
                 qemu_put_be64(f, offset | cont | RAM_SAVE_FLAG_COMPRESS);
@@ -166,10 +164,7 @@ static int ram_save_block(QEMUFile *f)
             if (!block)
                 block = QLIST_FIRST(&ram_list.blocks);
         }
-
-        current_addr = block->offset + offset;
-
-    } while (current_addr != last_block->offset + last_offset);
+    } while (block != last_block || offset != last_offset);
 
     last_block = block;
     last_offset = offset;
@@ -186,9 +181,9 @@ static ram_addr_t ram_save_remaining(void)
 
     QLIST_FOREACH(block, &ram_list.blocks, next) {
         ram_addr_t addr;
-        for (addr = block->offset; addr < block->offset + block->length;
-             addr += TARGET_PAGE_SIZE) {
-            if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) {
+        for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
+            if (memory_region_get_dirty(block->mr, addr,
+                                        DIRTY_MEMORY_MIGRATION)) {
                 count++;
             }
         }
@@ -275,11 +270,10 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
 
         /* Make sure all dirty bits are set */
         QLIST_FOREACH(block, &ram_list.blocks, next) {
-            for (addr = block->offset; addr < block->offset + block->length;
-                 addr += TARGET_PAGE_SIZE) {
-                if (!cpu_physical_memory_get_dirty(addr,
-                                                   MIGRATION_DIRTY_FLAG)) {
-                    cpu_physical_memory_set_dirty(addr);
+            for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
+                if (!memory_region_get_dirty(block->mr, addr,
+                                             DIRTY_MEMORY_MIGRATION)) {
+                    memory_region_set_dirty(block->mr, addr);
                 }
             }
         }
Signature · 1 line
1.7.7.1
author
#9 lore raw

PATCH 5/8 Convert ram_load() to the memory API

In reply to Avi Kivity
---
 arch_init.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch_init.c +2 -2
index 9b8a1f3..381c055 100644
@@ -344,7 +344,7 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
             return NULL;
         }
 
-        return block->host + offset;
+        return memory_region_get_ram_ptr(block->mr) + offset;
     }
 
     len = qemu_get_byte(f);
@@ -353,7 +353,7 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
 
     QLIST_FOREACH(block, &ram_list.blocks, next) {
         if (!strncmp(id, block->idstr, sizeof(id)))
-            return block->host + offset;
+            return memory_region_get_ram_ptr(block->mr) + offset;
     }
 
     fprintf(stderr, "Can't find block %s!\n", id);
Signature · 1 line
1.7.7.1
author
#12 lore raw

PATCH 8/8 memory: obsolete more dirty memory related functions

In reply to Avi Kivity

No longer used outside memory.c and exec.c.

---
 cpu-all.h       |   49 -------------------------------------------------
 exec-obsolete.h |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 49 deletions(-)
cpu-all.h +0 -49
index 2fdb856..31d5b27 100644
@@ -515,55 +515,6 @@ extern int mem_prealloc;
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO        (1 << 5)
 
-#define VGA_DIRTY_FLAG       0x01
-#define CODE_DIRTY_FLAG      0x02
-#define MIGRATION_DIRTY_FLAG 0x08
-
-/* read dirty bit (return 0 or 1) */
-static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
-{
-    return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
-}
-
-static inline int cpu_physical_memory_get_dirty_flags(ram_addr_t addr)
-{
-    return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS];
-}
-
-static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
-                                                int dirty_flags)
-{
-    return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
-}
-
-static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
-{
-    ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] = 0xff;
-}
-
-static inline int cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
-                                                      int dirty_flags)
-{
-    return ram_list.phys_dirty[addr >> TARGET_PAGE_BITS] |= dirty_flags;
-}
-
-static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
-                                                        int length,
-                                                        int dirty_flags)
-{
-    int i, mask, len;
-    uint8_t *p;
-
-    len = length >> TARGET_PAGE_BITS;
-    mask = ~dirty_flags;
-    p = ram_list.phys_dirty + (start >> TARGET_PAGE_BITS);
-    for (i = 0; i < len; i++) {
-        p[i] &= mask;
-    }
-}
-
-void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
-                                     int dirty_flags);
 void cpu_tlb_update_dirty(CPUState *env);
 
 void dump_exec_info(FILE *f, fprintf_function cpu_fprintf);
exec-obsolete.h +49 -0

55 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#4 lore raw

Re: [Qemu-devel] [PATCH 3/8] Sort RAMBlocks by ID for migration, not by ram_addr

In reply to Avi Kivity
Quoted 3 lines · Avi Kivity

ram_addr is (a) unstable (b) going away. Sort by idstr instead.

I don't see this as a problem, per say, but this is a significant behavioral change. ram_addr does correspond roughly to the location in memory and historically we would send memory starting from 0 upward whereas now, the order that we send RAMBlocks will be random for all intents and purposes.

Again, I don't think it's a problem, but we should note this in the commit message in case it creates a problem down the road.

Regards,

Anthony Liguori

Quoted 23 lines
---
  arch_init.c |    8 ++------
  1 files changed, 2 insertions(+), 6 deletions(-)
arch_init.c +2 -6
index 2743bfd..8a3f052 100644
@@ -217,12 +217,8 @@ static int block_compar(const void *a, const void *b)
  {
      RAMBlock * const *ablock = a;
      RAMBlock * const *bblock = b;
-    if ((*ablock)->offset<  (*bblock)->offset) {
-        return -1;
-    } else if ((*ablock)->offset>  (*bblock)->offset) {
-        return 1;
-    }
-    return 0;
+
+    return strcmp((*ablock)->idstr, (*bblock)->idstr);
  }
 
  static void sort_ram_list(void)
Reviewed-by on v1 4/8
#7 lore raw

Re: [Qemu-devel] [PATCH 4/8] Remove support for version 3 ram_load

In reply to Avi Kivity
Quoted 3 lines · Avi Kivity

Version 3 ram_load depends on ram_addrs, which are not stable. Version 4 was introduced in 0.13 (and RHEL 6), so this means live migration from 0.12 and earlier to 1.1 or later will not work.

Can you please make a note on http://wiki.qemu.org/ChangeLog/Next

Regards,

Anthony Liguori

Quoted 56 lines
---
  arch_init.c |   18 ++++--------------
  1 files changed, 4 insertions(+), 14 deletions(-)
arch_init.c +4 -14
index 8a3f052..9b8a1f3 100644
@@ -366,7 +366,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
      int flags;
      int error;
 
-    if (version_id<  3 || version_id>  4) {
+    if (version_id<  4 || version_id>  4) {
          return -EINVAL;
      }
 
@@ -377,11 +377,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
          addr&= TARGET_PAGE_MASK;
 
          if (flags&  RAM_SAVE_FLAG_MEM_SIZE) {
-            if (version_id == 3) {
-                if (addr != ram_bytes_total()) {
-                    return -EINVAL;
-                }
-            } else {
+            if (version_id == 4) {
                  /* Synchronize RAM block list */
                  char id[256];
                  ram_addr_t length;
@@ -419,10 +415,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
              void *host;
              uint8_t ch;
 
-            if (version_id == 3)
-                host = qemu_get_ram_ptr(addr);
-            else
-                host = host_from_stream_offset(f, addr, flags);
+            host = host_from_stream_offset(f, addr, flags);
              if (!host) {
                  return -EINVAL;
              }
@@ -438,10 +431,7 @@ int ram_load(QEMUFile *f, void *opaque, int version_id)
          } else if (flags&  RAM_SAVE_FLAG_PAGE) {
              void *host;
 
-            if (version_id == 3)
-                host = qemu_get_ram_ptr(addr);
-            else
-                host = host_from_stream_offset(f, addr, flags);
+            host = host_from_stream_offset(f, addr, flags);
 
              qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
          }
Reviewed-by on the v1 series
#13 lore raw
In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

These patches, on top of "vmstate, memory: decouple vmstate from memory API", convert live migration to use the memory API.

Patch 4 is an ABI change, please review carefully.

Using ram_addr_t for migration was badly, badly broken. I think it's been long enough that we can safely remove that logic.

Other than my request for a better commit message, this whole series:

Regards,

Anthony Liguori

Quoted 19 lines

Avi Kivity (8):
Store MemoryRegion in RAMBlock
Switch ram_save to the memory API
Sort RAMBlocks by ID for migration, not by ram_addr Remove support for version 3 ram_load
Convert ram_load() to the memory API
memory: obsolete cpu_physical_memory_[gs]et_dirty_tracking()
xen: convert framebuffer dirty tracking to memory API
memory: obsolete more dirty memory related functions

  arch_init.c     |   71 +++++++++++++++++++-----------------------------------
  cpu-all.h       |   54 +-----------------------------------------
  exec-obsolete.h |   51 +++++++++++++++++++++++++++++++++++++++
  exec.c          |   11 +-------
  memory.c        |    2 +
  xen-all.c       |    5 +--
  6 files changed, 82 insertions(+), 112 deletions(-)
author
#5 lore raw

Re: [Qemu-devel] [PATCH 3/8] Sort RAMBlocks by ID for migration, not by ram_addr

In reply to Anthony Liguori
Quoted 5 lines · Anthony Liguori
Quoted 3 lines · Avi Kivity

ram_addr is (a) unstable (b) going away. Sort by idstr instead.

Quoted 9 lines

I don't see this as a problem, per say, but this is a significant behavioral change. ram_addr does correspond roughly to the location in memory and historically we would send memory starting from 0 upward whereas now, the order that we send RAMBlocks will be random for all intents and purposes.

Again, I don't think it's a problem, but we should note this in the commit message in case it creates a problem down the road.

I (and the new commit message) note that the sort order used to be random a while ago (before b2e0a138e), so this isn't entirely new.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#8 lore raw

Re: [Qemu-devel] [PATCH 4/8] Remove support for version 3 ram_load

In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 5 lines · Avi Kivity

Version 3 ram_load depends on ram_addrs, which are not stable. Version 4
was introduced in 0.13 (and RHEL 6), so this means live migration from 0.12
and earlier to 1.1 or later will not work.

Can you please make a note on http://wiki.qemu.org/ChangeLog/Next

Done.

Signature · 1 line
error compiling committee.c: too many arguments to function