From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btb74-0001H3-QU for qemu-devel@nongnu.org; Mon, 10 Oct 2016 09:59:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1btb73-00087m-H6 for qemu-devel@nongnu.org; Mon, 10 Oct 2016 09:59:30 -0400 Received: from mail-qk0-x241.google.com ([2607:f8b0:400d:c09::241]:34018) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1btb73-00087a-Cn for qemu-devel@nongnu.org; Mon, 10 Oct 2016 09:59:29 -0400 Received: by mail-qk0-x241.google.com with SMTP id n189so6872734qke.1 for ; Mon, 10 Oct 2016 06:59:29 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Mon, 10 Oct 2016 15:59:04 +0200 Message-Id: <1476107947-31430-3-git-send-email-pbonzini@redhat.com> In-Reply-To: <1476107947-31430-1-git-send-email-pbonzini@redhat.com> References: <1476107947-31430-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 2/5] cpus: use atomic_read to read seqlock-protected variables List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: cota@braap.org, alex.bennee@linaro.org There is a data race if the variable is written concurrently to the read. In C11 this has undefined behavior. Use atomic_read. The write side does not need atomic_set, because it is protected by a mutex. Signed-off-by: Paolo Bonzini --- cpus.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cpus.c b/cpus.c index b2fbe33..3fc2f6e 100644 --- a/cpus.c +++ b/cpus.c @@ -170,7 +170,8 @@ int64_t cpu_get_icount_raw(void) static int64_t cpu_get_icount_locked(void) { int64_t icount = cpu_get_icount_raw(); - return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount); + int64_t ns = cpu_icount_to_ns(icount); + return atomic_read(&timers_state.qemu_icount_bias) + ns; } int64_t cpu_get_icount(void) @@ -206,7 +207,7 @@ int64_t cpu_get_ticks(void) } ticks = timers_state.cpu_ticks_offset; - if (timers_state.cpu_ticks_enabled) { + if (atomic_read(&timers_state.cpu_ticks_enabled)) { ticks += cpu_get_host_ticks(); } @@ -225,8 +226,8 @@ static int64_t cpu_get_clock_locked(void) { int64_t time; - time = timers_state.cpu_clock_offset; - if (timers_state.cpu_ticks_enabled) { + time = atomic_read(&timers_state.cpu_clock_offset); + if (atomic_read(&timers_state.cpu_ticks_enabled)) { time += get_clock(); } -- 2.7.4