From patchwork Wed Oct 6 13:20:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 66930 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DFE43B6F01 for ; Thu, 7 Oct 2010 00:44:21 +1100 (EST) Received: from localhost ([127.0.0.1]:59335 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P3UI6-0003eU-KA for incoming@patchwork.ozlabs.org; Wed, 06 Oct 2010 09:44:18 -0400 Received: from [140.186.70.92] (port=46633 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P3U4K-0007ME-5u for qemu-devel@nongnu.org; Wed, 06 Oct 2010 09:30:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P3U0m-0002Fe-I4 for qemu-devel@nongnu.org; Wed, 06 Oct 2010 09:26:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:24044) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P3U0m-0002FM-8Y for qemu-devel@nongnu.org; Wed, 06 Oct 2010 09:26:24 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o96DQNQw007359 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 6 Oct 2010 09:26:23 -0400 Received: from redhat.com (vpn-10-1.rdu.redhat.com [10.11.10.1]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id o96DQJMM017704; Wed, 6 Oct 2010 09:26:20 -0400 Date: Wed, 6 Oct 2010 15:20:17 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org, avi@redhat.com, mtosatti@redhat.com, Alex Williamson , Juan Quintela , Amit Shah Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mutt-Fcc: =sent User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Subject: [Qemu-devel] [PATCH 1/3] virtio: change set guest notifier to per-device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org When using irqfd with vhost-net to inject interrupts, a single evenfd might inject multiple interrupts. Implementing this is much easier with a single per-device callback to set guest notifiers. Signed-off-by: Michael S. Tsirkin --- hw/vhost.c | 52 ++++++++++++++++++++++++++++------------------------ hw/virtio-pci.c | 29 ++++++++++++++++++++++++++++- hw/virtio.h | 2 +- 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/hw/vhost.c b/hw/vhost.c index 34c4745..5ac283b 100644 --- a/hw/vhost.c +++ b/hw/vhost.c @@ -455,11 +455,6 @@ static int vhost_virtqueue_init(struct vhost_dev *dev, }; struct VirtQueue *vvq = virtio_get_queue(vdev, idx); - if (!vdev->binding->set_guest_notifier) { - fprintf(stderr, "binding does not support guest notifiers\n"); - return -ENOSYS; - } - if (!vdev->binding->set_host_notifier) { fprintf(stderr, "binding does not support host notifiers\n"); return -ENOSYS; @@ -512,12 +507,6 @@ static int vhost_virtqueue_init(struct vhost_dev *dev, r = -errno; goto fail_alloc; } - r = vdev->binding->set_guest_notifier(vdev->binding_opaque, idx, true); - if (r < 0) { - fprintf(stderr, "Error binding guest notifier: %d\n", -r); - goto fail_guest_notifier; - } - r = vdev->binding->set_host_notifier(vdev->binding_opaque, idx, true); if (r < 0) { fprintf(stderr, "Error binding host notifier: %d\n", -r); @@ -542,8 +531,6 @@ fail_call: fail_kick: vdev->binding->set_host_notifier(vdev->binding_opaque, idx, false); fail_host_notifier: - vdev->binding->set_guest_notifier(vdev->binding_opaque, idx, false); -fail_guest_notifier: fail_alloc: cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx), 0, 0); @@ -569,13 +556,6 @@ static void vhost_virtqueue_cleanup(struct vhost_dev *dev, .index = idx, }; int r; - r = vdev->binding->set_guest_notifier(vdev->binding_opaque, idx, false); - if (r < 0) { - fprintf(stderr, "vhost VQ %d guest cleanup failed: %d\n", idx, r); - fflush(stderr); - } - assert (r >= 0); - r = vdev->binding->set_host_notifier(vdev->binding_opaque, idx, false); if (r < 0) { fprintf(stderr, "vhost VQ %d host cleanup failed: %d\n", idx, r); @@ -648,15 +628,26 @@ void vhost_dev_cleanup(struct vhost_dev *hdev) int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) { int i, r; + if (!vdev->binding->set_guest_notifiers) { + fprintf(stderr, "binding does not support guest notifiers\n"); + r = -ENOSYS; + goto fail; + } + + r = vdev->binding->set_guest_notifiers(vdev->binding_opaque, true); + if (r < 0) { + fprintf(stderr, "Error binding guest notifier: %d\n", -r); + goto fail_notifiers; + } r = vhost_dev_set_features(hdev, hdev->log_enabled); if (r < 0) { - goto fail; + goto fail_features; } r = ioctl(hdev->control, VHOST_SET_MEM_TABLE, hdev->mem); if (r < 0) { r = -errno; - goto fail; + goto fail_mem; } for (i = 0; i < hdev->nvqs; ++i) { r = vhost_virtqueue_init(hdev, @@ -676,13 +667,14 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) (uint64_t)(unsigned long)hdev->log); if (r < 0) { r = -errno; - goto fail_vq; + goto fail_log; } } hdev->started = true; return 0; +fail_log: fail_vq: while (--i >= 0) { vhost_virtqueue_cleanup(hdev, @@ -690,13 +682,18 @@ fail_vq: hdev->vqs + i, i); } +fail_mem: +fail_features: + vdev->binding->set_guest_notifiers(vdev->binding_opaque, false); +fail_notifiers: fail: return r; } void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev) { - int i; + int i, r; + for (i = 0; i < hdev->nvqs; ++i) { vhost_virtqueue_cleanup(hdev, vdev, @@ -705,6 +702,13 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev) } vhost_client_sync_dirty_bitmap(&hdev->client, 0, (target_phys_addr_t)~0x0ull); + r = vdev->binding->set_guest_notifiers(vdev->binding_opaque, false); + if (r < 0) { + fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); + fflush(stderr); + } + assert (r >= 0); + hdev->started = false; qemu_free(hdev->log); hdev->log_size = 0; diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c index 1357d40..232f943 100644 --- a/hw/virtio-pci.c +++ b/hw/virtio-pci.c @@ -482,6 +482,33 @@ static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign) return 0; } +static int virtio_pci_set_guest_notifiers(void *opaque, bool assign) +{ + VirtIOPCIProxy *proxy = opaque; + VirtIODevice *vdev = proxy->vdev; + int r, n; + + for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { + if (!virtio_queue_get_num(vdev, n)) { + break; + } + + r = virtio_pci_set_guest_notifier(opaque, n, assign); + if (r < 0) { + goto assign_error; + } + } + + return 0; + +assign_error: + /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ + while (--n >= 0) { + virtio_pci_set_guest_notifier(opaque, n, !assign); + } + return r; +} + static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign) { VirtIOPCIProxy *proxy = opaque; @@ -519,7 +546,7 @@ static const VirtIOBindings virtio_pci_bindings = { .load_queue = virtio_pci_load_queue, .get_features = virtio_pci_get_features, .set_host_notifier = virtio_pci_set_host_notifier, - .set_guest_notifier = virtio_pci_set_guest_notifier, + .set_guest_notifiers = virtio_pci_set_guest_notifiers, }; static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev, diff --git a/hw/virtio.h b/hw/virtio.h index 5836ab6..457edb2 100644 --- a/hw/virtio.h +++ b/hw/virtio.h @@ -92,7 +92,7 @@ typedef struct { int (*load_config)(void * opaque, QEMUFile *f); int (*load_queue)(void * opaque, int n, QEMUFile *f); unsigned (*get_features)(void * opaque); - int (*set_guest_notifier)(void * opaque, int n, bool assigned); + int (*set_guest_notifiers)(void * opaque, bool assigned); int (*set_host_notifier)(void * opaque, int n, bool assigned); } VirtIOBindings;