From patchwork Fri Jul 30 01:58:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QUlFUlBBVElKSUFORzEgW+iJvuWwlOW4leaPkOaxn8K36Zi/5biD6YO96LWb5Lmw5o+QXQ==?= X-Patchwork-Id: 12410153 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6988C4338F for ; Fri, 30 Jul 2021 01:59:49 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C7DFC603E9 for ; Fri, 30 Jul 2021 01:59:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C7DFC603E9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kingsoft.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:45812 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m9HoR-0000Sq-Qr for qemu-devel@archiver.kernel.org; Thu, 29 Jul 2021 21:59:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46256) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m9Hng-00088c-AB for qemu-devel@nongnu.org; Thu, 29 Jul 2021 21:59:00 -0400 Received: from [114.255.44.146] (port=39573 helo=mail.kingsoft.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m9Hnd-0007aT-3B for qemu-devel@nongnu.org; Thu, 29 Jul 2021 21:58:59 -0400 X-AuditID: 0a580157-e15ff7000006309b-29-61035cd15bb9 Received: from mail.kingsoft.com (localhost [10.88.1.79]) (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 2B.0D.12443.2DC53016; Fri, 30 Jul 2021 09:58:42 +0800 (HKT) Received: from KSbjmail3.kingsoft.cn (10.88.1.78) by KSBJMAIL4.kingsoft.cn (10.88.1.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.14; Fri, 30 Jul 2021 09:58:41 +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; Fri, 30 Jul 2021 09:58:41 +0800 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" , "mst@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: AQHXfhCIoeZCoIzDgkWI30flJ82In6tWICuAgASwioA= Date: Fri, 30 Jul 2021 01:58:41 +0000 Message-ID: <918AA6D7-F6C6-4A0E-8E65-6C7ECF148304@kingsoft.com> References: <2904D378-AA27-4510-A3C8-7E2E34DF37EF@kingsoft.com> <330EE4BB-DE8D-4D4A-9E6E-08E50BDA5E45@kingsoft.com> In-Reply-To: <330EE4BB-DE8D-4D4A-9E6E-08E50BDA5E45@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-ID: <4592EBBE170B6A4C8AA901D4B726320F@kingsoft.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupkkeLIzCtJLcpLzFFi42LhimD0170Uw5xo0L6O0eLz3TnsFp8apCz+ /3rFanG8dweLxetJ/1kdWD02repk83hybTOTx/t9V9kCmKO4bFJSczLLUov07RK4Ms5du8Ne 0CJesa1xLXsD4w6xLkZODgkBE4m/t1ezdjFycQgJTGeSOPLjOzOE84JRYuumPqjMHkaJD8e/ sIE4bALtjBKHD08Hy4gI3GSUOLf0OBPIMGGBYomnv/8D9XMAJSokbv1JAQmLCFhJtFxdywZi swioSpzZdYYRpIRXwF5i42l+kLCQQKlE3/5uVhCbU8BB4uaxbSwgNqOArMS0R/fBpjMLiEvM nTaLFeJsAYkle84zQ9iiEi8f/4OKy0lsONHJDjKeWUBTYv0ufYhWa4mdeyYxQtiKElO6H7KD 2LwCghInZz5hmcAoNgvJhlkI3bOQdM9C0j0LSfcCRtZVjCzFuemGmxghsRW+g3Fe00e9Q4xM HIyHGCU4mJVEeF//+J8gxJuSWFmVWpQfX1Sak1p8iFGag0VJnLfk4L8EIYH0xJLU7NTUgtQi mCwTB6dUA5Pd/DcFqd3GPekJnBfiuf67PjJUNshRdu1Rrux8dMg8ob2qUXF9ml1vd0TOk0Bl nXDuufudYu7WcRuv/xCvxOs86dN9wznhGRZl33Wbne1j4urrLgsfKnXsXNFpfkOyyfa/6euo 5btObTZUe+q4si/lescezr9yj5n5Vgkdu/JeZjbbhNMhoWJlffcL+H9bfLZ/2ryf1WNSl+3E s+ofv0z69jFwtX6xLgtT4tXk03fjomL/JZ+dmlgTulw7Wd1To9aoNup0cnbIlmTj0BqNr36v f9W/crhQfNTurUpgV1vV0vzgiPQl2pcl0qZOE9Ur4JfoPOmxt6d+d+6fxe8XVNjV6nKdWK1t Z5y06akSS3FGoqEWc1FxIgAW9PPNHAMAAA== X-Host-Lookup-Failed: Reverse DNS lookup failed for 114.255.44.146 (failed) 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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. Because closing the virtio serial device does not reset the queue, we cannot directly discard this element, otherwise the control variables of the front and back ends of the queue are inconsistent such as used_index. We should unpop the VirtQueueElement to queue, let discard_vq_data process it. The test environment: kernel: linux-5.12 Qemu command: Qemu-system-x86 -machine pc,accel=kvm \ -cpu host,host-phys-bits \ -smp 4 \ -m 4G \ -kernel ./kernel \ -display none \ -nodefaults \ -serial mon:stdio \ -append "panic=1 no_timer_check noreplace-smp rootflags=data=ordered rootfstype=ext4 console=ttyS0 reboot=k root=/dev/vda1 rw" \ -drive id=os,file=./disk,if=none \ -device virtio-blk-pci,drive=os \ -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4 \ -chardev socket,id=charchannel0,path=/tmp/char-dev-test,server,nowait \ -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 full up virtio queue after VM started: Cat /large-file > /dev/vport1p1 Host side: Open and close character device sockets repeatedly After awhile we can’t write any request to /dev/vport1p1 at VM side, VM kernel soft lockup at drivers/char/virtio_console.c: __send_to_port Signed-off-by: Arafatms 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, VirtIODevice *vdev) static void discard_throttle_data(VirtIOSerialPort *port) { + if (!virtio_queue_ready(port->ovq)) { + return; + } + if (port->elem) { - virtqueue_detach_element(port->ovq, port->elem, 0); + virtqueue_unpop(port->ovq, port->elem, 0); g_free(port->elem); port->elem = NULL; }