From patchwork Tue Aug 27 03:21:01 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 270012 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 406BD2C00A4 for ; Tue, 27 Aug 2013 13:24:03 +1000 (EST) Received: from localhost ([::1]:53865 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VE9sv-0002V2-5u for incoming@patchwork.ozlabs.org; Mon, 26 Aug 2013 23:24:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50361) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VE9sM-0002Oc-4X for qemu-devel@nongnu.org; Mon, 26 Aug 2013 23:23:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VE9sA-0006Hp-03 for qemu-devel@nongnu.org; Mon, 26 Aug 2013 23:23:26 -0400 Received: from mail-oa0-x236.google.com ([2607:f8b0:4003:c02::236]:49824) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VE9s9-0006Gp-QP for qemu-devel@nongnu.org; Mon, 26 Aug 2013 23:23:13 -0400 Received: by mail-oa0-f54.google.com with SMTP id o6so4627485oag.41 for ; Mon, 26 Aug 2013 20:23:13 -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=pyQXNncfe5HCoofb3ZsHRHsMqPlKJB3Gcff+Yuy0WwI=; b=KiwWXJW4R9pP/jwVKxCASw46rY32XNaHUyqMwc6pQlkxnC60Lc6yd9zFzqBb5ThCMr EJbOhrAVrbrc4IzqUVRGcKeeqboC4mzYbJ7tmj43JTQ+5OBk80Gv8o5xrLjnWZUHHmbs 03g27Y7sz4+nS8QOJ5+AkeHORM2FRqE0Tpx9QtxE+9W5bq1vST0a1io2AbsKptryTD90 r9WW3nX05wvmC8zMCjS7NBDQxWOyTikPaGsvYcfMsF0cA/vKrfNIUzZKbtNSlj0b2qV/ 2T0F1zqHXBx/yA1xs4xlDuYmULrD8d3OVQhuMPFkdrGG6TIdaIzLS3SfNl3jJmkSsCiF WUQA== X-Received: by 10.60.63.68 with SMTP id e4mr17039496oes.23.1377573793290; Mon, 26 Aug 2013 20:23:13 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id rl1sm18461034oeb.7.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 26 Aug 2013 20:23:12 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Tue, 27 Aug 2013 11:21:01 +0800 Message-Id: <1377573663-16727-3-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1377573663-16727-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1377573663-16727-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::236 Cc: Kevin Wolf , Paolo Bonzini , Alex Bligh , Stefan Hajnoczi , Jan Kiszka Subject: [Qemu-devel] [PATCH v3 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 b9e5685..bcead3b 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" @@ -112,6 +113,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; @@ -137,6 +145,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) { @@ -161,33 +170,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_retry(&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 @@ -371,6 +393,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;