Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5981085ybc; Wed, 27 Nov 2019 12:50:27 -0800 (PST) X-Google-Smtp-Source: APXvYqwBd7+O4ZBX8nGtpGRIbjllqaoOveQqfOjPGlp3bBIBfWxIqu9QwH6g+Oxc8Y4TuzKyoW0T X-Received: by 2002:a17:906:d0c8:: with SMTP id bq8mr51889912ejb.263.1574887827063; Wed, 27 Nov 2019 12:50:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574887827; cv=none; d=google.com; s=arc-20160816; b=QzkN4pgKBqPLqt17HgH67OPkxW+HnkIE3D+ytsXDCAKEoT83eSPbuAcVF9AHrSmGM4 sAwNuisO2VWwitOxa1cGgbwIDnD5ogg5ufay33+5o7W0R+B+vG0KHNdnCtFiMVcNHKq8 eTyriHZUuciR6XBQOfLByfftS2ru115mdDG2NqJEt+UdTCMGX9Kt19kjc8lLm2UFpOG4 DcglUbgYV/J3uFU3lxG7SVlu0ck0k0JQItQ/vKzis3CfOJIHvp+tsRtaLwlzqXmwarAj 5E+a9xu5hd4YL3OZoZVR59VQk6+gynjk3nUcGs1WcIyhizsvqooiYJi/zxMU6+ETZR8t dp/g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dmY15HAzFT0AtKnbLf6k46wJdQZMovF9DR1YUOZ4A28=; b=dAXLu4ovhVkTFeJ1TdmEuNly/sQ38BUeNBHviGZJjOXAV5d6o5xcvEoik216S0dYcu GcMLvSQ86nQ6M95qWHiHWwy1pdMmV/G1duyIxHKngeN2A5AvTdjZWQsIGdCX5tF8ivkN YZ17NbvC9XkS1C1e1PqI7xUX2qVdok5rdXidSTP2x9d5ep1Mpb/nltoFrlSqPPLO2rFT HsZgW4JYCgEMCeQPLTAKe4GoIYzCjghhoa/EzYN589Nz2A/vCTKvJS4f8ASD91Qr8aHY qj3QhROfYnrud2EHcow0DX1uT/ahpVo9cN99tNSPyKhucJVyR6KGfARUt/uoJgXT5Dmh M0pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=J9N5fiBG; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p8si10549691edq.55.2019.11.27.12.50.03; Wed, 27 Nov 2019 12:50:27 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=J9N5fiBG; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729581AbfK0UsB (ORCPT + 99 others); Wed, 27 Nov 2019 15:48:01 -0500 Received: from mail.kernel.org ([198.145.29.99]:32954 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728261AbfK0Ur7 (ORCPT ); Wed, 27 Nov 2019 15:47:59 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3803D21845; Wed, 27 Nov 2019 20:47:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574887678; bh=1MWREmX53yQ9QQFDFRugDP7QzeX5bWAcw/ywG/g9DdI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J9N5fiBG7mvwUy80sYXGDlXqOIchQBWVK6G1XN0mfzBOu3g7wRJS2BYjcfMXl0R/G hv0cFcp+DKnBQL/ZpzDHvv8gpiqg2bLCWfmMczft3onfAyRSyGwzC14zmCRt9Q2jyo KcUmI72Gij/XoRp5CsdzoKbuQThPKjT/s9i5SKyk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, mst@redhat.com, Laurent Vivier Subject: [PATCH 4.14 011/211] virtio_console: allocate inbufs in add_port() only if it is needed Date: Wed, 27 Nov 2019 21:29:04 +0100 Message-Id: <20191127203050.999718897@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203049.431810767@linuxfoundation.org> References: <20191127203049.431810767@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Laurent Vivier commit d791cfcbf98191122af70b053a21075cb450d119 upstream. 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 ignore ENOSPC error in fill_queue(), which means queue has already been filled. Fixes: a7a69ec0d8e4 ("virtio_console: free buffers after reset") Cc: mst@redhat.com Cc: stable@vger.kernel.org Signed-off-by: Laurent Vivier Signed-off-by: Michael S. Tsirkin Signed-off-by: Greg Kroah-Hartman --- drivers/char/virtio_console.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -1366,24 +1366,24 @@ static void set_console_size(struct port port->cons.ws.ws_col = cols; } -static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock) +static int fill_queue(struct virtqueue *vq, spinlock_t *lock) { struct port_buffer *buf; - unsigned int nr_added_bufs; + int nr_added_bufs; int ret; nr_added_bufs = 0; do { buf = alloc_buf(vq->vdev, PAGE_SIZE, 0); if (!buf) - break; + return -ENOMEM; spin_lock_irq(lock); ret = add_inbuf(vq, buf); if (ret < 0) { spin_unlock_irq(lock); free_buf(buf, true); - break; + return ret; } nr_added_bufs++; spin_unlock_irq(lock); @@ -1403,7 +1403,6 @@ static int add_port(struct ports_device char debugfs_name[16]; struct port *port; dev_t devt; - unsigned int nr_added_bufs; int err; port = kmalloc(sizeof(*port), GFP_KERNEL); @@ -1462,11 +1461,13 @@ static int add_port(struct ports_device 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) { + /* We can safely ignore ENOSPC because it means + * the queue already has buffers. Buffers are removed + * only by virtcons_remove(), not by unplug_port() + */ + err = fill_queue(port->in_vq, &port->inbuf_lock); + if (err < 0 && err != -ENOSPC) { dev_err(port->dev, "Error allocating inbufs\n"); - err = -ENOMEM; goto free_device; } @@ -2099,14 +2100,11 @@ static int virtcons_probe(struct virtio_ INIT_WORK(&portdev->control_work, &control_work_handler); if (multiport) { - unsigned int nr_added_bufs; - spin_lock_init(&portdev->c_ivq_lock); spin_lock_init(&portdev->c_ovq_lock); - nr_added_bufs = fill_queue(portdev->c_ivq, - &portdev->c_ivq_lock); - if (!nr_added_bufs) { + err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock); + if (err < 0) { dev_err(&vdev->dev, "Error allocating buffers for control queue\n"); /* @@ -2117,7 +2115,7 @@ static int virtcons_probe(struct virtio_ VIRTIO_CONSOLE_DEVICE_READY, 0); /* Device was functional: we need full cleanup. */ virtcons_remove(vdev); - return -ENOMEM; + return err; } } else { /*