From nobody Tue May 7 21:44:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1541085192776956.6618054753247; Thu, 1 Nov 2018 08:13:12 -0700 (PDT) Received: from localhost ([::1]:42541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gIEeg-00062T-8n for importer@patchew.org; Thu, 01 Nov 2018 11:13:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55770) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gIEdd-0005Rm-EN for qemu-devel@nongnu.org; Thu, 01 Nov 2018 11:12:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gIEdZ-0005tS-Fo for qemu-devel@nongnu.org; Thu, 01 Nov 2018 11:12:01 -0400 Received: from mail-lf1-x142.google.com ([2a00:1450:4864:20::142]:40518) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gIEdZ-0005su-6q for qemu-devel@nongnu.org; Thu, 01 Nov 2018 11:11:57 -0400 Received: by mail-lf1-x142.google.com with SMTP id n3-v6so14449151lfe.7 for ; Thu, 01 Nov 2018 08:11:57 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id q2-v6sm4633441lfa.37.2018.11.01.08.11.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Nov 2018 08:11:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=lStSumnxCSD9JzyV4oh9MD7mA6FKZv0Rwwl7BoX3j+U=; b=L9csV8CThK90UJOoKmSA/mGwFRBRQekNiTnLtoZF+WMQEXfKOzXK640Xal9zmd7I8o WuunJW/3iBDvc6n5U0dQb0RbHZ6v2ZtLbUIzlp0Ww5lnCqRdrf78BRKEQ23VDbzgYzL3 yiHAlHoeR7Ee+R58QRojYmwGBlPa4MepF5dDsFhwUmQlYhklJMfU5IF9iCNaMKyo1eK4 YUVAIUcQelmK/LC4HmfJ1Ips7uF/94NdOwM2x68t+/ogN7zk3eRtHCQ5PT/hzM7k4qDo y2UW/18TSGXRqopfwJFYrRE2uZaSJxTjjmAc7W97PkqEPvdmVII/ANsAM+sd5Eky73GQ /5HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=lStSumnxCSD9JzyV4oh9MD7mA6FKZv0Rwwl7BoX3j+U=; b=ryju/BeuAAxKCgixi6qn3+e55t3MCX+42ODYJugEf+pjCFUx91JwzKYfnkoc0Ge4o5 uQ7NUxIC1psAZyVOElq7vYJLU/mFkXkmMLJQp93iIjkhkU4f7eXyqXCbLDYXhkak2Rcc 6QSHYmglvbuTTXVDYPcvMX1HiY8RllehlLbJyGUg/XzB7CS6tsvOG2laCBWW3OCY8MQz Mtl7srSpdfj3+lxBUD0gvmBK31bjx12Jio/93O2sYObp1G2DctyvDaa7RjTRG3dn+iiD fnEjsP8Gv0GQl/VDpizdP1NaLwSQjTE4qw1Ovw2j/xXcFdc7+ex91Yt/Six+4VcTvHAZ 3kYg== X-Gm-Message-State: AGRZ1gIFJuepkLXmLqYad2lwoAgat6F1E9Rw31a/EnF6ez9F8MUvs3oT hqVaxPei2YOf3rW1fYR2a95At51m X-Google-Smtp-Source: AJdET5d0RvN5VS5f5QAP1TlK9v/sjMCICgsFTF+na7scftf4YmTcQHeUmipObLvpxLaC6TebCMSOYQ== X-Received: by 2002:a19:f813:: with SMTP id a19mr4548040lff.67.1541085115179; Thu, 01 Nov 2018 08:11:55 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Thu, 1 Nov 2018 21:11:42 +0600 Message-Id: <49e9640f3fc040a9c9ab40f93935a6572b34bdff.1541085026.git.artem.k.pisarenko@gmail.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::142 Subject: [Qemu-devel] [PATCH] virtserialport/virtconsole: fix messy opening/closing port X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , "Michael S. Tsirkin" , Artem Pisarenko , Amit Shah Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This fixes wrong interfacing between virtio serial port and bus models, and corresponding chardev backends, caused extra and incorrect activity during guest boot process (when virtserialport device used). Signed-off-by: Artem Pisarenko --- Notes: Although this doesn't trigger any issue/bug (known to me), this may pre= vent them in future. Also this patch optimizes emulation performance by avoiding extra activ= ity and fixes a case, when serial port wasn't closed if backend closed whil= e being disabled (even worse - serial port will open again!). hw/char/virtio-console.c | 24 ++++++++++++++++++++---- hw/char/virtio-serial-bus.c | 8 +++++++- include/hw/virtio/virtio-serial.h | 8 ++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/hw/char/virtio-console.c b/hw/char/virtio-console.c index 2cbe1d4..634e9bf 100644 --- a/hw/char/virtio-console.c +++ b/hw/char/virtio-console.c @@ -25,6 +25,7 @@ typedef struct VirtConsole { VirtIOSerialPort parent_obj; =20 + bool backend_active; CharBackend chr; guint watch; } VirtConsole; @@ -149,6 +150,11 @@ static void chr_event(void *opaque, int event) VirtIOSerialPort *port =3D VIRTIO_SERIAL_PORT(vcon); =20 trace_virtio_console_chr_event(port->id, event); + + if (!vcon->backend_active) { + return; + } + switch (event) { case CHR_EVENT_OPENED: virtio_serial_open(port); @@ -187,17 +193,24 @@ static int chr_be_change(void *opaque) return 0; } =20 +static bool virtconsole_is_backend_enabled(VirtIOSerialPort *port) +{ + VirtConsole *vcon =3D VIRTIO_CONSOLE(port); + + return vcon->backend_active; +} + static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable) { VirtConsole *vcon =3D VIRTIO_CONSOLE(port); =20 - if (!qemu_chr_fe_backend_connected(&vcon->chr)) { - return; - } - if (enable) { VirtIOSerialPortClass *k =3D VIRTIO_SERIAL_PORT_GET_CLASS(port); =20 + if (!k->is_console && virtio_serial_is_opened(port) + && !qemu_chr_fe_backend_open(&vcon->chr)) { + virtio_serial_close(port); + } qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read, k->is_console ? NULL : chr_event, chr_be_change, vcon, NULL, false); @@ -205,6 +218,7 @@ static void virtconsole_enable_backend(VirtIOSerialPort= *port, bool enable) qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL, NULL, NULL, NULL, false); } + vcon->backend_active =3D enable; } =20 static void virtconsole_realize(DeviceState *dev, Error **errp) @@ -220,6 +234,7 @@ static void virtconsole_realize(DeviceState *dev, Error= **errp) } =20 if (qemu_chr_fe_backend_connected(&vcon->chr)) { + vcon->backend_active =3D true; /* * For consoles we don't block guest data transfer just * because nothing is connected - we'll just let it go @@ -278,6 +293,7 @@ static void virtserialport_class_init(ObjectClass *klas= s, void *data) k->unrealize =3D virtconsole_unrealize; k->have_data =3D flush_buf; k->set_guest_connected =3D set_guest_connected; + k->is_backend_enabled =3D virtconsole_is_backend_enabled; k->enable_backend =3D virtconsole_enable_backend; k->guest_writable =3D guest_writable; dc->props =3D virtserialport_properties; diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index 04e3ebe..d23d99d 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -258,6 +258,11 @@ static size_t send_control_event(VirtIOSerial *vser, u= int32_t port_id, } =20 /* Functions for use inside qemu to open and read from/write to ports */ +bool virtio_serial_is_opened(VirtIOSerialPort *port) +{ + return port->host_connected; +} + int virtio_serial_open(VirtIOSerialPort *port) { /* Don't allow opening an already-open port */ @@ -643,7 +648,8 @@ static void set_status(VirtIODevice *vdev, uint8_t stat= us) =20 QTAILQ_FOREACH(port, &vser->ports, next) { VirtIOSerialPortClass *vsc =3D VIRTIO_SERIAL_PORT_GET_CLASS(port); - if (vsc->enable_backend) { + if (vsc->is_backend_enabled && vsc->enable_backend + && (vsc->is_backend_enabled(port) !=3D vdev->vm_running)) { vsc->enable_backend(port, vdev->vm_running); } } diff --git a/include/hw/virtio/virtio-serial.h b/include/hw/virtio/virtio-s= erial.h index 12657a9..4b72562 100644 --- a/include/hw/virtio/virtio-serial.h +++ b/include/hw/virtio/virtio-serial.h @@ -58,6 +58,8 @@ typedef struct VirtIOSerialPortClass { /* Guest opened/closed device. */ void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connecte= d); =20 + /* Is backend currently enabled for virtio serial port */ + bool (*is_backend_enabled)(VirtIOSerialPort *port); /* Enable/disable backend for virtio serial port */ void (*enable_backend)(VirtIOSerialPort *port, bool enable); =20 @@ -194,6 +196,12 @@ struct VirtIOSerial { /* Interface to the virtio-serial bus */ =20 /* + * Checks status of connection to the port + * Returns true if connected, false otherwise. + */ +bool virtio_serial_is_opened(VirtIOSerialPort *port); + +/* * Open a connection to the port * Returns 0 on success (always). */ --=20 2.7.4