Skip to contentSkip to conversation

libxl: Fix deadlock with pygrub

PATCH Applied Ian Jackson 5 participants 26 messages Feb 24, 2014 – Apr 4, 2014

lore
Share & tools
lore
#0 lore raw

cover libxl: Fix deadlock with pygrub

Thanks to Michael Young for reporting this, and my apologies for introducing this bug.

1/3 libxl: Fix libxl_postfork_child_noexec deadlock etc. 2/3 libxl: Hold the atfork lock while closing carefd 3/3 libxl: Fix carefd lock leak in save callout

Patch 1 is the actual bugfix. The bug is IMO clearly a blocker for 4.4 so the patch should go in if it is correct. I have checked that it seems to fix the problem for me.

Patches 2 and 3 are other theoretical locking bugs I discovered while looking for the cause of the pygrub deadlock. I haven't tried to construct test cases that might exercise these bugs; doing so would be quite difficult.

The atfork carefd race (patch 2) might be relevant to callers which are multithreaded and also call libxl_postfork_child_noexec. libvirt does not make any such call. xl is single-threaded. I haven't investigated other toolstacks, but the race is theoretical rather than observed.

The carefd lock leak (patch 3) is obvious but theoretical except in deeply pathological situations. I don't think it's worth adding risk to 4.4 to fix it.

So I think patch 1 should go into 4.4.0 after review. 2-3 should wait for 4.4.1 (coming via unstable in the usual way).

#1 lore raw

PATCH 1/3 libxl: Fix libxl_postfork_child_noexec deadlock etc.

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

Also, the use of sigchld_user_remove in libxl_postfork_child_noexec is not correct with SIGCHLD sharing. libxl_postfork_child_noexec is documented to suffice if called only on one ctx. So deregistering the ctx it's called on is not sufficient. Instead, we need a new approach which discards the whole sigchld_user list and unconditionally removes our SIGCHLD handler if we had one.

Prompted by this, clarify the semantics of libxl_postfork_child_noexec. Specifically, expand on the meaning of "quickly" by explaining what operations are not permitted; and document the fact that the function doesn't reclaim the resources in the ctxs.

And add a comment in libxl_postfork_child_noexec explaining the internal concurrency situation.

This is an important bugfix. IMO the bug is a blocker for Xen 4.4.

---
 tools/libxl/libxl_event.h |   16 ++++++++++++++++
 tools/libxl/libxl_fork.c  |   44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)
tools/libxl/libxl_event.h +16 -0
index ca43cb9..b5db83c 100644
@@ -601,6 +601,22 @@ void libxl_childproc_sigchld_occurred(libxl_ctx *ctx)
  * this all previously existing libxl_ctx's are invalidated and
  * must not be used - or even freed.  It is harmless to call this
  * postfork function and then exec anyway.
+ *
+ * Until libxl_postfork_child_noexec has returned:
+ *  - No other libxl calls may be made.
+ *  - If any libxl ctx was configured handle the process's SIGCHLD,
+ *    the child may not create further (grand)child processes, nor
+ *    manipulate SIGCHLD.
+ *
+ * libxl_postfork_child_noexec may not reclaim all the resources
+ * associated with the libxl ctx.  This includes but is not limited
+ * to: ordinary memory; files on disk and in /var/run; file
+ * descriptors; memory mapped into the process from domains being
+ * managed (grant maps); Xen event channels.  Use of libxl in
+ * processes which fork long-lived children is not recommended for
+ * this reason.  libxl_postfork_child_noexec is provided so that
+ * an application can make further libxl calls in a child which
+ * is going to exec or exit soon.
  */
 void libxl_postfork_child_noexec(libxl_ctx *ctx);
 
tools/libxl/libxl_fork.c +43 -1

62 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 8 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

"behaviour".

Michael reported this earlier on -rc2 as well but it fell through the cracks because I failed to properly appreciate the severity. Sorry.

