Skip to contentSkip to conversation

convert ram_list to RCU DQ

RFC PATCH In review Mike Day 2 participants 6 messages Aug 28, 2013 – Aug 30, 2013

lore
Share & tools
lore

Mike Day

author
#0 lore raw

RFC PATCH 1/1 convert ram_list to RCU DQ

Allow "unlocked" reads of the ram_list by using an RCU-enabled DQ. Most readers of the list no longer require holding the list mutex.

The ram_list now uses a QLIST instead of a QTAILQ. The difference is minimal.

This patch has been built and make-checked for the x86_64, ppc64, s390x, and arm targets. It has not been tested further than that at this point.

To apply this patch, you must base upon Paolo Bonzini's rcu tree and also apply the RCU DQ patch (below).

https://github.com/bonzini/qemu/tree/rcu
http://article.gmane.org/gmane.comp.emulators.qemu/230159/

---
 arch_init.c               |  51 ++++++++++++---------
 exec.c                    | 111 +++++++++++++++++++++++++++++-----------------
 hw/9pfs/virtio-9p-synth.c |   2 +-
 include/exec/cpu-all.h    |   4 +-
 include/qemu/rcu_queue.h  |   8 ++++
 5 files changed, 111 insertions(+), 65 deletions(-)
arch_init.c +29 -22
index 68a7ab7..5c7b284 100644
@@ -49,6 +49,7 @@
 #include "trace.h"
 #include "exec/cpu-all.h"
 #include "hw/acpi/acpi.h"
+#include "qemu/rcu_queue.h"
 
 #ifdef DEBUG_ARCH_INIT
 #define DPRINTF(fmt, ...) \
@@ -397,8 +398,8 @@ static void migration_bitmap_sync(void)
 
     trace_migration_bitmap_sync_start();
     address_space_sync_dirty_bitmap(&address_space_memory);
-
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
+    rcu_read_lock();
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
         for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
             if (memory_region_test_and_clear_dirty(block->mr,
                                                    addr, TARGET_PAGE_SIZE,
@@ -407,6 +408,7 @@ static void migration_bitmap_sync(void)
             }
         }
     }
+    rcu_read_unlock();
     trace_migration_bitmap_sync_end(migration_dirty_pages
                                     - num_dirty_pages_init);
     num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
@@ -457,8 +459,9 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
     MemoryRegion *mr;
     ram_addr_t current_addr;
 
+    rcu_read_lock();
     if (!block)
-        block = QTAILQ_FIRST(&ram_list.blocks);
+        block = QLIST_FIRST_RCU(&ram_list.blocks);
 
     while (true) {
         mr = block->mr;
@@ -469,9 +472,9 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
         }
         if (offset >= block->length) {
             offset = 0;
-            block = QTAILQ_NEXT(block, next);
+            block = QLIST_NEXT_RCU(block, next);
             if (!block) {
-                block = QTAILQ_FIRST(&ram_list.blocks);
+                block = QLIST_FIRST_RCU(&ram_list.blocks);
                 complete_round = true;
                 ram_bulk_stage = false;
             }
@@ -526,6 +529,7 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
             }
         }
     }
+    rcu_read_unlock();
     last_seen_block = block;
     last_offset = offset;
 
@@ -565,10 +569,10 @@ uint64_t ram_bytes_total(void)
 {
     RAMBlock *block;
     uint64_t total = 0;
-
-    QTAILQ_FOREACH(block, &ram_list.blocks, next)
+    rcu_read_lock();
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
         total += block->length;
-
+    rcu_read_unlock();
     return total;
 }
 
@@ -631,7 +635,7 @@ static int ram_save_setup(QEMUFile *f, void *opaque)
     }
 
     qemu_mutex_lock_iothread();
-    qemu_mutex_lock_ramlist();
+    rcu_read_lock();
     bytes_transferred = 0;
     reset_ram_globals();
 
