From patchwork Tue Aug 13 05:43:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 266720 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (unknown [IPv6:2001:4830:134:3::12]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A87082C011E for ; Tue, 13 Aug 2013 15:45:33 +1000 (EST) Received: from localhost ([::1]:44426 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V97QB-0005ko-SV for incoming@patchwork.ozlabs.org; Tue, 13 Aug 2013 01:45:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39050) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V97PY-0005X9-Kx for qemu-devel@nongnu.org; Tue, 13 Aug 2013 01:45:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V97PQ-00009s-1O for qemu-devel@nongnu.org; Tue, 13 Aug 2013 01:44:52 -0400 Received: from mail-oa0-x22e.google.com ([2607:f8b0:4003:c02::22e]:52465) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V97PP-00009o-T7 for qemu-devel@nongnu.org; Tue, 13 Aug 2013 01:44:43 -0400 Received: by mail-oa0-f46.google.com with SMTP id l10so10896736oag.33 for ; Mon, 12 Aug 2013 22:44:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gdlj8GhJ1XqIHJnytOvhDI39K+VkFJh4D2VZdMPjUrw=; b=Cp1ijpA8shdLv1Hx1/bJjC0dAZL28PAgspYznigWlmNOtAoytl32nBD4i2Wwy6Fv+b LdqAADOns8zLconFn/vg9TbuXBKqaByIp2L5hWtmw3z7J7NxF4hUWOveC9VAl5pt2olX HwamBytqyRZefIZ1vGes5WMXaamQ2Fh6NLjgKV3m6Sixn8jh797fRn1N9qCh7KiwYLYi zErcO0zdWSUy3yIxrMoOR3jxz3tkck0+YaDU5Rj94KO0A9Uvl9LCUsEIQ1TYrrNqq+xM qYBJu6Fu/1qBt4UAh8bFO5OnxccVh1ob7rYYOj6bJ+IiXuE1FU4TP+a1CfrBPMnbjd37 oKNg== X-Received: by 10.182.19.229 with SMTP id i5mr5334222obe.46.1376372683344; Mon, 12 Aug 2013 22:44:43 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id xr8sm1448559obc.12.2013.08.12.22.44.39 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 22:44:42 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Tue, 13 Aug 2013 13:43:48 +0800 Message-Id: <1376372630-1983-3-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1376372630-1983-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1376372630-1983-1-git-send-email-pingfank@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c02::22e Cc: Kevin Wolf , Stefan Hajnoczi , Jan Kiszka , Alex Bligh , Paolo Bonzini , MORITA Kazutaka Subject: [Qemu-devel] [PATCH v2 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 The 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 private lock's protection. 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, 30 insertions(+), 6 deletions(-) diff --git a/cpus.c b/cpus.c index a6d7833..a748608 100644 --- a/cpus.c +++ b/cpus.c @@ -37,6 +37,7 @@ #include "sysemu/qtest.h" #include "qemu/main-loop.h" #include "qemu/bitmap.h" +#include "qemu/seqlock.h" #ifndef _WIN32 #include "qemu/compatfd.h" @@ -107,6 +108,13 @@ static int64_t qemu_icount; typedef struct TimersState { int64_t cpu_ticks_prev; int64_t cpu_ticks_offset; + /* cpu_clock_offset will be read out of BQL, so protect it with private + * lock. As for cpu_ticks_*, no requirement to read it outside BQL yet. + * Lock rule: innermost + */ + QemuSeqLock clock_seqlock; + /* mutex for seqlock */ + QemuMutex mutex; int64_t cpu_clock_offset; int32_t cpu_ticks_enabled; int64_t dummy; @@ -132,6 +140,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 +165,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_offset. */ + 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_offset. */ + 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 @@ -366,6 +388,8 @@ static const VMStateDescription vmstate_timers = { void configure_icount(const char *option) { + qemu_mutex_init(&timers_state.mutex); + seqlock_init(&timers_state.clock_seqlock, &timers_state.mutex); vmstate_register(NULL, 0, &vmstate_timers, &timers_state); if (!option) { return;