Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1753393ybg; Sat, 19 Oct 2019 01:46:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZ6DFQqNPXxaO3YA6xgfYanPQz8oyQX+s8PgY8y8EiTFv7JV0tt39LhnV9ZKK9qqBr2ANo X-Received: by 2002:aa7:d6cd:: with SMTP id x13mr14102786edr.272.1571474807681; Sat, 19 Oct 2019 01:46:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571474807; cv=none; d=google.com; s=arc-20160816; b=zQPH8QWtT45XEomwbCXXoH9UZzlr3D34/WeClbvXR+DkiJkVrBNxJXdOTvIuXJBScV DAcUltfJsBbpUEBezvXjkp5LUfqu7QejoLK0X1ITZBPwhwbX9ugo6wUpSXGlVebCBuTx yD+/QfB9NIT69py2YoSM5Kcrxci/rOZzZyJWaCULznDfzW2IwSiAikmNYGljvwjdD6uZ 43Qj+F8lDx1KCKVFYwJVlSLFKG7kLqEZIfKzrMii8kqm2LFKuxEiTV/oz25F8QsgshsH hMu5j+VbfPHBXjEsYkagX/DAgdONJwxHfgQbGkc83QdQq/z998ArXxerNoPSa+J594Ye acBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=RkLE1EUNLRHB0ZeeDFtauL07lHVc/KRDBNkXiYNiS8s=; b=pOZ9x1w3B+9bwn9hxyZRaehh4lIbIjERjMhEnPA4OJXzA4/FdwNxD+Pc16jX2g5xzP h8zXSPTj7+TFtPNqFGnnKUtRlf9m/l9uT9M1lmnjlIdQ94yANOWs67G8oka6bHPc1Gii eb/JcGO565qz5Zk/kWn3VlalQGs52kHiTbuMxONOHVU0zm9Jv0qsz5Bb2CCbcQi8WU3H 6dDq4VBe4ni0weaZMpcRg9yQG4M0fLflz6r2VOVucUfZSTewoxjj/w9mx33NSLoEfHHn U/55x2DGjzV+MXmkT+IBpv8dfFPGuU24JQPITC7VyV8g4iVCAEcq/K5P7PBuxpYRX+Fa +4qg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w26si5141461ejb.6.2019.10.19.01.46.24; Sat, 19 Oct 2019 01:46:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2439713AbfJRQr0 (ORCPT + 99 others); Fri, 18 Oct 2019 12:47:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57818 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728368AbfJRQr0 (ORCPT ); Fri, 18 Oct 2019 12:47:26 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C28CA328E; Fri, 18 Oct 2019 16:47:25 +0000 (UTC) Received: from thinkpad.redhat.com (ovpn-116-247.ams2.redhat.com [10.36.116.247]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A0915D713; Fri, 18 Oct 2019 16:47:19 +0000 (UTC) From: Laurent Vivier To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , Amit Shah , virtualization@lists.linux-foundation.org, Arnd Bergmann , "Michael S . Tsirkin" , Laurent Vivier Subject: [PATCH] virtio_console: allocate inbufs in add_port() only if it is needed Date: Fri, 18 Oct 2019 18:47:18 +0200 Message-Id: <20191018164718.15999-1-lvivier@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 18 Oct 2019 16:47:25 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we hot unplug a virtserialport and then try to hot plug again, it fails: (qemu) chardev-add socket,id=serial0,path=/tmp/serial0,server,nowait (qemu) device_add virtserialport,bus=virtio-serial0.0,nr=2,\ chardev=serial0,id=serial0,name=serial0 (qemu) device_del serial0 (qemu) device_add virtserialport,bus=virtio-serial0.0,nr=2,\ chardev=serial0,id=serial0,name=serial0 kernel error: virtio-ports vport2p2: Error allocating inbufs qemu error: virtio-serial-bus: Guest failure in adding port 2 for device \ virtio-serial0.0 This happens because buffers for the in_vq are allocated when the port is added but are not released when the port is unplugged. They are only released when virtconsole is removed (see a7a69ec0d8e4) To avoid the problem and to be symmetric, we could allocate all the buffers in init_vqs() as they are released in remove_vqs(), but it sounds like a waste of memory. Rather than that, this patch changes add_port() logic to only allocate the buffers if the in_vq has available free slots. Fixes: a7a69ec0d8e4 ("virtio_console: free buffers after reset") Cc: mst@redhat.com Signed-off-by: Laurent Vivier --- drivers/char/virtio_console.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index 7270e7b69262..77105166fe01 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -1421,12 +1421,17 @@ static int add_port(struct ports_device *portdev, u32 id) spin_lock_init(&port->outvq_lock); init_waitqueue_head(&port->waitqueue); - /* Fill the in_vq with buffers so the host can send us data. */ - nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock); - if (!nr_added_bufs) { - dev_err(port->dev, "Error allocating inbufs\n"); - err = -ENOMEM; - goto free_device; + /* if the in_vq has not already been filled (the port has already been + * used and unplugged), fill the in_vq with buffers so the host can + * send us data. + */ + if (port->in_vq->num_free != 0) { + nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock); + if (!nr_added_bufs) { + dev_err(port->dev, "Error allocating inbufs\n"); + err = -ENOMEM; + goto free_device; + } } if (is_rproc_serial(port->portdev->vdev)) -- 2.21.0