qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] KVM: dirty ring: add missing memory barrier
@ 2022-08-27  8:22 Paolo Bonzini
  2022-08-30 12:07 ` Philippe Mathieu-Daudé via
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Paolo Bonzini @ 2022-08-27  8:22 UTC (permalink / raw)
  To: qemu-devel; +Cc: f4bug, Peter Xu, Gavin Shan

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 | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 8d81ab74de..136c8eaed3 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -719,7 +719,11 @@ 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;
+    /*
+     * Read the flags before the value.  Pairs with barrier in
+     * KVM's kvm_dirty_ring_push() function.
+     */
+    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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] KVM: dirty ring: add missing memory barrier
  2022-08-27  8:22 [PATCH v2] KVM: dirty ring: add missing memory barrier Paolo Bonzini
@ 2022-08-30 12:07 ` Philippe Mathieu-Daudé via
  2022-08-30 14:24 ` Peter Xu
  2022-09-07  0:37 ` Gavin Shan
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-08-30 12:07 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: Peter Xu, Gavin Shan

On 27/8/22 10:22, Paolo Bonzini 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 | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 8d81ab74de..136c8eaed3 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -719,7 +719,11 @@ 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;
> +    /*
> +     * Read the flags before the value.  Pairs with barrier in
> +     * KVM's kvm_dirty_ring_push() function.
> +     */
> +    return qatomic_load_acquire(&gfn->flags) == KVM_DIRTY_GFN_F_DIRTY;
>   }
>   
>   static void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] KVM: dirty ring: add missing memory barrier
  2022-08-27  8:22 [PATCH v2] KVM: dirty ring: add missing memory barrier Paolo Bonzini
  2022-08-30 12:07 ` Philippe Mathieu-Daudé via
@ 2022-08-30 14:24 ` Peter Xu
  2022-09-07  0:37 ` Gavin Shan
  2 siblings, 0 replies; 4+ messages in thread
From: Peter Xu @ 2022-08-30 14:24 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: qemu-devel, f4bug, Gavin Shan

On Sat, Aug 27, 2022 at 10:22:18AM +0200, Paolo Bonzini 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>

Reviewed-by: Peter Xu <peterx@redhat.com>

Thanks!

-- 
Peter Xu



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] KVM: dirty ring: add missing memory barrier
  2022-08-27  8:22 [PATCH v2] KVM: dirty ring: add missing memory barrier Paolo Bonzini
  2022-08-30 12:07 ` Philippe Mathieu-Daudé via
  2022-08-30 14:24 ` Peter Xu
@ 2022-09-07  0:37 ` Gavin Shan
  2 siblings, 0 replies; 4+ messages in thread
From: Gavin Shan @ 2022-09-07  0:37 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: f4bug, Peter Xu

On 8/27/22 6:22 PM, Paolo Bonzini 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 | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 

Reviewed-by: Gavin Shan <gshan@redhat.com>

> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 8d81ab74de..136c8eaed3 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -719,7 +719,11 @@ 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;
> +    /*
> +     * Read the flags before the value.  Pairs with barrier in
> +     * KVM's kvm_dirty_ring_push() function.
> +     */
> +    return qatomic_load_acquire(&gfn->flags) == KVM_DIRTY_GFN_F_DIRTY;
>   }
>   
>   static void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn)
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-07  0:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-27  8:22 [PATCH v2] KVM: dirty ring: add missing memory barrier Paolo Bonzini
2022-08-30 12:07 ` Philippe Mathieu-Daudé via
2022-08-30 14:24 ` Peter Xu
2022-09-07  0:37 ` Gavin Shan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).