Skip to contentSkip to conversation

do not call monitor_resume() from migrate_fd_put_buffer() error path

PATCH In review Michael Tokarev 3 participants 7 messages Aug 3, 2011 – Aug 5, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 do not call monitor_resume() from migrate_fd_put_buffer() error path

If we do, it results in double monitor_resume() (second being called from migrate_fd_cleanup() anyway) and monitor suspend count becoming negative.

---
 migration.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
migration.c +0 -3
index 2a15b98..7ca883f 100644
@@ -330,9 +330,6 @@ ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
     if (ret == -EAGAIN) {
         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
     } else if (ret < 0) {
-        if (s->mon) {
-            monitor_resume(s->mon);
-        }
         s->state = MIG_STATE_ERROR;
         notifier_list_notify(&migration_state_notifiers, NULL);
     }
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 3 lines · On Wed, 3 Aug 2011 18:51:44 +0400 Michael Tokarev <[email protected]> wrote:

If we do, it results in double monitor_resume() (second being called from migrate_fd_cleanup() anyway) and monitor suspend count becoming negative.

Are you hitting an specific issue or did you find this by code inspection?

IIRC, I asked Marcelo to add the monitor_resume() call in the fix for e447b1a603 because the monitor wasn't being resumed in some cases. Don't remember which though, do you Marcelo?

I see two possibilities here:

1. After e447b1a603 there was some change that made the monitor_resume() call in migrate_fd_put_buffer() unnecessary

2. We're calling it in the wrong place

Taking a quick look at the code I see that migrate_fd_cleanup() doesn't seem to be called when qemu_savevm_state_iterate() fails, for example.

Quoted 21 lines
---
 migration.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
migration.c +0 -3
index 2a15b98..7ca883f 100644
@@ -330,9 +330,6 @@ ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
     if (ret == -EAGAIN) {
         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
     } else if (ret < 0) {
-        if (s->mon) {
-            monitor_resume(s->mon);
-        }
         s->state = MIG_STATE_ERROR;
         notifier_list_notify(&migration_state_notifiers, NULL);
     }
#2 lore raw

04.08.2011 23:24, Luiz Capitulino пишет:

Quoted 8 lines
Quoted 3 lines · On Wed, 3 Aug 2011 18:51:44 +0400 Michael Tokarev <[email protected]> wrote:

If we do, it results in double monitor_resume() (second being called from migrate_fd_cleanup() anyway) and monitor suspend count becoming negative.

Are you hitting an specific issue or did you find this by code inspection?

The specific case I'm talking about can trivially be triggered. Run migrate "exec:nosuchfile" and your monitor will be stuck forever.

Quoted 3 lines

IIRC, I asked Marcelo to add the monitor_resume() call in the fix for e447b1a603 because the monitor wasn't being resumed in some cases. Don't remember which though, do you Marcelo?

And this (e447b1a603) is actually exactly the same thing: when the child terminates before qemu completes sending stuff to it.

Quoted 9 lines

I see two possibilities here:

1. After e447b1a603 there was some change that made the monitor_resume() call in migrate_fd_put_buffer() unnecessary

2. We're calling it in the wrong place

Taking a quick look at the code I see that migrate_fd_cleanup() doesn't seem to be called when qemu_savevm_state_iterate() fails, for example.

Yes indeed, but that will lead to a havoc in other places, like leaving callback notifier registered even after migration gets cancelled, or keeping the file open.

Note that qemu_savevm_state_iterate() gets called from a callback routine.

What I observe here is that when the exec: process exits prematurely, qemu will continue writing stuff to pipe up till migrate_fd_put_buffer() hits error (EPIPE iirc), at which point we'll call mon->resume() twice. The same happens in case of tcp migration when the other end closes the connection.

I don't know when qemu_savevm_state_iterate() may fail, or why qemu_file_has_error() in there does not return true.

