Skip to contentSkip to conversation

KVM: dirty ring: add missing memory barrier

PATCH In review Paolo Bonzini 4 participants 4 messages Aug 27, 2022 – Sep 7, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: dirty ring: add missing memory barrier

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.

---
 accel/kvm/kvm-all.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +5 -1
index 8d81ab74de..136c8eaed3 100644
@@ -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)
Signature · 1 line
2.37.1
Reviewed-by Philippe Mathieu-Daudé on v2 1/1
#1 lore raw
Quoted 18 lines · Paolo Bonzini

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.

---
  accel/kvm/kvm-all.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +0 -0
index 8d81ab74de..136c8eaed3 100644
@@ -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)

accel/kvm/kvm-all.c static void kvm_dirty_ring_mark_page(KVMState *s, uint32_t as_id, · line 729

Quoted 10 lines
accel/kvm/kvm-all.c +5 -1
  {
-    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 on v2 1/1
#3 lore raw
In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

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.

---
  accel/kvm/kvm-all.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 18 lines
accel/kvm/kvm-all.c +5 -1
index 8d81ab74de..136c8eaed3 100644
@@ -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)