From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Pavel Fedin <p.fedin@samsung.com>,
Ying-Shiuan Pan <yingshiuan.pan@gmail.com>
Subject: [Qemu-devel] [PULL 57/57] virtio-mmio: ioeventfd support
Date: Sun, 31 May 2015 20:38:00 +0200 [thread overview]
Message-ID: <1433097192-8988-58-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1433097192-8988-1-git-send-email-mst@redhat.com>
From: Ying-Shiuan Pan <yingshiuan.pan@gmail.com>
set_host_notifier and set_guest_notifiers supported by virtio-mmio now.
Most code copied from virtio-pci.
This makes it possible to use vhost-net with virtio-mmio,
improving performance by about 30%.
The kvm-arm does not yet support irqfd, need to fix the hard-coded part after
kvm-arm gets irqfd support.
Signed-off-by: Ying-Shiuan Pan <yingshiuan.pan@gmail.com>
Signed-off-by: Pavel Fedin <p.fedin@samsung.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/virtio/virtio-mmio.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 181 insertions(+)
diff --git a/hw/virtio/virtio-mmio.c b/hw/virtio/virtio-mmio.c
index a3cfd30..c8f7294 100644
--- a/hw/virtio/virtio-mmio.c
+++ b/hw/virtio/virtio-mmio.c
@@ -22,7 +22,9 @@
#include "hw/sysbus.h"
#include "hw/virtio/virtio.h"
#include "qemu/host-utils.h"
+#include "sysemu/kvm.h"
#include "hw/virtio/virtio-bus.h"
+#include "qemu/error-report.h"
/* #define DEBUG_VIRTIO_MMIO */
@@ -86,8 +88,96 @@ typedef struct {
uint32_t guest_page_shift;
/* virtio-bus */
VirtioBusState bus;
+ bool ioeventfd_disabled;
+ bool ioeventfd_started;
} VirtIOMMIOProxy;
+static int virtio_mmio_set_host_notifier_internal(VirtIOMMIOProxy *proxy,
+ int n, bool assign,
+ bool set_handler)
+{
+ VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+ VirtQueue *vq = virtio_get_queue(vdev, n);
+ EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
+ int r = 0;
+
+ if (assign) {
+ r = event_notifier_init(notifier, 1);
+ if (r < 0) {
+ error_report("%s: unable to init event notifier: %d",
+ __func__, r);
+ return r;
+ }
+ virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
+ memory_region_add_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUENOTIFY, 4,
+ true, n, notifier);
+ } else {
+ memory_region_del_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUENOTIFY, 4,
+ true, n, notifier);
+ virtio_queue_set_host_notifier_fd_handler(vq, false, false);
+ event_notifier_cleanup(notifier);
+ }
+ return r;
+}
+
+static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy *proxy)
+{
+ VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+ int n, r;
+
+ if (!kvm_eventfds_enabled() ||
+ proxy->ioeventfd_disabled ||
+ proxy->ioeventfd_started) {
+ return;
+ }
+
+ for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
+ if (!virtio_queue_get_num(vdev, n)) {
+ continue;
+ }
+
+ r = virtio_mmio_set_host_notifier_internal(proxy, n, true, true);
+ if (r < 0) {
+ goto assign_error;
+ }
+ }
+ proxy->ioeventfd_started = true;
+ return;
+
+assign_error:
+ while (--n >= 0) {
+ if (!virtio_queue_get_num(vdev, n)) {
+ continue;
+ }
+
+ r = virtio_mmio_set_host_notifier_internal(proxy, n, false, false);
+ assert(r >= 0);
+ }
+ proxy->ioeventfd_started = false;
+ error_report("%s: failed. Fallback to a userspace (slower).", __func__);
+}
+
+static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy *proxy)
+{
+ int r;
+ int n;
+ VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+
+ if (!proxy->ioeventfd_started) {
+ return;
+ }
+
+ for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
+ if (!virtio_queue_get_num(vdev, n)) {
+ continue;
+ }
+
+ r = virtio_mmio_set_host_notifier_internal(proxy, n, false, false);
+ assert(r >= 0);
+ }
+ proxy->ioeventfd_started = false;
+}
+
static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
{
VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
@@ -265,7 +355,16 @@ static void virtio_mmio_write(void *opaque, hwaddr offset, uint64_t value,
virtio_update_irq(vdev);
break;
case VIRTIO_MMIO_STATUS:
+ if (!(value & VIRTIO_CONFIG_S_DRIVER_OK)) {
+ virtio_mmio_stop_ioeventfd(proxy);
+ }
+
virtio_set_status(vdev, value & 0xff);
+
+ if (value & VIRTIO_CONFIG_S_DRIVER_OK) {
+ virtio_mmio_start_ioeventfd(proxy);
+ }
+
if (vdev->status == 0) {
virtio_reset(vdev);
}
@@ -328,12 +427,92 @@ static void virtio_mmio_reset(DeviceState *d)
{
VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
+ virtio_mmio_stop_ioeventfd(proxy);
virtio_bus_reset(&proxy->bus);
proxy->host_features_sel = 0;
proxy->guest_features_sel = 0;
proxy->guest_page_shift = 0;
}
+static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
+ bool with_irqfd)
+{
+ VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
+ VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+ VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
+ VirtQueue *vq = virtio_get_queue(vdev, n);
+ EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
+
+ if (assign) {
+ int r = event_notifier_init(notifier, 0);
+ if (r < 0) {
+ return r;
+ }
+ virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
+ } else {
+ virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
+ event_notifier_cleanup(notifier);
+ }
+
+ if (vdc->guest_notifier_mask) {
+ vdc->guest_notifier_mask(vdev, n, !assign);
+ }
+
+ return 0;
+}
+
+static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
+ bool assign)
+{
+ VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
+ VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+ /* TODO: need to check if kvm-arm supports irqfd */
+ bool with_irqfd = false;
+ int r, n;
+
+ nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
+
+ for (n = 0; n < nvqs; n++) {
+ if (!virtio_queue_get_num(vdev, n)) {
+ break;
+ }
+
+ r = virtio_mmio_set_guest_notifier(d, n, assign, with_irqfd);
+ if (r < 0) {
+ goto assign_error;
+ }
+ }
+
+ return 0;
+
+assign_error:
+ /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
+ assert(assign);
+ while (--n >= 0) {
+ virtio_mmio_set_guest_notifier(d, n, !assign, false);
+ }
+ return r;
+}
+
+static int virtio_mmio_set_host_notifier(DeviceState *opaque, int n,
+ bool assign)
+{
+ VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
+
+ /* Stop using ioeventfd for virtqueue kick if the device starts using host
+ * notifiers. This makes it easy to avoid stepping on each others' toes.
+ */
+ proxy->ioeventfd_disabled = assign;
+ if (assign) {
+ virtio_mmio_stop_ioeventfd(proxy);
+ }
+ /* We don't need to start here: it's not needed because backend
+ * currently only stops on status change away from ok,
+ * reset, vmstop and such. If we do add code to start here,
+ * need to check vmstate, device state etc. */
+ return virtio_mmio_set_host_notifier_internal(proxy, n, assign, false);
+}
+
/* virtio-mmio device */
static void virtio_mmio_realizefn(DeviceState *d, Error **errp)
@@ -375,6 +554,8 @@ static void virtio_mmio_bus_class_init(ObjectClass *klass, void *data)
k->notify = virtio_mmio_update_irq;
k->save_config = virtio_mmio_save_config;
k->load_config = virtio_mmio_load_config;
+ k->set_host_notifier = virtio_mmio_set_host_notifier;
+ k->set_guest_notifiers = virtio_mmio_set_guest_notifiers;
k->has_variable_vring_alignment = true;
bus_class->max_dev = 1;
}
--
MST
next prev parent reply other threads:[~2015-05-31 18:38 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-31 18:34 [Qemu-devel] [PULL 00/57] pc, pci, tpm, virtio enhancements and fixes Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 01/57] hw/virtio/virtio-balloon: move adding property to virtio_balloon_instance_init Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 02/57] hw/virtio/virtio-pci: use alias property for virtio-balloon-pci Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 03/57] hw/s390x/virtio-ccw: use alias property for virtio-balloon-ccw Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 04/57] pc: Replace tab with spaces Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 05/57] hw: Move commas inside HW_COMPAT_2_1 macro Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 06/57] pc: Move commas inside PC_COMPAT_* macros Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 07/57] spapr: Move commas inside SPAPR_COMPAT_* macros Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 08/57] hw: Define empty HW_COMPAT_2_[23] macros Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 09/57] pc: Define PC_COMPAT_2_[123] macros Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 10/57] spapr: Use HW_COMPAT_* inside SPAPR_COMPAT_* macros Michael S. Tsirkin
2015-05-31 18:34 ` [Qemu-devel] [PULL 11/57] spapr: define SPAPR_COMPAT_2_3 Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 12/57] piix: Move pc-0.14 qxl compat properties to PC_COMPAT_0_14 Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 13/57] piix: Move pc-0.11 drive version compat props to PC_COMPAT_0_11 Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 14/57] piix: Move pc-0.13 virtio-9p-pci compat to PC_COMPAT_0_13 Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 15/57] piix: Move pc-0.1[23] rombar compat props " Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 16/57] piix: Define PC_COMPAT_0_10 Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 17/57] pc: Define MACHINE_OPTIONS macros consistently for all machines Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 18/57] pc: Define machines using a DEFINE_PC_MACHINE macro Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 19/57] pc: Convert *_MACHINE_OPTIONS macros into functions Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 20/57] pc: Move compat_props setting inside *_machine_options() functions Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 21/57] pc: Don't use QEMUMachine anymore Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 22/57] pc: Remove qemu_register_pc_machine() function Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 23/57] machine: Remove unused fields from QEMUMachine Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 24/57] piix: Add kvmclock_enabled, pci_enabled globals Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 25/57] piix: Eliminate pc_init_pci() Michael S. Tsirkin
2015-05-31 18:35 ` [Qemu-devel] [PULL 26/57] pc: Generate init functions with a macro Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 27/57] pc: acpi: fix pvpanic for buggy guests Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 28/57] virtio: move host_features Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 29/57] virtio-ccw: Don't advertise VIRTIO_F_BAD_FEATURE Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 30/57] virtio: move VIRTIO_F_NOTIFY_ON_EMPTY into core Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 31/57] virtio-net: adding all queues in .realize() Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 32/57] virtio: device_plugged() can fail Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 33/57] virtio: introduce virtio_get_num_queues() Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 34/57] virtio-ccw: introduce ccw specific queue limit Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 35/57] virtio-ccw: validate the number of queues against bus limitation Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 36/57] virtio-s390: introduce virito s390 queue limit Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 37/57] virtio-s390: introduce virtio_s390_device_plugged() Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 38/57] virtio: rename VIRTIO_PCI_QUEUE_MAX to VIRTIO_QUEUE_MAX Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 39/57] virtio: increase the queue limit to 1024 Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 40/57] i386/pc: pc_basic_device_init(): delegate FDC creation request Michael S. Tsirkin
2015-05-31 18:36 ` [Qemu-devel] [PULL 41/57] i386/pc: '-drive if=floppy' should imply a board-default FDC Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 42/57] i386/pc_q35: don't insist on board FDC if there's no default floppy Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 43/57] i386: drop FDC in pc-q35-2.4+ if neither it nor floppy drives are wanted Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 44/57] acpi: Simplify printing to dynamic string Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 45/57] Add stream ID to MSI write Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 46/57] Extend TPM TIS interface to support TPM 2 Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 47/57] tpm: Probe for connected TPM 1.2 or " Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 48/57] TPM2 ACPI table support Michael S. Tsirkin
2015-06-04 13:57 ` Igor Mammedov
2015-06-05 20:42 ` Stefan Berger
2015-05-31 18:37 ` [Qemu-devel] [PULL 49/57] acpi: add aml_add() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 50/57] acpi: add aml_lless() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 51/57] acpi: add aml_index() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 52/57] acpi: add aml_shiftleft() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 53/57] acpi: add aml_shiftright() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 54/57] acpi: add aml_increment() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 55/57] acpi: add aml_while() term Michael S. Tsirkin
2015-05-31 18:37 ` [Qemu-devel] [PULL 56/57] hw/acpi/aml-build: Fix memory leak Michael S. Tsirkin
2015-05-31 18:38 ` Michael S. Tsirkin [this message]
2015-06-01 12:04 ` [Qemu-devel] [PULL 00/57] pc, pci, tpm, virtio enhancements and fixes Peter Maydell
2015-06-01 12:14 ` Michael S. Tsirkin
2015-06-02 14:49 ` Igor Mammedov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1433097192-8988-58-git-send-email-mst@redhat.com \
--to=mst@redhat.com \
--cc=p.fedin@samsung.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=yingshiuan.pan@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).