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