Convert ram_list to RCU DQ V3
PATCH 1/1 Convert ram_list to RCU DQ V3
Changes from V2:
* added rcu reclaim function to free ram blocks
* reduced the number of comments
* made rcu locking policy consistent for different caller scenarios
* rcu updates to ram_block now are protected only by the iothread mutex
Changes from V1:
* Omitted locks or rcu critical sections within Some functions that read or write the ram_list but are called in a protected context (the caller holds the iothread lock, the ram_list mutex, or an rcu critical section).
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 | 103 ++++++++++++++++++------------ exec.c | 160 +++++++++++++++++++++++++++++------------------ include/exec/cpu-all.h | 6 +- include/qemu/rcu_queue.h | 8 +++ 4 files changed, 173 insertions(+), 104 deletions(-)
arch_init.c +63 -40
@@ -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, ...) \
@@ -398,7 +399,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 +409,8 @@ 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;
@@ -446,6 +450,8 @@ static void migration_bitmap_sync(void)* * Returns: The number of bytes written. * 0 means no dirty pages +* +* assumes that the caller has rcu-locked the ram_list */ static int ram_save_block (QEMUFile *f,bool last_stage)
@@ -457,8 +463,9 @@ static int ram_save_block(QEMUFile *f, bool last_stage)MemoryRegion *mr;ram_addr_t current_addr; +if (!block) - block =QTAILQ_FIRST (&ram_list.blocks); + block =QLIST_FIRST_RCU (&ram_list.blocks);while (true) { mr = block->mr;
@@ -469,9 +476,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,9 +533,9 @@ static int ram_save_block(QEMUFile *f, bool last_stage) } } } + last_seen_block = block; last_offset = offset; -return bytes_sent; }
@@ -565,10 +572,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; }
@@ -602,10 +609,20 @@ static void reset_ram_globals(void) last_offset =0 ; last_version = ram_list.version; ram_bulk_stage = true; +smp_wmb (); }#define MAX_WAIT 50/* ms, half buffered_file limit */ + +/* ram_save_* functions each implement a long-running RCU critical +* section. When rcu-reclaims in the code start to become numerous +* it will be necessary to reduce the granularity of these critical +* sections. +* +* (ram_save_setup, ram_save_iterate, and ram_save_complete.) +*/ +static int ram_save_setup (QEMUFile *f,void *opaque) {RAMBlock *block;
@@ -631,7 +648,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 +658,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 );
@@ -657,6 +674,7 @@ static int ram_save_setup(QEMUFile *f, void *opaque)return 0 ; } +static int ram_save_iterate (QEMUFile *f,void *opaque) {int ret;
@@ -664,16 +682,14 @@ 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) { +if (atomic_rcu_read (&ram_list.version) != last_version) {reset_ram_globals (); }ram_control_before_iterate (f,RAM_CONTROL_ROUND ); - t0 =qemu_get_clock_ns (rt_clock); i =0 ; +rcu_read_lock ();while ((ret =qemu_file_rate_limit (f)) ==0 ) {int bytes_sent;
@@ -700,9 +716,7 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) } i++; } - -qemu_mutex_unlock_ramlist (); - +rcu_read_unlock ();/* * Must occur before EOS (or any QEMUFile operation) * because of RDMA protocol.
@@ -717,19 +731,21 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)qemu_put_be64 (f,RAM_SAVE_FLAG_EOS ); total_sent +=8 ; bytes_transferred += total_sent; -return total_sent; }static int ram_save_complete (QEMUFile *f,void *opaque) { -qemu_mutex_lock_ramlist (); + +qemu_mutex_lock_iothread ();migration_bitmap_sync (); +qemu_mutex_unlock_iothread ();ram_control_before_iterate (f,RAM_CONTROL_FINISH );/* try transferring iterative blocks of memory */ +rcu_read_lock ();/* flush all remaining blocks regardless of rate limiting */ while (true) {int bytes_sent;
@@ -741,11 +757,10 @@ static int ram_save_complete(QEMUFile *f, void *opaque) } bytes_transferred += bytes_sent; } - +rcu_read_unlock ();ram_control_after_iterate (f,RAM_CONTROL_FINISH );migration_end (); -qemu_mutex_unlock_ramlist ();qemu_put_be64 (f,RAM_SAVE_FLAG_EOS );return 0 ;
@@ -807,6 +822,9 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)return rc; } +/* Must be called from within a rcu critical section. +* Returns a pointer from within the RCU-protected ram_list. +*/ static inline void *host_from_stream_offset (QEMUFile *f,ram_addr_t offset,int flags)
@@ -828,9 +846,10 @@ 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))) + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { +if (!strncmp (id, block->idstr,sizeof (id))) {return memory_region_get_ram_ptr (block->mr) + offset; + } }fprintf (stderr,"Can't find block %s!\n" , id);
@@ -867,7 +886,12 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)if (version_id <4 || version_id >4 ) {return -EINVAL ; } - +/* this implements a long-running RCU critical section. +* When rcu reclaims in the code start to become numerous +* it will be necessary to reduce the granularity of this critical +* section. +*/ +rcu_read_lock (); do { addr = qemu_get_be64(f);
@@ -889,21 +913,19 @@ 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) { + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {if (!strncmp (id, block->idstr,sizeof (id))) {if (block->length != length) {fprintf (stderr,"Length mismatch: %s: " RAM_ADDR_FMT " in != " RAM_ADDR_FMT "\n" , id, length, block->length); - ret = -EINVAL ; + ret = -EINVAL ; goto done; }break ; } } -if (!block) {fprintf (stderr,"Unknown ramblock \"%s\", cannot " "accept migration\n" , id);
@@ -916,30 +938,30 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) } } +/* Call host_from_stream_offset while holding an rcu read lock. +* It returns a pointer from within the rcu-protected ram_list. +*/ if (flags &RAM_SAVE_FLAG_COMPRESS ) { -void *host;uint8_t ch; - - host =host_from_stream_offset (f, addr, flags); +void *host =host_from_stream_offset (f, addr, flags);if (!host) { -return -EINVAL ; + ret = -EINVAL ; + goto done; } - ch =qemu_get_byte (f);ram_handle_compressed (host, ch,TARGET_PAGE_SIZE ); }else if (flags &RAM_SAVE_FLAG_PAGE ) { -void *host; - - host =host_from_stream_offset (f, addr, flags); +void *host =host_from_stream_offset (f, addr, flags);if (!host) { -return -EINVAL ; + ret = -EINVAL ; + goto done; } -qemu_get_buffer (f, host,TARGET_PAGE_SIZE ); }else if (flags &RAM_SAVE_FLAG_XBZRLE ) {void *host =host_from_stream_offset (f, addr, flags);if (!host) { -return -EINVAL ; + ret = -EINVAL ; + goto done; }if (load_xbzrle (f, addr, host) <0 ) {
@@ -957,6 +979,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) }while (!(flags &RAM_SAVE_FLAG_EOS )); done: +rcu_read_unlock ();DPRINTF ("Completed load of VM with exit code %d seq iteration " "%" PRIu64"\n" , ret, seq_iter);return ret;
exec.c +98 -62
359 lines not on this page · raw message
include/exec/cpu-all.h +4 -2
@@ -24,6 +24,7 @@#include "qemu/thread.h" #include "qom/cpu.h" #include "qemu/tls.h" +#include "qemu/rcu.h" /* some important defines: *
@@ -448,6 +449,7 @@ extern ram_addr_t ram_size;#define RAM_PREALLOC_MASK (1 << 0) typedef struct RAMBlock { + struct rcu_head rcu; struct MemoryRegion *mr; uint8_t *host; ram_addr_t offset;
@@ -457,7 +459,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 +471,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
@@ -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
Il 04/09/2013 15:29, Mike Day ha scritto:
Quoted 5 lines
arch_init.c +0
-1@@ -717,19 +731,21 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)qemu_put_be64 (f,RAM_SAVE_FLAG_EOS ); total_sent +=8 ; bytes_transferred += total_sent; -
arch_init.c static int ram_save_iterate(QEMUFile *f, void *opaque) · line 742
Quoted 10 lines
arch_init.c +3
-1return total_sent; }static int ram_save_complete (QEMUFile *f,void *opaque) { -qemu_mutex_lock_ramlist (); + +qemu_mutex_lock_iothread ();migration_bitmap_sync (); +qemu_mutex_unlock_iothread ();
ram_save_complete runs with the iothread taken. Since the lock is not recursive, this will cause a deadlock.
To test migration, I suggest you use virt-test. Install autotest-framework if you run Fedora, clone
https://github.com/autotest/virt-test.git and do "./run -t qemu --qemu-bin=/path/to/qemu-system-x86_64".
Quoted 35 lines
arch_init.c +2
-2ram_control_before_iterate (f,RAM_CONTROL_FINISH );/* try transferring iterative blocks of memory */ +rcu_read_lock ();/* flush all remaining blocks regardless of rate limiting */ while (true) {int bytes_sent;@@ -741,11 +757,10 @@ static int ram_save_complete(QEMUFile *f, void *opaque) } bytes_transferred += bytes_sent; } - +rcu_read_unlock ();ram_control_after_iterate (f,RAM_CONTROL_FINISH );migration_end (); -qemu_mutex_unlock_ramlist ();qemu_put_be64 (f,RAM_SAVE_FLAG_EOS );return 0 ;exec.c +1
-1@@ -46,7 +46,7 @@#endif #include "exec/cpu-all.h" #include "qemu/tls.h" - +#include "qemu/rcu_queue.h" #include "exec/cputlb.h" #include "translate-all.h"
exec.c line 62
Quoted 7 lines
exec.c +3
-1@@ -57,7 +57,10 @@#if !defined(CONFIG_USER_ONLY )static int in_migration; -RAMList ram_list = { .blocks =QTAILQ_HEAD_INITIALIZER (ram_list.blocks) }; +/* ram_list is enabled for RCU access - writes should be protected +* by the iothread lock or an equivalent mutex. +*/
/* ram_list is read under rcu_read_lock()/rcu_read_unlock(). Writes
* are protected by a lock, currently the iothread lock. */
exec.c static int memory_try_enable_merging(void *addr, size_t len) · line 1142
Quoted 10 lines
exec.c +3
-0+RAMList ram_list = { .blocks =QLIST_HEAD_INITIALIZER (ram_list.blocks) };static MemoryRegion *system_memory;static MemoryRegion *system_io;@@ -1123,16 +1138,18 @@ static int memory_try_enable_merging(void *addr, size_t len)return qemu_madvise (addr, len,QEMU_MADV_MERGEABLE ); } +/* Called with the iothread lock being held, so we can +* foregoe protecting the ram_list. When that changes,
s/foregoe/forgo/ ...
exec.c static int memory_try_enable_merging(void *addr, size_t len) · line 1143
Quoted 1 line
exec.c +1
-0+* acquire the iothread mutex before writing the list below.
... but I don't think the comment is accurate. Being a very coarse lock, the iothread mutex will almost always be taken by the caller of a function that requires it, for two reasons:
1) a function will almost always have callers that hold the lock and callers that don't
2) the iothread mutex must always be taken _outside_ other locks to prevent ABBA deadlock.
So I would just write /* Called with the iothread lock held */ in the write sides (in fact the current standard is to document stuff that is called _without_ the lock held! :)). Reads that do not take the rcu_read_lock() of course need more explanation, which is what you did for find_ram_offset() and qemu_ram_set_idstr().
Quoted 32 lines
exec.c +9
-5+*/ ram_addr_t qemu_ram_alloc_from_ptr (ram_addr_t size,void *host,MemoryRegion *mr) { -RAMBlock *block, *new_block; +RAMBlock *block, *new_block, *last_block =0 ; size =TARGET_PAGE_ALIGN (size); new_block =g_malloc0 (sizeof (*new_block)); -/* This assumes the iothread lock is taken here too. */ -qemu_mutex_lock_ramlist (); new_block->mr = mr; new_block->offset =find_ram_offset (size);if (host) {@@ -1200,33 +1220,40 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)return qemu_ram_alloc_from_ptr (size,NULL , mr); } +static void reclaim_ramblock (struct rcu_head *prcu) +{ + RAMBlock *block = container_of(prcu, struct RAMBlock, rcu); +g_free (block); +} + +void qemu_ram_free_from_ptr (ram_addr_t addr) {RAMBlock *block; -/* This assumes the iothread lock is taken here too. */ -qemu_mutex_lock_ramlist ();
exec.c ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr) · line 1240
Quoted 10 lines
exec.c +4
-3- QTAILQ_FOREACH(block, &ram_list.blocks, next) { +/* This assumes the iothread lock is taken here too. */ + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {if (addr == block->offset) { -QTAILQ_REMOVE (&ram_list.blocks, block, next); +QLIST_REMOVE_RCU (block, next); ram_list.mru_block =NULL ; ram_list.version++; -g_free (block); +call_rcu1 (&block->rcu, reclaim_ramblock);
You can use call_rcu too:
call_rcu(block, reclaim_ramblock, rcu);
or possibly even this:
call_rcu(block, g_free, rcu);
This removes the container_of from reclaim_ramblock, which can take a RAMBlock * directly.
Quoted 25 lines
exec.c +6
-5break ; } } -qemu_mutex_unlock_ramlist (); }void qemu_ram_free (ram_addr_t addr) {RAMBlock *block; -/* This assumes the iothread lock is taken here too. */ -qemu_mutex_lock_ramlist (); - QTAILQ_FOREACH(block, &ram_list.blocks, next) { +/* This assumes the iothread lock is taken here too. +* if that changes, accesses to ram_list need to be protected +* by a mutex (writes) or an rcu read lock (reads) +*/ + QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {if (addr == block->offset) { -QTAILQ_REMOVE (&ram_list.blocks, block, next); +QLIST_REMOVE_RCU (block, next); ram_list.mru_block =NULL ; ram_list.version++;if (block->flags &RAM_PREALLOC_MASK ) {
exec.c void qemu_ram_free(ram_addr_t addr) · line 1279
Quoted 5 lines
exec.c +1
-1@@ -1249,12 +1276,10 @@ void qemu_ram_free(ram_addr_t addr)qemu_anon_ram_free (block->host, block->length); } } -g_free (block); +call_rcu1 (&block->rcu, reclaim_ramblock);
Same here.
Quoted 29 lines
exec.c +0
-2break ; } } -qemu_mutex_unlock_ramlist (); - }#ifndef _WIN32include/exec/cpu-all.h +2
-0@@ -24,6 +24,7 @@#include "qemu/thread.h" #include "qom/cpu.h" #include "qemu/tls.h" +#include "qemu/rcu.h" /* some important defines: * @@ -448,6 +449,7 @@ extern ram_addr_t ram_size;#define RAM_PREALLOC_MASK (1 << 0) typedef struct RAMBlock { + struct rcu_head rcu; struct MemoryRegion *mr; uint8_t *host; ram_addr_t offset;
include/exec/cpu-all.h typedef struct RAMBlock { · line 461
Quoted 3 lines
include/exec/cpu-all.h +0
-0@@ -457,7 +459,7 @@ typedef struct RAMBlock {/* Reads can take either the iothread or the ramlist lock. * Writes must take both locks. */
Not true anymore---please remove the ramlist lock altogether.
include/exec/cpu-all.h typedef struct RAMList { · line 473
Quoted 9 lines
include/exec/cpu-all.h +1
-1-QTAILQ_ENTRY (RAMBlock) next; +QLIST_ENTRY (RAMBlock) next;#if defined(__linux__) && !defined(TARGET_S390X )int fd;#endif @@ -469,7 +471,7 @@ typedef struct RAMList {uint8_t *phys_dirty;RAMBlock *mru_block;/* Protected by the ramlist lock. */
Not true anymore.
Almost there! I'm confident that v4 will be fine!
Paolo
Quoted 25 lines
include/exec/cpu-all.h +1
-1- QTAILQ_HEAD(, RAMBlock) blocks; + QLIST_HEAD(, RAMBlock) blocks;uint32_t version; } RAMList;extern RAMList ram_list;include/qemu/rcu_queue.h +8
-0@@ -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. */