From patchwork Mon Sep 8 15:29:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 4863291 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 2788DC0338 for ; Mon, 8 Sep 2014 15:30:05 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DDB3B20154 for ; Mon, 8 Sep 2014 15:30:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 24EAB20149 for ; Mon, 8 Sep 2014 15:29:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754466AbaIHP3x (ORCPT ); Mon, 8 Sep 2014 11:29:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:1531 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754458AbaIHP3w (ORCPT ); Mon, 8 Sep 2014 11:29:52 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s88FTnRo000675 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 8 Sep 2014 11:29:49 -0400 Received: from localhost (vpn1-115-149.nay.redhat.com [10.66.115.149]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s88FTkes012259; Mon, 8 Sep 2014 11:29:47 -0400 Date: Mon, 8 Sep 2014 23:29:51 +0800 From: Amos Kong To: Amit Shah Cc: virtualization@lists.linux-foundation.org, stable@vger.kernel.org, kvm@vger.kernel.org Subject: Re: [PATCH] virtio-rng: complete have_data completion in removing device Message-ID: <20140908152951.GA28459@zen.redhat.com> References: <1407260115-26767-1-git-send-email-akong@redhat.com> <20140806080541.GA3304@z.redhat.com> <20140806082529.GC25951@grmbl.mre> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20140806082529.GC25951@grmbl.mre> User-Agent: Mutt/1.5.23 (2014-03-12) X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Wed, Aug 06, 2014 at 01:55:29PM +0530, Amit Shah wrote: > On (Wed) 06 Aug 2014 [16:05:41], Amos Kong wrote: > > On Wed, Aug 06, 2014 at 01:35:15AM +0800, Amos Kong wrote: > > > When we try to hot-remove a busy virtio-rng device from QEMU monitor, > > > the device can't be hot-removed. Because virtio-rng driver hangs at > > > wait_for_completion_killable(). > > > > > > This patch fixed the hang by completing have_data completion before > > > unregistering a virtio-rng device. > > > > Hi Amit, > > > > Before applying this patch, it's blocking insider wait_for_completion_killable() > > Applied this patch, wait_for_completion_killable() returns 0, > > and vi->data_avail becomes 0, then rng_get_date() will return 0. > > Thanks for checking this. > > > Is it expected result? > > I think what will happen is vi->data_avail will be set to whatever it > was set last. In case of a previous successful read request, the > data_avail will be set to whatever number of bytes the host gave. On > doing a hot-unplug on the succeeding wait, the value in data_avail > will be re-used, and the hwrng core will wrongly take some bytes in > the buffer as input from the host. > > So, I think we need to set vi->data_avail = 0; before calling > wait_event_completion_killable(). > > Amit In my latest debugging, I found the hang is caused by unexpected reading when we started to remove the device. I have two draft fix, 1) is skip unexpected reading by checking a remove flag. 2) is unregistering device at the beginning of remove_common(). I think second patch is better if it won't cause new problem. The original patch (complete in remove_common()) is still necessary. Test results: hotplug issue disappeared (dd process will quit). kfree(vi); --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c index 2e3139e..028797c 100644 --- a/drivers/char/hw_random/virtio-rng.c +++ b/drivers/char/hw_random/virtio-rng.c @@ -35,6 +35,7 @@ struct virtrng_info { unsigned int data_avail; int index; bool busy; + bool remove; bool hwrng_register_done; }; @@ -68,6 +69,9 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait) int ret; struct virtrng_info *vi = (struct virtrng_info *)rng->priv; + if (vi->remove) + return 0; + if (!vi->busy) { vi->busy = true; init_completion(&vi->have_data); @@ -137,6 +141,8 @@ static void remove_common(struct virtio_device *vdev) { struct virtrng_info *vi = vdev->priv; + vi->remove = true; + complete(&vi->have_data); vdev->config->reset(vdev); vi->busy = false; if (vi->hwrng_register_done) diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c index 2e3139e..9b8c2ce 100644 --- a/drivers/char/hw_random/virtio-rng.c +++ b/drivers/char/hw_random/virtio-rng.c @@ -137,10 +137,11 @@ static void remove_common(struct virtio_device *vdev) { struct virtrng_info *vi = vdev->priv; - vdev->config->reset(vdev); - vi->busy = false; if (vi->hwrng_register_done) hwrng_unregister(&vi->hwrng); + complete(&vi->have_data); + vdev->config->reset(vdev); + vi->busy = false; vdev->config->del_vqs(vdev); ida_simple_remove(&rng_index_ida, vi->index);