Skip to contentSkip to conversation

ui/gtk: prevent QEMU lock up

PATCH Superseded Volker Rümelin 4 participants 11 messages Jul 18, 2021 – Jul 21, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ui/gtk: prevent QEMU lock up

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

Patch 1/2 is a bug fix for 6.1.

Patch 2/2 is a preview for 6.2. If there are no objections, I will resend this patch for the 6.2 development cycle together with changes to ui/console similar to the changes in patch 1/2. This will remove the QEMUFIFO code in ui/console.c.

Volker Rümelin (2):
  ui/gtk: add a keyboard fifo to the VTE consoles ui/gtk: drop chars if the chardev frontend makes no progress

 include/ui/gtk.h |  7 ++++
 ui/gtk.c         | 84 +++++++++++++++++++++++++++++++++++++++++++-----

 2 files changed, 83 insertions(+), 8 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 ui/gtk: add a keyboard fifo to the VTE consoles

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
 include/ui/gtk.h |  5 +++++
 ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 8 deletions(-)
include/ui/gtk.h +5 -0
index 9516670ebc..4714218376 100644
@@ -25,6 +25,9 @@
 #include "ui/egl-helpers.h"
 #include "ui/egl-context.h"
 #endif
+#ifdef CONFIG_VTE
+#include "qemu/fifo8.h"
+#endif
 
 #define MAX_VCS 10
 
@@ -62,6 +65,8 @@ typedef struct VirtualVteConsole {
     GtkWidget *scrollbar;
     GtkWidget *terminal;
     Chardev *chr;
+    QEMUTimer *kbd_timer;
+    Fifo8 out_fifo;
     bool echo;
 } VirtualVteConsole;
 #endif
ui/gtk.c +45 -8

83 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

Quoted 12 lines · Volker Rümelin

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Quoted 6 lines
Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
 include/ui/gtk.h |  5 +++++
 ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 8 deletions(-)

This feels like maybe it's the kind of thing that should be handled more generically rather than in one particular UI frontend ?

thanks
-- PMM

#3 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

Quoted 20 lines · Peter Maydell
Quoted 12 lines · Volker Rümelin

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Quoted 6 lines
Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
 include/ui/gtk.h |  5 +++++
 ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 8 deletions(-)
Quoted 2 lines

This feels like maybe it's the kind of thing that should be handled more generically rather than in one particular UI frontend ?

IIUC none of the other frontends directly talk to the chardevs for the serial ports. Instead they communicate via the text console interfaces. So GTK is a bit special already here.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

In reply to Peter Maydell

Am 18.07.21 um 15:00 schrieb Peter Maydell:

Quoted 18 lines · Volker Rümelin
Quoted 11 lines

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Quoted 6 lines
Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
  include/ui/gtk.h |  5 +++++
  ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
  2 files changed, 50 insertions(+), 8 deletions(-)
Quoted 5 lines

This feels like maybe it's the kind of thing that should be handled more generically rather than in one particular UI frontend ?

thanks
-- PMM

All other UI frontends (except Spice) use the correct code in kbd_send_chars(). I think only the GTK UI code is wrong.

With best regards
Volker

#5 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

Quoted 21 lines · Volker Rümelin

Am 18.07.21 um 15:00 schrieb Peter Maydell:

Quoted 18 lines · Volker Rümelin
Quoted 17 lines

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
  include/ui/gtk.h |  5 +++++
  ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
  2 files changed, 50 insertions(+), 8 deletions(-)
Quoted 5 lines
Quoted 2 lines

This feels like maybe it's the kind of thing that should be handled more generically rather than in one particular UI frontend ?

All other UI frontends (except Spice) use the correct code in kbd_send_chars(). I think only the GTK UI code is wrong.

Why isn't GTK able to do things the same way all the other UI frontends do, then ?

-- PMM

#6 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

Quoted 22 lines · Peter Maydell
Quoted 21 lines · Volker Rümelin