@@ -641,13 +645,13 @@ static int ram_save_setup(QEMUFile *f, void *opaque)
 
     qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
 
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
         qemu_put_byte(f, strlen(block->idstr));
         qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
         qemu_put_be64(f, block->length);
     }
 
-    qemu_mutex_unlock_ramlist();
+    rcu_read_unlock();
 
     ram_control_before_iterate(f, RAM_CONTROL_SETUP);
     ram_control_after_iterate(f, RAM_CONTROL_SETUP);
@@ -664,8 +668,6 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)
     int64_t t0;
     int total_sent = 0;
 
-    qemu_mutex_lock_ramlist();
-
     if (ram_list.version != last_version) {
         reset_ram_globals();
     }
@@ -701,8 +703,6 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)
         i++;
     }
 
-    qemu_mutex_unlock_ramlist();
-
     /*
      * Must occur before EOS (or any QEMUFile operation)
      * because of RDMA protocol.
@@ -814,6 +814,7 @@ static inline void *host_from_stream_offset(QEMUFile *f,
     static RAMBlock *block = NULL;
     char id[256];
     uint8_t len;
+    void *ptr = NULL;
 
     if (flags & RAM_SAVE_FLAG_CONTINUE) {
         if (!block) {
@@ -828,13 +829,18 @@ static inline void *host_from_stream_offset(QEMUFile *f,
     qemu_get_buffer(f, (uint8_t *)id, len);
     id[len] = 0;
 
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        if (!strncmp(id, block->idstr, sizeof(id)))
-            return memory_region_get_ram_ptr(block->mr) + offset;
+    rcu_read_lock();
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
+        if (!strncmp(id, block->idstr, sizeof(id))) {
+            ptr = memory_region_get_ram_ptr(block->mr) + offset;
+            goto unlock_out;
+        }
     }
 
     fprintf(stderr, "Can't find block %s!\n", id);
-    return NULL;
+unlock_out:
+    rcu_read_unlock();
+    return ptr;
 }
 
 /*
@@ -889,8 +895,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
                     qemu_get_buffer(f, (uint8_t *)id, len);
                     id[len] = 0;
                     length = qemu_get_be64(f);
-
-                    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
+                    rcu_read_lock();
+                    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
                         if (!strncmp(id, block->idstr, sizeof(id))) {
                             if (block->length != length) {
                                 fprintf(stderr,
@@ -898,12 +904,13 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
                                         " in != " RAM_ADDR_FMT "\n", id, length,
                                         block->length);
                                 ret =  -EINVAL;
+                                rcu_read_unlock();
                                 goto done;
                             }
                             break;
                         }
                     }
-
+                    rcu_read_unlock();
                     if (!block) {
                         fprintf(stderr, "Unknown ramblock \"%s\", cannot "
                                 "accept migration\n", id);
exec.c +71 -40

299 lines not on this page · raw message

hw/9pfs/virtio-9p-synth.c +1 -1
index fdfea21..c2efaca 100644
@@ -18,7 +18,7 @@
 #include "fsdev/qemu-fsdev.h"
 #include "virtio-9p-synth.h"
 #include "qemu/rcu.h"
-
+#include "qemu/rcu_queue.h"
 #include <sys/stat.h>
 
 /* Root node for synth file system */
