From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60193) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buF9H-000512-5p for qemu-devel@nongnu.org; Wed, 12 Oct 2016 04:44:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1buF9A-0000tz-31 for qemu-devel@nongnu.org; Wed, 12 Oct 2016 04:44:23 -0400 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]:38778) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1buF99-0000tM-Sv for qemu-devel@nongnu.org; Wed, 12 Oct 2016 04:44:20 -0400 Received: by mail-wm0-x22b.google.com with SMTP id c78so17963803wme.1 for ; Wed, 12 Oct 2016 01:44:19 -0700 (PDT) References: <1476107947-31430-1-git-send-email-pbonzini@redhat.com> <1476107947-31430-3-git-send-email-pbonzini@redhat.com> From: Alex =?utf-8?Q?Benn=C3=A9e?= In-reply-to: <1476107947-31430-3-git-send-email-pbonzini@redhat.com> Date: Wed, 12 Oct 2016 09:44:17 +0100 Message-ID: <87zimanehq.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [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: Paolo Bonzini Cc: qemu-devel@nongnu.org, cota@braap.org Paolo Bonzini writes: > 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 Reviewed-by: Alex Bennée > --- > 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(); > } -- Alex Bennée