Maybe the problem here is much more severe actually, -- that's why I initially asked what it is all about, -- before offering the patch.

Thanks,

/mjt

Quoted 21 lines
Quoted 20 lines
---
 migration.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
migration.c +0 -3
index 2a15b98..7ca883f 100644
@@ -330,9 +330,6 @@ ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
     if (ret == -EAGAIN) {
         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
     } else if (ret < 0) {
-        if (s->mon) {
-            monitor_resume(s->mon);
-        }
         s->state = MIG_STATE_ERROR;
         notifier_list_notify(&migration_state_notifiers, NULL);
     }
#3 lore raw

04.08.2011 23:52, Michael Tokarev пишет:

Quoted 44 lines

04.08.2011 23:24, Luiz Capitulino пишет:

Quoted 8 lines
Quoted 3 lines · On Wed, 3 Aug 2011 18:51:44 +0400 Michael Tokarev <[email protected]> wrote:

If we do, it results in double monitor_resume() (second being called from migrate_fd_cleanup() anyway) and monitor suspend count becoming negative.

Are you hitting an specific issue or did you find this by code inspection?

The specific case I'm talking about can trivially be triggered. Run migrate "exec:nosuchfile" and your monitor will be stuck forever.

Quoted 3 lines

IIRC, I asked Marcelo to add the monitor_resume() call in the fix for e447b1a603 because the monitor wasn't being resumed in some cases. Don't remember which though, do you Marcelo?

And this (e447b1a603) is actually exactly the same thing: when the child terminates before qemu completes sending stuff to it.

Quoted 9 lines

I see two possibilities here:

1. After e447b1a603 there was some change that made the monitor_resume() call in migrate_fd_put_buffer() unnecessary

2. We're calling it in the wrong place

Taking a quick look at the code I see that migrate_fd_cleanup() doesn't seem to be called when qemu_savevm_state_iterate() fails, for example.

Yes indeed, but that will lead to a havoc in other places, like leaving callback notifier registered even after migration gets cancelled, or keeping the file open.

Note that qemu_savevm_state_iterate() gets called from a callback routine.

What I observe here is that when the exec: process exits prematurely, qemu will continue writing stuff to pipe up till migrate_fd_put_buffer() hits error (EPIPE iirc), at which point we'll call mon->resume() twice. The same happens in case of tcp migration when the other end closes the connection.

Quoted 5 lines

I don't know when qemu_savevm_state_iterate() may fail, or why qemu_file_has_error() in there does not return true.

Maybe the problem here is much more severe actually, -- that's why I initially asked what it is all about, -- before offering the patch.

This looks like a race condition: which of the two places will detect the error first.

Sometimes I see this (-monitor stdio):

(qemu) migrate "exec:nothing"
sh: nothing: not found

(qemu)

and after this, monitor is stuck as I described. But sometimes I see this:

(qemu) migrate "exec:nothing"
sh: nothing: not found
(qemu)

And it continues working. Note the extra newline. If the error gets detected sooner (like in case of exec:nothing, or even better, after removing /bin/sh so that system(3) will fail), the chance to have stuck monitor is much higher. If the error gets detected later, it will survive most likely. Or something like that anyway.

/mjt

#4 lore raw
Quoted 68 lines · Michael Tokarev

04.08.2011 23:52, Michael Tokarev пишет:

Quoted 44 lines

04.08.2011 23:24, Luiz Capitulino пишет:

Quoted 8 lines
Quoted 3 lines · On Wed, 3 Aug 2011 18:51:44 +0400 Michael Tokarev <[email protected]> wrote:

If we do, it results in double monitor_resume() (second being called from migrate_fd_cleanup() anyway) and monitor suspend count becoming negative.

Are you hitting an specific issue or did you find this by code inspection?

The specific case I'm talking about can trivially be triggered. Run migrate "exec:nosuchfile" and your monitor will be stuck forever.

Quoted 3 lines

IIRC, I asked Marcelo to add the monitor_resume() call in the fix for e447b1a603 because the monitor wasn't being resumed in some cases. Don't remember which though, do you Marcelo?

And this (e447b1a603) is actually exactly the same thing: when the child terminates before qemu completes sending stuff to it.

Quoted 9 lines

I see two possibilities here:

1. After e447b1a603 there was some change that made the monitor_resume() call in migrate_fd_put_buffer() unnecessary

2. We're calling it in the wrong place

Taking a quick look at the code I see that migrate_fd_cleanup() doesn't seem to be called when qemu_savevm_state_iterate() fails, for example.

Yes indeed, but that will lead to a havoc in other places, like leaving callback notifier registered even after migration gets cancelled, or keeping the file open.

Note that qemu_savevm_state_iterate() gets called from a callback routine.

What I observe here is that when the exec: process exits prematurely, qemu will continue writing stuff to pipe up till migrate_fd_put_buffer() hits error (EPIPE iirc), at which point we'll call mon->resume() twice. The same happens in case of tcp migration when the other end closes the connection.

Quoted 5 lines

I don't know when qemu_savevm_state_iterate() may fail, or why qemu_file_has_error() in there does not return true.

Maybe the problem here is much more severe actually, -- that's why I initially asked what it is all about, -- before offering the patch.

This looks like a race condition: which of the two places will detect the error first.

Sometimes I see this (-monitor stdio):

(qemu) migrate "exec:nothing"
sh: nothing: not found

(qemu)

and after this, monitor is stuck as I described. But sometimes I see this:

(qemu) migrate "exec:nothing"
sh: nothing: not found
(qemu)

Quoted 7 lines

And it continues working. Note the extra newline. If the error gets detected sooner (like in case of exec:nothing, or even better, after removing /bin/sh so that system(3) will fail), the chance to have stuck monitor is much higher. If the error gets detected later, it will survive most likely. Or something like that anyway.

Resume on error in migrate_fd_put_buffer raced with migrate_fd_cleanup triggered via migrate_fd_put_ready called from migrate_fd_connect.

This migration code is a horrible maze. Patch below tries to move monitor_resume a bit out of this. Please check if it works for you, then I'll send it out properly.

Thanks,
Jan

---

migration.c +9 -10
index 2a15b98..756fa62 100644
@@ -292,18 +292,17 @@ int migrate_fd_cleanup(FdMigrationState *s)
             ret = -1;
         }
         s->file = NULL;
+    } else {
+        if (s->mon) {
+            monitor_resume(s->mon);
+        }
     }
 
-    if (s->fd != -1)
+    if (s->fd != -1) {
         close(s->fd);
-
-    /* Don't resume monitor until we've flushed all of the buffers */
-    if (s->mon) {
-        monitor_resume(s->mon);
+        s->fd = -1;
     }
 
-    s->fd = -1;
-
     return ret;
 }
 
@@ -330,9 +329,6 @@ ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
     if (ret == -EAGAIN) {
         qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
     } else if (ret < 0) {
-        if (s->mon) {
-            monitor_resume(s->mon);
-        }
         s->state = MIG_STATE_ERROR;
         notifier_list_notify(&migration_state_notifiers, NULL);
     }
@@ -458,6 +454,9 @@ int migrate_fd_close(void *opaque)
 {
     FdMigrationState *s = opaque;
 
+    if (s->mon) {
+        monitor_resume(s->mon);
+    }
     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
     return s->close(s);
 }
#5 lore raw

05.08.2011 02:19, Jan Kiszka wrote:
[]

Quoted 6 lines

Resume on error in migrate_fd_put_buffer raced with migrate_fd_cleanup triggered via migrate_fd_put_ready called from migrate_fd_connect.

This migration code is a horrible maze. Patch below tries to move monitor_resume a bit out of this. Please check if it works for you, then I'll send it out properly.

Yes it's a maze.

