From patchwork Wed Jul 21 09:12:30 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: 12390829 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=-10.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 B7F7BC636C9 for ; Wed, 21 Jul 2021 13:22:26 +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 260B361241 for ; Wed, 21 Jul 2021 13:22:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 260B361241 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kingsoft.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:50346 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m6CB7-0006bZ-8S for qemu-devel@archiver.kernel.org; Wed, 21 Jul 2021 09:22:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41336) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m68W8-00085t-Cc for qemu-devel@nongnu.org; Wed, 21 Jul 2021 05:27:52 -0400 Received: from [114.255.44.146] (port=38983 helo=mail.kingsoft.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m68W4-0001hJ-Mm for qemu-devel@nongnu.org; Wed, 21 Jul 2021 05:27:51 -0400 X-AuditID: 0a580157-8cdff700000015d4-b9-60f7e4ffb7b7 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 13.68.05588.FF4E7F06; Wed, 21 Jul 2021 17:12:31 +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; Wed, 21 Jul 2021 17:12:30 +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; Wed, 21 Jul 2021 17:12:30 +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" 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: AQHXfhCIoeZCoIzDgkWI30flJ82Inw== Date: Wed, 21 Jul 2021 09:12:30 +0000 Message-ID: <2904D378-AA27-4510-A3C8-7E2E34DF37EF@kingsoft.com> Accept-Language: zh-CN, en-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A== 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, HTML_MESSAGE=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 21 Jul 2021 09:19:23 -0400 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: =?utf-8?b?WUFOR0ZFTkcxIFvmnajls7Bd?= , =?utf-8?b?QUlFUlBBVElKSUFORzEgW+iJvuWwlOW4leaPkOaxn8K36Zi/5biD6YO96LWb5Lmw?= =?utf-8?b?5o+QXQ==?= , =?utf-8?b?REVOR0xJTldF?= =?utf-8?b?TiBb6YKT5p6X5paHXQ==?= , =?utf-8?b?c3Vu?= =?utf-8?b?aGFvMiBb5a2Z5piKXQ==?= 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; }