Am 18.07.21 um 15:00 schrieb Peter Maydell:

Quoted 18 lines · Volker Rümelin
Quoted 17 lines

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
  include/ui/gtk.h |  5 +++++
  ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
  2 files changed, 50 insertions(+), 8 deletions(-)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This feels like maybe it's the kind of thing that should be handled more generically rather than in one particular UI frontend ?

All other UI frontends (except Spice) use the correct code in kbd_send_chars(). I think only the GTK UI code is wrong.

Why isn't GTK able to do things the same way all the other UI frontends do, then ?

In the other frontends the text consoles are not directly exposed, you just switch into them using QEMU's console switching shortcuts.

In the GTK frontend, each text console is exposed directly as a notebook tab in the UI, so you don't need to use QEMU's console switching.

IOW, the GTK frontend is more feature rich than the other frontends and causes it to hit this mistake.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

Quoted 33 lines · Daniel P. Berrangé
Quoted 22 lines · Peter Maydell
Quoted 21 lines · Volker Rümelin

Am 18.07.21 um 15:00 schrieb Peter Maydell:

Quoted 18 lines · Volker Rümelin
Quoted 17 lines

Since commit 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters") it's very easy to lock up QEMU with the gtk ui. If you configure a guest with a serial device and the guest doesn't listen on this device, QEMU will lock up after entering two characters in the serial console.

To fix this problem copy the function kbd_send_chars() and related code from ui/console.c to ui/gtk.c. kbd_send_chars() doesn't lock up because it uses a timer instead of a busy loop for the write retries.

Fixes 8eb13bbbac ("ui/gtk: vte: fix sending multiple characeters")
---
  include/ui/gtk.h |  5 +++++
  ui/gtk.c         | 53 ++++++++++++++++++++++++++++++++++++++++--------
  2 files changed, 50 insertions(+), 8 deletions(-)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This feels like maybe it's the kind of thing that should be handled more generically rather than in one particular UI frontend ?

All other UI frontends (except Spice) use the correct code in kbd_send_chars(). I think only the GTK UI code is wrong.

Why isn't GTK able to do things the same way all the other UI frontends do, then ?

Quoted 8 lines

In the other frontends the text consoles are not directly exposed, you just switch into them using QEMU's console switching shortcuts.

In the GTK frontend, each text console is exposed directly as a notebook tab in the UI, so you don't need to use QEMU's console switching.

IOW, the GTK frontend is more feature rich than the other frontends and causes it to hit this mistake.

Then we should probably have generic code to support "the frontend implements multiple tabs or whatever itself", so that the 2nd frontend that decides it wants to add this feature doesn't have to implement the same code again... (Not something we need to do for 6.1, to be clear. I just tend to feel we don't do enough UI stuff in common code and have weird disparities in featureset etc between our various UI frontends.)

-- PMM

#8 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

In reply to Volker Rümelin

Hi,

