76daa3
From b6f11743278aeb3889ff42c98176b3e265654587 Mon Sep 17 00:00:00 2001
76daa3
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
76daa3
Date: Thu, 27 Jul 2017 12:06:57 +0200
76daa3
Subject: [PATCH 09/17] migration/rdma: Allow cancelling while waiting for wrid
76daa3
76daa3
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
76daa3
Message-id: <20170727120659.8640-4-dgilbert@redhat.com>
76daa3
Patchwork-id: 75857
76daa3
O-Subject: [Pegas-1.0 qemu-kvm PATCH 3/5] migration/rdma: Allow cancelling while waiting for wrid
76daa3
Bugzilla: 1475751
76daa3
RH-Acked-by: Peter Xu <peterx@redhat.com>
76daa3
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
76daa3
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
76daa3
76daa3
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
76daa3
76daa3
When waiting for a WRID, if the other side dies we end up waiting
76daa3
for ever with no way to cancel the migration.
76daa3
Cure this by poll()ing the fd first with a timeout and checking
76daa3
error flags and migration state.
76daa3
76daa3
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
76daa3
Reviewed-by: Peter Xu <peterx@redhat.com>
76daa3
Message-Id: <20170717110936.23314-5-dgilbert@redhat.com>
76daa3
Signed-off-by: Juan Quintela <quintela@redhat.com>
76daa3
(cherry picked from commit 9c98cfbe72b21d9d84b9ea8d231bde103b9fb7ae)
76daa3
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
76daa3
---
76daa3
 migration/rdma.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++------
76daa3
 1 file changed, 53 insertions(+), 6 deletions(-)
76daa3
76daa3
diff --git a/migration/rdma.c b/migration/rdma.c
76daa3
index 44a174f..666c84f 100644
76daa3
--- a/migration/rdma.c
76daa3
+++ b/migration/rdma.c
76daa3
@@ -1464,6 +1464,56 @@ static uint64_t qemu_rdma_poll(RDMAContext *rdma, uint64_t *wr_id_out,
76daa3
     return  0;
76daa3
 }
76daa3
 
76daa3
+/* Wait for activity on the completion channel.
76daa3
+ * Returns 0 on success, none-0 on error.
76daa3
+ */
76daa3
+static int qemu_rdma_wait_comp_channel(RDMAContext *rdma)
76daa3
+{
76daa3
+    /*
76daa3
+     * Coroutine doesn't start until migration_fd_process_incoming()
76daa3
+     * so don't yield unless we know we're running inside of a coroutine.
76daa3
+     */
76daa3
+    if (rdma->migration_started_on_destination) {
76daa3
+        yield_until_fd_readable(rdma->comp_channel->fd);
76daa3
+    } else {
76daa3
+        /* This is the source side, we're in a separate thread
76daa3
+         * or destination prior to migration_fd_process_incoming()
76daa3
+         * we can't yield; so we have to poll the fd.
76daa3
+         * But we need to be able to handle 'cancel' or an error
76daa3
+         * without hanging forever.
76daa3
+         */
76daa3
+        while (!rdma->error_state  && !rdma->received_error) {
76daa3
+            GPollFD pfds[1];
76daa3
+            pfds[0].fd = rdma->comp_channel->fd;
76daa3
+            pfds[0].events = G_IO_IN | G_IO_HUP | G_IO_ERR;
76daa3
+            /* 0.1s timeout, should be fine for a 'cancel' */
76daa3
+            switch (qemu_poll_ns(pfds, 1, 100 * 1000 * 1000)) {
76daa3
+            case 1: /* fd active */
76daa3
+                return 0;
76daa3
+
76daa3
+            case 0: /* Timeout, go around again */
76daa3
+                break;
76daa3
+
76daa3
+            default: /* Error of some type -
76daa3
+                      * I don't trust errno from qemu_poll_ns
76daa3
+                     */
76daa3
+                error_report("%s: poll failed", __func__);
76daa3
+                return -EPIPE;
76daa3
+            }
76daa3
+
76daa3
+            if (migrate_get_current()->state == MIGRATION_STATUS_CANCELLING) {
76daa3
+                /* Bail out and let the cancellation happen */
76daa3
+                return -EPIPE;
76daa3
+            }
76daa3
+        }
76daa3
+    }
76daa3
+
76daa3
+    if (rdma->received_error) {
76daa3
+        return -EPIPE;
76daa3
+    }
76daa3
+    return rdma->error_state;
76daa3
+}
76daa3
+
76daa3
 /*
76daa3
  * Block until the next work request has completed.
76daa3
  *
76daa3
@@ -1511,12 +1561,9 @@ static int qemu_rdma_block_for_wrid(RDMAContext *rdma, int wrid_requested,
76daa3
     }
76daa3
 
76daa3
     while (1) {
76daa3
-        /*
76daa3
-         * Coroutine doesn't start until migration_fd_process_incoming()
76daa3
-         * so don't yield unless we know we're running inside of a coroutine.
76daa3
-         */
76daa3
-        if (rdma->migration_started_on_destination) {
76daa3
-            yield_until_fd_readable(rdma->comp_channel->fd);
76daa3
+        ret = qemu_rdma_wait_comp_channel(rdma);
76daa3
+        if (ret) {
76daa3
+            goto err_block_for_wrid;
76daa3
         }
76daa3
 
76daa3
         ret = ibv_get_cq_event(rdma->comp_channel, &cq, &cq_ctx);
76daa3
-- 
76daa3
1.8.3.1
76daa3