495e37
From 14582cfec72e52894f16ed5c3fb14adb2d6d8e25 Mon Sep 17 00:00:00 2001
495e37
From: Rao Lei <lei.rao@intel.com>
495e37
Date: Wed, 5 Jan 2022 10:08:08 +0800
495e37
Subject: [PATCH 4/6] ui/vnc.c: Fixed a deadlock bug.
495e37
MIME-Version: 1.0
495e37
Content-Type: text/plain; charset=UTF-8
495e37
Content-Transfer-Encoding: 8bit
495e37
495e37
RH-Author: Gerd Hoffmann <kraxel@redhat.com>
495e37
RH-MergeRequest: 75: fix vnc cut+paste crash
495e37
RH-Commit: [4/4] 5321e447de974d91e9a6c0cf01f4352166ffb7ce (kraxel/centos-qemu-kvm)
495e37
RH-Bugzilla: 2042820
495e37
RH-Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com>
495e37
RH-Acked-by: Daniel P. Berrangé <berrange@redhat.com>
495e37
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
495e37
495e37
The GDB statck is as follows:
495e37
(gdb) bt
495e37
0  __lll_lock_wait (futex=futex@entry=0x56211df20360, private=0) at lowlevellock.c:52
495e37
1  0x00007f263caf20a3 in __GI___pthread_mutex_lock (mutex=0x56211df20360) at ../nptl/pthread_mutex_lock.c:80
495e37
2  0x000056211a757364 in qemu_mutex_lock_impl (mutex=0x56211df20360, file=0x56211a804857 "../ui/vnc-jobs.h", line=60)
495e37
    at ../util/qemu-thread-posix.c:80
495e37
3  0x000056211a0ef8c7 in vnc_lock_output (vs=0x56211df14200) at ../ui/vnc-jobs.h:60
495e37
4  0x000056211a0efcb7 in vnc_clipboard_send (vs=0x56211df14200, count=1, dwords=0x7ffdf1701338) at ../ui/vnc-clipboard.c:138
495e37
5  0x000056211a0f0129 in vnc_clipboard_notify (notifier=0x56211df244c8, data=0x56211dd1bbf0) at ../ui/vnc-clipboard.c:209
495e37
6  0x000056211a75dde8 in notifier_list_notify (list=0x56211afa17d0 <clipboard_notifiers>, data=0x56211dd1bbf0) at ../util/notify.c:39
495e37
7  0x000056211a0bf0e6 in qemu_clipboard_update (info=0x56211dd1bbf0) at ../ui/clipboard.c:50
495e37
8  0x000056211a0bf05d in qemu_clipboard_peer_release (peer=0x56211df244c0, selection=QEMU_CLIPBOARD_SELECTION_CLIPBOARD)
495e37
    at ../ui/clipboard.c:41
495e37
9  0x000056211a0bef9b in qemu_clipboard_peer_unregister (peer=0x56211df244c0) at ../ui/clipboard.c:19
495e37
10 0x000056211a0d45f3 in vnc_disconnect_finish (vs=0x56211df14200) at ../ui/vnc.c:1358
495e37
11 0x000056211a0d4c9d in vnc_client_read (vs=0x56211df14200) at ../ui/vnc.c:1611
495e37
12 0x000056211a0d4df8 in vnc_client_io (ioc=0x56211ce70690, condition=G_IO_IN, opaque=0x56211df14200) at ../ui/vnc.c:1649
495e37
13 0x000056211a5b976c in qio_channel_fd_source_dispatch
495e37
    (source=0x56211ce50a00, callback=0x56211a0d4d71 <vnc_client_io>, user_data=0x56211df14200) at ../io/channel-watch.c:84
495e37
14 0x00007f263ccede8e in g_main_context_dispatch () at /lib/x86_64-linux-gnu/libglib-2.0.so.0
495e37
15 0x000056211a77d4a1 in glib_pollfds_poll () at ../util/main-loop.c:232
495e37
16 0x000056211a77d51f in os_host_main_loop_wait (timeout=958545) at ../util/main-loop.c:255
495e37
17 0x000056211a77d630 in main_loop_wait (nonblocking=0) at ../util/main-loop.c:531
495e37
18 0x000056211a45bc8e in qemu_main_loop () at ../softmmu/runstate.c:726
495e37
19 0x000056211a0b45fa in main (argc=69, argv=0x7ffdf1701778, envp=0x7ffdf17019a8) at ../softmmu/main.c:50
495e37
495e37
From the call trace, we can see it is a deadlock bug.
495e37
vnc_disconnect_finish will acquire the output_mutex.
495e37
But, the output_mutex will be acquired again in vnc_clipboard_send.
495e37
Repeated locking will cause deadlock. So, I move
495e37
qemu_clipboard_peer_unregister() behind vnc_unlock_output();
495e37
495e37
Fixes: 0bf41cab93e ("ui/vnc: clipboard support")
495e37
Signed-off-by: Lei Rao <lei.rao@intel.com>
495e37
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
495e37
Message-Id: <20220105020808.597325-1-lei.rao@intel.com>
495e37
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
495e37
(cherry picked from commit 1dbbe6f172810026c51dc84ed927a3cc23017949)
495e37
---
495e37
 ui/vnc.c | 4 ++--
495e37
 1 file changed, 2 insertions(+), 2 deletions(-)
495e37
495e37
diff --git a/ui/vnc.c b/ui/vnc.c
495e37
index af02522e84..b253e85c65 100644
495e37
--- a/ui/vnc.c
495e37
+++ b/ui/vnc.c
495e37
@@ -1354,12 +1354,12 @@ void vnc_disconnect_finish(VncState *vs)
495e37
         /* last client gone */
495e37
         vnc_update_server_surface(vs->vd);
495e37
     }
495e37
+    vnc_unlock_output(vs);
495e37
+
495e37
     if (vs->cbpeer.update.notify) {
495e37
         qemu_clipboard_peer_unregister(&vs->cbpeer);
495e37
     }
495e37
 
495e37
-    vnc_unlock_output(vs);
495e37
-
495e37
     qemu_mutex_destroy(&vs->output_mutex);
495e37
     if (vs->bh != NULL) {
495e37
         qemu_bh_delete(vs->bh);
495e37
-- 
495e37
2.27.0
495e37