From patchwork Mon Aug 5 07:33:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfank@linux.vnet.ibm.com X-Patchwork-Id: 264590 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 934972C0084 for ; Mon, 5 Aug 2013 17:34:59 +1000 (EST) Received: from localhost ([::1]:40034 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V6FJh-0000h2-G1 for incoming@patchwork.ozlabs.org; Mon, 05 Aug 2013 03:34:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49506) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V6FJ4-0000RL-RH for qemu-devel@nongnu.org; Mon, 05 Aug 2013 03:34:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V6FIo-0006Em-3O for qemu-devel@nongnu.org; Mon, 05 Aug 2013 03:34:18 -0400 Received: from e23smtp06.au.ibm.com ([202.81.31.148]:54337) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V6FIm-0006Dv-Uo for qemu-devel@nongnu.org; Mon, 05 Aug 2013 03:34:02 -0400 Received: from /spool/local by e23smtp06.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 5 Aug 2013 17:25:49 +1000 Received: from d23dlp02.au.ibm.com (202.81.31.213) by e23smtp06.au.ibm.com (202.81.31.212) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 5 Aug 2013 17:25:47 +1000 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [9.190.235.21]) by d23dlp02.au.ibm.com (Postfix) with ESMTP id 30E752BB0052 for ; Mon, 5 Aug 2013 17:33:52 +1000 (EST) Received: from d23av01.au.ibm.com (d23av01.au.ibm.com [9.190.234.96]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r757XfwG9503062 for ; Mon, 5 Aug 2013 17:33:41 +1000 Received: from d23av01.au.ibm.com (localhost [127.0.0.1]) by d23av01.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id r757Xp7m024060 for ; Mon, 5 Aug 2013 17:33:51 +1000 Received: from localhost ([9.115.122.107]) by d23av01.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id r757XoGj024043; Mon, 5 Aug 2013 17:33:50 +1000 From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Mon, 5 Aug 2013 15:33:24 +0800 Message-Id: <1375688006-16780-3-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1375688006-16780-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1375688006-16780-1-git-send-email-pingfank@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13080507-7014-0000-0000-0000036C3AB0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.148 Cc: Kevin Wolf , Stefan Hajnoczi , Jan Kiszka , Alex Bligh , Paolo Bonzini , MORITA Kazutaka Subject: [Qemu-devel] [PATCH 2/4] timer: protect timers_state's clock with seqlock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org In kvm mode, vm_clock may be read outside BQL. This will make timers_state --the foundation of vm_clock exposed to race condition. Using private lock to protect it. Note in tcg mode, vm_clock still read inside BQL, so icount is left without change. As for cpu_ticks in timers_state, it is still protected by BQL. Lock rule: private lock innermost, ie BQL->"this lock" Signed-off-by: Liu Ping Fan --- cpus.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/cpus.c b/cpus.c index 85e743d..ab92db9 100644 --- a/cpus.c +++ b/cpus.c @@ -107,12 +107,17 @@ static int64_t qemu_icount; typedef struct TimersState { int64_t cpu_ticks_prev; int64_t cpu_ticks_offset; + /* QemuClock will be read out of BQL, so protect is with private lock. + * As for cpu_ticks, no requirement to read it outside BQL. + * Lock rule: innermost + */ + QemuSeqLock clock_seqlock; int64_t cpu_clock_offset; int32_t cpu_ticks_enabled; int64_t dummy; } TimersState; -TimersState timers_state; +static TimersState timers_state; /* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount(void) @@ -132,6 +137,7 @@ int64_t cpu_get_icount(void) } /* return the host CPU cycle counter and handle stop/restart */ +/* cpu_ticks is safely if holding BQL */ int64_t cpu_get_ticks(void) { if (use_icount) { @@ -156,33 +162,46 @@ int64_t cpu_get_ticks(void) int64_t cpu_get_clock(void) { int64_t ti; - if (!timers_state.cpu_ticks_enabled) { - return timers_state.cpu_clock_offset; - } else { - ti = get_clock(); - return ti + timers_state.cpu_clock_offset; - } + unsigned start; + + do { + start = seqlock_read_begin(&timers_state.clock_seqlock); + if (!timers_state.cpu_ticks_enabled) { + ti = timers_state.cpu_clock_offset; + } else { + ti = get_clock(); + ti += timers_state.cpu_clock_offset; + } + } while (seqlock_read_check(&timers_state.clock_seqlock, start); + + return ti; } /* enable cpu_get_ticks() */ void cpu_enable_ticks(void) { + /* Here, the really thing protected by seqlock is cpu_clock. */ + seqlock_write_lock(&timers_state.clock_seqlock); if (!timers_state.cpu_ticks_enabled) { timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); timers_state.cpu_clock_offset -= get_clock(); timers_state.cpu_ticks_enabled = 1; } + seqlock_write_unlock(&timers_state.clock_seqlock); } /* disable cpu_get_ticks() : the clock is stopped. You must not call cpu_get_ticks() after that. */ void cpu_disable_ticks(void) { + /* Here, the really thing protected by seqlock is cpu_clock. */ + seqlock_write_lock(&timers_state.clock_seqlock); if (timers_state.cpu_ticks_enabled) { timers_state.cpu_ticks_offset = cpu_get_ticks(); timers_state.cpu_clock_offset = cpu_get_clock(); timers_state.cpu_ticks_enabled = 0; } + seqlock_write_unlock(&timers_state.clock_seqlock); } /* Correlation between real and virtual time is always going to be @@ -364,6 +383,9 @@ static const VMStateDescription vmstate_timers = { void configure_icount(const char *option) { + QemuMutex *mutex = g_malloc0(sizeof(QemuMutex)); + qemu_mutex_init(mutex); + seqlock_init(&timers_state.clock_seqlock, mutex); vmstate_register(NULL, 0, &vmstate_timers, &timers_state); if (!option) { return;