Received: by 10.192.165.148 with SMTP id m20csp4118977imm; Mon, 23 Apr 2018 19:42:14 -0700 (PDT) X-Google-Smtp-Source: AIpwx48rxBchwa2nOIHN+nLvZziSc77e/t9Jcz8+uWp/J0inbNoRed03oyvcWJSKBFfJz069lV9b X-Received: by 2002:a17:902:5508:: with SMTP id f8-v6mr23556513pli.254.1524537734388; Mon, 23 Apr 2018 19:42:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524537734; cv=none; d=google.com; s=arc-20160816; b=hWLEKpSUpATkHFOVMl5nIKsjx5hm3LkhrKg0KNXYbbTM+Ez+0wTb4ZrJ7KsmCry0Fi avVO8WvF3qfkEamIlobnU+iJzeAqXvQmGfd18GwPZkUvZkaC+faaou/QhsopAXclwebH qTd0YKd48uvzaUvlQ9hDCTW8DBqKKEZAY7emL3hQ3eIJP2J0YDrPTSB2C6xcFlUPDMTr GHlyyZcvurhkHtEvlU3sjJrT3fZlHHWTCEYNQNdJXlSIDA3GwyX7vxkcMF9z5rIs47sB Z9LXx70+Dy1X39x+fSfb/GIXAqQWIZ1HSlwlfcWMI/hPnHLwzrn+LI1o1V3tllMu2kZb 3EAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=BVMYpnY5x4jmdaqRjwVdzZZuBi+4zqcvKpzMHDuU/kI=; b=gh8P9zKkk8glXebD8ObIZIw6CyVrOj7IBvd+BiEJJQ3beqk06FSEwyJnCAJHZcpUBj MrUOPq1GCrwkdtYTzi8Yls2DvYmjtS2jXcW0rV4fgf9cnToBZm/3bOeeUobpb17h1z0P mag0+WnPfDD8xgQOGzY28hOc5q+CKe8aogQlvmj1gWecscqHsd2jdAJTHubDDPHT9J1c qgh+eT6uSyeJ2GqbFUXY34j0670btl8PAOzP+1Ra76ye4sepJWhIoD154hwFgTCO1nyU vZgVzuh2TPAW/EV8ZPBoaae4tJP64qPTp6FlEIkAb82qUv3Y3LLwNaMur6d9xxcWa+c/ R7YA== 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 b2si10455867pgt.44.2018.04.23.19.41.58; Mon, 23 Apr 2018 19:42:14 -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 S932770AbeDXCks (ORCPT + 99 others); Mon, 23 Apr 2018 22:40:48 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:38916 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932688AbeDXCko (ORCPT ); Mon, 23 Apr 2018 22:40:44 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B7C6DFB65E; Tue, 24 Apr 2018 02:40:43 +0000 (UTC) Received: from [10.72.12.52] (ovpn-12-52.pek2.redhat.com [10.72.12.52]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1BEF41208F86; Tue, 24 Apr 2018 02:40:20 +0000 (UTC) Subject: Re: [PATCH 3/6] virtio_console: free buffers after reset To: "Michael S. Tsirkin" , linux-kernel@vger.kernel.org Cc: Amit Shah , Arnd Bergmann , Greg Kroah-Hartman , virtualization@lists.linux-foundation.org, stable@vger.kernel.org, Tiwei Bie , stable@kernel.org References: <1524248223-393618-1-git-send-email-mst@redhat.com> <1524248223-393618-4-git-send-email-mst@redhat.com> From: Jason Wang Message-ID: <27e87df7-2835-7f3e-c3da-76b844dbd780@redhat.com> Date: Tue, 24 Apr 2018 10:40:18 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <1524248223-393618-4-git-send-email-mst@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Tue, 24 Apr 2018 02:40:43 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Tue, 24 Apr 2018 02:40:43 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jasowang@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018年04月21日 02:18, Michael S. Tsirkin wrote: > Console driver is out of spec. The spec says: > A driver MUST NOT decrement the available idx on a live > virtqueue (ie. there is no way to “unexpose” buffers). > and it does exactly that by trying to detach unused buffers > without doing a device reset first. > > Defer detaching the buffers until device unplug. > > Of course this means we might get an interrupt for > a vq without an attached port now. Handle that by > discarding the consumed buffer. > > Reported-by: Tiwei Bie > Fixes: b3258ff1d6 ("virtio: Decrement avail idx on buffer detach") > CC: stable@kernel.org > Signed-off-by: Michael S. Tsirkin I wonder whether or not we can have some BUG_ON() in virtqueue_detach_unused_buf() to detect such bugs (e.g by checking status?). Thanks > --- > drivers/char/virtio_console.c | 49 +++++++++++++++++++++---------------------- > 1 file changed, 24 insertions(+), 25 deletions(-) > > diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c > index 3e56f32..26a66ff 100644 > --- a/drivers/char/virtio_console.c > +++ b/drivers/char/virtio_console.c > @@ -1402,7 +1402,6 @@ static int add_port(struct ports_device *portdev, u32 id) > { > char debugfs_name[16]; > struct port *port; > - struct port_buffer *buf; > dev_t devt; > unsigned int nr_added_bufs; > int err; > @@ -1513,8 +1512,6 @@ static int add_port(struct ports_device *portdev, u32 id) > return 0; > > free_inbufs: > - while ((buf = virtqueue_detach_unused_buf(port->in_vq))) > - free_buf(buf, true); > free_device: > device_destroy(pdrvdata.class, port->dev->devt); > free_cdev: > @@ -1539,34 +1536,14 @@ static void remove_port(struct kref *kref) > > static void remove_port_data(struct port *port) > { > - struct port_buffer *buf; > - > spin_lock_irq(&port->inbuf_lock); > /* Remove unused data this port might have received. */ > discard_port_data(port); > spin_unlock_irq(&port->inbuf_lock); > > - /* Remove buffers we queued up for the Host to send us data in. */ > - do { > - spin_lock_irq(&port->inbuf_lock); > - buf = virtqueue_detach_unused_buf(port->in_vq); > - spin_unlock_irq(&port->inbuf_lock); > - if (buf) > - free_buf(buf, true); > - } while (buf); > - > spin_lock_irq(&port->outvq_lock); > reclaim_consumed_buffers(port); > spin_unlock_irq(&port->outvq_lock); > - > - /* Free pending buffers from the out-queue. */ > - do { > - spin_lock_irq(&port->outvq_lock); > - buf = virtqueue_detach_unused_buf(port->out_vq); > - spin_unlock_irq(&port->outvq_lock); > - if (buf) > - free_buf(buf, true); > - } while (buf); > } > > /* > @@ -1791,13 +1768,24 @@ static void control_work_handler(struct work_struct *work) > spin_unlock(&portdev->c_ivq_lock); > } > > +static void flush_bufs(struct virtqueue *vq, bool can_sleep) > +{ > + struct port_buffer *buf; > + unsigned int len; > + > + while ((buf = virtqueue_get_buf(vq, &len))) > + free_buf(buf, can_sleep); > +} > + > static void out_intr(struct virtqueue *vq) > { > struct port *port; > > port = find_port_by_vq(vq->vdev->priv, vq); > - if (!port) > + if (!port) { > + flush_bufs(vq, false); > return; > + } > > wake_up_interruptible(&port->waitqueue); > } > @@ -1808,8 +1796,10 @@ static void in_intr(struct virtqueue *vq) > unsigned long flags; > > port = find_port_by_vq(vq->vdev->priv, vq); > - if (!port) > + if (!port) { > + flush_bufs(vq, false); > return; > + } > > spin_lock_irqsave(&port->inbuf_lock, flags); > port->inbuf = get_inbuf(port); > @@ -1984,6 +1974,15 @@ static const struct file_operations portdev_fops = { > > static void remove_vqs(struct ports_device *portdev) > { > + struct virtqueue *vq; > + > + virtio_device_for_each_vq(portdev->vdev, vq) { > + struct port_buffer *buf; > + > + flush_bufs(vq, true); > + while ((buf = virtqueue_detach_unused_buf(vq))) > + free_buf(buf, true); > + } > portdev->vdev->config->del_vqs(portdev->vdev); > kfree(portdev->in_vqs); > kfree(portdev->out_vqs);