Skip to contentSkip to conversation

dataplane: RCU dirty memory bitmap for safe RAM hotplug

PATCH In review Stefan Hajnoczi 3 participants 6 messages Jan 25, 2016 – Jan 28, 2016

lore
Share & tools
lore
#0 lore raw

cover dataplane: RCU dirty memory bitmap for safe RAM hotplug

Make RAM hotplug play nicely with dataplane. By RCU-protecting the dirty memory bitmap it is possible to hotplug RAM while other threads are dirtying guest RAM.

See Patch 1 for the meat of the code. Patch 2 is just a fixup on top of Paolo's dataplane branch.

This series is based on: https://github.com/bonzini/qemu.git dataplane

Stefan Hajnoczi (2):
memory: RCU ram_list.dirty_memory[] for safe RAM hotplug
virtio-blk: drop unused migration.h include

 exec.c                  |  73 +++++++++++++++----
 hw/block/virtio-blk.c   |   1 -
 include/exec/ram_addr.h | 189 ++++++++++++++++++++++++++++++++++++++++++------
 migration/ram.c         |   4 -
 4 files changed, 223 insertions(+), 44 deletions(-)
Signature · 1 line
2.5.0
#2 lore raw

PATCH 2/2 virtio-blk: drop unused migration.h include

The migration blocker functionality is no longer necessary since dataplane is now compatible with live migration. Drop the unused header include.

---
This patch can be squashed into "virtio-blk: do not use vring in dataplane".

---
 hw/block/virtio-blk.c | 1 -
 1 file changed, 1 deletion(-)
hw/block/virtio-blk.c +0 -1
index 1106d13..3e34d35 100644
@@ -20,7 +20,6 @@
 #include "sysemu/blockdev.h"
 #include "hw/virtio/virtio-blk.h"
 #include "dataplane/virtio-blk.h"
-#include "migration/migration.h"
 #include "block/scsi.h"
 #ifdef __linux__
 # include <scsi/sg.h>
Signature · 1 line
2.5.0
#1 lore raw

PATCH 1/2 memory: RCU ram_list.dirty_memory[] for safe RAM hotplug

In reply to Stefan Hajnoczi

Although accesses to ram_list.dirty_memory[] use atomics so multiple threads can safely dirty the bitmap, the data structure is not fully thread-safe yet.

This patch handles the RAM hotplug case where ram_list.dirty_memory[] is grown. ram_list.dirty_memory[] is change from a regular bitmap to an RCU array of pointers to fixed-size bitmap blocks. Threads can continue accessing bitmap blocks while the array is being extended. See the comments in the code for an in-depth explanation of struct DirtyMemoryBlocks.

I have tested that live migration with virtio-blk dataplane works.

---
Note this patch is based on Paolo's https://github.com/bonzini/qemu.git
dataplane branch.

---
 exec.c                  |  73 +++++++++++++++----
 include/exec/ram_addr.h | 189 ++++++++++++++++++++++++++++++++++++++++++------
 migration/ram.c         |   4 -
 3 files changed, 223 insertions(+), 43 deletions(-)
exec.c +58 -15
index 7f0ce42..25ecf79 100644
@@ -945,8 +945,9 @@ bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
                                               ram_addr_t length,
                                               unsigned client)
 {
+    DirtyMemoryBlocks *blocks;
     unsigned long end, page;
-    bool dirty;
+    bool dirty = false;
 
     if (length == 0) {
         return false;
@@ -954,8 +955,22 @@ bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
 
     end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
     page = start >> TARGET_PAGE_BITS;
-    dirty = bitmap_test_and_clear_atomic(ram_list.dirty_memory[client],
-                                         page, end - page);
+
+    rcu_read_lock();
+
+    blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
+
+    while (page < end) {
+        unsigned long idx = page / DIRTY_MEMORY_BLOCK_SIZE;
+        unsigned long offset = page % DIRTY_MEMORY_BLOCK_SIZE;
+        unsigned long num = MIN(end - page, DIRTY_MEMORY_BLOCK_SIZE - offset);
+
+        dirty |= bitmap_test_and_clear_atomic(blocks->blocks[idx],
+                                              offset, num);
+        page += num;
+    }
+
+    rcu_read_unlock();
 
     if (dirty && tcg_enabled()) {
         tlb_reset_dirty_range_all(start, length);
@@ -1469,6 +1484,45 @@ int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp)
     return 0;
 }
 
+/* Called with ram_list.mutex held */
+static void dirty_memory_extend(ram_addr_t old_ram_size,
+                                ram_addr_t new_ram_size)
+{
+    ram_addr_t old_num_blocks = DIV_ROUND_UP(old_ram_size,
+                                             DIRTY_MEMORY_BLOCK_SIZE);
+    ram_addr_t new_num_blocks = DIV_ROUND_UP(new_ram_size,
+                                             DIRTY_MEMORY_BLOCK_SIZE);
+    int i;
+
+    /* Only need to extend if block count increased */
+    if (new_num_blocks <= old_num_blocks) {
+        return;
+    }
+
+    for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
+        DirtyMemoryBlocks *old_blocks;
+        DirtyMemoryBlocks *new_blocks;
+        int j;
+
+        old_blocks = atomic_rcu_read(&ram_list.dirty_memory[i]);
+        new_blocks = g_malloc(sizeof(*new_blocks) +
+                              sizeof(new_blocks->blocks[0]) * new_num_blocks);
+
+        memcpy(new_blocks->blocks, old_blocks->blocks,
+               old_num_blocks * sizeof(old_blocks->blocks[0]));
+
+        for (j = old_num_blocks; j < new_num_blocks; j++) {
+            new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE);
+        }
+
+        atomic_rcu_set(&ram_list.dirty_memory[i], new_blocks);
+
+        if (old_blocks) {
+            g_free_rcu(old_blocks, rcu);
+        }
+    }
+}
+
 static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
 {
     RAMBlock *block;
@@ -1502,6 +1556,7 @@ static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
               (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS);
     if (new_ram_size > old_ram_size) {
         migration_bitmap_extend(old_ram_size, new_ram_size);
+        dirty_memory_extend(old_ram_size, new_ram_size);
     }
     /* Keep the list sorted from biggest to smallest block.  Unlike QTAILQ,
      * QLIST (which has an RCU-friendly variant) does not have insertion at
@@ -1527,18 +1582,6 @@ static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
     ram_list.version++;
     qemu_mutex_unlock_ramlist();
 
-    new_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
-
-    if (new_ram_size > old_ram_size) {
-        int i;
-
-        /* ram_list.dirty_memory[] is protected by the iothread lock.  */
-        for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
-            ram_list.dirty_memory[i] =
-                bitmap_zero_extend(ram_list.dirty_memory[i],
-                                   old_ram_size, new_ram_size);
-       }
-    }
     cpu_physical_memory_set_dirty_range(new_block->offset,
                                         new_block->used_length,
                                         DIRTY_CLIENTS_ALL);
