From c039e998f2cb65405c65fb1fa0b2c2c5c90b4222 Mon Sep 17 00:00:00 2001
From: Kevin Wolf <kwolf@redhat.com>
Date: Tue, 14 Feb 2012 11:14:16 +0100
Subject: [PATCH 51/99] dma: Avoid reentrancy in DMA transfer handlers

RH-Author: Kevin Wolf <kwolf@redhat.com>
Message-id: <1329218101-24213-52-git-send-email-kwolf@redhat.com>
Patchwork-id: 37245
O-Subject: [RHEL-6.3 qemu-kvm PATCH v2 51/96] dma: Avoid reentrancy in DMA transfer handlers
Bugzilla: 783950
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>

Bugzilla: 783950

With the conversion of the block layer to coroutines, bdrv_read/write
have changed to run a nested event loop that calls qemu_bh_poll.
Consequently a scheduled BH can be called while a DMA transfer handler
runs and this means that DMA_run becomes reentrant.

Devices haven't been designed to cope with that, so instead of running a
nested transfer handler just wait for the next invocation of the BH from the
main loop.

This fixes some problems with the floppy device.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit acae6f1c4c1dae1b7e059751347ca4225b01a391)
---
 hw/dma.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/dma.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/hw/dma.c b/hw/dma.c
index fe69ddd..0d7b431 100644
--- a/hw/dma.c
+++ b/hw/dma.c
@@ -358,6 +358,14 @@ static void DMA_run (void)
     struct dma_cont *d;
     int icont, ichan;
     int rearm = 0;
+    static int running = 0;
+
+    if (running) {
+        rearm = 1;
+        goto out;
+    } else {
+        running = 1;
+    }
 
     d = dma_controllers;
 
@@ -374,6 +382,8 @@ static void DMA_run (void)
         }
     }
 
+    running = 0;
+out:
     if (rearm)
         qemu_bh_schedule_idle(dma_bh);
 }
-- 
1.7.7.5

