Skip to contentSkip to conversation

xl: track child processes for the benefit of libxl

PATCH In review Ian Jackson 2 participants 8 messages May 18, 2012 – May 29, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xl: track child processes for the benefit of libxl

Each time xl forks, it needs to record the pid, so that its exit status can be preserved if it happens that libxl's event loop reaped it. Consequently we also have a new wrapper for waitpid, which in that case returns the previously-reaped status.

When we get a console ready callback from libxl, check to see if we have spawned but not reaped a previous console client, and if so reap it now. (This is necessary to prevent improper use of the xlchild struct, but has the happy consequence of checking the exit status of the first console client in the pygrub case.)

Refactor the two calls to libxl_ctx_alloc into a new function xl_ctx_alloc which also sets the child reaped handler callback.

All of this has the effect of suppressing a message
unknown child [nnnn] unexpected exited status zero
which would sometimes (depending on a race) appear with `xl create -c' and pygrub.

Changes since v1:
* Replace macro-based iteration over children with enum-based version.
* Add comment explaining that xl_waitpid clears the xlchild for reuse.
* Add comment re clearing other children in xl_fork.
* Fix incorrect comment re pid==-1 in xlchild.
* Change local variable "child" in xl_waitpid to "ch" for consistency.
* Introduce xl_child_pid to hide .pid somewhat more.
* Introduce migration_child local variable in migration_child_report to simplify error logging.

---
 tools/libxl/xl.c         |   86 +++++++++++++++++++++++++++++++++++++++-------
 tools/libxl/xl.h         |   30 +++++++++++++++-
 tools/libxl/xl_cmdimpl.c |   63 +++++++++++++++++++---------------
 3 files changed, 136 insertions(+), 43 deletions(-)
tools/libxl/xl.c +73 -13
index 750a61c..5ab9c06 100644
@@ -102,22 +102,85 @@ void postfork(void)
     libxl_postfork_child_noexec(ctx); /* in case we don't exit/exec */
     ctx = 0;
 
-    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, (xentoollog_logger*)logger)) {
-        fprintf(stderr, "cannot reinit xl context after fork\n");
-        exit(-1);
-    }
+    xl_ctx_alloc();
 }
 
-pid_t xl_fork(libxl_ctx *ctx) {
-    pid_t pid;
+pid_t xl_fork(xlchildnum child) {
+    xlchild *ch = &children[child];
+    int i;
+
+    assert(!ch->pid);
+    ch->reaped = 0;
 
-    pid = fork();
-    if (pid == -1) {
+    ch->pid = fork();
+    if (ch->pid == -1) {
         perror("fork failed");
         exit(-1);
     }
 
-    return pid;
+    if (!ch->pid) {
+        /* We are in the child now.  So all these children are not ours. */
+        for (i=0; i<child_max; i++)
+            children[i].pid = 0;
+    }
+
+    return ch->pid;
+}
+
+pid_t xl_waitpid(xlchildnum child, int *status, int flags)
+{
+    xlchild *ch = &children[child];
+    pid_t got = ch->pid;
+    assert(got);
+    if (ch->reaped) {
+        *status = ch->status;
+        ch->pid = 0;
+        return got;
+    }
+    for (;;) {
+        got = waitpid(ch->pid, status, flags);
+        if (got < 0 && errno == EINTR) continue;
+        if (got > 0) {
+            assert(got == ch->pid);
+            ch->pid = 0;
+        }
+        return got;
+    }
+}
+
+int xl_child_pid(xlchildnum child)
+{
+    xlchild *ch = &children[child];
+    return ch->pid;
+}
+
+static int xl_reaped_callback(pid_t got, int status, void *user)
+{
+    int i;
+    assert(got);
+    for (i=0; i<child_max; i++) {
+        xlchild *ch = &children[i];
+        if (ch->pid == got) {
+            ch->reaped = 1;
+            ch->status = status;
+            return 1;
+        }
+    }
+    return 0;
+}
+
+static const libxl_childproc_hooks childproc_hooks = {
+    .chldowner = libxl_sigchld_owner_libxl,
+    .reaped_callback = xl_reaped_callback,
+};
+
+void xl_ctx_alloc(void) {
+    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, (xentoollog_logger*)logger)) {
+        fprintf(stderr, "cannot init xl context\n");
+        exit(1);
+    }
+
+    libxl_childproc_setmode(ctx, &childproc_hooks, 0);
 }
 
 int main(int argc, char **argv)
@@ -159,10 +222,7 @@ int main(int argc, char **argv)
     logger = xtl_createlogger_stdiostream(stderr, minmsglevel,  0);
     if (!logger) exit(1);
 
-    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, (xentoollog_logger*)logger)) {
-        fprintf(stderr, "cannot init xl context\n");
-        exit(1);
-    }
+    xl_ctx_alloc();
 
     /* Read global config file options */
     ret = asprintf(&config_file, "%s/xl.conf", libxl_xen_config_dir_path());
tools/libxl/xl.h +28 -2
index b7eacaa..2af9428 100644
@@ -15,6 +15,8 @@
 #ifndef XL_H
 #define XL_H
 
+#include <assert.h>
+
 #include "xentoollog.h"
 
 struct cmd_spec {
@@ -108,8 +110,32 @@ struct cmd_spec *cmdtable_lookup(const char *s);
 
 extern libxl_ctx *ctx;
 extern xentoollog_logger_stdiostream *logger;
-pid_t xl_fork(libxl_ctx *ctx); /* like fork, but prints and dies if it fails */
-void postfork(void);
+
+void xl_ctx_alloc(void);
+
+/* child processes */
+
+typedef struct {
+    /* every struct like this must be in XLCHILD_LIST */
+    pid_t pid; /* 0: not in use */
+    int reaped; /* valid iff pid!=0 */
+    int status; /* valid iff reaped */
+} xlchild;
+
+typedef enum {
+    child_console, child_waitdaemon, child_migration,
+    child_max
+} xlchildnum;
+
+extern xlchild children[child_max];
+
+pid_t xl_fork(xlchildnum); /* like fork, but prints and dies if it fails */
+void postfork(void); /* needed only if we aren't going to exec right away */
+
+/* Handles EINTR.  Clears out the xlchild so it can be reused. */
+pid_t xl_waitpid(xlchildnum, int *status, int flags);
+
+int xl_child_pid(xlchildnum); /* returns 0 if child struct is not in use */
 
 /* global options */
 extern int autoballoon;
tools/libxl/xl_cmdimpl.c +35 -28

177 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#1 lore raw

Ian Jackson writes ("[PATCH v2] xl: track child processes for the benefit of libxl"):

Quoted 4 lines

Each time xl forks, it needs to record the pid, so that its exit status can be preserved if it happens that libxl's event loop reaped it. Consequently we also have a new wrapper for waitpid, which in that case returns the previously-reaped status.

I have tested this on top of current tip ("x86/mem_sharing: Allow paging-in pages to be replaced by shared ones") plus my event debugging series and:

Loading new save file incoming migration stream (new xl fmt info 0x0/0x0/853)
Savefile contains xl domain config
xc: Saving memory: iter 0 (last sent 0 skipped 0): 131072/131072 100%
xc: Saving memory: iter 1 (last sent 130814 skipped 258): 131072/131072 100%
xc: Saving memory: iter 2 (last sent 306 skipped 0): 131072/131072 100%
xc: Saving memory: iter 3 (last sent 53 skipped 0): 131072/131072 100%
xc: Saving memory: iter 4 (last sent 0 skipped 0): 131072/131072 100%
xc: error: Max batch size exceeded (-18). Giving up.: Internal error
xc: error: Error when reading batch (90 = Message too long): Internal error
libxl: error: libxl_dom.c:430:libxl__domain_restore_common: restoring domain: Resource temporarily unavailable
libxl: error: libxl_create.c:596:do_domain_create: cannot (re-)build domain: -3
libxl: error: libxl.c:1043:libxl_domain_destroy: non-existant domain 123
migration target: Domain creation failed (code -3).
libxl: error: libxl_utils.c:364:libxl_read_exactly: file/stream truncated reading ready message from migration receiver stream
libxl: info: libxl_exec.c:108:libxl_report_child_exitstatus: migration target process [28059] exited with error status 3 Migration failed, resuming at sender.
root@bedbug:~/shadow/tools/libxl#

I don't think this is my fault. Also after this happens the guest crashes:

root@bedbug:~# xl console debian.guest.osstest
[   47.118283] ------------[ cut here ]------------
[   47.118283] kernel BUG at /tmp/buildd/linux-2.6-2.6.32/debian/build/source_i386_none/drivers/xen/events.c:839!
[   47.118283] invalid opcode: 0000 [#1] SMP 
[   47.118283] last sysfs file: /sys/devices/virtual/net/lo/operstate
[   47.118283] Modules linked in: evdev snd_pcm snd_timer snd soundcore snd_page_alloc pcspkr ext3 jbd mbcache xen_netfront xen_blkfront
[   47.118283] 
[   47.118283] Pid: 531, comm: kstop/0 Not tainted (2.6.32-5-686-bigmem #1) 
[   47.118283] EIP: 0061:[<c1196709>] EFLAGS: 00010082 CPU: 0
[   47.118283] EIP is at xen_irq_resume+0xd9/0x21f
[   47.118283] EAX: ffffffef EBX: 00000000 ECX: deadbeef EDX: de2d5f40
[   47.118283] ESI: 00000000 EDI: de2d5fb0 EBP: c14f29fc ESP: de2d5f10
[   47.118283]  DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0069
[   47.118283] Process kstop/0 (pid: 531, ti=de2d4000 task=df968000 task.ti=de2d4000)
[   47.118283] Stack:
[   47.118283]  00000000 00000003 c143b474 c24184f0 00000000 00000000 00000002 00000010
[   47.118283] <0> c1005744 deadbeef 00000003 de2d5fb0 00000000 00000000 0000f3f3 deadbeef
[   47.118283] <0> 00000003 de2d5fb0 00000000 c1197494 df851f8c c14995ec 00000003 de2d5fb0
[   47.118283] Call Trace:
[   47.118283]  [<c1005744>] ? xen_force_evtchn_callback+0xc/0x10
[   47.118283]  [<c1197494>] ? xen_suspend+0x99/0xb0
[   47.118283]  [<c106abd0>] ? stop_cpu+0x6d/0xab
[   47.118283]  [<c1047c37>] ? worker_thread+0x141/0x1bd
[   47.118283]  [<c106ab63>] ? stop_cpu+0x0/0xab
[   47.118283]  [<c104a97a>] ? autoremove_wake_function+0x0/0x2d
[   47.118283]  [<c1047af6>] ? worker_thread+0x0/0x1bd
[   47.118283]  [<c104a748>] ? kthread+0x61/0x66
[   47.118283]  [<c104a6e7>] ? kthread+0x0/0x66
[   47.118283]  [<c1008d87>] ? kernel_thread_helper+0x7/0x10
[   47.118283] Code: fe 0f b7 45 08 39 d8 74 04 0f 0b eb fe 8b 44 24 10 8d 54 24 30 89 5c 24 30 89 44 24 34 b8 01 00 00 00 e8 f3 fa ff ff 85 c0 74 04 <0f> 0b eb fe 8b 4c 24 38 8d 7c 24 24 89 0c 24 89 34 8d e0 1e 3c 
[   47.118283] EIP: [<c1196709>] xen_irq_resume+0xd9/0x21f SS:ESP 0069:de2d5f10
[   47.118283] ---[ end trace 838e9608136663d9 ]---
[   47.118283] ------------[ cut here ]------------
[   47.118283] WARNING: at /tmp/buildd/linux-2.6-2.6.32/debian/build/source_i386_none/kernel/time/timekeeping.c:260 ktime_get+0x1f/0xcb()
[   47.118283] Modules linked in: evdev snd_pcm snd_timer snd soundcore snd_page_alloc pcspkr ext3 jbd mbcache xen_netfront xen_blkfront
[   47.118283] Pid: 0, comm: swapper Tainted: G      D    2.6.32-5-686-bigmem #1
[   47.118283] Call Trace:
[   47.118283]  [<c1051b1c>] ? ktime_get+0x1f/0xcb
[   47.118283]  [<c1051b1c>] ? ktime_get+0x1f/0xcb
[   47.118283]  [<c1036ac5>] ? warn_slowpath_common+0x5e/0x8a
[   47.118283]  [<c1036afb>] ? warn_slowpath_null+0xa/0xc
[   47.118283]  [<c1051b1c>] ? ktime_get+0x1f/0xcb
[   47.118283]  [<c1055bc8>] ? tick_nohz_stop_sched_tick+0x5d/0x36e
[   47.118283]  [<c104d83d>] ? hrtimer_start_range_ns+0xf/0x13
[   47.118283]  [<c1055b66>] ? tick_nohz_restart_sched_tick+0x128/0x12d
[   47.118283]  [<c1007395>] ? cpu_idle+0x67/0xa2
[   47.118283]  [<c13dc80d>] ? start_kernel+0x318/0x31d
[   47.118283]  [<c13de2eb>] ? xen_start_kernel+0x46a/0x471
[   47.118283]  [<c1409043>] ? udp_init+0x30/0x8c
[   47.118283] ---[ end trace 838e9608136663da ]---
#2 lore raw
In reply to Ian Jackson
Quoted 19 lines · Ian Jackson

Each time xl forks, it needs to record the pid, so that its exit status can be preserved if it happens that libxl's event loop reaped it. Consequently we also have a new wrapper for waitpid, which in that case returns the previously-reaped status.

When we get a console ready callback from libxl, check to see if we have spawned but not reaped a previous console client, and if so reap it now. (This is necessary to prevent improper use of the xlchild struct, but has the happy consequence of checking the exit status of the first console client in the pygrub case.)

Refactor the two calls to libxl_ctx_alloc into a new function xl_ctx_alloc which also sets the child reaped handler callback.

All of this has the effect of suppressing a message
unknown child [nnnn] unexpected exited status zero
which would sometimes (depending on a race) appear with `xl create -c' and pygrub.

