thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-virtio-scsi-don-t-waste-CPU-polling-the-event-virtqu.patch

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