22c213
From 8f6311159977b8ee4b78172caa411d3cee4d2ae5 Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Tue, 14 Jan 2020 20:23:30 +0000
22c213
Subject: [PATCH 4/5] usbredir: Prevent recursion in usbredir_write
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-id: <20200114202331.51831-2-dgilbert@redhat.com>
22c213
Patchwork-id: 93344
22c213
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 1/2] usbredir: Prevent recursion in usbredir_write
22c213
Bugzilla: 1790844
22c213
RH-Acked-by: Peter Xu <peterx@redhat.com>
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
22c213
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
22c213
I've got a case where usbredir_write manages to call back into itself
22c213
via spice; this patch causes the recursion to fail (0 bytes) the write;
22c213
this seems to avoid the deadlock I was previously seeing.
22c213
22c213
I can't say I fully understand the interaction of usbredir and spice;
22c213
but there are a few similar guards in spice and usbredir
22c213
to catch other cases especially onces also related to spice_server_char_device_wakeup
22c213
22c213
This case seems to be triggered by repeated migration+repeated
22c213
reconnection of the viewer; but my debugging suggests the migration
22c213
finished before this hits.
22c213
22c213
The backtrace of the hang looks like:
22c213
  reds_handle_ticket
22c213
  reds_handle_other_links
22c213
  reds_channel_do_link
22c213
  red_channel_connect
22c213
  spicevmc_connect
22c213
  usbredir_create_parser
22c213
  usbredirparser_do_write
22c213
  usbredir_write
22c213
  qemu_chr_fe_write
22c213
  qemu_chr_write
22c213
  qemu_chr_write_buffer
22c213
  spice_chr_write
22c213
  spice_server_char_device_wakeup
22c213
  red_char_device_wakeup
22c213
  red_char_device_write_to_device
22c213
  vmc_write
22c213
  usbredirparser_do_write
22c213
  usbredir_write
22c213
  qemu_chr_fe_write
22c213
  qemu_chr_write
22c213
  qemu_chr_write_buffer
22c213
  qemu_mutex_lock_impl
22c213
22c213
and we fail as we land through qemu_chr_write_buffer's lock
22c213
twice.
22c213
22c213
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1752320
22c213
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-Id: <20191218113012.13331-1-dgilbert@redhat.com>
22c213
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
22c213
(cherry picked from commit 394642a8d3742c885e397d5bb5ee0ec40743cdc6)
22c213
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
22c213
---
22c213
 hw/usb/redirect.c | 9 +++++++++
22c213
 1 file changed, 9 insertions(+)
22c213
22c213
diff --git a/hw/usb/redirect.c b/hw/usb/redirect.c
22c213
index e0f5ca6..97f2c3a 100644
22c213
--- a/hw/usb/redirect.c
22c213
+++ b/hw/usb/redirect.c
22c213
@@ -113,6 +113,7 @@ struct USBRedirDevice {
22c213
     /* Properties */
22c213
     CharBackend cs;
22c213
     bool enable_streams;
22c213
+    bool in_write;
22c213
     uint8_t debug;
22c213
     int32_t bootindex;
22c213
     char *filter_str;
22c213
@@ -290,6 +291,13 @@ static int usbredir_write(void *priv, uint8_t *data, int count)
22c213
         return 0;
22c213
     }
22c213
 
22c213
+    /* Recursion check */
22c213
+    if (dev->in_write) {
22c213
+        DPRINTF("usbredir_write recursion\n");
22c213
+        return 0;
22c213
+    }
22c213
+    dev->in_write = true;
22c213
+
22c213
     r = qemu_chr_fe_write(&dev->cs, data, count);
22c213
     if (r < count) {
22c213
         if (!dev->watch) {
22c213
@@ -300,6 +308,7 @@ static int usbredir_write(void *priv, uint8_t *data, int count)
22c213
             r = 0;
22c213
         }
22c213
     }
22c213
+    dev->in_write = false;
22c213
     return r;
22c213
 }
22c213
 
22c213
-- 
22c213
1.8.3.1
22c213