From patchwork Thu Oct 17 15:48:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 284284 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 890622C00BC for ; Fri, 18 Oct 2013 02:54:20 +1100 (EST) Received: from localhost ([::1]:52973 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWpty-0005cc-HD for incoming@patchwork.ozlabs.org; Thu, 17 Oct 2013 11:54:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41365) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWppJ-00070a-Cx for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VWpp7-0000kR-DU for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:29 -0400 Received: from mail-ea0-x22a.google.com ([2a00:1450:4013:c01::22a]:37411) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWpp7-0000k2-2h for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:17 -0400 Received: by mail-ea0-f170.google.com with SMTP id q10so947184eaj.1 for ; Thu, 17 Oct 2013 08:49:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=OWC8E4HxrpLrN132I8EDOmFr+AqKBwr0Q5fd3my8ZUI=; b=oW7c5QSt7EoQgJktYMch9DInzG8lVuEdB4QD+uEqW+7oOZJsVhib/f8V8+hnNlHtSC gv3g7wrzTMEevoJTwePlS8x/W9WfdqAFw3f4lVLHCfxL9diE4GZTaFj82Ydz09Xy+/H/ HodFiS8IK1hJ22Of5XrOICtQtNP8oIuEAdELj3ovEzx5czTKSy8N/ItoKdHgn7dwVaPJ iZxiB3f4V8akJYLYsaHOzGaNBMDwTq5mdNrkckEkLpY+0Z0yiIefdsQ0fVq/baMsjY0i PIcXT+B7icInKrcnrOs141KCyiLeVg17XylTyHP3WBKfcjt4CVkAtQEhA4C5S7zj9pSh nt2g== X-Received: by 10.15.65.14 with SMTP id p14mr2096062eex.96.1382024956226; Thu, 17 Oct 2013 08:49:16 -0700 (PDT) Received: from playground.lan (net-37-116-196-7.cust.dsl.vodafone.it. [37.116.196.7]) by mx.google.com with ESMTPSA id a43sm194431296eep.9.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 17 Oct 2013 08:49:15 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 17 Oct 2013 17:48:45 +0200 Message-Id: <1382024935-28297-8-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1382024935-28297-1-git-send-email-pbonzini@redhat.com> References: <1382024935-28297-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::22a Cc: Liu Ping Fan , Liu Ping Fan Subject: [Qemu-devel] [PULL 07/17] 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 From: Liu Ping Fan 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 Signed-off-by: Paolo Bonzini --- cpus.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- include/qemu/timer.h | 2 ++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/cpus.c b/cpus.c index e566297..f075335 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" @@ -110,8 +111,14 @@ static int64_t vm_clock_warp_start; static int64_t qemu_icount; typedef struct TimersState { + /* Protected by BQL. */ int64_t cpu_ticks_prev; int64_t cpu_ticks_offset; + + /* cpu_clock_offset can be read out of BQL, so protect it with + * this lock. + */ + QemuSeqLock vm_clock_seqlock; int64_t cpu_clock_offset; int32_t cpu_ticks_enabled; int64_t dummy; @@ -137,6 +144,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) { @@ -157,37 +165,63 @@ int64_t cpu_get_ticks(void) } } -/* return the host CPU monotonic timer and handle stop/restart */ -int64_t cpu_get_clock(void) +static int64_t cpu_get_clock_locked(void) { int64_t ti; + if (!timers_state.cpu_ticks_enabled) { - return timers_state.cpu_clock_offset; + ti = timers_state.cpu_clock_offset; } else { ti = get_clock(); - return ti + timers_state.cpu_clock_offset; + ti += timers_state.cpu_clock_offset; } + + return ti; } -/* enable cpu_get_ticks() */ +/* return the host CPU monotonic timer and handle stop/restart */ +int64_t cpu_get_clock(void) +{ + int64_t ti; + unsigned start; + + do { + start = seqlock_read_begin(&timers_state.vm_clock_seqlock); + ti = cpu_get_clock_locked(); + } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); + + return ti; +} + +/* enable cpu_get_ticks() + * Caller must hold BQL which server as mutex for vm_clock_seqlock. + */ void cpu_enable_ticks(void) { + /* Here, the really thing protected by seqlock is cpu_clock_offset. */ + seqlock_write_lock(&timers_state.vm_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.vm_clock_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 vm_clock_seqlock. + */ void cpu_disable_ticks(void) { + /* Here, the really thing protected by seqlock is cpu_clock_offset. */ + seqlock_write_lock(&timers_state.vm_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_clock_offset = cpu_get_clock_locked(); timers_state.cpu_ticks_enabled = 0; } + seqlock_write_unlock(&timers_state.vm_clock_seqlock); } /* Correlation between real and virtual time is always going to be @@ -371,6 +405,7 @@ static const VMStateDescription vmstate_timers = { void configure_icount(const char *option) { + seqlock_init(&timers_state.vm_clock_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 b58903b..016e29a 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -653,7 +653,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)