From nobody Mon May 13 05:52:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626594589682409.90934499199307; Sun, 18 Jul 2021 00:49:49 -0700 (PDT) Received: from localhost ([::1]:52216 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m51Ya-00077i-L8 for importer@patchew.org; Sun, 18 Jul 2021 03:49:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40248) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m51X2-0005FV-7v for qemu-devel@nongnu.org; Sun, 18 Jul 2021 03:48:12 -0400 Received: from mailout01.t-online.de ([194.25.134.80]:46454) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m51X0-0002sL-CK for qemu-devel@nongnu.org; Sun, 18 Jul 2021 03:48:12 -0400 Received: from fwd20.aul.t-online.de (fwd20.aul.t-online.de [172.20.26.140]) by mailout01.t-online.de (Postfix) with SMTP id AFA2C9232; Sun, 18 Jul 2021 09:48:05 +0200 (CEST) Received: from linpower.localnet (ExTfWsZlYh4ra4GQ7C2DAlw-PN-6q3H6FwVNsfruPgFoOL-WfYigMD1jLLRKYdcgq6@[79.208.26.7]) by fwd20.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384 encrypted) esmtp id 1m51Wo-2Qhts80; Sun, 18 Jul 2021 09:47:58 +0200 Received: by linpower.localnet (Postfix, from userid 1000) id EF7DD20044E; Sun, 18 Jul 2021 09:47:57 +0200 (CEST) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann Subject: [PATCH for 6.1 1/2] ui/gtk: add a keyboard fifo to the VTE consoles Date: Sun, 18 Jul 2021 09:47:56 +0200 Message-Id: <20210718074757.22489-1-vr_qemu@t-online.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <9e436e5c-ed11-69ec-3cb9-a19cbf96cb08@t-online.de> References: <9e436e5c-ed11-69ec-3cb9-a19cbf96cb08@t-online.de> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ID: ExTfWsZlYh4ra4GQ7C2DAlw-PN-6q3H6FwVNsfruPgFoOL-WfYigMD1jLLRKYdcgq6 X-TOI-EXPURGATEID: 150726::1626594478-00000BA7-DB7DAB97/0/0 CLEAN NORMAL X-TOI-MSGID: d67671f5-8a6d-4bfd-ba99-5b44e8ea1799 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.80; envelope-from=volker.ruemelin@t-online.de; helo=mailout01.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zack Marvel , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1626594591574100001 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") Signed-off-by: Volker R=C3=BCmelin --- include/ui/gtk.h | 5 +++++ ui/gtk.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/include/ui/gtk.h b/include/ui/gtk.h index 9516670ebc..4714218376 100644 --- a/include/ui/gtk.h +++ b/include/ui/gtk.h @@ -25,6 +25,9 @@ #include "ui/egl-helpers.h" #include "ui/egl-context.h" #endif +#ifdef CONFIG_VTE +#include "qemu/fifo8.h" +#endif =20 #define MAX_VCS 10 =20 @@ -62,6 +65,8 @@ typedef struct VirtualVteConsole { GtkWidget *scrollbar; GtkWidget *terminal; Chardev *chr; + QEMUTimer *kbd_timer; + Fifo8 out_fifo; bool echo; } VirtualVteConsole; #endif diff --git a/ui/gtk.c b/ui/gtk.c index 376b4d528d..b95b077b65 100644 --- a/ui/gtk.c +++ b/ui/gtk.c @@ -48,6 +48,7 @@ #include #if defined(CONFIG_VTE) #include +#include "chardev/char-fe.h" #endif #include =20 @@ -1710,10 +1711,46 @@ static const TypeInfo char_gd_vc_type_info =3D { .class_init =3D char_gd_vc_class_init, }; =20 +static void gd_vc_send_chars(VirtualConsole *vc) +{ + uint32_t len, avail; + const uint8_t *buf; + + len =3D qemu_chr_be_can_write(vc->vte.chr); + avail =3D fifo8_num_used(&vc->vte.out_fifo); + if (len > avail) { + len =3D avail; + } + while (len > 0) { + uint32_t size; + + buf =3D fifo8_pop_buf(&vc->vte.out_fifo, len, &size); + qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size); + len -=3D size; + avail -=3D 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); + } +} + +static void gd_vc_timer_send_chars(void *opaque) +{ + VirtualConsole *vc =3D opaque; + + gd_vc_send_chars(vc); +} + static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size, gpointer user_data) { VirtualConsole *vc =3D user_data; + CharBackend *be =3D vc->vte.chr->be; =20 if (vc->vte.echo) { VteTerminal *term =3D VTE_TERMINAL(vc->vte.terminal); @@ -1733,16 +1770,13 @@ static gboolean gd_vc_in(VteTerminal *terminal, gch= ar *text, guint size, } } =20 - int remaining =3D size; - uint8_t* p =3D (uint8_t *)text; - while (remaining > 0) { - int can_write =3D qemu_chr_be_can_write(vc->vte.chr); - int written =3D MIN(remaining, can_write); - qemu_chr_be_write(vc->vte.chr, p, written); + if (be && be->chr_read) { + uint32_t free =3D fifo8_num_free(&vc->vte.out_fifo); =20 - remaining -=3D written; - p +=3D written; + fifo8_push_all(&vc->vte.out_fifo, (uint8_t *)text, MIN(free, size)= ); + gd_vc_send_chars(vc); } + return TRUE; } =20 @@ -1759,6 +1793,9 @@ static GSList *gd_vc_vte_init(GtkDisplayState *s, Vir= tualConsole *vc, vc->s =3D s; vc->vte.echo =3D vcd->echo; vc->vte.chr =3D chr; + fifo8_create(&vc->vte.out_fifo, 16); + vc->vte.kbd_timer =3D timer_new_ms(QEMU_CLOCK_VIRTUAL, + gd_vc_timer_send_chars, vc); vcd->console =3D vc; =20 snprintf(buffer, sizeof(buffer), "vc%d", idx); --=20 2.26.2 From nobody Mon May 13 05:52:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626594578599603.314101316828; Sun, 18 Jul 2021 00:49:38 -0700 (PDT) Received: from localhost ([::1]:51872 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m51YP-0006ri-H9 for importer@patchew.org; Sun, 18 Jul 2021 03:49:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40260) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m51X4-0005HO-K1 for qemu-devel@nongnu.org; Sun, 18 Jul 2021 03:48:14 -0400 Received: from mailout08.t-online.de ([194.25.134.20]:54284) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m51X2-0002vG-Ji for qemu-devel@nongnu.org; Sun, 18 Jul 2021 03:48:14 -0400 Received: from fwd33.aul.t-online.de (fwd33.aul.t-online.de [172.20.27.144]) by mailout08.t-online.de (Postfix) with SMTP id 555164380; Sun, 18 Jul 2021 09:48:05 +0200 (CEST) Received: from linpower.localnet (ExTgZZZQwhTdfvh3hEOGAMLKCM-rdo+oFRahncigUbNR48DYAzAnUxRrTdJiFqtgpy@[79.208.26.7]) by fwd33.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384 encrypted) esmtp id 1m51Wr-48wYLo0; Sun, 18 Jul 2021 09:48:01 +0200 Received: by linpower.localnet (Postfix, from userid 1000) id F16922000A1; Sun, 18 Jul 2021 09:47:57 +0200 (CEST) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann Subject: [PATCH for 6.2 2/2] ui/gtk: drop chars if the chardev frontend makes no progress Date: Sun, 18 Jul 2021 09:47:57 +0200 Message-Id: <20210718074757.22489-2-vr_qemu@t-online.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <9e436e5c-ed11-69ec-3cb9-a19cbf96cb08@t-online.de> References: <9e436e5c-ed11-69ec-3cb9-a19cbf96cb08@t-online.de> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ID: ExTgZZZQwhTdfvh3hEOGAMLKCM-rdo+oFRahncigUbNR48DYAzAnUxRrTdJiFqtgpy X-TOI-EXPURGATEID: 150726::1626594481-00013209-AD406C60/0/0 CLEAN NORMAL X-TOI-MSGID: 0b44b172-fef3-418d-8d20-c28e4b823a00 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zack Marvel , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1626594579047100001 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 =3D 1.92 s. Signed-off-by: Volker R=C3=BCmelin --- include/ui/gtk.h | 2 ++ ui/gtk.c | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/ui/gtk.h b/include/ui/gtk.h index 4714218376..2771b1aa91 100644 --- a/include/ui/gtk.h +++ b/include/ui/gtk.h @@ -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 diff --git a/ui/gtk.c b/ui/gtk.c index b95b077b65..c22f4a0329 100644 --- a/ui/gtk.c +++ b/ui/gtk.c @@ -1711,8 +1711,20 @@ static const TypeInfo char_gd_vc_type_info =3D { .class_init =3D char_gd_vc_class_init, }; =20 +static uint32_t gd_vc_send_backoff(uint32_t elapsed) +{ + if (elapsed <=3D 500) { + return 1000; + } else if (elapsed >=3D 50000) { + return 100000; + } + + return elapsed * 2; +} + static void gd_vc_send_chars(VirtualConsole *vc) { + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); uint32_t len, avail; const uint8_t *buf; =20 @@ -1721,21 +1733,39 @@ static void gd_vc_send_chars(VirtualConsole *vc) if (len > avail) { len =3D avail; } - while (len > 0) { + if (len > 0) { uint32_t size; =20 - buf =3D fifo8_pop_buf(&vc->vte.out_fifo, len, &size); - qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size); - len -=3D size; - avail -=3D size; + do { + buf =3D fifo8_pop_buf(&vc->vte.out_fifo, len, &size); + qemu_chr_be_write(vc->vte.chr, (uint8_t *)buf, size); + len -=3D size; + avail -=3D size; + } while (len > 0); + + vc->vte.be_can_write =3D true; + vc->vte.be_last_write =3D now; } /* * 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); + if (vc->vte.be_can_write) { + uint64_t elapsed =3D (now - vc->vte.be_last_write) / SCALE_US; + + if (elapsed < 1920000) { + timer_mod(vc->vte.kbd_timer, + now / SCALE_US + gd_vc_send_backoff(elapsed)); + } else { + /* no progress since 1.92s */ + vc->vte.be_can_write =3D false; + fifo8_reset(&vc->vte.out_fifo); + } + } else { + /* the chardev frontend hasn't accepted chars in a long time */ + fifo8_reset(&vc->vte.out_fifo); + } } } =20 @@ -1794,7 +1824,8 @@ static GSList *gd_vc_vte_init(GtkDisplayState *s, Vir= tualConsole *vc, vc->vte.echo =3D vcd->echo; vc->vte.chr =3D chr; fifo8_create(&vc->vte.out_fifo, 16); - vc->vte.kbd_timer =3D timer_new_ms(QEMU_CLOCK_VIRTUAL, + vc->vte.be_can_write =3D false; + vc->vte.kbd_timer =3D timer_new_us(QEMU_CLOCK_VIRTUAL, gd_vc_timer_send_chars, vc); vcd->console =3D vc; =20 --=20 2.26.2