include/exec/cpu-all.h +2 -2
index e088089..9cd8a30 100644
@@ -457,7 +457,7 @@ typedef struct RAMBlock {
     /* Reads can take either the iothread or the ramlist lock.
      * Writes must take both locks.
      */
-    QTAILQ_ENTRY(RAMBlock) next;
+    QLIST_ENTRY(RAMBlock) next;
 #if defined(__linux__) && !defined(TARGET_S390X)
     int fd;
 #endif
@@ -469,7 +469,7 @@ typedef struct RAMList {
     uint8_t *phys_dirty;
     RAMBlock *mru_block;
     /* Protected by the ramlist lock.  */
-    QTAILQ_HEAD(, RAMBlock) blocks;
+    QLIST_HEAD(, RAMBlock) blocks;
     uint32_t version;
 } RAMList;
 extern RAMList ram_list;
include/qemu/rcu_queue.h +8 -0
index e2b8ba5..d159850 100644
@@ -37,6 +37,14 @@
 extern "C" {
 #endif
 
+
+/*
+ * List access methods.
+ */
+#define QLIST_EMPTY_RCU(head) (atomic_rcu_read(&(head)->lh_first) == NULL)
+#define QLIST_FIRST_RCU(head) (atomic_rcu_read(&(head)->lh_first))
+#define QLIST_NEXT_RCU(elm, field) (atomic_rcu_read(&(elm)->field.le_next))
+
 /*
  * List functions.
  */
Signature · 1 line
1.8.3.1
#1 lore raw

Il 28/08/2013 18:02, Mike Day ha scritto:

Quoted 24 lines
arch_init.c +4 -3
@@ -457,8 +459,9 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
     MemoryRegion *mr;
     ram_addr_t current_addr;
 
+    rcu_read_lock();
     if (!block)
-        block = QTAILQ_FIRST(&ram_list.blocks);
+        block = QLIST_FIRST_RCU(&ram_list.blocks);
 
     while (true) {
         mr = block->mr;
@@ -469,9 +472,9 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
         }
         if (offset >= block->length) {
             offset = 0;
-            block = QTAILQ_NEXT(block, next);
+            block = QLIST_NEXT_RCU(block, next);
             if (!block) {
-                block = QTAILQ_FIRST(&ram_list.blocks);
+                block = QLIST_FIRST_RCU(&ram_list.blocks);
                 complete_round = true;
                 ram_bulk_stage = false;
             }

arch_init.c static int ram_save_block(QEMUFile *f, bool last_stage) · line 532

Quoted 4 lines
arch_init.c +1 -0
@@ -526,6 +529,7 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
             }
         }
     }
+    rcu_read_unlock();

block lives across calls to ram_save_block, which is why the mutex was locked in the caller (ram_save_iterate) rather than here. For a first conversion, keeping the long RCU critical sections is fine. We don't use RCU enough yet to care about delaying other call_rcu callbacks.

We can later check push the check for ram_list.version inside ram_save_block, which should let us make the critical section smaller. But that would be a bit tricky, so it's better to do it in a separate patch.

Quoted 11 lines
arch_init.c +4 -3
@@ -828,13 +829,18 @@ static inline void *host_from_stream_offset(QEMUFile *f,
     qemu_get_buffer(f, (uint8_t *)id, len);
     id[len] = 0;
 
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        if (!strncmp(id, block->idstr, sizeof(id)))
-            return memory_region_get_ram_ptr(block->mr) + offset;
+    rcu_read_lock();
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
+        if (!strncmp(id, block->idstr, sizeof(id))) {
+            ptr = memory_region_get_ram_ptr(block->mr) + offset;

arch_init.c static inline void *host_from_stream_offset(QEMUFile *f, · line 844

Quoted 10 lines
arch_init.c +5 -1
+            goto unlock_out;
+        }
     }
 
     fprintf(stderr, "Can't find block %s!\n", id);
-    return NULL;
+unlock_out:
+    rcu_read_unlock();
+    return ptr;
 }

Similarly, here the critical section includes the caller, and block is living across calls to host. Again, for now just put all of ram_load under a huge RCU critical section. Later we can use ram_list.version to refresh the list and make the critical sections smaller.

Paolo

author
#2 lore raw
Quoted 2 lines · Paolo Bonzini

Review static inline void

Quoted 2 lines
(quoted diff) +0 -0
@@ -828,13 +829,18 @@ static inline void

