Skip to contentSkip to conversation

migration/fd: abort migration if receive POLLHUP event

PATCH In review Wang Xin 4 participants 7 messages Apr 21, 2018 – Apr 25, 2018

lore
Share & tools
lore

Wang Xin

author
#0 lore raw

PATCH 1/1 migration/fd: abort migration if receive POLLHUP event

If the fd socket peer closed shortly, ppoll may receive a POLLHUP event before the expected POLLIN event, and qemu will do nothing but goes into an infinite loop of the POLLHUP event.

So, abort the migration if we receive a POLLHUP event.

migration/fd.c +7 -1
index cd06182..5932c87 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "channel.h"
 #include "fd.h"
 #include "monitor/monitor.h"
@@ -46,6 +47,11 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
                                              GIOCondition condition,
                                              gpointer opaque)
 {
+    if (condition & G_IO_HUP) {
+        error_report("The migration peer closed, job abort");
+        exit(EXIT_FAILURE);
+    }
+
     migration_channel_process_incoming(ioc);
     object_unref(OBJECT(ioc));
     return G_SOURCE_REMOVE;
@@ -67,7 +73,7 @@ void fd_start_incoming_migration(const char *infd, Error **errp)
 
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-fd-incoming");
     qio_channel_add_watch(ioc,
-                          G_IO_IN,
+                          G_IO_IN | G_IO_HUP,
                           fd_accept_incoming_migration,
                           NULL,
                           NULL);
Signature · 1 line
2.8.1.windows.1
#1 lore raw
Quoted 5 lines · * Wang Xin ([email protected]) wrote:

If the fd socket peer closed shortly, ppoll may receive a POLLHUP event before the expected POLLIN event, and qemu will do nothing but goes into an infinite loop of the POLLHUP event.

So, abort the migration if we receive a POLLHUP event.

Hi Wang Xin,
Can you explain how you manage to trigger this case; I've not hit it.

Quoted 15 lines
migration/fd.c +1 -0
index cd06182..5932c87 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "channel.h"
 #include "fd.h"
 #include "monitor/monitor.h"

migration/fd.c static gboolean fd_accept_incoming_migration(QIOChannel *ioc, · line 54

Quoted 8 lines
migration/fd.c +5 -0
@@ -46,6 +47,11 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
                                              GIOCondition condition,
                                              gpointer opaque)
 {
+    if (condition & G_IO_HUP) {
+        error_report("The migration peer closed, job abort");
+        exit(EXIT_FAILURE);
+    }
+

OK, I wish we had a nicer way for failing; especially for the multifd/postcopy recovery worlds where one failed connection might not be fatal; but I don't see how to do that here.

migration/fd.c void fd_start_incoming_migration(const char *infd, Error **errp) · line 79

Quoted 12 lines
migration/fd.c +1 -1
     migration_channel_process_incoming(ioc);
     object_unref(OBJECT(ioc));
     return G_SOURCE_REMOVE;
@@ -67,7 +73,7 @@ void fd_start_incoming_migration(const char *infd, Error **errp)
 
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-fd-incoming");
     qio_channel_add_watch(ioc,
-                          G_IO_IN,
+                          G_IO_IN | G_IO_HUP,
                           fd_accept_incoming_migration,
                           NULL,
                           NULL);

Dave

Quoted 4 lines
Signature · 1 line
2.8.1.windows.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#2 lore raw
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Wang Xin ([email protected]) wrote:

If the fd socket peer closed shortly, ppoll may receive a POLLHUP event before the expected POLLIN event, and qemu will do nothing but goes into an infinite loop of the POLLHUP event.

So, abort the migration if we receive a POLLHUP event.

Hi Wang Xin,
Can you explain how you manage to trigger this case; I've not hit it.

Quoted 19 lines
migration/fd.c +2 -0
index cd06182..5932c87 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "channel.h"
 #include "fd.h"
 #include "monitor/monitor.h"
@@ -46,6 +47,11 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
                                              GIOCondition condition,
                                              gpointer opaque)
 {
+    if (condition & G_IO_HUP) {
Quoted 8 lines

migration/fd.c static gboolean fd_accept_incoming_migration(QIOChannel *ioc, · line 54

Quoted 4 lines
migration/fd.c +4 -0
+        error_report("The migration peer closed, job abort");
+        exit(EXIT_FAILURE);
+    }
+

OK, I wish we had a nicer way for failing; especially for the multifd/postcopy recovery worlds where one failed connection might not be fatal; but I don't see how to do that here.

This doesn't feel right to me.

We have passed in a pre-opened FD to QEMU, and we registered a watch on it to detect when there is data from the src QEMU that is available to read. Normally the src will have sent something so we'll get G_IO_IN, but you're suggesting the client has quit immediately, so we're getting G_IO_HUP due to end of file.

The migration_channel_process_incoming() method that we pass the ioc object to will be calling qio_channel_read(ioc) somewhere to try to read that data.

For QEMU to spin in infinite loop there must be code in the migration_channel_process_incoming() that is ignoring the return value of qio_channel_read() in some manner causing it to retry the read again & again I presume.

Putting this check for G_IO_HUP fixes your immediate problem scenario, but whatever code was spinning in infinite loop is still broken and I'd guess it was possible to still trigger the loop. eg by writing 1 single byte and then closing the socket.

So, IMHO this fix is wrong - we need to find the root cause and fix that, not try to avoid calling the buggy code.

Quoted 16 lines

migration/fd.c void fd_start_incoming_migration(const char *infd, Error **errp) · line 79

Quoted 13 lines
migration/fd.c +1 -1
     migration_channel_process_incoming(ioc);
     object_unref(OBJECT(ioc));
     return G_SOURCE_REMOVE;
@@ -67,7 +73,7 @@ void fd_start_incoming_migration(const char *infd, Error **errp)
 
     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-fd-incoming");
     qio_channel_add_watch(ioc,
-                          G_IO_IN,
+                          G_IO_IN | G_IO_HUP,
                           fd_accept_incoming_migration,
                           NULL,
                           NULL);

Dave

Quoted 6 lines
Quoted 4 lines
Signature · 1 line
2.8.1.windows.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 56 lines · Daniel P. Berrangé
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Wang Xin ([email protected]) wrote:

If the fd socket peer closed shortly, ppoll may receive a POLLHUP event before the expected POLLIN event, and qemu will do nothing but goes into an infinite loop of the POLLHUP event.

So, abort the migration if we receive a POLLHUP event.

Hi Wang Xin,
Can you explain how you manage to trigger this case; I've not hit it.

Quoted 19 lines
migration/fd.c +2 -0
index cd06182..5932c87 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "channel.h"
 #include "fd.h"
 #include "monitor/monitor.h"
@@ -46,6 +47,11 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
                                              GIOCondition condition,
                                              gpointer opaque)
 {
+    if (condition & G_IO_HUP) {
Quoted 8 lines

migration/fd.c static gboolean fd_accept_incoming_migration(QIOChannel *ioc, · line 54

Quoted 4 lines
migration/fd.c +4 -0
+        error_report("The migration peer closed, job abort");
+        exit(EXIT_FAILURE);
+    }
+

OK, I wish we had a nicer way for failing; especially for the multifd/postcopy recovery worlds where one failed connection might not be fatal; but I don't see how to do that here.

This doesn't feel right to me.

We have passed in a pre-opened FD to QEMU, and we registered a watch on it to detect when there is data from the src QEMU that is available to read. Normally the src will have sent something so we'll get G_IO_IN, but you're suggesting the client has quit immediately, so we're getting G_IO_HUP due to end of file.

The migration_channel_process_incoming() method that we pass the ioc object to will be calling qio_channel_read(ioc) somewhere to try to read that data.

For QEMU to spin in infinite loop there must be code in the migration_channel_process_incoming() that is ignoring the return value of qio_channel_read() in some manner causing it to retry the read again & again I presume.

Quoted 7 lines

Putting this check for G_IO_HUP fixes your immediate problem scenario, but whatever code was spinning in infinite loop is still broken and I'd guess it was possible to still trigger the loop. eg by writing 1 single byte and then closing the socket.

So, IMHO this fix is wrong - we need to find the root cause and fix that, not try to avoid calling the buggy code.

I agree. AFAIU the first read should be in qemu_loadvm_state():

    v = qemu_get_be32(f);
    if (v != QEMU_VM_FILE_MAGIC) {
        error_report("Not a migration stream");
        return -EINVAL;
    }

So I would be curious more about how that infinite loop happened.

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 67 lines · Peter Xu
Quoted 56 lines · Daniel P. Berrangé
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Wang Xin ([email protected]) wrote:

If the fd socket peer closed shortly, ppoll may receive a POLLHUP event before the expected POLLIN event, and qemu will do nothing but goes into an infinite loop of the POLLHUP event.

So, abort the migration if we receive a POLLHUP event.

Hi Wang Xin,
Can you explain how you manage to trigger this case; I've not hit it.

Quoted 19 lines
migration/fd.c +2 -0
index cd06182..5932c87 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "channel.h"
 #include "fd.h"
 #include "monitor/monitor.h"
@@ -46,6 +47,11 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
                                              GIOCondition condition,
                                              gpointer opaque)
 {
+    if (condition & G_IO_HUP) {
Quoted 8 lines

migration/fd.c static gboolean fd_accept_incoming_migration(QIOChannel *ioc, · line 54

Quoted 4 lines
migration/fd.c +4 -0
+        error_report("The migration peer closed, job abort");
+        exit(EXIT_FAILURE);
+    }
+

OK, I wish we had a nicer way for failing; especially for the multifd/postcopy recovery worlds where one failed connection might not be fatal; but I don't see how to do that here.

This doesn't feel right to me.

We have passed in a pre-opened FD to QEMU, and we registered a watch on it to detect when there is data from the src QEMU that is available to read. Normally the src will have sent something so we'll get G_IO_IN, but you're suggesting the client has quit immediately, so we're getting G_IO_HUP due to end of file.

The migration_channel_process_incoming() method that we pass the ioc object to will be calling qio_channel_read(ioc) somewhere to try to read that data.

For QEMU to spin in infinite loop there must be code in the migration_channel_process_incoming() that is ignoring the return value of qio_channel_read() in some manner causing it to retry the read again & again I presume.

Quoted 7 lines

Putting this check for G_IO_HUP fixes your immediate problem scenario, but whatever code was spinning in infinite loop is still broken and I'd guess it was possible to still trigger the loop. eg by writing 1 single byte and then closing the socket.

So, IMHO this fix is wrong - we need to find the root cause and fix that, not try to avoid calling the buggy code.

I agree. AFAIU the first read should be in qemu_loadvm_state():

Quoted 7 lines
    v = qemu_get_be32(f);
    if (v != QEMU_VM_FILE_MAGIC) {
        error_report("Not a migration stream");
        return -EINVAL;
    }

So I would be curious more about how that infinite loop happened.

Ah, wait. I just noticed that Xin mentioned about the loop already - it's an infinite loop of SIGHUP. I suppose it means that we'll just never go into fd_accept_incoming_migration() at all?

If so, I'm not sure whether we should just always watch on G_IO_HUP (and possibly G_IO_ERR too) in qio_channel_create_watch():

  GSource *ret = klass->io_create_watch(ioc, condition | G_IO_HUP | G_IO_ERR);

Otherwise I'm not sure the same loop will happen for other users of qio_channel_add_watch().

Regards,

Signature · 1 line
Peter Xu
#5 lore raw
Quoted 10 lines

-----Original Message-----
From: Peter Xu [mailto:[email protected]]
Sent: Wednesday, April 25, 2018 11:32 AM
To: Daniel P. Berrangé <[email protected]>
Cc: Dr. David Alan Gilbert <[email protected]>; wangxin (U) <[email protected]>; [email protected]; [email protected]; Gonglei (Arei) <[email protected]>
Subject: Re: [PATCH] migration/fd: abort migration if receive POLLHUP event

Quoted 12 lines
Quoted 11 lines · Peter Xu
Quoted 10 lines · Daniel P. Berrangé
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 5 lines · * Wang Xin ([email protected]) wrote:

If the fd socket peer closed shortly, ppoll may receive a POLLHUP event before the expected POLLIN event, and qemu will do nothing but goes into an infinite loop of the POLLHUP event.

So, abort the migration if we receive a POLLHUP event.

Hi Wang Xin,
Can you explain how you manage to trigger this case; I've not hit it.

Hi David,

I haven't find a way of stable reproduction.
It happened when I try to use libvirt command to restore a VM from an remote image, and the net broken unexpectedly.

virsh restore -------> libvirtd
run/ \ pass fd
Remote image <----> libvirt_iohelper <--pipe--> qemu -migration

Here are some key log:
1) virsh resotre /opt/galax/mnt/hibernating/db898219-1cde-45bf-b46e-4e439aa2d573/db898219-1cde-45bf-b46e-4e439aa2d573.img --bypass-cache

2018-04-17T09:45:04.128979+08:00|info|libvirtd[4834]|[5098]|virDomainRestoreFlags[1140]|: enter virDomainRestoreFlags from=/opt/galax/mnt/hibernating/db898219-1cde-45bf-b46e-4e439aa2d573/db898219-1cde-45bf-b46e-4e439aa2d573.img, flags=3

2)some qemu log we catched:

2018-04-17T09:45:06.111682+08:00|info|qemu[1628]|[1628]|do_qmp_dispatch[109]|: qmp_cmd_name: migrate-incoming, arguments: {"uri": "fd:37"}
2018-04-17T09:45:06.111735+08:00|info|qemu[1628]|[1628]|monitor_qapi_event_emit[423]|: {"timestamp": {"seconds": 1523929506, "microseconds": 111706}, "event": "MIGRATION", "data": {"status": "setup"}}
2018-04-17T09:45:06.112353+08:00|info|qemu[1628]|[1628]|monitor_qapi_event_emit[423]|: {"timestamp": {"seconds": 1523929506, "microseconds": 112317}, "event": "MIGRATION", "data": {"status": "active"}}

... then nothing print

3) Remote storage broken, process libvirt_iohelper do error exit. The system key messages as below:

2018-04-17T09:45:04.298085+08:00|info|systemd-machined[-]|New machine qemu-1-i-0000023F.
2018-04-17T09:45:04.298547+08:00|info|systemd[-]|Started Virtual Machine qemu-1-i-0000023F.
2018-04-17T09:45:04.298918+08:00|info|systemd[-]|Starting Virtual Machine qemu-1-i-0000023F.
2018-04-17T09:45:06.188259+08:00|info|kernel[-]|[55369.414640]  connection6:0: detected conn error (1020)  //Remote storage broken, libvirt_iohelper do error exit
2018-04-17T09:45:06.198229+08:00|info|kernel[-]|[55369.423977]  connection7:0: detected conn error (1020)
2018-04-17T09:45:06.776992+08:00|warning|iscsid[-]|Kernel reported iSCSI connection 6:0 error (1020 - ISCSI_ERR_TCP_CONN_CLOSE: TCP connection closed) state (3)
2018-04-17T09:45:06.777200+08:00|warning|iscsid[-]|Kernel reported iSCSI connection 7:0 error (1020 - ISCSI_ERR_TCP_CONN_CLOSE: TCP connection closed) state (3)

4) Qemu ppoll() exit frequently because of POLLHUP event (gdb) p ((GPollFD *)gpollfds->date)[8]
$117 = {fd = 37, events = 1, revents = 16}

Quoted 17 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
migration/fd.c +0 -0
index cd06182..5932c87 100644
@@ -15,6 +15,7 @@
  */
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

migration/fd.c static gboolean

Quoted 6 lines
migration/fd.c +1 -0
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "channel.h"
 #include "fd.h"
 #include "monitor/monitor.h"
@@ -46,6 +47,11 @@ static gboolean

fd_accept_incoming_migration(QIOChannel *ioc,

Quoted 14 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

migration/fd.c static gboolean · line 47

Quoted 1 line
migration/fd.c +0 -0
                                              GIOCondition

condition,

Quoted 1 line
Quoted 1 line
Quoted 1 line

migration/fd.c static gboolean · line 48

Quoted 1 line
migration/fd.c +0 -0
                                              gpointer

opaque)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

migration/fd.c static gboolean fd_accept_incoming_migration(QIOChannel *ioc, · line 54

Quoted 6 lines
migration/fd.c +5 -0
 {
+    if (condition & G_IO_HUP) {
+        error_report("The migration peer closed, job abort");
+        exit(EXIT_FAILURE);
+    }
+

OK, I wish we had a nicer way for failing; especially for the multifd/postcopy recovery worlds where one failed connection might not be fatal; but I don't see how to do that here.

Any suggestion? Closing the fd is the normal way to handle G_IO_HUP event.

Quoted 34 lines
Quoted 34 lines
Quoted 18 lines

This doesn't feel right to me.

We have passed in a pre-opened FD to QEMU, and we registered a watch on it to detect when there is data from the src QEMU that is available to read. Normally the src will have sent something so we'll get G_IO_IN, but you're suggesting the client has quit immediately, so we're getting G_IO_HUP due to end of file.

The migration_channel_process_incoming() method that we pass the ioc object to will be calling qio_channel_read(ioc) somewhere to try to read that data.

For QEMU to spin in infinite loop there must be code in the migration_channel_process_incoming() that is ignoring the return value of qio_channel_read() in some manner causing it to retry the read again & again I presume.

Quoted 7 lines

Putting this check for G_IO_HUP fixes your immediate problem scenario, but whatever code was spinning in infinite loop is still broken and I'd guess it was possible to still trigger the loop. eg by writing 1 single byte and then closing the socket.

So, IMHO this fix is wrong - we need to find the root cause and fix that, not try to avoid calling the buggy code.

I agree. AFAIU the first read should be in qemu_loadvm_state():

    v = qemu_get_be32(f);
    if (v != QEMU_VM_FILE_MAGIC) {
        error_report("Not a migration stream");
        return -EINVAL;
    }
Quoted 5 lines
Quoted 1 line

So I would be curious more about how that infinite loop happened.

Ah, wait. I just noticed that Xin mentioned about the loop already - it's an infinite loop of SIGHUP. I suppose it means that we'll just never go into fd_accept_incoming_migration() at all?

Yeah, that's what I want to fix.

Quoted 10 lines

If so, I'm not sure whether we should just always watch on G_IO_HUP (and possibly G_IO_ERR too) in qio_channel_create_watch():

GSource *ret = klass->io_create_watch(ioc, condition | G_IO_HUP |
G_IO_ERR);

Otherwise I'm not sure the same loop will happen for other users of qio_channel_add_watch().

Hi Daniel and Peter,

In my scenario, it's clear the client quit immediately and we never got a POLLIN event, otherwise, the watch should be unregistered when POLLIN event coming.
As Daniel said, normally G_IO_IN will be the first event, we need to find why POLLIN event never happened, I'll try it.

Quoted 3 lines

Regards,
--
Peter Xu

#6 lore raw
Quoted 24 lines · wangxin (U)
Quoted 11 lines

-----Original Message-----
From: Peter Xu [mailto:[email protected]]
Sent: Wednesday, April 25, 2018 11:32 AM
To: Daniel P. Berrangé <[email protected]>
Cc: Dr. David Alan Gilbert <[email protected]>; wangxin (U) <[email protected]>; [email protected]; [email protected]; Gonglei (Arei) <[email protected]>
Subject: Re: [PATCH] migration/fd: abort migration if receive POLLHUP event

Quoted 3 lines

Ah, wait. I just noticed that Xin mentioned about the loop already - it's an infinite loop of SIGHUP. I suppose it means that we'll just never go into fd_accept_incoming_migration() at all?

Yeah, that's what I want to fix.

Quoted 7 lines

If so, I'm not sure whether we should just always watch on G_IO_HUP (and possibly G_IO_ERR too) in qio_channel_create_watch():

GSource *ret = klass->io_create_watch(ioc, condition | G_IO_HUP |
G_IO_ERR);

Quoted 8 lines
Quoted 2 lines

Otherwise I'm not sure the same loop will happen for other users of qio_channel_add_watch().

In my scenario, it's clear the client quit immediately and we never got a POLLIN event, otherwise,
the watch should be unregistered when POLLIN event coming. As Daniel said, normally G_IO_IN will be the first event, we need to find why POLLIN event never happened, I'll try it.

Yes, I have always been under the belief that you're guaranteed to get a POLLIN event when the client closes the connection, in addition to the POLLHUP event, but it sounds like that is not happening so I guess I'm mistaken in that.

In this case, it should be sufficient to just add the G_IO_HUP|G_IO_ERR events when creating the watch - probably no need to add extra code to the fd_accept_incoming_migration() method.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|