Commit d7cd3694 authored by Stefan Hajnoczi's avatar Stefan Hajnoczi Committed by Anthony Liguori
Browse files

migration: restrict scope of incoming fd read handler

The incoming migration is processed in a coroutine and uses an fd read
handler to enter the yielded coroutine when data becomes available.

The read handler was set too broadly, so that spurious coroutine entries
were be triggered if other coroutine users yielded (like the block
layer's bdrv_write() function).

Install the fd read only only when yielding for more data to become
available.  This prevents spurious coroutine entries which break code
that assumes only a specific set of places can re-enter the coroutine.

This patch fixes crashes in block/raw-posix.c that are triggered with
"migrate -b" when qiov becomes a dangling pointer due to a spurious
coroutine entry that frees qiov early.
Signed-off-by: default avatarStefan Hajnoczi <>
Signed-off-by: default avatarAnthony Liguori <>
parent d0bce760
......@@ -95,7 +95,6 @@ static void process_incoming_migration_co(void *opaque)
int ret;
ret = qemu_loadvm_state(f);
qemu_set_fd_handler(qemu_get_fd(f), NULL, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "load of migration failed\n");
......@@ -115,12 +114,6 @@ static void process_incoming_migration_co(void *opaque)
static void enter_migration_coroutine(void *opaque)
Coroutine *co = opaque;
qemu_coroutine_enter(co, NULL);
void process_incoming_migration(QEMUFile *f)
Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
......@@ -128,7 +121,6 @@ void process_incoming_migration(QEMUFile *f)
assert(fd != -1);
qemu_set_fd_handler(fd, enter_migration_coroutine, NULL, co);
qemu_coroutine_enter(co, f);
......@@ -140,6 +140,34 @@ typedef struct QEMUFileSocket
QEMUFile *file;
} QEMUFileSocket;
typedef struct {
Coroutine *co;
int fd;
} FDYieldUntilData;
static void fd_coroutine_enter(void *opaque)
FDYieldUntilData *data = opaque;
qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
qemu_coroutine_enter(data->co, NULL);
* Yield until a file descriptor becomes readable
* Note that this function clobbers the handlers for the file descriptor.
static void coroutine_fn yield_until_fd_readable(int fd)
FDYieldUntilData data;
assert(qemu_in_coroutine()); = qemu_coroutine_self();
data.fd = fd;
qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
static int socket_get_fd(void *opaque)
QEMUFileSocket *s = opaque;
......@@ -158,8 +186,7 @@ static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
if (socket_error() == EAGAIN) {
} else if (socket_error() != EINTR) {
......@@ -205,8 +232,7 @@ static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
if (errno == EAGAIN) {
} else if (errno != EINTR) {
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment