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 exits the waiting by completing have_data completion before
unregistering, resets data_avail to avoid the hwrng core use wrong
buffer bytes. Before real unregistering we should return -ENODEV for
reading.

When we hot-unplug the device, dd process in guest will exit.
  $ dd if=/dev/hwrng of=/dev/null
  dd: error reading ‘/dev/hwrng’: No such device

Signed-off-by: Amos Kong <ak...@redhat.com>
Cc: sta...@vger.kernel.org
---
V2: reset data_avail (Amit)
    adjust unregister order
---
 drivers/char/hw_random/virtio-rng.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/virtio-rng.c 
b/drivers/char/hw_random/virtio-rng.c
index 2e3139e..e76433b 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -68,6 +68,10 @@ 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->hwrng_register_done) {
+               return -ENODEV;
+       }
+
        if (!vi->busy) {
                vi->busy = true;
                init_completion(&vi->have_data);
@@ -137,10 +141,14 @@ static void remove_common(struct virtio_device *vdev)
 {
        struct virtrng_info *vi = vdev->priv;
 
+       vi->data_avail = 0;
+       complete(&vi->have_data);
        vdev->config->reset(vdev);
        vi->busy = false;
-       if (vi->hwrng_register_done)
+       if (vi->hwrng_register_done) {
+               vi->hwrng_register_done = false;
                hwrng_unregister(&vi->hwrng);
+       }
        vdev->config->del_vqs(vdev);
        ida_simple_remove(&rng_index_ida, vi->index);
        kfree(vi);
-- 
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to