render / rpms / qemu

Forked from rpms/qemu 10 months ago
Clone

Blame 0003-virtio-scsi-don-t-waste-CPU-polling-the-event-virtqu.patch

a575c5
From: Stefan Hajnoczi <stefanha@redhat.com>
a575c5
Date: Tue, 17 May 2022 09:27:45 +0100
a575c5
Subject: [PATCH] virtio-scsi: don't waste CPU polling the event virtqueue
a575c5
Content-type: text/plain
a575c5
a575c5
The virtio-scsi event virtqueue is not emptied by its handler function.
a575c5
This is typical for rx virtqueues where the device uses buffers when
a575c5
some event occurs (e.g. a packet is received, an error condition
a575c5
happens, etc).
a575c5
a575c5
Polling non-empty virtqueues wastes CPU cycles. We are not waiting for
a575c5
new buffers to become available, we are waiting for an event to occur,
a575c5
so it's a misuse of CPU resources to poll for buffers.
a575c5
a575c5
Introduce the new virtio_queue_aio_attach_host_notifier_no_poll() API,
a575c5
which is identical to virtio_queue_aio_attach_host_notifier() except
a575c5
that it does not poll the virtqueue.
a575c5
a575c5
Before this patch the following command-line consumed 100% CPU in the
a575c5
IOThread polling and calling virtio_scsi_handle_event():
a575c5
a575c5
  $ qemu-system-x86_64 -M accel=kvm -m 1G -cpu host \
a575c5
      --object iothread,id=iothread0 \
a575c5
      --device virtio-scsi-pci,iothread=iothread0 \
a575c5
      --blockdev file,filename=test.img,aio=native,cache.direct=on,node-name=drive0 \
a575c5
      --device scsi-hd,drive=drive0
a575c5
a575c5
After this patch CPU is no longer wasted.
a575c5
a575c5
Reported-by: Nir Soffer <nsoffer@redhat.com>
a575c5
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
a575c5
Tested-by: Nir Soffer <nsoffer@redhat.com>
a575c5
Message-id: 20220427143541.119567-3-stefanha@redhat.com
a575c5
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
a575c5
(cherry picked from commit 38738f7dbbda90fbc161757b7f4be35b52205552)
a575c5
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
a575c5
---
a575c5
 hw/scsi/virtio-scsi-dataplane.c |  2 +-
a575c5
 hw/virtio/virtio.c              | 13 +++++++++++++
a575c5
 include/hw/virtio/virtio.h      |  1 +
a575c5
 3 files changed, 15 insertions(+), 1 deletion(-)
a575c5
a575c5
diff --git a/hw/scsi/virtio-scsi-dataplane.c b/hw/scsi/virtio-scsi-dataplane.c
a575c5
index 29575cbaf6..8bb6e6acfc 100644
a575c5
--- a/hw/scsi/virtio-scsi-dataplane.c
a575c5
+++ b/hw/scsi/virtio-scsi-dataplane.c
a575c5
@@ -138,7 +138,7 @@ int virtio_scsi_dataplane_start(VirtIODevice *vdev)
a575c5
 
a575c5
     aio_context_acquire(s->ctx);
a575c5
     virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx);
a575c5
-    virtio_queue_aio_attach_host_notifier(vs->event_vq, s->ctx);
a575c5
+    virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx);
a575c5
 
a575c5
     for (i = 0; i < vs->conf.num_queues; i++) {
a575c5
         virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx);
a575c5
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
a575c5
index 9d637e043e..67a873f54a 100644
a575c5
--- a/hw/virtio/virtio.c
a575c5
+++ b/hw/virtio/virtio.c
a575c5
@@ -3534,6 +3534,19 @@ void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx)
a575c5
                                 virtio_queue_host_notifier_aio_poll_end);
a575c5
 }
a575c5
 
a575c5
+/*
a575c5
+ * Same as virtio_queue_aio_attach_host_notifier() but without polling. Use
a575c5
+ * this for rx virtqueues and similar cases where the virtqueue handler
a575c5
+ * function does not pop all elements. When the virtqueue is left non-empty
a575c5
+ * polling consumes CPU cycles and should not be used.
a575c5
+ */
a575c5
+void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx)
a575c5
+{
a575c5
+    aio_set_event_notifier(ctx, &vq->host_notifier, true,
a575c5
+                           virtio_queue_host_notifier_read,
a575c5
+                           NULL, NULL);
a575c5
+}
a575c5
+
a575c5
 void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx)
a575c5
 {
a575c5
     aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL, NULL);
a575c5
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
a575c5
index b31c4507f5..b62a35fdca 100644
a575c5
--- a/include/hw/virtio/virtio.h
a575c5
+++ b/include/hw/virtio/virtio.h
a575c5
@@ -317,6 +317,7 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
a575c5
 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
a575c5
 void virtio_queue_host_notifier_read(EventNotifier *n);
a575c5
 void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx);
a575c5
+void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx);
a575c5
 void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx);
a575c5
 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
a575c5
 VirtQueue *virtio_vector_next_queue(VirtQueue *vq);