From nobody Mon Feb 9 19:16:49 2026 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; dmarc=fail(p=none dis=none) header.from=kingsoft.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1627352566423133.94190211883563; Mon, 26 Jul 2021 19:22:46 -0700 (PDT) Received: from localhost ([::1]:58572 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m8Ck0-0000A8-Eq for importer@patchew.org; Mon, 26 Jul 2021 22:22:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m8CjP-0007xk-MY for qemu-devel@nongnu.org; Mon, 26 Jul 2021 22:22:07 -0400 Received: from [114.255.44.146] (port=39367 helo=mail.kingsoft.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m8CjL-00050P-RE for qemu-devel@nongnu.org; Mon, 26 Jul 2021 22:22:07 -0400 Received: from mail.kingsoft.com (localhost [10.88.1.78]) (using TLS with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.kingsoft.com (SMG-1-NODE-87) with SMTP id 4B.28.05588.CBD6FF06; Tue, 27 Jul 2021 10:21:48 +0800 (HKT) Received: from KSbjmail3.kingsoft.cn (10.88.1.78) by KSBJMAIL3.kingsoft.cn (10.88.1.78) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.14; Tue, 27 Jul 2021 10:21:48 +0800 Received: from KSbjmail3.kingsoft.cn ([fe80::7d5f:5fcb:9c30:789b]) by KSBJMAIL3.kingsoft.cn ([fe80::7d5f:5fcb:9c30:789b%6]) with mapi id 15.01.2176.014; Tue, 27 Jul 2021 10:21:48 +0800 X-AuditID: 0a580157-8b5ff700000015d4-ff-60ff6dbc702a From: =?utf-8?B?QUlFUlBBVElKSUFORzEgW+iJvuWwlOW4leaPkOaxn8K36Zi/5biD6YO96LWb?= =?utf-8?B?5Lmw5o+QXQ==?= To: "lvivier@redhat.com" , "amit@kernel.org" , "qemu-devel@nongnu.org" , "stefanha@redhat.com" Subject: [PATCH] hw/char/virtio-serial-bus: fix: Unpop throttled VirtQueueElement to queue before discard vq data Thread-Topic: [PATCH] hw/char/virtio-serial-bus: fix: Unpop throttled VirtQueueElement to queue before discard vq data Thread-Index: AQHXfhCIoeZCoIzDgkWI30flJ82In6tWICuA Date: Tue, 27 Jul 2021 02:21:48 +0000 Message-ID: <330EE4BB-DE8D-4D4A-9E6E-08E50BDA5E45@kingsoft.com> References: <2904D378-AA27-4510-A3C8-7E2E34DF37EF@kingsoft.com> In-Reply-To: <2904D378-AA27-4510-A3C8-7E2E34DF37EF@kingsoft.com> Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.88.2.22] Content-Type: text/plain; charset="utf-8" Content-ID: <0F75A6B234C88348B35D5366FB4F3411@kingsoft.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmphkeLIzCtJLcpLzFFi42LhimD0092T+z/BYOMiY4vPd+ewW3xqkLI4 3ruDxeL1pP+sDiwem1Z1snk8ubaZyeP9vqtsAcxRXDYpqTmZZalF+nYJXBkLumezFUyQqNi4 aiFrA+MB8S5GTg4JAROJiWvvMHUxcnEICUxnklg0/TULhPOCUeL0pmvsEM4eRomnFxcygjhs Au2MEocPT2cFcUQEtjBK/G1rZAUZJixQLPH093/mLkYOoESFxK0/KSBhEQEjian3FzKD2CwC qhIdDQ/AbF4Be4kna54zgpQLAdm7FtuAhDkFHCQ+tV4EK2EUkJWY9ug+E4jNLCAuMXfaLFaI swUkluw5zwxhi0q8fPwPKi4nseFEJzvISGYBTYn1u/QhWq0ldsx8zQ5hK0pM6X7IDnGBoMTJ mU9YJjCKzUKyYRZC9ywk3bOQdM9C0r2AkXUVI0txbrrhJkZIRIXvYJzX9FHvECMTB+MhRgkO ZiURXocVvxOEeFMSK6tSi/Lji0pzUosPMUpzsCiJ85Yc/JcgJJCeWJKanZpakFoEk2Xi4JRq YKpS/bTuY8DkqI1HpYJinWsXXJHePT9+h7a2XzqbvWdEwGbpG9rZjDvqrIM3N1bJJ4ge6Zu8 vs2jzPrnk5uvTm3KyExrTOdJWNaRwGzTYKj16fz1mDdRRj4zde/t2OliUZJ93+xif6RqImuW 66qS7RzL+q89jol3cVkXJZe2am+Fg+WHa/dNpGMqgretkDBaon5VKLf14qJpdw0cK+b+nPpo DfvZOUsl63zaZpnem7T6SFKqxZewGVXLnZ/srfp9I/zUPD4HAzGtfZHMypcOfdS/eTbwZeF0 TaX7vM0XSuZlRcZd35S9o6dya/ekR8mJl46s2and3RNhF1QTv6678NOzmqu2PDOsmr6wB1x9 p8RSnJFoqMVcVJwIAPdPZAcXAwAA X-Host-Lookup-Failed: Reverse DNS lookup failed for 114.255.44.146 (failed) 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: pass client-ip=114.255.44.146; envelope-from=AIERPATIJIANG1@kingsoft.com; helo=mail.kingsoft.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1627352569840100001 Ports enter a "throttled" state when writing to the chardev would block. The current output VirtQueueElement is kept around until the chardev becomes writable again. =C2=A0 Because closing the virtio serial device does not reset the queue, we cannot directly discard this element, =C2=A0otherwise the control variables of the= front and back ends of the queue are inconsistent such as used_index. We should u= npop the VirtQueueElement to queue, let discard_vq_data process it. =C2=A0 The test environment: kernel: linux-5.12 Qemu command: Qemu-system-x86 -machine pc,accel=3Dkvm \ =C2=A0=C2=A0=C2=A0 -cpu host,host-phys-bits \ =C2=A0=C2=A0=C2=A0 -smp 4 \ =C2=A0=C2=A0=C2=A0 -m 4G \ =C2=A0=C2=A0=C2=A0 -kernel ./kernel \ =C2=A0=C2=A0=C2=A0 -display none \ =C2=A0=C2=A0=C2=A0 -nodefaults \ =C2=A0=C2=A0=C2=A0 -serial mon:stdio \ =C2=A0=C2=A0=C2=A0 -append "panic=3D1 no_timer_check noreplace-smp rootflag= s=3Ddata=3Dordered rootfstype=3Dext4 console=3DttyS0 reboot=3Dk root=3D/dev= /vda1 rw" \ =C2=A0=C2=A0=C2=A0 -drive id=3Dos,file=3D./disk,if=3Dnone \ =C2=A0=C2=A0=C2=A0 -device virtio-blk-pci,drive=3Dos \ =C2=A0=C2=A0=C2=A0 -device virtio-serial-pci,id=3Dvirtio-serial0,bus=3Dpci.= 0,addr=3D0x4 \ =C2=A0=C2=A0=C2=A0 -chardev socket,id=3Dcharchannel0,path=3D/tmp/char-dev-t= est,server,nowait \ -device virtserialport,bus=3Dvirtio-serial0.0,nr=3D1,chardev=3Dcharchanne= l0,id=3Dchannel0,name=3Dorg.qemu.guest_agent.0 =C2=A0 full up virtio queue after VM started: Cat /large-file > /dev/vport1p1 =C2=A0 Host side: Open and close character device sockets repeatedly =C2=A0 After awhile we can=E2=80=99t write any request to /dev/vport1p1 at VM side= , VM kernel soft lockup at drivers/char/virtio_console.c: __send_to_port =C2=A0 =C2=A0 Signed-off-by: Arafatms =C2=A0 diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index dd6bc27b3b..36236defdf 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -150,8 +150,12 @@ static void discard_vq_data(VirtQueue *vq, VirtIODevic= e *vdev) =C2=A0 static void discard_throttle_data(VirtIOSerialPort *port) { +=C2=A0=C2=A0=C2=A0 if (!virtio_queue_ready(port->ovq)) { +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return; +=C2=A0=C2=A0=C2=A0 } + =C2=A0=C2=A0=C2=A0=C2=A0 if (port->elem) { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 virtqueue_detach_element(port->= ovq, port->elem, 0); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 virtqueue_unpop(port->ovq, port= ->elem, 0); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 g_free(port->elem); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 port->elem =3D NULL; =C2=A0=C2=A0=C2=A0=C2=A0 }