diff mbox

[v1,1/2] cpus: don't use atomic_read for vm_clock_warp_start

Message ID 1458577386-9984-2-git-send-email-alex.bennee@linaro.org (mailing list archive)
State New, archived
Headers show

Commit Message

Alex Bennée March 21, 2016, 4:23 p.m. UTC
As vm_clock_warp_start is a 64 bit value this causes problems for the
compiler trying to come up with a suitable atomic operation on 32 bit
hosts. The variable documentation says this is meant to be protected by
vm_clock_seqlock so lets just move the code into that section.

All other references to vm_clock_warp_start are already protected by the
seqlock.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 cpus.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

Comments

Paolo Bonzini March 21, 2016, 4:26 p.m. UTC | #1
On 21/03/2016 17:23, Alex Bennée wrote:
> As vm_clock_warp_start is a 64 bit value this causes problems for the
> compiler trying to come up with a suitable atomic operation on 32 bit
> hosts. The variable documentation says this is meant to be protected by
> vm_clock_seqlock so lets just move the code into that section.

This neglects the fact that there is a comment explaining the code, so

    Because the variable is protected by vm_clock_seqlock, we check its
    value inside a seqlock critical section.

is better.  In addition, I would prefer if you used seqlock_read_*.

Paolo

> All other references to vm_clock_warp_start are already protected by the
> seqlock.
> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  cpus.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/cpus.c b/cpus.c
> index 23cf7aa..2fd5381 100644
> --- a/cpus.c
> +++ b/cpus.c
> @@ -338,15 +338,10 @@ static int64_t qemu_icount_round(int64_t count)
>  
>  static void icount_warp_rt(void)
>  {
> -    /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
> -     * changes from -1 to another value, so the race here is okay.
> -     */
> -    if (atomic_read(&vm_clock_warp_start) == -1) {
> -        return;
> -    }
> +    bool check_clock = false;
>  
>      seqlock_write_lock(&timers_state.vm_clock_seqlock);
> -    if (runstate_is_running()) {
> +    if (vm_clock_warp_start != -1 && runstate_is_running()) {
>          int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
>                                       cpu_get_clock_locked());
>          int64_t warp_delta;
> @@ -362,11 +357,12 @@ static void icount_warp_rt(void)
>              warp_delta = MIN(warp_delta, delta);
>          }
>          timers_state.qemu_icount_bias += warp_delta;
> +        check_clock = true;
>      }
>      vm_clock_warp_start = -1;
>      seqlock_write_unlock(&timers_state.vm_clock_seqlock);
>  
> -    if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
> +    if (check_clock && qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
>          qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
>      }
>  }
Alex Bennée March 21, 2016, 5:45 p.m. UTC | #2
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 21/03/2016 17:23, Alex Bennée wrote:
>> As vm_clock_warp_start is a 64 bit value this causes problems for the
>> compiler trying to come up with a suitable atomic operation on 32 bit
>> hosts. The variable documentation says this is meant to be protected by
>> vm_clock_seqlock so lets just move the code into that section.
>
> This neglects the fact that there is a comment explaining the code, so
>
>     Because the variable is protected by vm_clock_seqlock, we check its
>     value inside a seqlock critical section.
>
> is better.  In addition, I would prefer if you used seqlock_read_*.

I see you comment on the other thread passed the posting. I'll respin.

>
> Paolo
>
>> All other references to vm_clock_warp_start are already protected by the
>> seqlock.
>>
>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>> ---
>>  cpus.c | 12 ++++--------
>>  1 file changed, 4 insertions(+), 8 deletions(-)
>>
>> diff --git a/cpus.c b/cpus.c
>> index 23cf7aa..2fd5381 100644
>> --- a/cpus.c
>> +++ b/cpus.c
>> @@ -338,15 +338,10 @@ static int64_t qemu_icount_round(int64_t count)
>>
>>  static void icount_warp_rt(void)
>>  {
>> -    /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
>> -     * changes from -1 to another value, so the race here is okay.
>> -     */
>> -    if (atomic_read(&vm_clock_warp_start) == -1) {
>> -        return;
>> -    }
>> +    bool check_clock = false;
>>
>>      seqlock_write_lock(&timers_state.vm_clock_seqlock);
>> -    if (runstate_is_running()) {
>> +    if (vm_clock_warp_start != -1 && runstate_is_running()) {
>>          int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
>>                                       cpu_get_clock_locked());
>>          int64_t warp_delta;
>> @@ -362,11 +357,12 @@ static void icount_warp_rt(void)
>>              warp_delta = MIN(warp_delta, delta);
>>          }
>>          timers_state.qemu_icount_bias += warp_delta;
>> +        check_clock = true;
>>      }
>>      vm_clock_warp_start = -1;
>>      seqlock_write_unlock(&timers_state.vm_clock_seqlock);
>>
>> -    if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
>> +    if (check_clock && qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
>>          qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
>>      }
>>  }


--
Alex Bennée
diff mbox

Patch

diff --git a/cpus.c b/cpus.c
index 23cf7aa..2fd5381 100644
--- a/cpus.c
+++ b/cpus.c
@@ -338,15 +338,10 @@  static int64_t qemu_icount_round(int64_t count)
 
 static void icount_warp_rt(void)
 {
-    /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
-     * changes from -1 to another value, so the race here is okay.
-     */
-    if (atomic_read(&vm_clock_warp_start) == -1) {
-        return;
-    }
+    bool check_clock = false;
 
     seqlock_write_lock(&timers_state.vm_clock_seqlock);
-    if (runstate_is_running()) {
+    if (vm_clock_warp_start != -1 && runstate_is_running()) {
         int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
                                      cpu_get_clock_locked());
         int64_t warp_delta;
@@ -362,11 +357,12 @@  static void icount_warp_rt(void)
             warp_delta = MIN(warp_delta, delta);
         }
         timers_state.qemu_icount_bias += warp_delta;
+        check_clock = true;
     }
     vm_clock_warp_start = -1;
     seqlock_write_unlock(&timers_state.vm_clock_seqlock);
 
-    if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
+    if (check_clock && qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
         qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
     }
 }