Quoted 16 lines
ui/gtk.c +16 -0
+static void gd_vc_send_chars(VirtualConsole *vc)
+{
+    uint32_t len, avail;
+    const uint8_t *buf;
+
+    len = qemu_chr_be_can_write(vc->vte.chr);
+    avail = fifo8_num_used(&vc->vte.out_fifo);
+    if (len > avail) {
+        len = avail;
+    }
+    while (len > 0) {
+        uint32_t size;
+
+        buf = fifo8_pop_buf(&vc->vte.out_fifo, len, &size);
+        qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size);
+        len -= size;

ui/gtk.c static const TypeInfo char_gd_vc_type_info = { · line 1739

Quoted 10 lines
ui/gtk.c +10 -0
+        avail -= size;
+    }
+    /*
+     * characters are pending: we send them a bit later (XXX:
+     * horrible, should change char device API)
+     */
+    if (avail > 0) {
+        timer_mod(vc->vte.kbd_timer,
+                  qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1);
+    }

There is ChardevClass->chr_accept_input() which gets called when you can send more data, so there is no need to use a timer for that.

Typical workflow is to only read data when it can be pushed forward to the guest, so when the guest stops reading data qemu stops doing so too, effectively forwarding the stalls. Which works fine for things like tcp sockets. Not so much for user input though.

So, yes, just throw away data is the only option we have here. Adding a reasonable-sized fifo makes sense too to cover bulky input, so you can cut+paste a longish URL even if the guest accepts only a few chars at a time (16550 fifo is 16 chars IIRC ...).

I would suggest to keep things simple, just throw away what you can't store in the fifo, I don't see the point taking different actions depending on how long the stalls are lasting (patch 2/2).

take care,
Gerd

#9 lore raw

Re: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles

Quoted 20 lines
Quoted 20 lines
ui/gtk.c +20 -0
+static void gd_vc_send_chars(VirtualConsole *vc)
+{
+    uint32_t len, avail;
+    const uint8_t *buf;
+
+    len = qemu_chr_be_can_write(vc->vte.chr);
+    avail = fifo8_num_used(&vc->vte.out_fifo);
+    if (len > avail) {
+        len = avail;
+    }
+    while (len > 0) {
+        uint32_t size;
+
+        buf = fifo8_pop_buf(&vc->vte.out_fifo, len, &size);
+        qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size);
+        len -= size;
+        avail -= size;
+    }
+    /*
+     * characters are pending: we send them a bit later (XXX:
Quoted 8 lines

ui/gtk.c static const TypeInfo char_gd_vc_type_info = { · line 1739

Quoted 6 lines
ui/gtk.c +6 -0
+     * horrible, should change char device API)
+     */
+    if (avail > 0) {
+        timer_mod(vc->vte.kbd_timer,
+                  qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1);
+    }

There is ChardevClass->chr_accept_input() which gets called when you can send more data, so there is no need to use a timer for that.

Oh, I didn't notice this callback function. With this, the retry timer and my attempt to quickly slow down the write retries are really not necessary.

Quoted 6 lines

Typical workflow is to only read data when it can be pushed forward to the guest, so when the guest stops reading data qemu stops doing so too, effectively forwarding the stalls. Which works fine for things like tcp sockets. Not so much for user input though.

Quoted 8 lines

So, yes, just throw away data is the only option we have here. Adding a reasonable-sized fifo makes sense too to cover bulky input, so you can cut+paste a longish URL even if the guest accepts only a few chars at a time (16550 fifo is 16 chars IIRC ...).

I would suggest to keep things simple, just throw away what you can't store in the fifo, I don't see the point taking different actions depending on how long the stalls are lasting (patch 2/2).

I will send a version 2 patch.

With best regards,
Volker

#10 lore raw

PATCH 2/2 ui/gtk: drop chars if the chardev frontend makes no progress

In reply to Volker Rümelin

Currently there is no limit how long the virtual console chardev backend retries to send remaining characters in the buffer.

Drop the characters after a timeout if the chardev frontend doesn't accept characters.

The timeout was calculated from arbitrarily selected constants. It was assumed that 50 baud is the slowest bit rate and that a character has at maximum one start bit, eight data bits, one parity bit and two stop bits. For a good safety margin, this time was multiplied by eight. This results in a timeout of 1 / (50 bits / s) * 12 bits * 8 = 1.92 s.

---
 include/ui/gtk.h |  2 ++
 ui/gtk.c         | 47 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 41 insertions(+), 8 deletions(-)
include/ui/gtk.h +2 -0
index 4714218376..2771b1aa91 100644
@@ -67,6 +67,8 @@ typedef struct VirtualVteConsole {
     Chardev *chr;
     QEMUTimer *kbd_timer;
     Fifo8 out_fifo;
+    int64_t be_last_write;
+    bool be_can_write;
     bool echo;
 } VirtualVteConsole;
 #endif
ui/gtk.c +39 -8

75 lines not on this page · raw message

Signature · 1 line
2.26.2