Quoted 6 lines

Changes since v1:
* Replace macro-based iteration over children with enum-based version.

I find this a lot easier to follow, thanks!

tools/libxl/xl.c void postfork(void) · line 141

Quoted 12 lines
tools/libxl/xl.c +12 -0
+pid_t xl_waitpid(xlchildnum child, int *status, int flags)
+{
+    xlchild *ch = &children[child];
+    pid_t got = ch->pid;
+    assert(got);
+    if (ch->reaped) {
+        *status = ch->status;
+        ch->pid = 0;
+        return got;
+    }
+    for (;;) {
+        got = waitpid(ch->pid, status, flags);

Is it always the case that xl has at most one child?

Because if not then we may reap the wrong one here.

I believe we do never have more than one child, the corner case would be if we have a console child and a daemon child simultaneously, but I don't think that can happen (likewise migration child and either of the others).

tools/libxl/xl.h struct cmd_spec *cmdtable_lookup(const char *s); · line 119

Quoted 13 lines
tools/libxl/xl.h +7 -2
@@ -108,8 +110,32 @@ struct cmd_spec *cmdtable_lookup(const char *s);
 
 extern libxl_ctx *ctx;
 extern xentoollog_logger_stdiostream *logger;
-pid_t xl_fork(libxl_ctx *ctx); /* like fork, but prints and dies if it fails */
-void postfork(void);
+
+void xl_ctx_alloc(void);
+
+/* child processes */
+
+typedef struct {
+    /* every struct like this must be in XLCHILD_LIST */

This comment is obsolete now.

[...]

#3 lore raw

Ian Campbell writes ("Re: [PATCH v2] xl: track child processes for the benefit of libxl"):

Quoted 7 lines · Ian Jackson
Quoted 6 lines
tools/libxl/xl.c +6 -0
+pid_t xl_waitpid(xlchildnum child, int *status, int flags)
+{
+    xlchild *ch = &children[child];
+    pid_t got = ch->pid;
+    assert(got);
+    if (ch->reaped) {
Quoted 8 lines

tools/libxl/xl.c void postfork(void) · line 141

Quoted 6 lines
tools/libxl/xl.c +6 -0
+        *status = ch->status;
+        ch->pid = 0;
+        return got;
+    }
+    for (;;) {
+        got = waitpid(ch->pid, status, flags);

Is it always the case that xl has at most one child?

I don't think it is necessarily the case.

Quoted 1 line

Because if not then we may reap the wrong one here.

No, the whole point of waitpid is that you get to specify which child to reap. Unless I'm missing something ?

ch->pid the same as children[child].pid which is the pid of the child we want to wait for, and the previous code has just checked whether we have reaped it already so we are guaranteed not to reap a new different child with the same pid and get confused (should such a thing happen to happen).

Quoted 4 lines

tools/libxl/xl.h struct cmd_spec *cmdtable_lookup(const char *s); · line 119

Quoted 2 lines
tools/libxl/xl.h +2 -0
+typedef struct {
+    /* every struct like this must be in XLCHILD_LIST */

This comment is obsolete now.

Oh yes.

Ian.

#4 lore raw
Quoted 17 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH v2] xl: track child processes for the benefit of libxl"):

Quoted 7 lines · Ian Jackson
Quoted 6 lines
tools/libxl/xl.c +6 -0
+pid_t xl_waitpid(xlchildnum child, int *status, int flags)
+{
+    xlchild *ch = &children[child];
+    pid_t got = ch->pid;
+    assert(got);
+    if (ch->reaped) {
Quoted 8 lines

tools/libxl/xl.c void postfork(void) · line 141

Quoted 6 lines
tools/libxl/xl.c +6 -0
+        *status = ch->status;
+        ch->pid = 0;
+        return got;
+    }
+    for (;;) {
+        got = waitpid(ch->pid, status, flags);

Is it always the case that xl has at most one child?

Quoted 6 lines

I don't think it is necessarily the case.

Quoted 1 line

Because if not then we may reap the wrong one here.

No, the whole point of waitpid is that you get to specify which child to reap. Unless I'm missing something ?

No I was, for some reason I thought we were waiting for any child here. You can safely ignore this comment ;-)

Ian.

#5 lore raw
In reply to Ian Jackson
Quoted 1 line · Ian Jackson

Each time xl forks, it needs to record the pid, so that its exit

I was just about to apply this but testing with pygrub gives me:

libxl: critical: libxl_fork.c:314:libxl__fork_selfpipe_woken: DISASTER in event loop: reported by libxl_childproc_hooks->reaped_callback (for pid=7023, status=0; error code 1): Success
libxl: critical: libxl_event.c:1005:libxl__event_disaster: DISASTER in event loop not handled by libxl application
libxl: fatal error, exiting program: DISASTER in event loop not handled by libxl application

I think the return codes of xl_reaped_callback are just bogus, according to the comment it should return 0 if it knows the child, ERROR_UNKNOWN_CHILD if not and anything else is a disaster. Incremental fix is:

tools/libxl/xl.c +2 -2
diff -r a5ff801ed897 tools/libxl/xl.c
@@ -163,10 +163,10 @@ static int xl_reaped_callback(pid_t got,
         if (ch->pid == got) {
             ch->reaped = 1;
             ch->status = status;
-            return 1;
+            return 0;
         }
     }
-    return 0;
+    return ERROR_UNKNOWN_CHILD;
 }
 
 static const libxl_childproc_hooks childproc_hooks = {
#6 lore raw

Ian Campbell writes ("Re: [PATCH v2] xl: track child processes for the benefit of libxl"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

Each time xl forks, it needs to record the pid, so that its exit

I was just about to apply this but testing with pygrub gives me:

...

Quoted 4 lines

I think the return codes of xl_reaped_callback are just bogus, according to the comment it should return 0 if it knows the child, ERROR_UNKNOWN_CHILD if not and anything else is a disaster. Incremental fix is:

Ooops, I'm sorry. I broke this when I removed the CHILD_LIST macro stuff.

Ian.

#7 lore raw
Quoted 5 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH v2] xl: track child processes for the benefit of libxl"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

Each time xl forks, it needs to record the pid, so that its exit

I was just about to apply this but testing with pygrub gives me:

Quoted 8 lines

...

Quoted 4 lines

I think the return codes of xl_reaped_callback are just bogus, according to the comment it should return 0 if it knows the child, ERROR_UNKNOWN_CHILD if not and anything else is a disaster. Incremental fix is:

Ooops, I'm sorry. I broke this when I removed the CHILD_LIST macro stuff.

No problem, I'm going to fold in my incremental fix, ack and apply.

Ian.