From 06e4ed9270d49adc928c7e9aa0655c23f59b6920 Mon Sep 17 00:00:00 2001
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Tue, 28 Feb 2012 12:53:18 +0100
Subject: [PATCH 5/7] kvm: avoid reentring kvm_flush_coalesced_mmio_buffer()

RH-Author: Paolo Bonzini <pbonzini@redhat.com>
Message-id: <1330433598-21534-6-git-send-email-pbonzini@redhat.com>
Patchwork-id: 37672
O-Subject: [RHEL 6.3 qemu-kvm PATCH 5/5] kvm: avoid reentring kvm_flush_coalesced_mmio_buffer()
Bugzilla: 796575
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Markus Armbruster <armbru@redhat.com>

From: Avi Kivity <avi@redhat.com>

mmio callbacks invoked by kvm_flush_coalesced_mmio_buffer() may
themselves indirectly call kvm_flush_coalesced_mmio_buffer().
Prevent reentering the function by checking a flag that indicates
we're processing coalesced mmio requests.

Signed-off-by: Avi Kivity <avi@redhat.com>
(cherry picked from commit 1cae88b9f4121c9af0bf677435c6129e643280fd)

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 kvm-all.c  |   10 ++++++++++
 qemu-kvm.c |    8 ++++++++
 qemu-kvm.h |    1 +
 3 files changed, 19 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 kvm-all.c  |   10 ++++++++++
 qemu-kvm.c |    8 ++++++++
 qemu-kvm.h |    1 +
 3 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/kvm-all.c b/kvm-all.c
index a50ec56..5c56e1a 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -65,6 +65,7 @@ struct KVMState
 #ifdef KVM_CAP_COALESCED_MMIO
     struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 #endif
+    bool coalesced_flush_in_progress;
     int broken_set_mem_region;
     int migration_log;
     int vcpu_events;
@@ -602,6 +603,13 @@ void kvm_flush_coalesced_mmio_buffer(void)
 {
 #ifdef KVM_CAP_COALESCED_MMIO
     KVMState *s = kvm_state;
+
+    if (s->coalesced_flush_in_progress) {
+        return;
+    }
+
+    s->coalesced_flush_in_progress = true;
+
     if (s->coalesced_mmio_ring) {
         struct kvm_coalesced_mmio_ring *ring = s->coalesced_mmio_ring;
         while (ring->first != ring->last) {
@@ -614,6 +622,8 @@ void kvm_flush_coalesced_mmio_buffer(void)
             ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
         }
     }
+
+    s->coalesced_flush_in_progress = false;
 #endif
 }
 
diff --git a/qemu-kvm.c b/qemu-kvm.c
index ba5a037..6edb5f8 100644
--- a/qemu-kvm.c
+++ b/qemu-kvm.c
@@ -970,6 +970,12 @@ static int kvm_handle_internal_error(kvm_context_t kvm,
 void kvm_flush_coalesced_mmio_buffer(void)
 {
 #if defined(KVM_CAP_COALESCED_MMIO)
+    if (kvm_state->coalesced_flush_in_progress) {
+        return;
+    }
+
+    kvm_state->coalesced_flush_in_progress = true;
+
     if (kvm_state->coalesced_mmio) {
         struct kvm_coalesced_mmio_ring *ring = kvm_state->coalesced_mmio_ring;
         while (ring->first != ring->last) {
@@ -980,6 +986,8 @@ void kvm_flush_coalesced_mmio_buffer(void)
             ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
         }
     }
+
+    kvm_state->coalesced_flush_in_progress = false;
 #endif
 }
 
diff --git a/qemu-kvm.h b/qemu-kvm.h
index 3be5fd0..738e2fb 100644
--- a/qemu-kvm.h
+++ b/qemu-kvm.h
@@ -1176,6 +1176,7 @@ typedef struct KVMState {
 #ifdef KVM_CAP_COALESCED_MMIO
     struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 #endif
+    bool coalesced_flush_in_progress;
     int broken_set_mem_region;
     int migration_log;
     int vcpu_events;
-- 
1.7.7.6