The patch was mime-damaged, I had to apply it manually, but it wasn't difficult (since it's understandable what it does etc).

And now I can't trigger the original problem anymore, with any of my variants.

Here we go:
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:cat > /tmp/foo"
(qemu) migrate "exec:cat > /tmp/foo"
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) q

As you can see, I can hit either of the two cases - with and without extra newline, and in both cases (and in successful case too) it works correctly.

There's a difference still between the two - namely that extra newline - but that's something else and merely cosmetic.

I also verified that -incoming migration works as expected, in both failure and success cases - and indeed it works.

Speaking of the patch: shouldn't migrate_fd_close() be called from migrate_fd_cleanup(), or vise versa, or both be combined into one? In migrate_fd_cleanup(), the new logic is not clear still. New version of it:

int migrate_fd_cleanup(FdMigrationState *s)

{
    int ret = 0;

    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);

    if (s->file) {
        DPRINTF("closing file\n");
        if (qemu_fclose(s->file) != 0) {
            ret = -1;
        }
        s->file = NULL;
    } else {
        if (s->mon) {
            monitor_resume(s->mon);
        }
    }

    if (s->fd != -1) {
        close(s->fd);
        s->fd = -1;
    }

    return ret;
}

Why it's EITHER s->file OR s->mon, but not both? Shouldn't the s->mon thing be unconditional?

And the whole thing is waiting coroutine conversion badly, since the logic is indeed a maze ;)

Thank you!

/mjt

#6 lore raw
Quoted 5 lines · Michael Tokarev

05.08.2011 02:19, Jan Kiszka wrote:
[]

Quoted 3 lines

Resume on error in migrate_fd_put_buffer raced with migrate_fd_cleanup triggered via migrate_fd_put_ready called from migrate_fd_connect.

Quoted 8 lines
Quoted 3 lines

This migration code is a horrible maze. Patch below tries to move monitor_resume a bit out of this. Please check if it works for you, then I'll send it out properly.

Yes it's a maze.

The patch was mime-damaged, I had to apply it manually, but it wasn't difficult (since it's understandable what it does etc).

Sorry, likely forgot to disable line-wrapping before hitting send.

Quoted 157 lines

And now I can't trigger the original problem anymore, with any of my variants.

Here we go:
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:cat > /tmp/foo"
(qemu) migrate "exec:cat > /tmp/foo"
(qemu) migrate "exec:foo"
sh: foo: not found
(qemu) migrate "exec:foo"
sh: foo: not found

(qemu) q

As you can see, I can hit either of the two cases - with and without extra newline, and in both cases (and in successful case too) it works correctly.

There's a difference still between the two - namely that extra newline - but that's something else and merely cosmetic.

Quoted 5 lines

I also verified that -incoming migration works as expected, in both failure and success cases - and indeed it works.

Speaking of the patch: shouldn't migrate_fd_close() be called from migrate_fd_cleanup(),

Maybe. But it requires careful review what the migration close callbacks are doing and if that is always equivalent (or harmless).

Quoted 30 lines

or vise versa, or both
be combined into one? In migrate_fd_cleanup(), the new logic is not clear still. New version of it:

int migrate_fd_cleanup(FdMigrationState *s)

{
    int ret = 0;

    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);

    if (s->file) {
        DPRINTF("closing file\n");
        if (qemu_fclose(s->file) != 0) {
            ret = -1;
        }
        s->file = NULL;
    } else {
        if (s->mon) {
            monitor_resume(s->mon);
        }
    }

    if (s->fd != -1) {
        close(s->fd);
        s->fd = -1;
    }

    return ret;
}
Quoted 2 lines

Why it's EITHER s->file OR s->mon, but not both? Shouldn't the s->mon thing be unconditional?

See description of the patch I just sent out.

Quoted 3 lines

And the whole thing is waiting coroutine conversion badly, since the logic is indeed a maze ;)

Yeah, though I think this will not magically remove all windings here.

Jan