[PATCH] KVM: dirty ring: add missing memory barrier

Paolo Bonzini posted 1 patch 3 years, 5 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20220826110029.59382-1-pbonzini@redhat.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
accel/kvm/kvm-all.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] KVM: dirty ring: add missing memory barrier
Posted by Paolo Bonzini 3 years, 5 months ago
The KVM_DIRTY_GFN_F_DIRTY flag ensures that the entry is valid.  If
the read of the fields are not ordered after the read of the flag,
QEMU might see stale values.

Cc: Peter Xu <peterx@redhat.com>
Cc: Gavin Shan <gshan@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 accel/kvm/kvm-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 8d81ab74de..f49643cd24 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -719,7 +719,7 @@ static void kvm_dirty_ring_mark_page(KVMState *s, uint32_t as_id,
 
 static bool dirty_gfn_is_dirtied(struct kvm_dirty_gfn *gfn)
 {
-    return gfn->flags == KVM_DIRTY_GFN_F_DIRTY;
+    return qatomic_load_acquire(&gfn->flags, KVM_DIRTY_GFN_F_DIRTY);
 }
 
 static void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn)
-- 
2.37.1
Re: [PATCH] KVM: dirty ring: add missing memory barrier
Posted by Philippe Mathieu-Daudé via 3 years, 5 months ago
Hi Paolo,

On Fri, Aug 26, 2022 at 1:17 PM Paolo Bonzini <pbonzini@redhat.com> wrote:

> The KVM_DIRTY_GFN_F_DIRTY flag ensures that the entry is valid.  If
> the read of the fields are not ordered after the read of the flag,
> QEMU might see stale values.
>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Gavin Shan <gshan@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  accel/kvm/kvm-all.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 8d81ab74de..f49643cd24 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -719,7 +719,7 @@ static void kvm_dirty_ring_mark_page(KVMState *s,
> uint32_t as_id,
>
>  static bool dirty_gfn_is_dirtied(struct kvm_dirty_gfn *gfn)
>  {
> -    return gfn->flags == KVM_DIRTY_GFN_F_DIRTY;
> +    return qatomic_load_acquire(&gfn->flags, KVM_DIRTY_GFN_F_DIRTY);
>

Is this patch based on another which changes the qatomic_load_acquire()
prototype?


>  }
>
>  static void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn)
> --
> 2.37.1
>
>
>
Re: [PATCH] KVM: dirty ring: add missing memory barrier
Posted by Paolo Bonzini 3 years, 5 months ago
Yikes no, the patch is obviously bogus.

Paolo

Il ven 26 ago 2022, 17:59 Philippe Mathieu-Daudé <f4bug@amsat.org> ha
scritto:

> Hi Paolo,
>
> On Fri, Aug 26, 2022 at 1:17 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
>> The KVM_DIRTY_GFN_F_DIRTY flag ensures that the entry is valid.  If
>> the read of the fields are not ordered after the read of the flag,
>> QEMU might see stale values.
>>
>> Cc: Peter Xu <peterx@redhat.com>
>> Cc: Gavin Shan <gshan@redhat.com>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>>  accel/kvm/kvm-all.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
>> index 8d81ab74de..f49643cd24 100644
>> --- a/accel/kvm/kvm-all.c
>> +++ b/accel/kvm/kvm-all.c
>> @@ -719,7 +719,7 @@ static void kvm_dirty_ring_mark_page(KVMState *s,
>> uint32_t as_id,
>>
>>  static bool dirty_gfn_is_dirtied(struct kvm_dirty_gfn *gfn)
>>  {
>> -    return gfn->flags == KVM_DIRTY_GFN_F_DIRTY;
>> +    return qatomic_load_acquire(&gfn->flags, KVM_DIRTY_GFN_F_DIRTY);
>>
>
> Is this patch based on another which changes the qatomic_load_acquire()
> prototype?
>
>
>>  }
>>
>>  static void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn)
>> --
>> 2.37.1
>>
>>
>>