*host_from_stream_offset(QEMUFile *f,

Quoted 22 lines
Quoted 10 lines
arch_init.c +4 -3
     qemu_get_buffer(f, (uint8_t *)id, len);
     id[len] = 0;
 
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        if (!strncmp(id, block->idstr, sizeof(id)))
-            return memory_region_get_ram_ptr(block->mr) + offset;
+    rcu_read_lock();
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
+        if (!strncmp(id, block->idstr, sizeof(id))) {
+            ptr = memory_region_get_ram_ptr(block->mr) + offset;

arch_init.c static inline void *host_from_stream_offset(QEMUFile *f, · line 844

Quoted 10 lines
arch_init.c +5 -1
+            goto unlock_out;
+        }
     }
 
     fprintf(stderr, "Can't find block %s!\n", id);
-    return NULL;
+unlock_out:
+    rcu_read_unlock();
+    return ptr;
 }
Quoted 4 lines

Similarly, here the critical section includes the caller, and block is living across calls to host. Again, for now just put all of ram_load under a huge RCU critical section. Later we can use ram_list.version to refresh the list and make the critical sections smaller.

And: rcu_read_lock() and rcu_read_unlock() can be called recursively, so I can still leave the lock/unlock pair in host_from_stream_offset.

#3 lore raw

Il 29/08/2013 21:18, Mike Day ha scritto:

Quoted 8 lines
Quoted 5 lines

Similarly, here the critical section includes the caller, and block is living across calls to host. Again, for now just put all of ram_load under a huge RCU critical section. Later we can use ram_list.version to refresh the list and make the critical sections smaller.

And: rcu_read_lock() and rcu_read_unlock() can be called recursively, so I can still leave the lock/unlock pair in host_from_stream_offset.

I'm not sure about that; returning an RCU-protected variable after rcu_read_unlock() seems wrong to me because the pointer may not be valid at that point. I suggest using a comment that asks to call host_from_stream_offset within rcu_read_lock()/rcu_read_unlock(). However, if existing practice in the kernel is different, I'll bow to that.

Paolo

author
#4 lore raw
Quoted 7 lines · Paolo Bonzini

I'm not sure about that; returning an RCU-protected variable after rcu_read_unlock() seems wrong to me because the pointer may not be valid at that point. I suggest using a comment that asks to call host_from_stream_offset within rcu_read_lock()/rcu_read_unlock(). However, if existing practice in the kernel is different, I'll bow to

that.

In this case the only caller is ram_load so I'm removing the critical section from within host_from_stream_offset, and adding comments to note that the ram_list needs to be protected by the caller. The current docs/rcu.txt information indicates that rcu critical sections can be "nested or overlapping." But your suggestion results in cleaner code - we will have to go back to this later as you noted earlier.

Mike

#5 lore raw
In reply to Mike Day

Il 28/08/2013 18:02, Mike Day ha scritto:

Quoted 6 lines
exec.c +0 -2
@@ -1102,15 +1110,15 @@ void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
     pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
 
     /* This assumes the iothread lock is taken here too.  */
-    qemu_mutex_lock_ramlist();
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {

exec.c void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev) · line 1121

Quoted 10 lines
exec.c +3 -1
+    rcu_read_lock();
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
         if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
             fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
                     new_block->idstr);
             abort();
         }
     }
-    qemu_mutex_unlock_ramlist();
+    rcu_read_unlock();

Forgot about this. Every time you see "This assumes the iothread lock is taken here too", you're in the write side so you do not need lock/unlock and you do not need...

exec.c void qemu_ram_free_from_ptr(ram_addr_t addr) · line 1227

Quoted 10 lines
exec.c +1 -1
     /* This assumes the iothread lock is taken here too.  */
     qemu_mutex_lock_ramlist();
-    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
+    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
         if (addr == block->offset) {
exec.c +1 -1
-            QTAILQ_REMOVE(&ram_list.blocks, block, next);
+            QLIST_REMOVE_RCU(block, next);
             ram_list.mru_block = NULL;
             ram_list.version++;
             g_free(block);

qemu_mutex_lock_ramlist/qemu_mutex_unlock_ramlist either.

Otherwise, the patch is pretty solid! Thanks,

Paolo