[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH 2/5] cpus: use atomic_read to read seqlock-prote
From: |
Alex Bennée |
Subject: |
Re: [Qemu-devel] [PATCH 2/5] cpus: use atomic_read to read seqlock-protected variables |
Date: |
Wed, 12 Oct 2016 09:44:17 +0100 |
User-agent: |
mu4e 0.9.17; emacs 25.1.50.7 |
Paolo Bonzini <address@hidden> 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 <address@hidden>
Reviewed-by: Alex Bennée <address@hidden>
> ---
> 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
- [Qemu-devel] [PATCH 0/5] More thread sanitizer fixes and atomic.h improvements, Paolo Bonzini, 2016/10/10
- [Qemu-devel] [PATCH 2/5] cpus: use atomic_read to read seqlock-protected variables, Paolo Bonzini, 2016/10/10
- [Qemu-devel] [PATCH 3/5] qemu-thread: use acquire/release to clarify semantics of QemuEvent, Paolo Bonzini, 2016/10/10
- [Qemu-devel] [PATCH 1/5] atomic: introduce smp_mb_acquire and smp_mb_release, Paolo Bonzini, 2016/10/10
- [Qemu-devel] [PATCH 4/5] rcu: simplify memory barriers, Paolo Bonzini, 2016/10/10
- [Qemu-devel] [PATCH 5/5] atomic: base mb_read/mb_set on load-acquire and store-release, Paolo Bonzini, 2016/10/10