include/exec/ram_addr.h +165 -24

275 lines not on this page · raw message

migration/ram.c +0 -4
index 4e606ab..fdf42df 100644
@@ -609,7 +609,6 @@ static void migration_bitmap_sync_init(void)
     iterations_prev = 0;
 }
 
-/* Called with iothread lock held, to protect ram_list.dirty_memory[] */
 static void migration_bitmap_sync(void)
 {
     RAMBlock *block;
@@ -1916,8 +1915,6 @@ static int ram_save_setup(QEMUFile *f, void *opaque)
         acct_clear();
     }
 
-    /* iothread lock needed for ram_list.dirty_memory[] */
-    qemu_mutex_lock_iothread();
     qemu_mutex_lock_ramlist();
     rcu_read_lock();
     bytes_transferred = 0;
@@ -1942,7 +1939,6 @@ static int ram_save_setup(QEMUFile *f, void *opaque)
     memory_global_dirty_log_start();
     migration_bitmap_sync();
     qemu_mutex_unlock_ramlist();
-    qemu_mutex_unlock_iothread();
 
     qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
 
Signature · 1 line
2.5.0
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-blk: drop unused migration.h include

In reply to Stefan Hajnoczi
Quoted 18 lines · Stefan Hajnoczi

The migration blocker functionality is no longer necessary since dataplane is now compatible with live migration. Drop the unused header include.

---
This patch can be squashed into "virtio-blk: do not use vring in dataplane".

---
 hw/block/virtio-blk.c | 1 -
 1 file changed, 1 deletion(-)
hw/block/virtio-blk.c +0 -0
index 1106d13..3e34d35 100644

hw/block/virtio-blk.c line 25

Quoted 7 lines
hw/block/virtio-blk.c +0 -1
@@ -20,7 +20,6 @@
 #include "sysemu/blockdev.h"
 #include "hw/virtio/virtio-blk.h"
 #include "dataplane/virtio-blk.h"
-#include "migration/migration.h"
 #include "block/scsi.h"
 #ifdef __linux__
 # include <scsi/sg.h>

Great, I'll check if the same is needed for hw/scsi/virtio-scsi.c.

Paolo

#4 lore raw
In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi

Make RAM hotplug play nicely with dataplane. By RCU-protecting the dirty memory bitmap it is possible to hotplug RAM while other threads are dirtying guest RAM.

See Patch 1 for the meat of the code. Patch 2 is just a fixup on top of Paolo's dataplane branch.

This series is based on: https://github.com/bonzini/qemu.git dataplane

Stefan Hajnoczi (2):
memory: RCU ram_list.dirty_memory[] for safe RAM hotplug
virtio-blk: drop unused migration.h include

Quoted 5 lines
 exec.c                  |  73 +++++++++++++++----
 hw/block/virtio-blk.c   |   1 -
 include/exec/ram_addr.h | 189 ++++++++++++++++++++++++++++++++++++++++++------
 migration/ram.c         |   4 -
 4 files changed, 223 insertions(+), 44 deletions(-)

Paolo, will you take this through your tree/dataplane branch?

I think that would work well since your vring.c removal relies on the memory API being thread-safe.

Stefan

#5 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Quoted 4 lines · Stefan Hajnoczi

Paolo, will you take this through your tree/dataplane branch?

I think that would work well since your vring.c removal relies on the memory API being thread-safe.

Yes, I'll ensure this gets in before I post the vring.c removal.

I have already reviewed it, but not tested it.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJWqfXwAAoJEL/70l94x66DqrgIAI04mitSSu5lY0LzIz8WmewU 04jSj3eVpUfHWhnksGTciwBultpI8VvVUmihip8d26F9qdDnbbXe5ZZTiT7LWK4L I3cvaWTl++mJQ40AE+aHG4Imq+rjrqPWhFYBUn2hmYpaxGzyrpDryet6uyEkzQt6 b2ZUicDzDD+qkInqpycPSJl7mEvOnbSSLeeVWCRReXPGxJCqATPMETpTJ0NrMWez pP0ebtEKOTd5OqiUmB5ajyI1Lfia3ePcaOI9RDf+BVPCedhRez7XDRu+t1xACDWm ofCxO/3/x2GxRB6yNgMj8K5jW9xGrHbYpRu1w5mb84ygY78z6F0ubfDqhdP/Gb8= =vp25
-----END PGP SIGNATURE-----