Quoted 13 lines

Also, the use of sigchld_user_remove in libxl_postfork_child_noexec is not correct with SIGCHLD sharing. libxl_postfork_child_noexec is documented to suffice if called only on one ctx. So deregistering the ctx it's called on is not sufficient. Instead, we need a new approach which discards the whole sigchld_user list and unconditionally removes our SIGCHLD handler if we had one.

Prompted by this, clarify the semantics of libxl_postfork_child_noexec. Specifically, expand on the meaning of "quickly" by explaining what operations are not permitted; and document the fact that the function doesn't reclaim the resources in the ctxs.

Quoted 7 lines

And add a comment in libxl_postfork_child_noexec explaining the internal concurrency situation.

This is an important bugfix. IMO the bug is a blocker for Xen 4.4.

Quoted 5 lines
CC George Dunlap <[email protected]>
---
 tools/libxl/libxl_event.h |   16 ++++++++++++++++
 tools/libxl/libxl_fork.c  |   44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)

Impressive considering the real meat is -1/+6 ;-)

Not that I'm going to complain about lots of docs!

Quoted 20 lines
tools/libxl/libxl_fork.c +11 -1
@@ -134,7 +150,33 @@ void libxl_postfork_child_noexec(libxl_ctx *ctx)
     }
     LIBXL_LIST_INIT(&carefds);
 
-    sigchld_user_remove(ctx);
+    if (sigchld_installed) {
+        defer_sigchld();
+
+        LIBXL_LIST_INIT(&sigchld_users);
+        /* After this the ->sigchld_user_registered entries in the
+         * now-obsolete contexts may be lies.  But that's OK because
+         * no-one will look at them. */
+
+        release_sigchld();
+        sigchld_removehandler_core();
+    }
 
     atfork_unlock();
 }
#3 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 6 lines · Ian Campbell
Quoted 5 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

Quoted 8 lines
Quoted 3 lines

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

"behaviour".

Michael reported this earlier on -rc2 as well but it fell through the cracks because I failed to properly appreciate the severity. Sorry.

