Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6029661ybc; Wed, 27 Nov 2019 13:42:16 -0800 (PST) X-Google-Smtp-Source: APXvYqwDzyrBHujBNA/JBB2FmPaD5CFSkJ4ZCmFppRnYc8KCKIBAgqXD/pdh1IDeAZeoke7IwTuB X-Received: by 2002:aa7:c518:: with SMTP id o24mr33994135edq.110.1574890936360; Wed, 27 Nov 2019 13:42:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574890936; cv=none; d=google.com; s=arc-20160816; b=RASke+qiyQwvUg/iZWuiJ/NIaqS6UmMeUeSJ2u2wiy2j+CiXN3wVumn+LfvESCqKxz 79PNrBjsRW0TeX991u8pdAGGqmviLI+3eDExEHM22J4xYVhL8H0/rutved42LNETd9/+ mdsxCwnmVgXaVrnSA9cR8ULscC+9VGONpgxR15Tkvqt9PH0aP3bBVEj/fbSwML7I7gKg D+zQ/xNRmDxBfib9XRx5AEu/SytuLlcXq0GpApwmp7s2/YeszJ4cJnpMy4p4lajbVBuL OJc4IlUFc8ArrGGHojBX6TZR5Cak+6dvoQ8C6ytZ0sPKwHXujJqmlRmA0abSgWEBGXgH cTmQ== 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=Ns4DCLYOxeVc2iVxHUSMMg7SoW4ssf2urssWBNlCW8I=; b=wKktXQIh4tCK9mz0YRVYv9ANYNCZ99jweBmJuGu+ylivQseKT9IMJ+dsMn4seO3SS/ hmxcm7SrHd4ReiUeB6CAyZID+mHA2sO3xrM9iugcBUyKDZll3LSB3+qRp61xWGNJRDyB LnrRzWgnEdn270ViDB/s4+FznkO+mkIBg70P8amyX2qvWOcpGEvAumq0ShDuuly15DAq SKiVo5eGAEvIbuOxS4EMeLlE1D/56YosBbaJMsLUksEQ82dnBRp8j4jk6VTj5AExphWs ZFp+Ur8xfGrxn3Q5VoXn6lwcbW/BZJ4GceKNxJBOSJWFghpEBqb2jxDNYbSBKsJ4fuqv gNOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IdOwMIXw; 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 dd21si4915769edb.111.2019.11.27.13.41.53; Wed, 27 Nov 2019 13:42:16 -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=IdOwMIXw; 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 S1729675AbfK0UpA (ORCPT + 99 others); Wed, 27 Nov 2019 15:45:00 -0500 Received: from mail.kernel.org ([198.145.29.99]:55034 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728218AbfK0Uo6 (ORCPT ); Wed, 27 Nov 2019 15:44:58 -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 59EFB2166E; Wed, 27 Nov 2019 20:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574887496; bh=1vNBDlIZgpV/AcG6p9zZo5ELyIjDay1EuilGCvtmWp4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IdOwMIXwUo4IYXLdLjnLfjX8RXPoAdRyBXkxtDwpFGUqLLjpkZpCnMf0oTyvQtlWW yvTlMY18XD9OM7W7ULdz8Qmxkba7uX85elT3F//qjpUifxvlapDf1gjDeXCrdAP66Y L6dOg4mb+eX0Q9r91PXVU6oXmweaVqt13xy+R6SU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, mst@redhat.com, Laurent Vivier , Sasha Levin Subject: [PATCH 4.9 134/151] virtio_console: allocate inbufs in add_port() only if it is needed Date: Wed, 27 Nov 2019 21:31:57 +0100 Message-Id: <20191127203046.663614848@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203000.773542911@linuxfoundation.org> References: <20191127203000.773542911@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 [ Upstream commit d791cfcbf98191122af70b053a21075cb450d119 ] 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: Sasha Levin --- drivers/char/virtio_console.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index b09fc4553dc81..d7ee031d776d8 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -1369,24 +1369,24 @@ static void set_console_size(struct port *port, u16 rows, u16 cols) 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); @@ -1406,7 +1406,6 @@ static int add_port(struct ports_device *portdev, u32 id) char debugfs_name[16]; struct port *port; dev_t devt; - unsigned int nr_added_bufs; int err; port = kmalloc(sizeof(*port), GFP_KERNEL); @@ -1465,11 +1464,13 @@ 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) { + /* 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; } @@ -2081,14 +2082,11 @@ static int virtcons_probe(struct virtio_device *vdev) 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"); /* @@ -2099,7 +2097,7 @@ static int virtcons_probe(struct virtio_device *vdev) VIRTIO_CONSOLE_DEVICE_READY, 0); /* Device was functional: we need full cleanup. */ virtcons_remove(vdev); - return -ENOMEM; + return err; } } else { /* -- 2.20.1