thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-balloon-fix-segfault-and-harden-the-stats-queue.patch

40b356
From 75255574498fad12727529c4ecbd4ccdabe86839 Mon Sep 17 00:00:00 2001
40b356
From: Ladi Prosek <lprosek@redhat.com>
40b356
Date: Wed, 5 Oct 2016 17:22:26 +0200
40b356
Subject: [PATCH 4/8] balloon: fix segfault and harden the stats queue
40b356
40b356
RH-Author: Ladi Prosek <lprosek@redhat.com>
40b356
Message-id: <1475666548-9186-5-git-send-email-lprosek@redhat.com>
40b356
Patchwork-id: 72483
40b356
O-Subject: [RHEL-7.4 qemu-kvm v2 PATCH 4/6] balloon: fix segfault and harden the stats queue
40b356
Bugzilla: 1393484
40b356
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
40b356
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
40b356
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
40b356
40b356
The segfault here is triggered by the driver notifying the stats queue
40b356
twice after adding a buffer to it. This effectively resets stats_vq_elem
40b356
back to NULL and QEMU crashes on the next stats timer tick in
40b356
balloon_stats_poll_cb.
40b356
40b356
This is a regression introduced in 51b19ebe4320f3dc, although admittedly
40b356
the device assumed too much about the stats queue protocol even before
40b356
that commit. This commit adds a few more checks and ensures that the one
40b356
stats buffer gets deallocated on device reset.
40b356
40b356
Cc: qemu-stable@nongnu.org
40b356
Signed-off-by: Ladi Prosek <lprosek@redhat.com>
40b356
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
40b356
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
40b356
(cherry picked from commit 4eae2a657d1ff5ada56eb9b4966eae0eff333b0b)
40b356
Signed-off-by: Ladi Prosek <lprosek@redhat.com>
40b356
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
40b356
40b356
Conflicts:
40b356
  * 1.5.3 does not return pointers from virtqueue_pop so only the
40b356
    "harden the stats queue" part of the upstream patch description
40b356
    applies
40b356
  * a new field stats_vq_elem_pending is introduced to keep track
40b356
    of the state of stats_vq_elem in lieu of its nullness upstream
40b356
  * virtio_balloon_device_reset only resets stats_vq_elem_pending
40b356
    because there is nothing to free
40b356
---
40b356
 hw/virtio/virtio-balloon.c         | 27 +++++++++++++++++++++++----
40b356
 include/hw/virtio/virtio-balloon.h |  1 +
40b356
 2 files changed, 24 insertions(+), 4 deletions(-)
40b356
40b356
diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
40b356
index 016dc60..17b3029 100644
40b356
--- a/hw/virtio/virtio-balloon.c
40b356
+++ b/hw/virtio/virtio-balloon.c
40b356
@@ -95,13 +95,14 @@ static void balloon_stats_poll_cb(void *opaque)
40b356
     VirtIOBalloon *s = opaque;
40b356
     VirtIODevice *vdev = VIRTIO_DEVICE(s);
40b356
 
40b356
-    if (!balloon_stats_supported(s)) {
40b356
+    if (!s->stats_vq_elem_pending || !balloon_stats_supported(s)) {
40b356
         /* re-schedule */
40b356
         balloon_stats_change_timer(s, s->stats_poll_interval);
40b356
         return;
40b356
     }
40b356
 
40b356
     virtqueue_push(s->svq, &s->stats_vq_elem, s->stats_vq_offset);
40b356
+    s->stats_vq_elem_pending = false;
40b356
     virtio_notify(vdev, s->svq);
40b356
 }
40b356
 
40b356
@@ -220,14 +221,22 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
40b356
 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
40b356
 {
40b356
     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
40b356
-    VirtQueueElement *elem = &s->stats_vq_elem;
40b356
+    VirtQueueElement elem;
40b356
     VirtIOBalloonStat stat;
40b356
     size_t offset = 0;
40b356
     qemu_timeval tv;
40b356
 
40b356
-    if (!virtqueue_pop(vq, elem)) {
40b356
+    if (!virtqueue_pop(vq, &elem)) {
40b356
         goto out;
40b356
     }
40b356
+    if (s->stats_vq_elem_pending) {
40b356
+        /* This should never happen if the driver follows the spec. */
40b356
+        virtqueue_push(vq, &s->stats_vq_elem, 0);
40b356
+        virtio_notify(vdev, vq);
40b356
+    }
40b356
+
40b356
+    s->stats_vq_elem = elem;
40b356
+    s->stats_vq_elem_pending = true;
40b356
 
40b356
     /* Initialize the stats to get rid of any stale values.  This is only
40b356
      * needed to handle the case where a guest supports fewer stats than it
40b356
@@ -235,7 +244,7 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
40b356
      */
40b356
     reset_stats(s);
40b356
 
40b356
-    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
40b356
+    while (iov_to_buf(elem.out_sg, elem.out_num, offset, &stat, sizeof(stat))
40b356
            == sizeof(stat)) {
40b356
         uint16_t tag = tswap16(stat.tag);
40b356
         uint64_t val = tswap64(stat.val);
40b356
@@ -384,6 +393,15 @@ static void virtio_balloon_device_exit(VirtIODevice *vdev)
40b356
     virtio_cleanup(vdev);
40b356
 }
40b356
 
40b356
+static void virtio_balloon_device_reset(VirtIODevice *vdev)
40b356
+{
40b356
+    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
40b356
+
40b356
+    if (s->stats_vq_elem_pending) {
40b356
+        s->stats_vq_elem_pending = false;
40b356
+    }
40b356
+}
40b356
+
40b356
 static Property virtio_balloon_properties[] = {
40b356
     DEFINE_PROP_END_OF_LIST(),
40b356
 };
40b356
@@ -396,6 +414,7 @@ static void virtio_balloon_class_init(ObjectClass *klass, void *data)
40b356
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
40b356
     vdc->init = virtio_balloon_device_init;
40b356
     vdc->exit = virtio_balloon_device_exit;
40b356
+    vdc->reset = virtio_balloon_device_reset;
40b356
     vdc->get_config = virtio_balloon_get_config;
40b356
     vdc->set_config = virtio_balloon_set_config;
40b356
     vdc->get_features = virtio_balloon_get_features;
40b356
diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h
40b356
index f863bfe..a84736b 100644
40b356
--- a/include/hw/virtio/virtio-balloon.h
40b356
+++ b/include/hw/virtio/virtio-balloon.h
40b356
@@ -63,6 +63,7 @@ typedef struct VirtIOBalloon {
40b356
     uint32_t actual;
40b356
     uint64_t stats[VIRTIO_BALLOON_S_NR];
40b356
     VirtQueueElement stats_vq_elem;
40b356
+    bool stats_vq_elem_pending;
40b356
     size_t stats_vq_offset;
40b356
     QEMUTimer *stats_timer;
40b356
     int64_t stats_last_update;
40b356
-- 
40b356
1.8.3.1
40b356