No that was a different problem (you get some warnings and a messed up terminal sesssion on dom0 after using pygrub). This is a new problem, probably introduced in rc4 (which I didn't get around to testing).

Michael Young

#4 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 12 lines · M A Young
Quoted 10 lines · Ian Campbell
Quoted 8 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

Quoted 8 lines
Quoted 4 lines

"behaviour".

Michael reported this earlier on -rc2 as well but it fell through the cracks because I failed to properly appreciate the severity. Sorry.

No that was a different problem (you get some warnings and a messed up terminal sesssion on dom0 after using pygrub). This is a new problem, probably introduced in rc4 (which I didn't get around to testing).

OK, thanks for clarifying!

Ian.

#5 lore raw

PATCH 1/1 tools/console: reset tty when xenconsole fails

In reply to M A Young

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because something writes the value "" to the tty xenstore key when using xenconsole. The cause of this is not yet known, but after this patch it just results in a harmless error message.

---
 tools/console/client/main.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +9 -2
index 3242008..add3313 100644
@@ -258,6 +258,13 @@ typedef enum {
        CONSOLE_SERIAL,
 } console_type;
 
+static struct termios stdin_old_attr;
+
+static void restore_term_stdin(void)
+{
+	restore_term(STDIN_FILENO, &stdin_old_attr);
+}
+
 int main(int argc, char **argv)
 {
 	struct termios attr;
@@ -384,9 +391,9 @@ int main(int argc, char **argv)
 	}
 
 	init_term(spty, &attr);
-	init_term(STDIN_FILENO, &attr);
+	init_term(STDIN_FILENO, &stdin_old_attr);
+        atexit(restore_term_stdin); /* if this fails, oh dear */
 	console_loop(spty, xs, path);
-	restore_term(STDIN_FILENO, &attr);
 
 	free(path);
 	free(dom_path);
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#6 lore raw

Re: [PATCH] tools/console: reset tty when xenconsole fails

Quoted 36 lines · Ian Jackson

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because something writes the value "" to the tty xenstore key when using xenconsole. The cause of this is not yet known, but after this patch it just results in a harmless error message.

---
 tools/console/client/main.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +7 -0
index 3242008..add3313 100644
@@ -258,6 +258,13 @@ typedef enum {
        CONSOLE_SERIAL,
 } console_type;
 
+static struct termios stdin_old_attr;
+
+static void restore_term_stdin(void)
+{
+	restore_term(STDIN_FILENO, &stdin_old_attr);
+}
+
 int main(int argc, char **argv)
 {
 	struct termios attr;

tools/console/client/main.c int main(int argc, char **argv) · line 395

Quoted 6 lines
tools/console/client/main.c +2 -1
@@ -384,9 +391,9 @@ int main(int argc, char **argv)
 	}
 
 	init_term(spty, &attr);
-	init_term(STDIN_FILENO, &attr);
+	init_term(STDIN_FILENO, &stdin_old_attr);
+        atexit(restore_term_stdin); /* if this fails, oh dear */

Looks like some sort of tab/space damage, but otherwise:

Quoted 5 lines
tools/console/client/main.c +0 -1
 	console_loop(spty, xs, path);
-	restore_term(STDIN_FILENO, &attr);
 
 	free(path);
 	free(dom_path);
#7 lore raw

Re: [PATCH] tools/console: reset tty when xenconsole fails

In reply to Ian Jackson
Quoted 11 lines · Ian Jackson

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because something writes the value "" to the tty xenstore key when using xenconsole. The cause of this is not yet known, but after this patch it just results in a harmless error message.

I'm pretty sure this has been here for quite a while -- possibly since 4.3. I've been working around it for some time, at any rate. :-) So I think at this point we should save this for 4.4.1.

-George

#8 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

In reply to Ian Jackson
Quoted 25 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

Also, the use of sigchld_user_remove in libxl_postfork_child_noexec is not correct with SIGCHLD sharing. libxl_postfork_child_noexec is documented to suffice if called only on one ctx. So deregistering the ctx it's called on is not sufficient. Instead, we need a new approach which discards the whole sigchld_user list and unconditionally removes our SIGCHLD handler if we had one.

Prompted by this, clarify the semantics of libxl_postfork_child_noexec. Specifically, expand on the meaning of "quickly" by explaining what operations are not permitted; and document the fact that the function doesn't reclaim the resources in the ctxs.

And add a comment in libxl_postfork_child_noexec explaining the internal concurrency situation.

Quoted 6 lines

This is an important bugfix. IMO the bug is a blocker for Xen 4.4.

So it looks like this path gets called from a number of other places in xl:

libxl_postfork_child_noexec() is called by xl.c:postfork().

postfork() is called in xl_cmdimpl.c by autoconnect_vncviewer(), autoconnect_console(), and do_daemonize().

do_daemonize() is called during "xl create", and "xl devd".

Was this deadlock not triggered for those, or was it triggered and nobody noticed?

-George

#9 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 39 lines · George Dunlap
Quoted 26 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

Also, the use of sigchld_user_remove in libxl_postfork_child_noexec is not correct with SIGCHLD sharing. libxl_postfork_child_noexec is documented to suffice if called only on one ctx. So deregistering the ctx it's called on is not sufficient. Instead, we need a new approach which discards the whole sigchld_user list and unconditionally removes our SIGCHLD handler if we had one.

Prompted by this, clarify the semantics of libxl_postfork_child_noexec. Specifically, expand on the meaning of "quickly" by explaining what operations are not permitted; and document the fact that the function doesn't reclaim the resources in the ctxs.

And add a comment in libxl_postfork_child_noexec explaining the internal concurrency situation.

Quoted 6 lines

This is an important bugfix. IMO the bug is a blocker for Xen 4.4.

So it looks like this path gets called from a number of other places in xl:

libxl_postfork_child_noexec() is called by xl.c:postfork().

Quoted 7 lines

postfork() is called in xl_cmdimpl.c by autoconnect_vncviewer(), autoconnect_console(), and do_daemonize().

do_daemonize() is called during "xl create", and "xl devd".

Was this deadlock not triggered for those, or was it triggered and nobody noticed?

In any case, I do think we need to fix this; the main question is, do we need to delay the release a bit further to make sure it gets sufficient testing?

-George

#10 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 44 lines · George Dunlap
Quoted 42 lines · George Dunlap
Quoted 26 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

Also, the use of sigchld_user_remove in libxl_postfork_child_noexec is not correct with SIGCHLD sharing. libxl_postfork_child_noexec is documented to suffice if called only on one ctx. So deregistering the ctx it's called on is not sufficient. Instead, we need a new approach which discards the whole sigchld_user list and unconditionally removes our SIGCHLD handler if we had one.

Prompted by this, clarify the semantics of libxl_postfork_child_noexec. Specifically, expand on the meaning of "quickly" by explaining what operations are not permitted; and document the fact that the function doesn't reclaim the resources in the ctxs.

And add a comment in libxl_postfork_child_noexec explaining the internal concurrency situation.

Quoted 6 lines

This is an important bugfix. IMO the bug is a blocker for Xen 4.4.

So it looks like this path gets called from a number of other places in xl:

libxl_postfork_child_noexec() is called by xl.c:postfork().

postfork() is called in xl_cmdimpl.c by autoconnect_vncviewer(), autoconnect_console(), and do_daemonize().

Quoted 8 lines
Quoted 4 lines

do_daemonize() is called during "xl create", and "xl devd".

Was this deadlock not triggered for those, or was it triggered and nobody noticed?

In any case, I do think we need to fix this; the main question is, do we need to delay the release a bit further to make sure it gets sufficient testing?

Also, it would be nice to get a Tested-by: from someone using it with libvirt (before the release at least, if not before the check-in).

Jim / Dario?

-George

#11 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 57 lines · George Dunlap
Quoted 44 lines · George Dunlap
Quoted 41 lines · George Dunlap
Quoted 25 lines · Ian Jackson

libxl_postfork_child_noexec would nestedly reaquire the non-recursive "no_forking" mutex: atfork_lock uses it, as does sigchld_user_remove. The result on Linux is that the process always deadlocks before returning from this function.

This is used by xl's console child. So, the ultimate effect is that xl with pygrub does not manage to connect to the pygrub console. This beahviour was reported by Michael Young in Xen 4.4.0 RC5.

Also, the use of sigchld_user_remove in libxl_postfork_child_noexec is not correct with SIGCHLD sharing. libxl_postfork_child_noexec is documented to suffice if called only on one ctx. So deregistering the ctx it's called on is not sufficient. Instead, we need a new approach which discards the whole sigchld_user list and unconditionally removes our SIGCHLD handler if we had one.

Prompted by this, clarify the semantics of libxl_postfork_child_noexec. Specifically, expand on the meaning of "quickly" by explaining what operations are not permitted; and document the fact that the function doesn't reclaim the resources in the ctxs.

And add a comment in libxl_postfork_child_noexec explaining the internal concurrency situation.

Quoted 7 lines

This is an important bugfix. IMO the bug is a blocker for Xen 4.4.

So it looks like this path gets called from a number of other places in xl:

libxl_postfork_child_noexec() is called by xl.c:postfork().

postfork() is called in xl_cmdimpl.c by autoconnect_vncviewer(), autoconnect_console(), and do_daemonize().

Quoted 9 lines
Quoted 5 lines

do_daemonize() is called during "xl create", and "xl devd".

Was this deadlock not triggered for those, or was it triggered and nobody noticed?

In any case, I do think we need to fix this; the main question is, do we need to delay the release a bit further to make sure it gets sufficient testing?

Quoted 5 lines

Also, it would be nice to get a Tested-by: from someone using it with libvirt (before the release at least, if not before the check-in).

Jim / Dario?

I'll update my test system to rc6 tomorrow and restart my tests.

FYI, the tests were running over the weekend on rc5 + libvirt 1.2.2 rc1. Over 25,000 domains started, shutdown, created, saved, restored, etc. with no problems noted.

Regards,
Jim

#12 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Jim Fehlig writes ("Re: [Xen-devel] [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc."):

Quoted 1 line

I'll update my test system to rc6 tomorrow and restart my tests.

Thanks. To be honest, I'm very confident that the change between rc5 and rc6 won't break libvirt because the changes to libxl are entirely confined to libxl_postfork_child_noexec which is not called by libvirt.

Quoted 3 lines

FYI, the tests were running over the weekend on rc5 + libvirt 1.2.2 rc1. Over 25,000 domains started, shutdown, created, saved, restored, etc. with no problems noted.

Great. Thanks a lot for your testing and bug reports (and sorry for the bugs).

Ian.

Tested-by
#13 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

In reply to Jim Fehlig
Quoted 11 lines · Jim Fehlig
Quoted 6 lines · George Dunlap

Also, it would be nice to get a Tested-by: from someone using it with libvirt (before the release at least, if not before the check-in).

Jim / Dario?

I'll update my test system to rc6 tomorrow and restart my tests.

FYI, the libvirt-based tests have been running for 18 hours with no problems. I need to stop them now to use the machine for other purposes, but will start a weekend run tomorrow. I know this patch is already committed, but nonetheless

Regards,
Jim

#14 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 9 lines · Jim Fehlig
Quoted 8 lines · Jim Fehlig
Quoted 6 lines · George Dunlap

Also, it would be nice to get a Tested-by: from someone using it with libvirt (before the release at least, if not before the check-in).

Jim / Dario?

Quoted 8 lines
Quoted 2 lines

I'll update my test system to rc6 tomorrow and restart my tests.

FYI, the libvirt-based tests have been running for 18 hours with no problems. I need to stop them now to use the machine for other purposes, but will start a weekend run tomorrow. I know this patch is already committed, but nonetheless

Awesome, thanks, Jim.

-George

#15 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

In reply to George Dunlap

George Dunlap writes ("Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc."):

Quoted 3 lines

So it looks like this path gets called from a number of other places in xl:

libxl_postfork_child_noexec() is called by xl.c:postfork().

In the old code the deadlock only happens when ctx->sigchld_user_registered (for the ctx passed to libxl_postfork_child_noexec).

Because xl uses .chldowner = libxl_sigchld_owner_libxl rather than libxl_sigchld_owner_libxl_always, sigchld_user_registered is only true when libxl actually has a child process.

In the single-threaded synchronous model used by xl for its long_running operations (ie always passing ao_how==0), libxl only has children _during_ the libxl call, when libxl calls back to the application with a progress callback.

That's what happens in the pygrub case: xl needs to restart the console viewer.

Quoted 2 lines

postfork() is called in xl_cmdimpl.c by autoconnect_vncviewer(), autoconnect_console(), and do_daemonize().

osstest doesn't use `xl create -c' so they weren't tested anyway. But it also works just fine in the non-pygrub case.

Likewise `xl create -V' (autoconnect_vncviewer) works too.

Quoted 1 line

do_daemonize() is called during "xl create", and "xl devd".

These work without the bugfix.

Quoted 2 lines

Was this deadlock not triggered for those, or was it triggered and nobody noticed?

Mostly, the former.

Ian.

#16 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

Quoted 13 lines · Ian Jackson

George Dunlap writes ("Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc."):

Quoted 3 lines

So it looks like this path gets called from a number of other places in xl:

libxl_postfork_child_noexec() is called by xl.c:postfork().

In the old code the deadlock only happens when ctx->sigchld_user_registered (for the ctx passed to libxl_postfork_child_noexec).

Because xl uses .chldowner = libxl_sigchld_owner_libxl rather than libxl_sigchld_owner_libxl_always, sigchld_user_registered is only true when libxl actually has a child process.

Quoted 7 lines

In the single-threaded synchronous model used by xl for its long_running operations (ie always passing ao_how==0), libxl only has children _during_ the libxl call, when libxl calls back to the application with a progress callback.

That's what happens in the pygrub case: xl needs to restart the console viewer.

Right. Having chatted f2f with Ian, and looked through the old and new code, I'm reasonably convinced that only the broken case can actually be affected.

So:

We're also going to do an RC6 -- please do a smoke-test if you get a chance. (I'll send out a separate e-mail to the list as well.)

-George

#17 lore raw

Re: [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc.

George Dunlap writes ("Re: [Xen-devel] [PATCH 1/3] libxl: Fix libxl_postfork_child_noexec deadlock etc."):

Quoted 2 lines

We're also going to do an RC6 -- please do a smoke-test if you get a chance. (I'll send out a separate e-mail to the list as well.)

Stefano, I wrote on IRC:

16:35 <Diziet> stefano_s: Can you make a tag for 4.4.0-rc6 at the same
point as -rc5 in qemu-xen-upstream-4.4-testing.git (or whatever it's called) please ?
16:36 <Diziet> (and push the tag to both the master and staging/ of that tree)

It seems you're busy on a conference call so I'm going to do this myself.

Thanks,
Ian.

#18 lore raw

PATCH 2/3 libxl: Hold the atfork lock while closing carefd

In reply to Ian Jackson

This avoids the process being forked while a carefd is recorded in the list but the actual fd has been closed. If that happened, a subsequent libxl_postfork_child_noexec would attempt to close the fd again. If we are lucky that results in a harmless warning; but if we are unlucky the fd number has been reused and we close an unrelated fd.

This race has not been observed anywhere as far as we are aware.

CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>
---
 tools/libxl/libxl_fork.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_fork.c +1 -1
index 8421296..fa15095 100644
@@ -184,9 +184,9 @@ void libxl_postfork_child_noexec(libxl_ctx *ctx)
 int libxl__carefd_close(libxl__carefd *cf)
 {
     if (!cf) return 0;
+    atfork_lock();
     int r = cf->fd < 0 ? 0 : close(cf->fd);
     int esave = errno;
-    atfork_lock();
     LIBXL_LIST_REMOVE(cf, entry);
     atfork_unlock();
     free(cf);
Signature · 1 line
1.7.10.4
Acked-by on v1 2/3
#19 lore raw

Re: [PATCH 2/3] libxl: Hold the atfork lock while closing carefd

Quoted 10 lines · Ian Jackson

This avoids the process being forked while a carefd is recorded in the list but the actual fd has been closed. If that happened, a subsequent libxl_postfork_child_noexec would attempt to close the fd again. If we are lucky that results in a harmless warning; but if we are unlucky the fd number has been reused and we close an unrelated fd.

This race has not been observed anywhere as far as we are aware.

Quoted 20 lines
CC George Dunlap <[email protected]>
---
 tools/libxl/libxl_fork.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_fork.c +1 -1
index 8421296..fa15095 100644
@@ -184,9 +184,9 @@ void libxl_postfork_child_noexec(libxl_ctx *ctx)
 int libxl__carefd_close(libxl__carefd *cf)
 {
     if (!cf) return 0;
+    atfork_lock();
     int r = cf->fd < 0 ? 0 : close(cf->fd);
     int esave = errno;
-    atfork_lock();
     LIBXL_LIST_REMOVE(cf, entry);
     atfork_unlock();
     free(cf);
#20 lore raw

PATCH 3/3 libxl: Fix carefd lock leak in save callout

In reply to Ian Jackson

If libxl_pipe fails we leave the carefd locked, which translates to the atfork lock remaining held. This would probably cause the process to deadlock shortly afterwards.

Of course libxl_pipe is very unlikely to fail unless things are already going very badly. This bug has not been observed anywhere as far as we are aware.

CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>
---
 tools/libxl/libxl_save_callout.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/libxl/libxl_save_callout.c +5 -1
index 6e45b2f..e3bda8f 100644
@@ -185,7 +185,11 @@ static void run_helper(libxl__egc *egc, libxl__save_helper_state *shs,
     for (childfd=0; childfd<2; childfd++) {
         /* Setting up the pipe for the child's fd childfd */
         int fds[2];
-        if (libxl_pipe(CTX,fds)) { rc = ERROR_FAIL; goto out; }
+        if (libxl_pipe(CTX,fds)) {
+            rc = ERROR_FAIL;
+            libxl__carefd_unlock();
+            goto out;
+        }
         int childs_end = childfd==0 ? 0 /*read*/  : 1 /*write*/;
         int our_end    = childfd==0 ? 1 /*write*/ : 0 /*read*/;
         childs_pipes[childfd] = libxl__carefd_record(CTX, fds[childs_end]);
Signature · 1 line
1.7.10.4
Acked-by on v1 3/3
#21 lore raw

Re: [PATCH 3/3] libxl: Fix carefd lock leak in save callout

Quoted 9 lines · Ian Jackson

If libxl_pipe fails we leave the carefd locked, which translates to the atfork lock remaining held. This would probably cause the process to deadlock shortly afterwards.

Of course libxl_pipe is very unlikely to fail unless things are already going very badly. This bug has not been observed anywhere as far as we are aware.

Quoted 22 lines
CC George Dunlap <[email protected]>
---
 tools/libxl/libxl_save_callout.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/libxl/libxl_save_callout.c +5 -1
index 6e45b2f..e3bda8f 100644
@@ -185,7 +185,11 @@ static void run_helper(libxl__egc *egc, libxl__save_helper_state *shs,
     for (childfd=0; childfd<2; childfd++) {
         /* Setting up the pipe for the child's fd childfd */
         int fds[2];
-        if (libxl_pipe(CTX,fds)) { rc = ERROR_FAIL; goto out; }
+        if (libxl_pipe(CTX,fds)) {
+            rc = ERROR_FAIL;
+            libxl__carefd_unlock();
+            goto out;
+        }
         int childs_end = childfd==0 ? 0 /*read*/  : 1 /*write*/;
         int our_end    = childfd==0 ? 1 /*write*/ : 0 /*read*/;
         childs_pipes[childfd] = libxl__carefd_record(CTX, fds[childs_end]);
#22 lore raw
In reply to Ian Jackson
Quoted 2 lines · Ian Jackson

So I think patch 1 should go into 4.4.0 after review. 2-3 should wait for 4.4.1 (coming via unstable in the usual way).

I agree with your logic and your conclusion.

Ian.

#23 lore raw

Ian Campbell writes ("Re: [PATCH 0/3] libxl: Fix deadlock with pygrub"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

So I think patch 1 should go into 4.4.0 after review. 2-3 should wait for 4.4.1 (coming via unstable in the usual way).

I agree with your logic and your conclusion.

I have now backported these two changes
libxl: Hold the atfork lock while closing carefd
libxl: Fix carefd lock leak in save callout
to 4.4, 4.3 and 4.2. (A trivial cherry pick in each case.)

They are not applicable to 4.1.

Thanks,
Ian.

#25 lore raw

Ian Campbell writes ("Re: [PATCH 0/3] libxl: Fix deadlock with pygrub"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

2-3 should wait for 4.4.1 (coming via unstable in the usual way).

Applied to staging.

Thanks. I have added them to my backport list.

Ian.