From patchwork Wed Sep 25 06:20:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 277679 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 B37312C00C5 for ; Wed, 25 Sep 2013 16:22:09 +1000 (EST) Received: from localhost ([::1]:49675 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOiUB-00073i-Hf for incoming@patchwork.ozlabs.org; Wed, 25 Sep 2013 02:22:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41399) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOiTZ-0006lc-9c for qemu-devel@nongnu.org; Wed, 25 Sep 2013 02:21:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOiTO-0003VO-CP for qemu-devel@nongnu.org; Wed, 25 Sep 2013 02:21:29 -0400 Received: from mail-ob0-x22e.google.com ([2607:f8b0:4003:c01::22e]:64931) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOiTO-0003VK-5w for qemu-devel@nongnu.org; Wed, 25 Sep 2013 02:21:18 -0400 Received: by mail-ob0-f174.google.com with SMTP id uz6so6142489obc.5 for ; Tue, 24 Sep 2013 23:21:17 -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=3oA9EpzSGFLQlXnDZ+tEFlzrWAeqdcmO1ayF1e+Maso=; b=vJHi98be1VnjKSzsmWs/jlgXzvnUQTKpQeMuKiKoS+QpqDVS2ox8HEiyUR8TdEcM4J qfHcQaj8lwReidJlXWGUjKyV4IRUtDY+honfvDMQ0JsPJ2dYIKKwUNIheaZVfmjeQljT KV1L15gz9oETkRHXpXNPChWRW6W4vAZMds3HJ9yzephqW2CwewdybKUXoDvSwEtK6KIR QUhdINxxVBuTEq3zh2QdsVLmsFUrmzs1Nsu+ci8X4nEnqkj4hghfNbRgKZNI1XysfPJW k3Fa/srEDUf7lJXEJ3ZgkGXV+bWtbpPf9WCCTDi2X+1ulZY95kU5/Fn4a9hUSF/s6rPN cdfw== X-Received: by 10.60.63.68 with SMTP id e4mr28842579oes.23.1380090077670; Tue, 24 Sep 2013 23:21:17 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id rr6sm34731324oeb.0.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 24 Sep 2013 23:21:16 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 25 Sep 2013 14:20:58 +0800 Message-Id: <1380090060-6764-3-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1380090060-6764-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1380090060-6764-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:c01::22e Cc: Kevin Wolf , Paolo Bonzini , Alex Bligh , Stefan Hajnoczi , Jan Kiszka Subject: [Qemu-devel] [PATCH v5 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 QEMU_CLOCK_VIRTUAL may be read outside BQL. This will make its foundation, i.e. cpu_clock_offset exposed to race condition. Using private lock to protect it. After this patch, reading QEMU_CLOCK_VIRTUAL is thread safe unless use_icount is true, in which case the existing callers still rely on the BQL Lock rule: private lock innermost, ie BQL->"this lock" Signed-off-by: Liu Ping Fan --- cpus.c | 41 +++++++++++++++++++++++++++++++++-------- include/qemu/timer.h | 2 ++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/cpus.c b/cpus.c index e566297..5baa76d 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,11 @@ 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 cpu_clock_offset_seqlock; int64_t cpu_clock_offset; int32_t cpu_ticks_enabled; int64_t dummy; @@ -137,6 +143,7 @@ int64_t cpu_get_icount(void) } /* return the host CPU cycle counter and handle stop/restart */ +/* Caller must hold the BQL */ int64_t cpu_get_ticks(void) { if (use_icount) { @@ -161,33 +168,50 @@ 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.cpu_clock_offset_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.cpu_clock_offset_seqlock, start)); + + return ti; } -/* enable cpu_get_ticks() */ +/* enable cpu_get_ticks() + * Caller must hold BQL which server as mutex for cpu_clock_offset_seqlock. + */ void cpu_enable_ticks(void) { + /* Here, the really thing protected by seqlock is cpu_clock_offset. */ + seqlock_write_lock(&timers_state.cpu_clock_offset_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.cpu_clock_offset_seqlock); } /* disable cpu_get_ticks() : the clock is stopped. You must not call - cpu_get_ticks() after that. */ + * cpu_get_ticks() after that. + * Caller must hold BQL which server as mutex for cpu_clock_offset_seqlock. + */ void cpu_disable_ticks(void) { + /* Here, the really thing protected by seqlock is cpu_clock_offset. */ + seqlock_write_lock(&timers_state.cpu_clock_offset_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.cpu_clock_offset_seqlock); } /* Correlation between real and virtual time is always going to be @@ -371,6 +395,7 @@ static const VMStateDescription vmstate_timers = { void configure_icount(const char *option) { + seqlock_init(&timers_state.cpu_clock_offset_seqlock, NULL); vmstate_register(NULL, 0, &vmstate_timers, &timers_state); if (!option) { return; diff --git a/include/qemu/timer.h b/include/qemu/timer.h index e4934dd..bb1de23 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -636,7 +636,9 @@ static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2) void init_clocks(void); int64_t cpu_get_ticks(void); +/* Caller must hold BQL */ void cpu_enable_ticks(void); +/* Caller must hold BQL */ void cpu_disable_ticks(void); static inline int64_t get_ticks_per_sec(void)