From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:48315) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QxK1f-00084l-KZ for qemu-devel@nongnu.org; Sat, 27 Aug 2011 10:38:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QxK1e-0006kN-L6 for qemu-devel@nongnu.org; Sat, 27 Aug 2011 10:38:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53254) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QxK1e-0006kH-E0 for qemu-devel@nongnu.org; Sat, 27 Aug 2011 10:38:22 -0400 Date: Fri, 26 Aug 2011 14:00:48 -0300 From: Marcelo Tosatti Message-ID: <20110826170047.GA26783@amt.cnet> References: <4ff40d24610fa9cff53518e469b9cd6782b71cdc.1314153301.git.udeshpan@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4ff40d24610fa9cff53518e469b9cd6782b71cdc.1314153301.git.udeshpan@redhat.com> Subject: Re: [Qemu-devel] [RFC PATCH v5 2/4] Migration thread mutex List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Umesh Deshpande Cc: pbonzini@redhat.com, qemu-devel@nongnu.org, kvm@vger.kernel.org, quintela@redhat.com On Tue, Aug 23, 2011 at 11:12:48PM -0400, Umesh Deshpande wrote: > ramlist mutex is implemented to protect the RAMBlock list traversal in the > migration thread from their addition/removal from the iothread. > > Note: Combination of iothread mutex and migration thread mutex works as a > rw-lock. Both mutexes are acquired while modifying the ram_list members or RAM > block list. > > Signed-off-by: Umesh Deshpande > --- > arch_init.c | 21 +++++++++++++++++++++ > cpu-all.h | 3 +++ > exec.c | 23 +++++++++++++++++++++++ > qemu-common.h | 2 ++ > 4 files changed, 49 insertions(+), 0 deletions(-) > > diff --git a/arch_init.c b/arch_init.c > index 484b39d..9d02270 100644 > --- a/arch_init.c > +++ b/arch_init.c > @@ -109,6 +109,7 @@ static int is_dup_page(uint8_t *page, uint8_t ch) > > static RAMBlock *last_block; > static ram_addr_t last_offset; > +static uint64_t last_version; > > static int ram_save_block(QEMUFile *f) > { > @@ -170,6 +171,7 @@ static int ram_save_block(QEMUFile *f) > > last_block = block; > last_offset = offset; > + last_version = ram_list.version; > > return bytes_sent; > } > @@ -270,6 +272,7 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) > bytes_transferred = 0; > last_block = NULL; > last_offset = 0; > + last_version = ram_list.version = 0; > sort_ram_list(); > > /* Make sure all dirty bits are set */ > @@ -298,6 +301,17 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) > bytes_transferred_last = bytes_transferred; > bwidth = qemu_get_clock_ns(rt_clock); > > + if (stage != 3) { > + qemu_mutex_lock_migthread(); > + qemu_mutex_unlock_iothread(); > + } > + > + if (ram_list.version != last_version) { > + /* RAM block added or removed */ > + last_block = NULL; > + last_offset = 0; > + } > + > while (!qemu_file_rate_limit(f)) { > int bytes_sent; > > @@ -308,6 +322,13 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) > } > } > > + if (stage != 3) { > + qemu_mutex_unlock_migthread(); > + qemu_mutex_lock_iothread(); > + /* Lock ordering : iothread mutex is always acquired outside migthread > + * mutex critical section to avoid deadlock */ > + } > + > bwidth = qemu_get_clock_ns(rt_clock) - bwidth; > bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; > > diff --git a/cpu-all.h b/cpu-all.h > index 6b217a2..b85483f 100644 > --- a/cpu-all.h > +++ b/cpu-all.h > @@ -21,6 +21,7 @@ > > #include "qemu-common.h" > #include "cpu-common.h" > +#include "qemu-thread.h" > > /* some important defines: > * > @@ -932,7 +933,9 @@ typedef struct RAMBlock { > } RAMBlock; > > typedef struct RAMList { > + QemuMutex mutex; /* Protects RAM block list */ > uint8_t *phys_dirty; > + uint32_t version; /* To detect ram block addition/removal */ > QLIST_HEAD(ram, RAMBlock) blocks; > QLIST_HEAD(, RAMBlock) blocks_mru; > } RAMList; > diff --git a/exec.c b/exec.c > index c5c247c..7627483 100644 > --- a/exec.c > +++ b/exec.c > @@ -582,6 +582,7 @@ void cpu_exec_init_all(unsigned long tb_size) > code_gen_alloc(tb_size); > code_gen_ptr = code_gen_buffer; > page_init(); > + qemu_mutex_init(&ram_list.mutex); > #if !defined(CONFIG_USER_ONLY) > io_mem_init(); > #endif > @@ -2802,6 +2803,16 @@ static long gethugepagesize(const char *path) > return fs.f_bsize; > } > > +void qemu_mutex_lock_migthread(void) > +{ > + qemu_mutex_lock(&ram_list.mutex); > +} qemu_mutex_lock_ramlist is a better name, _migthread is confusing.