Skip to contentSkip to conversation

libxl: asynchronous suspend

PATCH RESEND Superseded Ian Jackson 5 participants 70 messages Mar 4, 2014 – Mar 17, 2014 Arm/actions semi architecture

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover libxl: asynchronous suspend

This series removes the usleeps and waiting loops in libxl_dom.c and replaces them with event-callback code.

We want some additional libxl event facilities:
01/19 libxl: init: Provide a gc later in libxl_ctx_alloc 02/19 libxl: init: libxl__poller_init and _get take gc
a 03/19 libxl: events: const-correct *_inuse, *_isregistered
- 04/19 libxl: events: Provide libxl__xswait_*
a 05/19 libxl: events: Use libxl__xswait_* in spawn code
+ 06/19 libxl: events: Provide libxl__ev_evtchn*

We need to clean up some unfortunate code in libxc:
07/19 libxc: suspend: Rename, improve xc_suspend_evtchn_init 08/19 libxc: suspend: Fix suspend event channel locking

We do some shuffling around of the libxl suspend control flow:
- 09/19 libxl: suspend: Async libxl__domain_suspend_callback 10/19 libxl: suspend: Async domain_suspend_callback_common 11/19 libxl: suspend: Reorg domain_suspend_callback_common 12/19 libxl: suspend: New libxl__domain_pvcontrol_xspath 13/19 libxl: suspend: New domain_suspend_pvcontrol_acked
No functional change in those five. These changes are broken down just to make the changes reviewable.

Finally, we can start to work on the event code, removing the bugs, usleeps and loops one at a time:
14/19 libxl: suspend: domain_suspend_callback_common xs errs 15/19 libxl: suspend: Async xenstore pvcontrol wait 16/19 libxl: suspend: Abolish usleeps in domain suspend wait 17/19 libxl: suspend: Fix suspend wait corner cases 18/19 libxl: suspend: Async evtchn wait
19/19 libxl: suspend: Apply guest timeout in evtchn case

Notes:

   +   modified in v2
   -   modified in v2 but only in commit message or comments
   a   acked

I have tested v2 with a Debian pvops kernel (xenstore pvcontrol suspend signalling) and OpenSUSE 11 (event channel suspend signalling). v2.1 is a rebase onto current staging.

I haven't really touched the Remus-specific code here but this series ought to be suitable for the Remus developers to base things on.

#1 lore raw

PATCH 1/19 libxl: init: Provide a gc later in libxl_ctx_alloc

Provide libxl__gc *gc for the second half of libxl_ctx_alloc. (For the first half of the function, gc is in scope but set to NULL.)

This makes it possible to make gc-requiring calls. For example, it makes error logging more convenient.

Make use of this by changing the logging calls to use the LOG* convenience macros.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/libxl/libxl.c +7 -4
index 730f6e1..4b5abc8 100644
@@ -25,6 +25,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
                     unsigned flags, xentoollog_logger * lg)
 {
     libxl_ctx *ctx = NULL;
+    libxl__gc gc_buf, *gc = NULL;
     int rc;
 
     if (version != LIBXL_VERSION) { rc = ERROR_VERSION; goto out; }
@@ -79,6 +80,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     }
 
     /* Now ctx is safe for ctx_free; failures simply set rc and "goto out" */
+    LIBXL_INIT_GC(gc_buf,ctx);
+    gc = &gc_buf;
+    /* Now gc is useable */
 
     rc = libxl__atfork_init(ctx);
     if (rc) goto out;
@@ -88,8 +92,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
 
     ctx->xch = xc_interface_open(lg,lg,0);
     if (!ctx->xch) {
-        LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
-                        "cannot open libxc handle");
+        LOGEV(ERROR, errno, "cannot open libxc handle");
         rc = ERROR_FAIL; goto out;
     }
 
@@ -97,8 +100,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     if (!ctx->xsh)
         ctx->xsh = xs_domain_open();
     if (!ctx->xsh) {
-        LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
-                        "cannot connect to xenstore");
+        LOGEV(ERROR, errno, "cannot connect to xenstore");
         rc = ERROR_FAIL; goto out;
     }
 
@@ -106,6 +108,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     return 0;
 
  out:
+    if (gc) libxl__free_all(gc);
     libxl_ctx_free(ctx);
     *pctx = NULL;
     return rc;
Signature · 1 line
1.7.10.4
Acked-by on v1 1/19
#2 lore raw

Re: [PATCH 01/19] libxl: init: Provide a gc later in libxl_ctx_alloc

Quoted 11 lines · Ian Jackson

Provide libxl__gc *gc for the second half of libxl_ctx_alloc. (For the first half of the function, gc is in scope but set to NULL.)

This makes it possible to make gc-requiring calls. For example, it makes error logging more convenient.

Make use of this by changing the logging calls to use the LOG* convenience macros.

CC Stefano Stabellini <[email protected]>
#3 lore raw

PATCH 2/19 libxl: init: libxl__poller_init and _get take gc

In reply to Ian Jackson

Change libxl__poller_init and libxl__poller__get to take a libxl__gc* rather than a libxl_ctx*. The gc is not used for memory allocation but simply to provide the standard local variable "gc" expected by the convenience macros. Doing this makes the error logging more convenient.

Hence, convert the logging calls to use the LOG* convenience macros.

And consequently, change the call sites, and the function bodies to use CTX rather than ctx.

Also convert a call to malloc() (with error check) in libxl__poller_get, to libxl__zalloc (no error check needed).

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl.c          |    2 +-
 tools/libxl/libxl_event.c    |   21 ++++++++-------------
 tools/libxl/libxl_internal.h |    4 ++--
 3 files changed, 11 insertions(+), 16 deletions(-)
tools/libxl/libxl.c +1 -1
index 4b5abc8..5415ebd 100644
@@ -87,7 +87,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     rc = libxl__atfork_init(ctx);
     if (rc) goto out;
 
-    rc = libxl__poller_init(ctx, &ctx->poller_app);
+    rc = libxl__poller_init(gc, &ctx->poller_app);
     if (rc) goto out;
 
     ctx->xch = xc_interface_open(lg,lg,0);
tools/libxl/libxl_event.c +8 -13
index ea8c744..3e465af 100644
@@ -1347,13 +1347,13 @@ int libxl__self_pipe_eatall(int fd)
  * Manipulation of pollers
  */
 
-int libxl__poller_init(libxl_ctx *ctx, libxl__poller *p)
+int libxl__poller_init(libxl__gc *gc, libxl__poller *p)
 {
     int rc;
     p->fd_polls = 0;
     p->fd_rindices = 0;
 
-    rc = libxl__pipe_nonblock(ctx, p->wakeup_pipe);
+    rc = libxl__pipe_nonblock(CTX, p->wakeup_pipe);
     if (rc) goto out;
 
     return 0;
@@ -1370,25 +1370,20 @@ void libxl__poller_dispose(libxl__poller *p)
     free(p->fd_rindices);
 }
 
-libxl__poller *libxl__poller_get(libxl_ctx *ctx)
+libxl__poller *libxl__poller_get(libxl__gc *gc)
 {
     /* must be called with ctx locked */
     int rc;
 
-    libxl__poller *p = LIBXL_LIST_FIRST(&ctx->pollers_idle);
+    libxl__poller *p = LIBXL_LIST_FIRST(&CTX->pollers_idle);
     if (p) {
         LIBXL_LIST_REMOVE(p, entry);
         return p;
     }
 
-    p = malloc(sizeof(*p));
-    if (!p) {
-        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot allocate poller");
-        return 0;
-    }
-    memset(p, 0, sizeof(*p));
+    p = libxl__zalloc(NOGC, sizeof(*p));
 
-    rc = libxl__poller_init(ctx, p);
+    rc = libxl__poller_init(gc, p);
     if (rc) {
         free(p);
         return NULL;
@@ -1477,7 +1472,7 @@ int libxl_event_wait(libxl_ctx *ctx, libxl_event **event_r,
     EGC_INIT(ctx);
     CTX_LOCK;
 
-    poller = libxl__poller_get(ctx);
+    poller = libxl__poller_get(gc);
     if (!poller) { rc = ERROR_FAIL; goto out; }
 
     for (;;) {
@@ -1653,7 +1648,7 @@ libxl__ao *libxl__ao_create(libxl_ctx *ctx, uint32_t domid,
     if (how) {
         ao->how = *how;
     } else {
-        ao->poller = libxl__poller_get(ctx);
+        ao->poller = libxl__poller_get(&ao->gc);
         if (!ao->poller) goto out;
     }
     libxl__log(ctx,XTL_DEBUG,-1,file,line,func,
tools/libxl/libxl_internal.h +2 -2
index 9d17586..b67ed79 100644
@@ -834,13 +834,13 @@ _hidden void libxl__event_disaster(libxl__egc*, const char *msg, int errnoval,
 
 /* Fills in, or disposes of, the resources held by, a poller whose
  * space the caller has allocated.  ctx must be locked. */
-_hidden int libxl__poller_init(libxl_ctx *ctx, libxl__poller *p);
+_hidden int libxl__poller_init(libxl__gc *gc, libxl__poller *p);
 _hidden void libxl__poller_dispose(libxl__poller *p);
 
 /* Obtain a fresh poller from malloc or the idle list, and put it
  * away again afterwards.  _get can fail, returning NULL.
  * ctx must be locked. */
-_hidden libxl__poller *libxl__poller_get(libxl_ctx *ctx);
+_hidden libxl__poller *libxl__poller_get(libxl__gc *gc);
 _hidden void libxl__poller_put(libxl_ctx*, libxl__poller *p /* may be NULL */);
 
 /* Notifies whoever is polling using p that they should wake up.
Signature · 1 line
1.7.10.4
Acked-by on v1 2/19
#4 lore raw

Re: [PATCH 02/19] libxl: init: libxl__poller_init and _get take gc

Quoted 8 lines · Ian Jackson

Change libxl__poller_init and libxl__poller__get to take a libxl__gc* rather than a libxl_ctx*. The gc is not used for memory allocation but simply to provide the standard local variable "gc" expected by the convenience macros. Doing this makes the error logging more convenient.

Hence, convert the logging calls to use the LOG* convenience macros.

Quoted 8 lines

And consequently, change the call sites, and the function bodies to use CTX rather than ctx.

Also convert a call to malloc() (with error check) in libxl__poller_get, to libxl__zalloc (no error check needed).

CC Stefano Stabellini <[email protected]>
#5 lore raw

PATCH 3/19 libxl: events: const-correct *_inuse, *_isregistered

In reply to Ian Jackson

The comments for libxl__ev_time_isregistered and the corresponding watch function even say that these should be const. Make it so.

Also fix libxl__ev_child_inuse and libxl__ev_spawn_inuse.

---
 tools/libxl/libxl_internal.h |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/libxl/libxl_internal.h +4 -4
index b67ed79..9e02861 100644
@@ -727,7 +727,7 @@ _hidden int libxl__ev_fd_modify(libxl__gc*, libxl__ev_fd *ev,
 _hidden void libxl__ev_fd_deregister(libxl__gc*, libxl__ev_fd *ev);
 static inline void libxl__ev_fd_init(libxl__ev_fd *efd)
                     { efd->fd = -1; }
-static inline int libxl__ev_fd_isregistered(libxl__ev_fd *efd)
+static inline int libxl__ev_fd_isregistered(const libxl__ev_fd *efd)
                     { return efd->fd >= 0; }
 
 _hidden int libxl__ev_time_register_rel(libxl__gc*, libxl__ev_time *ev_out,
@@ -743,7 +743,7 @@ _hidden int libxl__ev_time_modify_abs(libxl__gc*, libxl__ev_time *ev,
 _hidden void libxl__ev_time_deregister(libxl__gc*, libxl__ev_time *ev);
 static inline void libxl__ev_time_init(libxl__ev_time *ev)
                 { ev->func = 0; }
-static inline int libxl__ev_time_isregistered(libxl__ev_time *ev)
+static inline int libxl__ev_time_isregistered(const libxl__ev_time *ev)
                 { return !!ev->func; }
 
 
@@ -783,7 +783,7 @@ _hidden pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *childw_out,
                                  libxl__ev_child_callback *death);
 static inline void libxl__ev_child_init(libxl__ev_child *childw_out)
                 { childw_out->pid = -1; }
-static inline int libxl__ev_child_inuse(libxl__ev_child *childw_out)
+static inline int libxl__ev_child_inuse(const libxl__ev_child *childw_out)
                 { return childw_out->pid >= 0; }
 
 /* Useable (only) in the child to once more make the ctx useable for
@@ -1225,7 +1225,7 @@ struct libxl__spawn_state {
     libxl__ev_xswatch xswatch;
 };
 
-static inline int libxl__spawn_inuse(libxl__spawn_state *ss)
+static inline int libxl__spawn_inuse(const libxl__spawn_state *ss)
     { return libxl__ev_child_inuse(&ss->mid); }
 
 /*
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH 4/19 libxl: events: Provide libxl__xswait_*

In reply to Ian Jackson

This is an ao utility for for conveniently doing a timed wait on xenstore. It handles setting up and cancelling the timeout, and also conveniently reads the key for you.

No callers yet in this patch.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>

---
v2 fix doc comments to refer to correct rc values

---
 tools/libxl/libxl_aoutils.c  |   77 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   52 ++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+)
tools/libxl/libxl_aoutils.c +77 -0
index b4eb6e5..477717b 100644
@@ -16,6 +16,83 @@
 
 #include "libxl_internal.h"
 
+/*----- xswait -----*/
+
+static libxl__ev_xswatch_callback xswait_xswatch_callback;
+static libxl__ev_time_callback xswait_timeout_callback;
+static void xswait_report_error(libxl__egc*, libxl__xswait_state*, int rc);
+
+void libxl__xswait_init(libxl__xswait_state *xswa)
+{
+    libxl__ev_time_init(&xswa->time_ev);
+    libxl__ev_xswatch_init(&xswa->watch_ev);
+}
+
+void libxl__xswait_stop(libxl__gc *gc, libxl__xswait_state *xswa)
+{
+    libxl__ev_time_deregister(gc, &xswa->time_ev);
+    libxl__ev_xswatch_deregister(gc, &xswa->watch_ev);
+}
+
+bool libxl__xswait_inuse(const libxl__xswait_state *xswa)
+{
+    bool time_inuse = libxl__ev_time_isregistered(&xswa->time_ev);
+    bool watch_inuse = libxl__ev_xswatch_isregistered(&xswa->watch_ev);
+    assert(time_inuse == watch_inuse);
+    return time_inuse;
+}
+
+int libxl__xswait_start(libxl__gc *gc, libxl__xswait_state *xswa)
+{
+    int rc;
+
+    rc = libxl__ev_time_register_rel(gc, &xswa->time_ev,
+                                     xswait_timeout_callback, xswa->timeout_ms);
+    if (rc) goto err;
+
+    rc = libxl__ev_xswatch_register(gc, &xswa->watch_ev,
+                                    xswait_xswatch_callback, xswa->path);
+    if (rc) goto err;
+
+    return 0;
+
+ err:
+    libxl__xswait_stop(gc, xswa);
+    return rc;
+}
+
+void xswait_xswatch_callback(libxl__egc *egc, libxl__ev_xswatch *xsw,
+                             const char *watch_path, const char *event_path)
+{
+    EGC_GC;
+    libxl__xswait_state *xswa = CONTAINER_OF(xsw, *xswa, watch_ev);
+    int rc;
+    const char *data;
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL, xswa->path, &data);
+    if (rc) { xswait_report_error(egc, xswa, rc); return; }
+
+    xswa->callback(egc, xswa, 0, data);
+}
+
+void xswait_timeout_callback(libxl__egc *egc, libxl__ev_time *ev,
+                             const struct timeval *requested_abs)
+{
+    EGC_GC;
+    libxl__xswait_state *xswa = CONTAINER_OF(ev, *xswa, time_ev);
+    LOG(DEBUG, "%s: xswait timeout (path=%s)", xswa->what, xswa->path);
+    xswait_report_error(egc, xswa, ERROR_TIMEDOUT);
+}
+
+static void xswait_report_error(libxl__egc *egc, libxl__xswait_state *xswa,
+                                int rc)
+{
+    EGC_GC;
+    libxl__xswait_stop(gc, xswa);
+    xswa->callback(egc, xswa, rc, 0);
+}
+
+
 /*----- data copier -----*/
 
 void libxl__datacopier_init(libxl__datacopier_state *dc)
tools/libxl/libxl_internal.h +52 -0

58 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#8 lore raw

PATCH 5/19 libxl: events: Use libxl__xswait_* in spawn code

In reply to Ian Jackson

Replace open-coded use of ev_time and ev_xswatch with xswait.

---
 tools/libxl/libxl_exec.c     |   49 +++++++++++++++---------------------------
 tools/libxl/libxl_internal.h |    3 +--
 2 files changed, 18 insertions(+), 34 deletions(-)
tools/libxl/libxl_exec.c +17 -32
index b6efa0f..4b012dc 100644
@@ -257,10 +257,8 @@ err:
  */
 
 /* Event callbacks. */
-static void spawn_watch_event(libxl__egc *egc, libxl__ev_xswatch *xsw,
-                              const char *watch_path, const char *event_path);
-static void spawn_timeout(libxl__egc *egc, libxl__ev_time *ev,
-                          const struct timeval *requested_abs);
+static void spawn_watch_event(libxl__egc *egc, libxl__xswait_state *xswa,
+                              int rc, const char *xsdata);
 static void spawn_middle_death(libxl__egc *egc, libxl__ev_child *childw,
                                pid_t pid, int status);
 
@@ -274,8 +272,7 @@ static void spawn_fail(libxl__egc *egc, libxl__spawn_state *ss);
 void libxl__spawn_init(libxl__spawn_state *ss)
 {
     libxl__ev_child_init(&ss->mid);
-    libxl__ev_time_init(&ss->timeout);
-    libxl__ev_xswatch_init(&ss->xswatch);
+    libxl__xswait_init(&ss->xswait);
 }
 
 int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss)
@@ -288,12 +285,12 @@ int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss)
     libxl__spawn_init(ss);
     ss->failed = ss->detaching = 0;
 
-    rc = libxl__ev_time_register_rel(gc, &ss->timeout,
-                                     spawn_timeout, ss->timeout_ms);
-    if (rc) goto out_err;
-
-    rc = libxl__ev_xswatch_register(gc, &ss->xswatch,
-                                    spawn_watch_event, ss->xspath);
+    ss->xswait.ao = ao;
+    ss->xswait.what = GCSPRINTF("%s startup", ss->what);
+    ss->xswait.path = ss->xspath;
+    ss->xswait.timeout_ms = ss->timeout_ms;
+    ss->xswait.callback = spawn_watch_event;
+    rc = libxl__xswait_start(gc, &ss->xswait);
     if (rc) goto out_err;
 
     pid_t middle = libxl__ev_child_fork(gc, &ss->mid, spawn_middle_death);
@@ -350,8 +347,7 @@ int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss)
 static void spawn_cleanup(libxl__gc *gc, libxl__spawn_state *ss)
 {
     assert(!libxl__ev_child_inuse(&ss->mid));
-    libxl__ev_time_deregister(gc, &ss->timeout);
-    libxl__ev_xswatch_deregister(gc, &ss->xswatch);
+    libxl__xswait_stop(gc, &ss->xswait);
 }
 
 static void spawn_detach(libxl__gc *gc, libxl__spawn_state *ss)
@@ -362,8 +358,7 @@ static void spawn_detach(libxl__gc *gc, libxl__spawn_state *ss)
     int r;
 
     assert(libxl__ev_child_inuse(&ss->mid));
-    libxl__ev_time_deregister(gc, &ss->timeout);
-    libxl__ev_xswatch_deregister(gc, &ss->xswatch);
+    libxl__xswait_stop(gc, &ss->xswait);
 
     pid_t child = ss->mid.pid;
     r = kill(child, SIGKILL);
@@ -387,25 +382,15 @@ static void spawn_fail(libxl__egc *egc, libxl__spawn_state *ss)
     spawn_detach(gc, ss);
 }
 
-static void spawn_timeout(libxl__egc *egc, libxl__ev_time *ev,
-                          const struct timeval *requested_abs)
-{
-    /* Before event, was Attached. */
-    EGC_GC;
-    libxl__spawn_state *ss = CONTAINER_OF(ev, *ss, timeout);
-    LOG(ERROR, "%s: startup timed out", ss->what);
-    spawn_fail(egc, ss); /* must be last */
-}
-
-static void spawn_watch_event(libxl__egc *egc, libxl__ev_xswatch *xsw,
-                              const char *watch_path, const char *event_path)
+static void spawn_watch_event(libxl__egc *egc, libxl__xswait_state *xswa,
+                              int rc, const char *p)
 {
     /* On entry, is Attached. */
     EGC_GC;
-    libxl__spawn_state *ss = CONTAINER_OF(xsw, *ss, xswatch);
-    char *p = libxl__xs_read(gc, 0, ss->xspath);
-    if (!p && errno != ENOENT) {
-        LOG(ERROR, "%s: xenstore read of %s failed", ss->what, ss->xspath);
+    libxl__spawn_state *ss = CONTAINER_OF(xswa, *ss, xswait);
+    if (rc) {
+        if (rc == ERROR_TIMEDOUT)
+            LOG(ERROR, "%s: startup timed out", ss->what);
         spawn_fail(egc, ss); /* must be last */
         return;
     }
tools/libxl/libxl_internal.h +1 -2
index 644fcee..b4e4e0f 100644
@@ -1273,8 +1273,7 @@ struct libxl__spawn_state {
     int detaching; /* we are in Detaching */
     int failed; /* might be true whenever we are not Idle */
     libxl__ev_child mid; /* always in use whenever we are not Idle */
-    libxl__ev_time timeout;
-    libxl__ev_xswatch xswatch;
+    libxl__xswait_state xswait;
 };
 
 static inline int libxl__spawn_inuse(const libxl__spawn_state *ss)
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [PATCH 05/19] libxl: events: Use libxl__xswait_* in spawn code

Quoted 43 lines · Ian Jackson

Replace open-coded use of ev_time and ev_xswatch with xswait.

---
 tools/libxl/libxl_exec.c     |   49 +++++++++++++++---------------------------
 tools/libxl/libxl_internal.h |    3 +--
 2 files changed, 18 insertions(+), 34 deletions(-)
tools/libxl/libxl_exec.c +3 -8
index b6efa0f..4b012dc 100644
@@ -257,10 +257,8 @@ err:
  */
 
 /* Event callbacks. */
-static void spawn_watch_event(libxl__egc *egc, libxl__ev_xswatch *xsw,
-                              const char *watch_path, const char *event_path);
-static void spawn_timeout(libxl__egc *egc, libxl__ev_time *ev,
-                          const struct timeval *requested_abs);
+static void spawn_watch_event(libxl__egc *egc, libxl__xswait_state *xswa,
+                              int rc, const char *xsdata);
 static void spawn_middle_death(libxl__egc *egc, libxl__ev_child *childw,
                                pid_t pid, int status);
 
@@ -274,8 +272,7 @@ static void spawn_fail(libxl__egc *egc, libxl__spawn_state *ss);
 void libxl__spawn_init(libxl__spawn_state *ss)
 {
     libxl__ev_child_init(&ss->mid);
-    libxl__ev_time_init(&ss->timeout);
-    libxl__ev_xswatch_init(&ss->xswatch);
+    libxl__xswait_init(&ss->xswait);
 }
 
 int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss)
@@ -288,12 +285,12 @@ int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss)
     libxl__spawn_init(ss);
     ss->failed = ss->detaching = 0;
 
-    rc = libxl__ev_time_register_rel(gc, &ss->timeout,
-                                     spawn_timeout, ss->timeout_ms);

tools/libxl/libxl_exec.c int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss) · line 293

Quoted 10 lines
tools/libxl/libxl_exec.c +6 -4
-    if (rc) goto out_err;
-
-    rc = libxl__ev_xswatch_register(gc, &ss->xswatch,
-                                    spawn_watch_event, ss->xspath);
+    ss->xswait.ao = ao;
+    ss->xswait.what = GCSPRINTF("%s startup", ss->what);
+    ss->xswait.path = ss->xspath;
+    ss->xswait.timeout_ms = ss->timeout_ms;
+    ss->xswait.callback = spawn_watch_event;
+    rc = libxl__xswait_start(gc, &ss->xswait);

This is another kind of open code, I guess it should be wrapped into a function.

#10 lore raw

Re: [PATCH 05/19] libxl: events: Use libxl__xswait_* in spawn code

Lai Jiangshan writes ("Re: [PATCH 05/19] libxl: events: Use libxl__xswait_* in spawn code"):

Quoted 2 lines · Ian Jackson
Quoted 1 line

Replace open-coded use of ev_time and ev_xswatch with xswait.

...

Quoted 9 lines

tools/libxl/libxl_exec.c int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss) · line 293

Quoted 6 lines
tools/libxl/libxl_exec.c +6 -0
+    ss->xswait.ao = ao;
+    ss->xswait.what = GCSPRINTF("%s startup", ss->what);
+    ss->xswait.path = ss->xspath;
+    ss->xswait.timeout_ms = ss->timeout_ms;
+    ss->xswait.callback = spawn_watch_event;
+    rc = libxl__xswait_start(gc, &ss->xswait);

This is another kind of open code, I guess it should be wrapped into a function.

I'm not sure I follow. These are, effectively, the arguments to libxl__xswait_start. libxl__xswait_start takes its "arguments" inside
the xswait structure. So the LHS are formulaic but the RHS are all
specific to the call site.

I guess we could use GCC's compound literal syntax, with something like this:

   ss->xswait = (libxl__xswait_state){
      .ao = ao,
      .what = GCSPRINTF("%s startup", ss->what),
      .path = ss->xspath,
      .timeout_ms = ss->timeout_ms,
      .callback = spawn_watch_event,
   };

But that's not an improvement IMO.

Ian.

#11 lore raw

Re: [PATCH 05/19] libxl: events: Use libxl__xswait_* in spawn code

Quoted 30 lines · Ian Jackson

Lai Jiangshan writes ("Re: [PATCH 05/19] libxl: events: Use libxl__xswait_* in spawn code"):

Quoted 2 lines · Ian Jackson
Quoted 1 line

Replace open-coded use of ev_time and ev_xswatch with xswait.

...

Quoted 9 lines

tools/libxl/libxl_exec.c int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *ss) · line 293

Quoted 6 lines
tools/libxl/libxl_exec.c +6 -0
+    ss->xswait.ao = ao;
+    ss->xswait.what = GCSPRINTF("%s startup", ss->what);
+    ss->xswait.path = ss->xspath;
+    ss->xswait.timeout_ms = ss->timeout_ms;
+    ss->xswait.callback = spawn_watch_event;
+    rc = libxl__xswait_start(gc, &ss->xswait);

This is another kind of open code, I guess it should be wrapped into a function.

I'm not sure I follow. These are, effectively, the arguments to libxl__xswait_start. libxl__xswait_start takes its "arguments" inside
the xswait structure. So the LHS are formulaic but the RHS are all
specific to the call site.

I guess we could use GCC's compound literal syntax, with something like this:

   ss->xswait = (libxl__xswait_state){
      .ao = ao,
      .what = GCSPRINTF("%s startup", ss->what),
      .path = ss->xspath,
      .timeout_ms = ss->timeout_ms,
      .callback = spawn_watch_event,
   };
Quoted 1 line

But that's not an improvement IMO.

Nor mine.

Ian.

#12 lore raw

PATCH 6/19 libxl: events: Provide libxl__ev_evtchn*

In reply to Ian Jackson
CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>

---
v2: Fix commit message not to refer to libxl_ctx_alloc's gc, done earlier
Change type of port in evtchn_fd_callback to evtchn_port_or_error_t Clarify comment about use of ctx->xce.
Fix typo in comment.

---
 tools/libxl/libxl.c          |    9 +++
 tools/libxl/libxl_event.c    |  153 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   48 +++++++++++++
 3 files changed, 210 insertions(+)
tools/libxl/libxl.c +9 -0
index 5415ebd..f91a398 100644
@@ -60,6 +60,10 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     LIBXL_SLIST_INIT(&ctx->watch_freeslots);
     libxl__ev_fd_init(&ctx->watch_efd);
 
+    ctx->xce = 0;
+    LIBXL_LIST_INIT(&ctx->evtchns_waiting);
+    libxl__ev_fd_init(&ctx->evtchn_efd);
+
     LIBXL_TAILQ_INIT(&ctx->death_list);
     libxl__ev_xswatch_init(&ctx->death_watch);
 
@@ -104,6 +108,8 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
         rc = ERROR_FAIL; goto out;
     }
 
+    rc = libxl__ctx_evtchn_init(gc);
+
     *pctx = ctx;
     return 0;
 
@@ -147,16 +153,19 @@ int libxl_ctx_free(libxl_ctx *ctx)
     for (i = 0; i < ctx->watch_nslots; i++)
         assert(!libxl__watch_slot_contents(gc, i));
     libxl__ev_fd_deregister(gc, &ctx->watch_efd);
+    libxl__ev_fd_deregister(gc, &ctx->evtchn_efd);
     libxl__ev_fd_deregister(gc, &ctx->sigchld_selfpipe_efd);
 
     /* Now there should be no more events requested from the application: */
 
     assert(LIBXL_LIST_EMPTY(&ctx->efds));
     assert(LIBXL_TAILQ_EMPTY(&ctx->etimes));
+    assert(LIBXL_LIST_EMPTY(&ctx->evtchns_waiting));
 
     if (ctx->xch) xc_interface_close(ctx->xch);
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
+    if (ctx->xce) xc_evtchn_close(ctx->xce);
 
     libxl__poller_dispose(&ctx->poller_app);
     assert(LIBXL_LIST_EMPTY(&ctx->pollers_event));
tools/libxl/libxl_event.c +153 -0

159 lines not on this page · raw message

tools/libxl/libxl_internal.h +48 -0

66 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#14 lore raw

PATCH 7/19 libxc: suspend: Rename, improve xc_suspend_evtchn_init

In reply to Ian Jackson

xc_suspend_evtchn_init expects to eat the first event on the xce. If the xce is used for any other purpose then this can break. Document this fact and rename the function to xc_suspend_evtchn_init_exclusive. (I haven't checked the call sites for improper shared use of the xce.)

Provide a corresponding xc_suspend_evtchn_init_sane which doesn't try to eat an event, and instead leaves the caller the ability to demultiplex.

Also document that xc_await_suspend needs exclusive use of the xce.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Shriram Rajagopalan <[email protected]>
---
 tools/libxc/xc_suspend.c                           |   21 ++++++++++++++++----
 tools/libxc/xenguest.h                             |   13 +++++++++++-
 tools/libxl/libxl_dom.c                            |    2 +-
 tools/misc/xen-hptool.c                            |    2 +-
 .../python/xen/lowlevel/checkpoint/libcheckpoint.c |    2 +-
 tools/xcutils/xc_save.c                            |    2 +-
 6 files changed, 33 insertions(+), 9 deletions(-)
tools/libxc/xc_suspend.c +17 -4
index 1ace411..7968a44 100644
@@ -14,6 +14,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <assert.h>
+
 #include "xc_private.h"
 #include "xenguest.h"
 
@@ -102,7 +104,7 @@ int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int
     return unlock_suspend_event(xch, domid);
 }
 
-int xc_suspend_evtchn_init(xc_interface *xch, xc_evtchn *xce, int domid, int port)
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, int port)
 {
     int rc, suspend_evtchn = -1;
 
@@ -121,9 +123,6 @@ int xc_suspend_evtchn_init(xc_interface *xch, xc_evtchn *xce, int domid, int por
         goto cleanup;
     }
 
-    /* event channel is pending immediately after binding */
-    xc_await_suspend(xch, xce, suspend_evtchn);
-
     return suspend_evtchn;
 
 cleanup:
@@ -132,3 +131,17 @@ cleanup:
 
     return -1;
 }
+
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce, int domid, int port)
+{
+    int suspend_evtchn;
+
+    suspend_evtchn = xc_suspend_evtchn_init_sane(xch, xce, domid, port);
+    if (suspend_evtchn < 0)
+        return suspend_evtchn;
+
+    /* event channel is pending immediately after binding */
+    xc_await_suspend(xch, xce, suspend_evtchn);
+
+    return suspend_evtchn;
+}
tools/libxc/xenguest.h +12 -1
index a0e30e1..ce5456c 100644
@@ -256,10 +256,21 @@ int xc_hvm_build_target_mem(xc_interface *xch,
 
 int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn);
 
-int xc_suspend_evtchn_init(xc_interface *xch, xc_evtchn *xce, int domid, int port);
+/**
+ * This function eats the initial notification.
+ * xce must not be used for anything else
+ */
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce, int domid, int port);
 
+/* xce must not be used for anything else */
 int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
 
+/**
+ * The port will be signaled immediately after this call
+ * The caller should check the domain status and look for the next event
+ */
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, int port);
+
 int xc_get_bit_size(xc_interface *xch,
                     const char *image_name, const char *cmdline,
                     const char *features, int *type);
tools/libxl/libxl_dom.c +1 -1
index 55f74b2..4b42856 100644
@@ -1358,7 +1358,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 
         if (port >= 0) {
             dss->suspend_eventchn =
-                xc_suspend_evtchn_init(CTX->xch, dss->xce, dss->domid, port);
+                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce, dss->domid, port);
 
             if (dss->suspend_eventchn < 0)
                 LOG(WARN, "Suspend event channel initialization failed");
tools/misc/xen-hptool.c +1 -1
index 24c3e95..db76f79 100644
@@ -112,7 +112,7 @@ static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid, int *evtc
         fprintf(stderr, "DOM%d: No suspend port, try live migration\n", domid);
         goto failed;
     }
-    suspend_evtchn = xc_suspend_evtchn_init(xch, xce, domid, port);
+    suspend_evtchn = xc_suspend_evtchn_init_exclusive(xch, xce, domid, port);
     if (suspend_evtchn < 0)
     {
         fprintf(stderr, "Suspend evtchn initialization failed\n");
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +1 -1
index 01c0d47..817d272 100644
@@ -360,7 +360,7 @@ static int setup_suspend_evtchn(checkpoint_state* s)
     return -1;
   }
 
-  s->suspend_evtchn = xc_suspend_evtchn_init(s->xch, s->xce, s->domid, port);
+  s->suspend_evtchn = xc_suspend_evtchn_init_exclusive(s->xch, s->xce, s->domid, port);
   if (s->suspend_evtchn < 0) {
       s->errstr = "failed to bind suspend event channel";
       return -1;
tools/xcutils/xc_save.c +1 -1
index e34bd2c..974f706 100644
@@ -202,7 +202,7 @@ main(int argc, char **argv)
         else
         {
             si.suspend_evtchn =
-              xc_suspend_evtchn_init(si.xch, si.xce, si.domid, port);
+              xc_suspend_evtchn_init_exclusive(si.xch, si.xce, si.domid, port);
 
             if (si.suspend_evtchn < 0)
                 warnx("suspend event channel initialization failed, "
Signature · 1 line
1.7.10.4
#15 lore raw

Re: [PATCH 07/19] libxc: suspend: Rename, improve xc_suspend_evtchn_init

Quoted 29 lines · Ian Jackson

xc_suspend_evtchn_init expects to eat the first event on the xce. If the xce is used for any other purpose then this can break. Document this fact and rename the function to xc_suspend_evtchn_init_exclusive. (I haven't checked the call sites for improper shared use of the xce.)

Provide a corresponding xc_suspend_evtchn_init_sane which doesn't try to eat an event, and instead leaves the caller the ability to demultiplex.

Also document that xc_await_suspend needs exclusive use of the xce.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Shriram Rajagopalan <[email protected]>
---
 tools/libxc/xc_suspend.c                           |   21 ++++++++++++++++----
 tools/libxc/xenguest.h                             |   13 +++++++++++-
 tools/libxl/libxl_dom.c                            |    2 +-
 tools/misc/xen-hptool.c                            |    2 +-
 .../python/xen/lowlevel/checkpoint/libcheckpoint.c |    2 +-
 tools/xcutils/xc_save.c                            |    2 +-
 6 files changed, 33 insertions(+), 9 deletions(-)
tools/libxc/xc_suspend.c +0 -0
index 1ace411..7968a44 100644

tools/libxc/xc_suspend.c line 18

Quoted 5 lines
tools/libxc/xc_suspend.c +2 -0
@@ -14,6 +14,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <assert.h>
+

This appears unused - is it debugging which slipped in?

~Andrew

Quoted 119 lines
tools/libxc/xc_suspend.c +15 -4
 #include "xc_private.h"
 #include "xenguest.h"
 
@@ -102,7 +104,7 @@ int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int
     return unlock_suspend_event(xch, domid);
 }
 
-int xc_suspend_evtchn_init(xc_interface *xch, xc_evtchn *xce, int domid, int port)
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, int port)
 {
     int rc, suspend_evtchn = -1;
 
@@ -121,9 +123,6 @@ int xc_suspend_evtchn_init(xc_interface *xch, xc_evtchn *xce, int domid, int por
         goto cleanup;
     }
 
-    /* event channel is pending immediately after binding */
-    xc_await_suspend(xch, xce, suspend_evtchn);
-
     return suspend_evtchn;
 
 cleanup:
@@ -132,3 +131,17 @@ cleanup:
 
     return -1;
 }
+
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce, int domid, int port)
+{
+    int suspend_evtchn;
+
+    suspend_evtchn = xc_suspend_evtchn_init_sane(xch, xce, domid, port);
+    if (suspend_evtchn < 0)
+        return suspend_evtchn;
+
+    /* event channel is pending immediately after binding */
+    xc_await_suspend(xch, xce, suspend_evtchn);
+
+    return suspend_evtchn;
+}
tools/libxc/xenguest.h +12 -1
index a0e30e1..ce5456c 100644
@@ -256,10 +256,21 @@ int xc_hvm_build_target_mem(xc_interface *xch,
 
 int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn);
 
-int xc_suspend_evtchn_init(xc_interface *xch, xc_evtchn *xce, int domid, int port);
+/**
+ * This function eats the initial notification.
+ * xce must not be used for anything else
+ */
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce, int domid, int port);
 
+/* xce must not be used for anything else */
 int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
 
+/**
+ * The port will be signaled immediately after this call
+ * The caller should check the domain status and look for the next event
+ */
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, int port);
+
 int xc_get_bit_size(xc_interface *xch,
                     const char *image_name, const char *cmdline,
                     const char *features, int *type);
tools/libxl/libxl_dom.c +1 -1
index 55f74b2..4b42856 100644
@@ -1358,7 +1358,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 
         if (port >= 0) {
             dss->suspend_eventchn =
-                xc_suspend_evtchn_init(CTX->xch, dss->xce, dss->domid, port);
+                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce, dss->domid, port);
 
             if (dss->suspend_eventchn < 0)
                 LOG(WARN, "Suspend event channel initialization failed");
tools/misc/xen-hptool.c +1 -1
index 24c3e95..db76f79 100644
@@ -112,7 +112,7 @@ static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid, int *evtc
         fprintf(stderr, "DOM%d: No suspend port, try live migration\n", domid);
         goto failed;
     }
-    suspend_evtchn = xc_suspend_evtchn_init(xch, xce, domid, port);
+    suspend_evtchn = xc_suspend_evtchn_init_exclusive(xch, xce, domid, port);
     if (suspend_evtchn < 0)
     {
         fprintf(stderr, "Suspend evtchn initialization failed\n");
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +1 -1
index 01c0d47..817d272 100644
@@ -360,7 +360,7 @@ static int setup_suspend_evtchn(checkpoint_state* s)
     return -1;
   }
 
-  s->suspend_evtchn = xc_suspend_evtchn_init(s->xch, s->xce, s->domid, port);
+  s->suspend_evtchn = xc_suspend_evtchn_init_exclusive(s->xch, s->xce, s->domid, port);
   if (s->suspend_evtchn < 0) {
       s->errstr = "failed to bind suspend event channel";
       return -1;
tools/xcutils/xc_save.c +1 -1
index e34bd2c..974f706 100644
@@ -202,7 +202,7 @@ main(int argc, char **argv)
         else
         {
             si.suspend_evtchn =
-              xc_suspend_evtchn_init(si.xch, si.xce, si.domid, port);
+              xc_suspend_evtchn_init_exclusive(si.xch, si.xce, si.domid, port);
 
             if (si.suspend_evtchn < 0)
                 warnx("suspend event channel initialization failed, "
#16 lore raw

Re: [PATCH 07/19] libxc: suspend: Rename, improve xc_suspend_evtchn_init

Andrew Cooper writes ("Re: [Xen-devel] [PATCH 07/19] libxc: suspend: Rename, improve xc_suspend_evtchn_init"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

+#include <assert.h>

This appears unused - is it debugging which slipped in?

I think so. I'll drop this hunk.

Ian.

Acked-by on v1 7/19
#17 lore raw

Re: [PATCH 07/19] libxc: suspend: Rename, improve xc_suspend_evtchn_init

In reply to Ian Jackson
Quoted 5 lines · Ian Jackson

xc_suspend_evtchn_init expects to eat the first event on the xce. If the xce is used for any other purpose then this can break. Document this fact and rename the function to xc_suspend_evtchn_init_exclusive. (I haven't checked the call sites for improper shared use of the xce.)

Quoted 8 lines

Provide a corresponding xc_suspend_evtchn_init_sane which doesn't try to eat an event, and instead leaves the caller the ability to demultiplex.

Also document that xc_await_suspend needs exclusive use of the xce.

CC Stefano Stabellini <[email protected]>

(just to clarify: I acked this on v1 a few minutes ago before I realised there was this resend)

#18 lore raw

PATCH 8/19 libxc: suspend: Fix suspend event channel locking

In reply to Ian Jackson

Use fcntl F_SETLK, rather than writing our pid into a "lock" file. That way if we crash we don't leave the lockfile lying about. Callers now need to keep the fd for our lockfile. (We don't use flock because we don't want anyone who inherits this fd across fork to end up with a handle onto the lock.)

While we are here:
* Move the lockfile to /var/run/xen
* De-duplicate the calculation of the pathname
* Compute the buffer size for the pathname so that it will definitely not overrun (and use the computed value everywhere)
* Fix various error handling bugs

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Shriram Rajagopalan <[email protected]>
---
 tools/libxc/xc_suspend.c                           |  159 +++++++++++++-------
 tools/libxc/xenguest.h                             |   16 +-
 tools/libxl/libxl_dom.c                            |    6 +-
 tools/libxl/libxl_internal.h                       |    1 +
 tools/misc/xen-hptool.c                            |   19 ++-
 tools/python/xen/lowlevel/checkpoint/checkpoint.h  |    2 +-
 .../python/xen/lowlevel/checkpoint/libcheckpoint.c |    7 +-
 tools/xcutils/xc_save.c                            |    8 +-
 8 files changed, 149 insertions(+), 69 deletions(-)
tools/libxc/xc_suspend.c +107 -52
index 7968a44..f0f70c1 100644
@@ -15,66 +15,115 @@
  */
 
 #include <assert.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include "xc_private.h"
 #include "xenguest.h"
 
-#define SUSPEND_LOCK_FILE "/var/lib/xen/suspend_evtchn"
-static int lock_suspend_event(xc_interface *xch, int domid)
+#define SUSPEND_LOCK_FILE "/var/run/xen/suspend-evtchn-%d.lock"
+
+/*
+ * locking
+ */
+
+#define ERR(x) do{                                                      \
+    ERROR("Can't " #x " lock file for suspend event channel %s: %s\n",  \
+          suspend_file, strerror(errno));                               \
+    goto err;                                                           \
+}while(0)
+
+#define SUSPEND_FILE_BUFLEN (sizeof(SUSPEND_LOCK_FILE) + 10)
+
+static void get_suspend_file(char buf[SUSPEND_FILE_BUFLEN], int domid)
 {
-    int fd, rc;
-    mode_t mask;
-    char buf[128];
-    char suspend_file[256];
-
-    snprintf(suspend_file, sizeof(suspend_file), "%s_%d_lock.d",
-	    SUSPEND_LOCK_FILE, domid);
-    mask = umask(022);
-    fd = open(suspend_file, O_CREAT | O_EXCL | O_RDWR, 0666);
-    if (fd < 0)
-    {
-        ERROR("Can't create lock file for suspend event channel %s\n",
-		suspend_file);
-        return -EINVAL;
+    snprintf(buf, sizeof(buf), SUSPEND_LOCK_FILE, domid);
+}
+
+static int lock_suspend_event(xc_interface *xch, int domid, int *lockfd)
+{
+    int fd = -1, r;
+    char suspend_file[SUSPEND_FILE_BUFLEN];
+    struct stat ours, theirs;
+    struct flock fl;
+
+    get_suspend_file(suspend_file, domid);
+
+    *lockfd = -1;
+
+    for (;;) {
+        if (fd >= 0)
+            close (fd);
+
+        fd = open(suspend_file, O_CREAT | O_RDWR, 0600);
+        if (fd < 0)
+            ERR("create");
+
+        r = fcntl(fd, F_SETFD, FD_CLOEXEC);
+        if (r)
+            ERR("fcntl F_SETFD FD_CLOEXEC");
+
+        memset(&fl, 0, sizeof(fl));
+        fl.l_type = F_WRLCK;
+        fl.l_whence = SEEK_SET;
+        fl.l_len = 1;
+        r = fcntl(fd, F_SETLK, &fl);
+        if (r)
+            ERR("fcntl F_SETLK");
+
+        r = fstat(fd, &ours);
+        if (r)
+            ERR("fstat");
+
+        r = stat(suspend_file, &theirs);
+        if (r) {
+            if (errno == ENOENT)
+                /* try again */
+                continue;
+            ERR("stat");
+        }
+
+        if (ours.st_ino != theirs.st_ino)
+            /* someone else must have removed it while we were locking it */
+            continue;
+
+        break;
     }
-    umask(mask);
-    snprintf(buf, sizeof(buf), "%10ld", (long)getpid());
 
-    rc = write_exact(fd, buf, strlen(buf));
-    close(fd);
+    *lockfd = fd;
+    return 0;
 
-    return rc;
+ err:
+    if (fd >= 0)
+        close(fd);
+
+    return -1;
 }
 
-static int unlock_suspend_event(xc_interface *xch, int domid)
+static int unlock_suspend_event(xc_interface *xch, int domid, int *lockfd)
 {
-    int fd, pid, n;
-    char buf[128];
-    char suspend_file[256];
+    int r;
+    char suspend_file[SUSPEND_FILE_BUFLEN];
 
-    snprintf(suspend_file, sizeof(suspend_file), "%s_%d_lock.d",
-	    SUSPEND_LOCK_FILE, domid);
-    fd = open(suspend_file, O_RDWR);
+    if (*lockfd < 0)
+        return 0;
 
-    if (fd < 0)
-        return -EINVAL;
+    get_suspend_file(suspend_file, domid);
 
-    n = read(fd, buf, 127);
+    r = unlink(suspend_file);
+    if (r)
+        ERR("unlink");
 
-    close(fd);
+    r = close(*lockfd);
+    *lockfd = -1;
+    if (r)
+        ERR("close");
 
-    if (n > 0)
-    {
-        sscanf(buf, "%d", &pid);
-        /* We are the owner, so we can simply delete the file */
-        if (pid == getpid())
-        {
-            unlink(suspend_file);
-            return 0;
-        }
-    }
+ err:
+    if (*lockfd >= 0)
+        close(*lockfd);
 
-    return -EPERM;
+    return -1;
 }
 
 int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
@@ -96,20 +145,26 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
     return 0;
 }
 
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn)
+/* Internal callers are allowed to call this with suspend_evtchn<0
+ * but *lockfd>0. */
+int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce,
+                              int domid, int suspend_evtchn, int *lockfd)
 {
     if (suspend_evtchn >= 0)
         xc_evtchn_unbind(xce, suspend_evtchn);
 
-    return unlock_suspend_event(xch, domid);
+    return unlock_suspend_event(xch, domid, lockfd);
 }
 
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, int port)
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+                                int domid, int port, int *lockfd)
 {
     int rc, suspend_evtchn = -1;
 
-    if (lock_suspend_event(xch, domid))
-        return -EINVAL;
+    if (lock_suspend_event(xch, domid, lockfd)) {
+        errno = EINVAL;
+        goto cleanup;
+    }
 
     suspend_evtchn = xc_evtchn_bind_interdomain(xce, domid, port);
     if (suspend_evtchn < 0) {
@@ -126,17 +181,17 @@ int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, in
     return suspend_evtchn;
 
 cleanup:
-    if (suspend_evtchn != -1)
-        xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn);
+    xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn, lockfd);
 
     return -1;
 }
 
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce, int domid, int port)
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+                                     int domid, int port, int *lockfd)
 {
     int suspend_evtchn;
 
-    suspend_evtchn = xc_suspend_evtchn_init_sane(xch, xce, domid, port);
+    suspend_evtchn = xc_suspend_evtchn_init_sane(xch, xce, domid, port, lockfd);
     if (suspend_evtchn < 0)
         return suspend_evtchn;
 
tools/libxc/xenguest.h +13 -3
index ce5456c..1f216cd 100644
@@ -254,13 +254,19 @@ int xc_hvm_build_target_mem(xc_interface *xch,
                             int target,
                             const char *image_name);
 
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn);
+/*
+ * Sets *lockfd to -1.
+ * Has deallocated everything even on error.
+ */
+int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn, int *lockfd);
 
 /**
  * This function eats the initial notification.
  * xce must not be used for anything else
+ * See xc_suspend_evtchn_init_sane re lockfd.
  */
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce, int domid, int port);
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+                                     int domid, int port, int *lockfd);
 
 /* xce must not be used for anything else */
 int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
@@ -268,8 +274,12 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
 /**
  * The port will be signaled immediately after this call
  * The caller should check the domain status and look for the next event
+ * On success, *lockfd will be set to >=0 and *lockfd must be preserved
+ * and fed to xc_suspend_evtchn_release.  (On error *lockfd is
+ * undefined and xc_suspend_evtchn_release is not allowed.)
  */
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int domid, int port);
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+                                int domid, int port, int *lockfd);
 
 int xc_get_bit_size(xc_interface *xch,
                     const char *image_name, const char *cmdline,
tools/libxl/libxl_dom.c +4 -2
index 4b42856..48a4b8e 100644
@@ -1340,6 +1340,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
           | (dss->hvm ? XCFLAGS_HVM : 0);
 
     dss->suspend_eventchn = -1;
+    dss->guest_evtchn_lockfd = -1;
     dss->guest_responded = 0;
     dss->dm_savefile = libxl__device_model_savefile(gc, domid);
 
@@ -1358,7 +1359,8 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 
         if (port >= 0) {
             dss->suspend_eventchn =
-                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce, dss->domid, port);
+                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce,
+                                  dss->domid, port, &dss->guest_evtchn_lockfd);
 
             if (dss->suspend_eventchn < 0)
                 LOG(WARN, "Suspend event channel initialization failed");
@@ -1522,7 +1524,7 @@ static void domain_suspend_done(libxl__egc *egc,
 
     if (dss->suspend_eventchn > 0)
         xc_suspend_evtchn_release(CTX->xch, dss->xce, domid,
-                                  dss->suspend_eventchn);
+                           dss->suspend_eventchn, &dss->guest_evtchn_lockfd);
     if (dss->xce != NULL)
         xc_evtchn_close(dss->xce);
 
tools/libxl/libxl_internal.h +1 -0
index 979b266..ebbfa09 100644
@@ -2417,6 +2417,7 @@ struct libxl__domain_suspend_state {
     /* private */
     xc_evtchn *xce; /* event channel handle */
     int suspend_eventchn;
+    int guest_evtchn_lockfd;
     int hvm;
     int xcflags;
     int guest_responded;
tools/misc/xen-hptool.c +13 -6
index db76f79..bd9d936 100644
@@ -99,10 +99,13 @@ static int hp_mem_query_func(int argc, char *argv[])
 
 extern int xs_suspend_evtchn_port(int domid);
 
-static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid, int *evtchn)
+static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
+                         int *evtchn, int *lockfd)
 {
     int port, rc, suspend_evtchn = -1;
 
+    *lockfd = -1;
+
     if (!evtchn)
         return -1;
 
@@ -112,7 +115,8 @@ static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid, int *evtc
         fprintf(stderr, "DOM%d: No suspend port, try live migration\n", domid);
         goto failed;
     }
-    suspend_evtchn = xc_suspend_evtchn_init_exclusive(xch, xce, domid, port);
+    suspend_evtchn = xc_suspend_evtchn_init_exclusive(xch, xce, domid,
+                                                      port, lockfd);
     if (suspend_evtchn < 0)
     {
         fprintf(stderr, "Suspend evtchn initialization failed\n");
@@ -135,7 +139,8 @@ static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid, int *evtc
 
 failed:
     if (suspend_evtchn != -1)
-        xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn);
+        xc_suspend_evtchn_release(xch, xce, domid,
+                                  suspend_evtchn, lockfd);
 
     return -1;
 }
@@ -193,7 +198,7 @@ static int hp_mem_offline_func(int argc, char *argv[])
                 }
                 else if (status & PG_OFFLINE_OWNED)
                 {
-                    int result, suspend_evtchn = -1;
+                    int result, suspend_evtchn = -1, suspend_lockfd = -1;
                     xc_evtchn *xce;
                     xce = xc_evtchn_open(NULL, 0);
 
@@ -205,7 +210,8 @@ static int hp_mem_offline_func(int argc, char *argv[])
                     }
 
                     domid = status >> PG_OFFLINE_OWNER_SHIFT;
-                    if (suspend_guest(xch, xce, domid, &suspend_evtchn))
+                    if (suspend_guest(xch, xce, domid,
+                                      &suspend_evtchn, &suspend_lockfd))
                     {
                         fprintf(stderr, "Failed to suspend guest %d for"
                                 " mfn %lx\n", domid, mfn);
@@ -231,7 +237,8 @@ static int hp_mem_offline_func(int argc, char *argv[])
                                 mfn, domid);
                     }
                     xc_domain_resume(xch, domid, 1);
-                    xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn);
+                    xc_suspend_evtchn_release(xch, xce, domid,
+                                              suspend_evtchn, &suspend_lockfd);
                     xc_evtchn_close(xce);
                 }
                 break;
tools/python/xen/lowlevel/checkpoint/checkpoint.h +1 -1
index 187d9d7..2414956 100644
@@ -27,7 +27,7 @@ typedef struct {
     checkpoint_domtype domtype;
     int fd;
 
-    int suspend_evtchn;
+    int suspend_evtchn, suspend_lockfd;
 
     char* errstr;
 
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +5 -2
index 817d272..74ca062 100644
@@ -57,6 +57,7 @@ void checkpoint_init(checkpoint_state* s)
     s->fd = -1;
 
     s->suspend_evtchn = -1;
+    s->suspend_lockfd = -1;
 
     s->errstr = NULL;
 
@@ -360,7 +361,8 @@ static int setup_suspend_evtchn(checkpoint_state* s)
     return -1;
   }
 
-  s->suspend_evtchn = xc_suspend_evtchn_init_exclusive(s->xch, s->xce, s->domid, port);
+  s->suspend_evtchn = xc_suspend_evtchn_init_exclusive(s->xch, s->xce,
+                                    s->domid, port, &s->suspend_lockfd);
   if (s->suspend_evtchn < 0) {
       s->errstr = "failed to bind suspend event channel";
       return -1;
@@ -377,7 +379,8 @@ static void release_suspend_evtchn(checkpoint_state *s)
 {
   /* TODO: teach xen to clean up if port is unbound */
   if (s->xce != NULL && s->suspend_evtchn >= 0) {
-    xc_suspend_evtchn_release(s->xch, s->xce, s->domid, s->suspend_evtchn);
+    xc_suspend_evtchn_release(s->xch, s->xce, s->domid,
+                              s->suspend_evtchn, &s->suspend_lockfd);
     s->suspend_evtchn = -1;
   }
 }
tools/xcutils/xc_save.c +5 -3
index 974f706..bf74e46 100644
@@ -167,7 +167,7 @@ int
 main(int argc, char **argv)
 {
     unsigned int maxit, max_f, lflags;
-    int io_fd, ret, port;
+    int io_fd, ret, port, suspend_lockfd = -1;
     struct save_callbacks callbacks;
     xentoollog_level lvl;
     xentoollog_logger *l;
@@ -202,7 +202,8 @@ main(int argc, char **argv)
         else
         {
             si.suspend_evtchn =
-              xc_suspend_evtchn_init_exclusive(si.xch, si.xce, si.domid, port);
+              xc_suspend_evtchn_init_exclusive(si.xch, si.xce, si.domid,
+                                               port, &suspend_lockfd);
 
             if (si.suspend_evtchn < 0)
                 warnx("suspend event channel initialization failed, "
@@ -216,7 +217,8 @@ main(int argc, char **argv)
                          &callbacks, !!(si.flags & XCFLAGS_HVM), 0);
 
     if (si.suspend_evtchn > 0)
-	 xc_suspend_evtchn_release(si.xch, si.xce, si.domid, si.suspend_evtchn);
+	 xc_suspend_evtchn_release(si.xch, si.xce, si.domid,
+                                   si.suspend_evtchn, &suspend_lockfd);
 
     if (si.xce > 0)
         xc_evtchn_close(si.xce);
Signature · 1 line
1.7.10.4
Acked-by on v1 8/19
#19 lore raw

Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking

Quoted 8 lines · Ian Jackson

Use fcntl F_SETLK, rather than writing our pid into a "lock" file. That way if we crash we don't leave the lockfile lying about. Callers now need to keep the fd for our lockfile. (We don't use flock because we don't want anyone who inherits this fd across fork to end up with a handle onto the lock.)

While we are here:
* Move the lockfile to /var/run/xen

There isn't some autoconf'y path we should use is there? SUBSYS_DIR, localstatedir etc?

Probably we don't really use those elsewhere so it is consistent to use /var directly here too.

Quoted 7 lines

* De-duplicate the calculation of the pathname
* Compute the buffer size for the pathname so that it will definitely not overrun (and use the computed value everywhere)
* Fix various error handling bugs

CC Stefano Stabellini <[email protected]>
Quoted 11 lines
CC Shriram Rajagopalan <[email protected]>
---
 tools/libxc/xc_suspend.c                           |  159 +++++++++++++-------
 tools/libxc/xenguest.h                             |   16 +-
 tools/libxl/libxl_dom.c                            |    6 +-
 tools/libxl/libxl_internal.h                       |    1 +
 tools/misc/xen-hptool.c                            |   19 ++-
 tools/python/xen/lowlevel/checkpoint/checkpoint.h  |    2 +-
 .../python/xen/lowlevel/checkpoint/libcheckpoint.c |    7 +-
 tools/xcutils/xc_save.c                            |    8 +-
 8 files changed, 149 insertions(+), 69 deletions(-)
#20 lore raw

Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking

Ian Campbell writes ("Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking"):

Quoted 7 lines · Ian Jackson
Quoted 6 lines

Use fcntl F_SETLK, rather than writing our pid into a "lock" file. That way if we crash we don't leave the lockfile lying about. Callers now need to keep the fd for our lockfile. (We don't use flock because we don't want anyone who inherits this fd across fork to end up with a handle onto the lock.)

Quoted 5 lines
Quoted 2 lines

While we are here:
* Move the lockfile to /var/run/xen

There isn't some autoconf'y path we should use is there? SUBSYS_DIR, localstatedir etc?

There does seem to be, but I'm not really convinced that it's the right thing.

Quoted 2 lines

Probably we don't really use those elsewhere so it is consistent to use /var directly here too.

I did a grep for uses of /var/run in the whole tree, and of /var in libxl. libxl has /var/lib and /var/log hardcoded but not /var/run yet.

But xenstored, xenmon, and many of our scripts, have it hardcoded in one way or another.

So I think this is OK. Systems which are using /run will have a symlink for /var/run.

Thanks,
Ian.

#21 lore raw

Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking

Quoted 20 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking"):

Quoted 7 lines · Ian Jackson
Quoted 6 lines

Use fcntl F_SETLK, rather than writing our pid into a "lock" file. That way if we crash we don't leave the lockfile lying about. Callers now need to keep the fd for our lockfile. (We don't use flock because we don't want anyone who inherits this fd across fork to end up with a handle onto the lock.)

Quoted 5 lines
Quoted 2 lines

While we are here:
* Move the lockfile to /var/run/xen

There isn't some autoconf'y path we should use is there? SUBSYS_DIR, localstatedir etc?

There does seem to be, but I'm not really convinced that it's the right thing.

Quoted 2 lines

Probably we don't really use those elsewhere so it is consistent to use /var directly here too.

Quoted 6 lines

I did a grep for uses of /var/run in the whole tree, and of /var in libxl. libxl has /var/lib and /var/log hardcoded but not /var/run yet.

But xenstored, xenmon, and many of our scripts, have it hardcoded in one way or another.

This rings a bell -- I think when we switch prefix from /usr to /usr/local we inadvertently ended up moving a load of stuff to /usr/local/var which was just broken -- so we ended up overriding to use FHS /var instead of GNU/autoconf $PREFIX/var.

So I think you've done the right thing.

My ack stands.

Quoted 6 lines

So I think this is OK. Systems which are using /run will have a symlink for /var/run.

Thanks,
Ian.

Acked-by on v1 8/19
#22 lore raw

Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking

In reply to Ian Jackson
Quoted 507 lines · Ian Jackson

Use fcntl F_SETLK, rather than writing our pid into a "lock" file. That way if we crash we don't leave the lockfile lying about. Callers now need to keep the fd for our lockfile. (We don't use flock because we don't want anyone who inherits this fd across fork to end up with a handle onto the lock.)

While we are here:
* Move the lockfile to /var/run/xen
* De-duplicate the calculation of the pathname
* Compute the buffer size for the pathname so that it will definitely not overrun (and use the computed value everywhere)
* Fix various error handling bugs

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
CC Shriram Rajagopalan <[email protected]>
---
 tools/libxc/xc_suspend.c                           |  159
+++++++++++++-------
 tools/libxc/xenguest.h                             |   16 +-
 tools/libxl/libxl_dom.c                            |    6 +-
 tools/libxl/libxl_internal.h                       |    1 +
 tools/misc/xen-hptool.c                            |   19 ++-
 tools/python/xen/lowlevel/checkpoint/checkpoint.h  |    2 +-
 .../python/xen/lowlevel/checkpoint/libcheckpoint.c |    7 +-
 tools/xcutils/xc_save.c                            |    8 +-
 8 files changed, 149 insertions(+), 69 deletions(-)
tools/libxc/xc_suspend.c +92 -43
index 7968a44..f0f70c1 100644
@@ -15,66 +15,115 @@
  */
 
 #include <assert.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include "xc_private.h"
 #include "xenguest.h"
 
-#define SUSPEND_LOCK_FILE "/var/lib/xen/suspend_evtchn"
-static int lock_suspend_event(xc_interface *xch, int domid)
+#define SUSPEND_LOCK_FILE "/var/run/xen/suspend-evtchn-%d.lock"
+
+/*
+ * locking
+ */
+
+#define ERR(x) do{                                                      \
+    ERROR("Can't " #x " lock file for suspend event channel %s: %s\n",  \
+          suspend_file, strerror(errno));                               \
+    goto err;                                                           \
+}while(0)
+
+#define SUSPEND_FILE_BUFLEN (sizeof(SUSPEND_LOCK_FILE) + 10)
+
+static void get_suspend_file(char buf[SUSPEND_FILE_BUFLEN], int domid)
 {
-    int fd, rc;
-    mode_t mask;
-    char buf[128];
-    char suspend_file[256];
-
-    snprintf(suspend_file, sizeof(suspend_file), "%s_%d_lock.d",
-           SUSPEND_LOCK_FILE, domid);
-    mask = umask(022);
-    fd = open(suspend_file, O_CREAT | O_EXCL | O_RDWR, 0666);
-    if (fd < 0)
-    {
-        ERROR("Can't create lock file for suspend event channel %s\n",
-               suspend_file);
-        return -EINVAL;
+    snprintf(buf, sizeof(buf), SUSPEND_LOCK_FILE, domid);
+}
+
+static int lock_suspend_event(xc_interface *xch, int domid, int *lockfd)
+{
+    int fd = -1, r;
+    char suspend_file[SUSPEND_FILE_BUFLEN];
+    struct stat ours, theirs;
+    struct flock fl;
+
+    get_suspend_file(suspend_file, domid);
+
+    *lockfd = -1;
+
+    for (;;) {
+        if (fd >= 0)
+            close (fd);
+
+        fd = open(suspend_file, O_CREAT | O_RDWR, 0600);
+        if (fd < 0)
+            ERR("create");
+
+        r = fcntl(fd, F_SETFD, FD_CLOEXEC);
+        if (r)
+            ERR("fcntl F_SETFD FD_CLOEXEC");
+
+        memset(&fl, 0, sizeof(fl));
+        fl.l_type = F_WRLCK;
+        fl.l_whence = SEEK_SET;
+        fl.l_len = 1;
+        r = fcntl(fd, F_SETLK, &fl);
+        if (r)
+            ERR("fcntl F_SETLK");
+
+        r = fstat(fd, &ours);
+        if (r)
+            ERR("fstat");
+
+        r = stat(suspend_file, &theirs);
+        if (r) {
+            if (errno == ENOENT)
+                /* try again */
+                continue;
+            ERR("stat");
+        }
+
+        if (ours.st_ino != theirs.st_ino)
+            /* someone else must have removed it while we were locking it
 */
+            continue;
+
+        break;
     }
-    umask(mask);
-    snprintf(buf, sizeof(buf), "%10ld", (long)getpid());
 
-    rc = write_exact(fd, buf, strlen(buf));
-    close(fd);
+    *lockfd = fd;
+    return 0;
 
-    return rc;
+ err:
+    if (fd >= 0)
+        close(fd);
+
+    return -1;
 }
 
-static int unlock_suspend_event(xc_interface *xch, int domid)
+static int unlock_suspend_event(xc_interface *xch, int domid, int *lockfd)
 {
-    int fd, pid, n;
-    char buf[128];
-    char suspend_file[256];
+    int r;
+    char suspend_file[SUSPEND_FILE_BUFLEN];
 
-    snprintf(suspend_file, sizeof(suspend_file), "%s_%d_lock.d",
-           SUSPEND_LOCK_FILE, domid);
-    fd = open(suspend_file, O_RDWR);
+    if (*lockfd < 0)
+        return 0;
 
-    if (fd < 0)
-        return -EINVAL;
+    get_suspend_file(suspend_file, domid);
 
-    n = read(fd, buf, 127);
+    r = unlink(suspend_file);
+    if (r)
+        ERR("unlink");
 
-    close(fd);
+    r = close(*lockfd);
+    *lockfd = -1;
+    if (r)
+        ERR("close");
 
-    if (n > 0)
-    {
-        sscanf(buf, "%d", &pid);
-        /* We are the owner, so we can simply delete the file */
-        if (pid == getpid())
-        {
-            unlink(suspend_file);
-            return 0;
-        }
-    }
+ err:
+    if (*lockfd >= 0)
+        close(*lockfd);
 
-    return -EPERM;
+    return -1;
 }
 

int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int
suspend_evtchn)

tools/libxc/xc_suspend.c +11 -5
@@ -96,20 +145,26 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn
 *xce, int suspend_evtchn)
     return 0;
 }
 
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int
 domid, int suspend_evtchn)
+/* Internal callers are allowed to call this with suspend_evtchn<0
+ * but *lockfd>0. */
+int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce,
+                              int domid, int suspend_evtchn, int *lockfd)
 {
     if (suspend_evtchn >= 0)
         xc_evtchn_unbind(xce, suspend_evtchn);
 
-    return unlock_suspend_event(xch, domid);
+    return unlock_suspend_event(xch, domid, lockfd);
 }
 
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int
 domid, int port)
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+                                int domid, int port, int *lockfd)
 {
     int rc, suspend_evtchn = -1;
 
-    if (lock_suspend_event(xch, domid))
-        return -EINVAL;
+    if (lock_suspend_event(xch, domid, lockfd)) {
+        errno = EINVAL;
+        goto cleanup;
+    }
     suspend_evtchn = xc_evtchn_bind_interdomain(xce, domid, port);
     if (suspend_evtchn < 0) {
tools/libxc/xc_suspend.c +0 -0
@@ -126,17 +181,17 @@ int xc_suspend_evtchn_init_sane(xc_interface *xch,

xc_evtchn *xce, int domid, in

     return suspend_evtchn;

cleanup:

tools/libxc/xc_suspend.c +1 -3
-    if (suspend_evtchn != -1)
-        xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn);
+    xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn, lockfd);
 
     return -1;
 }
 
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,

int domid, int port)

tools/libxc/xc_suspend.c +3 -1
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+                                     int domid, int port, int *lockfd)
 {
     int suspend_evtchn;
 
-    suspend_evtchn = xc_suspend_evtchn_init_sane(xch, xce, domid, port);
+    suspend_evtchn = xc_suspend_evtchn_init_sane(xch, xce, domid, port,

lockfd);
if (suspend_evtchn < 0)
return suspend_evtchn;

tools/libxc/xenguest.h +8 -2
index ce5456c..1f216cd 100644
@@ -254,13 +254,19 @@ int xc_hvm_build_target_mem(xc_interface *xch,
                             int target,
                             const char *image_name);
 
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int
 domid, int suspend_evtchn);
+/*
+ * Sets *lockfd to -1.
+ * Has deallocated everything even on error.
+ */
+int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int
 domid, int suspend_evtchn, int *lockfd);
 
 /**
  * This function eats the initial notification.
  * xce must not be used for anything else
+ * See xc_suspend_evtchn_init_sane re lockfd.
  */
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
 int domid, int port);
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+                                     int domid, int port, int *lockfd);

/* xce must not be used for anything else */
int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int
suspend_evtchn);

tools/libxc/xenguest.h +5 -1
@@ -268,8 +274,12 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn
 *xce, int suspend_evtchn);
 /**
  * The port will be signaled immediately after this call
  * The caller should check the domain status and look for the next event
+ * On success, *lockfd will be set to >=0 and *lockfd must be preserved
+ * and fed to xc_suspend_evtchn_release.  (On error *lockfd is
+ * undefined and xc_suspend_evtchn_release is not allowed.)
  */
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce, int
 domid, int port);
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+                                int domid, int port, int *lockfd);
 

int xc_get_bit_size(xc_interface *xch,
const char *image_name, const char *cmdline,

tools/libxl/libxl_dom.c +3 -1
index 4b42856..48a4b8e 100644
@@ -1340,6 +1340,7 @@ void libxl__domain_suspend(libxl__egc *egc,
 libxl__domain_suspend_state *dss)
           | (dss->hvm ? XCFLAGS_HVM : 0);
 
     dss->suspend_eventchn = -1;
+    dss->guest_evtchn_lockfd = -1;
     dss->guest_responded = 0;
     dss->dm_savefile = libxl__device_model_savefile(gc, domid);
@@ -1358,7 +1359,8 @@ void libxl__domain_suspend(libxl__egc *egc,
 libxl__domain_suspend_state *dss)
 
         if (port >= 0) {
             dss->suspend_eventchn =
-                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce,
 dss->domid, port);
+                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce,
+                                  dss->domid, port,
 &dss->guest_evtchn_lockfd);
             if (dss->suspend_eventchn < 0)
                 LOG(WARN, "Suspend event channel initialization failed");
tools/libxl/libxl_dom.c +1 -1
@@ -1522,7 +1524,7 @@ static void domain_suspend_done(libxl__egc *egc,
 
     if (dss->suspend_eventchn > 0)
         xc_suspend_evtchn_release(CTX->xch, dss->xce, domid,
-                                  dss->suspend_eventchn);
+                           dss->suspend_eventchn,
 &dss->guest_evtchn_lockfd);
     if (dss->xce != NULL)
         xc_evtchn_close(dss->xce);
tools/libxl/libxl_internal.h +1 -0
index 979b266..ebbfa09 100644
@@ -2417,6 +2417,7 @@ struct libxl__domain_suspend_state {
     /* private */
     xc_evtchn *xce; /* event channel handle */
     int suspend_eventchn;
+    int guest_evtchn_lockfd;
     int hvm;
     int xcflags;
     int guest_responded;
tools/misc/xen-hptool.c +6 -2
index db76f79..bd9d936 100644
@@ -99,10 +99,13 @@ static int hp_mem_query_func(int argc, char *argv[])
 
 extern int xs_suspend_evtchn_port(int domid);
 
-static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
 int *evtchn)
+static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
+                         int *evtchn, int *lockfd)
 {
     int port, rc, suspend_evtchn = -1;
 
+    *lockfd = -1;
+
     if (!evtchn)
         return -1;
@@ -112,7 +115,8 @@ static int suspend_guest(xc_interface *xch, xc_evtchn
 *xce, int domid, int *evtc
         fprintf(stderr, "DOM%d: No suspend port, try live migration\n",
 domid);
         goto failed;
     }
-    suspend_evtchn = xc_suspend_evtchn_init_exclusive(xch, xce, domid,
 port);
+    suspend_evtchn = xc_suspend_evtchn_init_exclusive(xch, xce, domid,
+                                                      port, lockfd);
     if (suspend_evtchn < 0)
     {
         fprintf(stderr, "Suspend evtchn initialization failed\n");
tools/misc/xen-hptool.c +2 -1
@@ -135,7 +139,8 @@ static int suspend_guest(xc_interface *xch, xc_evtchn
 *xce, int domid, int *evtc
 
 failed:
     if (suspend_evtchn != -1)
-        xc_suspend_evtchn_release(xch, xce, domid, suspend_evtchn);
+        xc_suspend_evtchn_release(xch, xce, domid,
+                                  suspend_evtchn, lockfd);
 
     return -1;

}

tools/misc/xen-hptool.c +5 -3
@@ -193,7 +198,7 @@ static int hp_mem_offline_func(int argc, char *argv[])
                 }
                 else if (status & PG_OFFLINE_OWNED)
                 {
-                    int result, suspend_evtchn = -1;
+                    int result, suspend_evtchn = -1, suspend_lockfd = -1;
                     xc_evtchn *xce;
                     xce = xc_evtchn_open(NULL, 0);
 
@@ -205,7 +210,8 @@ static int hp_mem_offline_func(int argc, char *argv[])
                     }
 
                     domid = status >> PG_OFFLINE_OWNER_SHIFT;
-                    if (suspend_guest(xch, xce, domid, &suspend_evtchn))
+                    if (suspend_guest(xch, xce, domid,
+                                      &suspend_evtchn, &suspend_lockfd))
                     {
                         fprintf(stderr, "Failed to suspend guest %d for"
                                 " mfn %lx\n", domid, mfn);
@@ -231,7 +237,8 @@ static int hp_mem_offline_func(int argc, char *argv[])
                                 mfn, domid);
                     }
                     xc_domain_resume(xch, domid, 1);
-                    xc_suspend_evtchn_release(xch, xce, domid,
 suspend_evtchn);
+                    xc_suspend_evtchn_release(xch, xce, domid,
+                                              suspend_evtchn,
 &suspend_lockfd);
                     xc_evtchn_close(xce);
                 }
                 break;
tools/python/xen/lowlevel/checkpoint/checkpoint.h +1 -1
index 187d9d7..2414956 100644
@@ -27,7 +27,7 @@ typedef struct {
     checkpoint_domtype domtype;
     int fd;
 
-    int suspend_evtchn;
+    int suspend_evtchn, suspend_lockfd;
 
     char* errstr;
 
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +3 -1
index 817d272..74ca062 100644
@@ -57,6 +57,7 @@ void checkpoint_init(checkpoint_state* s)
     s->fd = -1;
 
     s->suspend_evtchn = -1;
+    s->suspend_lockfd = -1;
 
     s->errstr = NULL;
 
@@ -360,7 +361,8 @@ static int setup_suspend_evtchn(checkpoint_state* s)
     return -1;
   }
 
-  s->suspend_evtchn = xc_suspend_evtchn_init_exclusive(s->xch, s->xce,
 s->domid, port);
+  s->suspend_evtchn = xc_suspend_evtchn_init_exclusive(s->xch, s->xce,
+                                    s->domid, port, &s->suspend_lockfd);
   if (s->suspend_evtchn < 0) {
       s->errstr = "failed to bind suspend event channel";
       return -1;
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +2 -1
@@ -377,7 +379,8 @@ static void release_suspend_evtchn(checkpoint_state *s)
 {
   /* TODO: teach xen to clean up if port is unbound */
   if (s->xce != NULL && s->suspend_evtchn >= 0) {
-    xc_suspend_evtchn_release(s->xch, s->xce, s->domid,
 s->suspend_evtchn);
+    xc_suspend_evtchn_release(s->xch, s->xce, s->domid,
+                              s->suspend_evtchn, &s->suspend_lockfd);
     s->suspend_evtchn = -1;
   }

}

tools/xcutils/xc_save.c +3 -2
index 974f706..bf74e46 100644
@@ -167,7 +167,7 @@ int
 main(int argc, char **argv)
 {
     unsigned int maxit, max_f, lflags;
-    int io_fd, ret, port;
+    int io_fd, ret, port, suspend_lockfd = -1;
     struct save_callbacks callbacks;
     xentoollog_level lvl;
     xentoollog_logger *l;
@@ -202,7 +202,8 @@ main(int argc, char **argv)
         else
         {
             si.suspend_evtchn =
-              xc_suspend_evtchn_init_exclusive(si.xch, si.xce, si.domid,
 port);
+              xc_suspend_evtchn_init_exclusive(si.xch, si.xce, si.domid,
+                                               port, &suspend_lockfd);
 
             if (si.suspend_evtchn < 0)

warnx("suspend event channel initialization failed, "

tools/xcutils/xc_save.c +2 -1
@@ -216,7 +217,8 @@ main(int argc, char **argv)
                          &callbacks, !!(si.flags & XCFLAGS_HVM), 0);
 
     if (si.suspend_evtchn > 0)
-        xc_suspend_evtchn_release(si.xch, si.xce, si.domid,
 si.suspend_evtchn);
+        xc_suspend_evtchn_release(si.xch, si.xce, si.domid,
+                                   si.suspend_evtchn, &suspend_lockfd);
 
     if (si.xce > 0)
         xc_evtchn_close(si.xce);
Quoted 4 lines

--
1.7.10.4

Thanks for this.

Slightly off-track:
If you got a chance to test this patch, were you able to live migrate
more than one
domain simultaneously? I don't see any reason why it shouldn't be possible but I sort of vaguely remember some bug fix in the past, where we couldn't start more than one live migration at the same time

Shriram

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#23 lore raw

Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking

Shriram Rajagopalan writes ("Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking"):

Quoted 7 lines · Ian Jackson

Use fcntl F_SETLK, rather than writing our pid into a "lock" file. That way if we crash we don't leave the lockfile lying about. Callers now need to keep the fd for our lockfile. (We don't use flock because we don't want anyone who inherits this fd across fork to end up with a handle onto the lock.)

...

Quoted 1 line

Thanks.

Quoted 6 lines

Slightly off-track:
If you got a chance to test this patch, were you able to live migrate more
than one
domain simultaneously? I don't see any reason why it shouldn't be possible but I sort of vaguely remember some bug fix in the past, where we couldn't start more than one live migration at the same time

I will test that.

Thanks,
Ian.

#24 lore raw

Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking

Ian Jackson writes ("Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking"):

Quoted 9 lines

Shriram Rajagopalan writes ("Re: [PATCH 08/19] libxc: suspend: Fix suspend event channel locking"):

Quoted 6 lines

Slightly off-track:
If you got a chance to test this patch, were you able to live migrate more
than one
domain simultaneously? I don't see any reason why it shouldn't be possible but I sort of vaguely remember some bug fix in the past, where we couldn't start more than one live migration at the same time

I will test that.

I am not able to do this. This is not because of my series - I have verified that the same problem appears without it. It is because of acquire_lock() in xl_cmdimpl.c, which is related to memory allocation races. This really needs to be sorted out :-/, but at least it isn't a blocker for my series.

Ian.

#25 lore raw

PATCH 9/19 libxl: suspend: Async libxl__domain_suspend_callback

In reply to Ian Jackson

Mark the suspend callback libxl__domain_suspend_callback as asynchronous in the helper stub generator (libxl_save_msgs_gen.pl). We are going to want to provide an asynchronous version of this function to get rid of the usleeps and waiting loops in the suspend code.

libxl__domain_suspend_common_callback, the common synchronous core, which used to be provided directly as the callback function for the helper machinery, becomes libxl__domain_suspend_callback_common. It can now take a typesafe parameter.

For now, provide two very similar asynchronous wrappers for it. Each is a simple function which contains only boilerplate, calls the common synchronous core, and returns the asynchronous response.

Essentially, we have just moved (in the case of suspend callbacks) the call site of libxl__srm_callout_callback_complete. It was in the switch statement in the autogenerated _libxl_save_msgs_callout.c, and is now in the handwritten libxl_dom.c.

No functional change.

Cc Shriram Rajagopalan <[email protected]>
Cc Ian Campbell <[email protected]>

---
v2: Commit message mentions usleeps, not Remus, as motivation.
CC: Stefano Stabellini <[email protected]>

---
 tools/libxl/libxl_dom.c            |   25 +++++++++++++++++++------
 tools/libxl/libxl_internal.h       |    2 +-
 tools/libxl/libxl_save_msgs_gen.pl |    2 +-
 3 files changed, 21 insertions(+), 8 deletions(-)
tools/libxl/libxl_dom.c +19 -6
index 48a4b8e..1c0f04f 100644
@@ -1020,10 +1020,8 @@ int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid)
     return 0;
 }
 
-int libxl__domain_suspend_common_callback(void *user)
+int libxl__domain_suspend_callback_common(libxl__domain_suspend_state *dss)
 {
-    libxl__save_helper_state *shs = user;
-    libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
     STATE_AO_GC(dss->ao);
     unsigned long hvm_s_state = 0, hvm_pvdrv = 0;
     int ret;
@@ -1242,12 +1240,27 @@ int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
     return 0;
 }
 
+static void libxl__domain_suspend_callback(void *data)
+{
+    libxl__save_helper_state *shs = data;
+    libxl__egc *egc = shs->egc;
+    libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
+
+    int ok = libxl__domain_suspend_callback_common(dss);
+    libxl__xc_domain_saverestore_async_callback_done(egc, shs, ok);
+}
+
 /*----- remus callbacks -----*/
 
-static int libxl__remus_domain_suspend_callback(void *data)
+static void libxl__remus_domain_suspend_callback(void *data)
 {
+    libxl__save_helper_state *shs = data;
+    libxl__egc *egc = shs->egc;
+    libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
+
     /* REMUS TODO: Issue disk and network checkpoint reqs. */
-    return libxl__domain_suspend_common_callback(data);
+    int ok = libxl__domain_suspend_callback_common(dss);
+    libxl__xc_domain_saverestore_async_callback_done(egc, shs, ok);
 }
 
 static int libxl__remus_domain_resume_callback(void *data)
@@ -1373,7 +1386,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
         callbacks->postcopy = libxl__remus_domain_resume_callback;
         callbacks->checkpoint = libxl__remus_domain_checkpoint_callback;
     } else
-        callbacks->suspend = libxl__domain_suspend_common_callback;
+        callbacks->suspend = libxl__domain_suspend_callback;
 
     callbacks->switch_qemu_logdirty = libxl__domain_suspend_common_switch_qemu_logdirty;
     dss->shs.callbacks.save.toolstack_save = libxl__toolstack_save;
tools/libxl/libxl_internal.h +1 -1
index ebbfa09..a32d75f 100644
@@ -2717,7 +2717,7 @@ _hidden void libxl__xc_domain_save_done(libxl__egc*, void *dss_void,
 void libxl__xc_domain_saverestore_async_callback_done(libxl__egc *egc,
                            libxl__save_helper_state *shs, int return_value);
 
-_hidden int libxl__domain_suspend_common_callback(void *data);
+_hidden int libxl__domain_suspend_callback_common(libxl__domain_suspend_state*);
 _hidden void libxl__domain_suspend_common_switch_qemu_logdirty
                                (int domid, unsigned int enable, void *data);
 _hidden int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
tools/libxl/libxl_save_msgs_gen.pl +1 -1
index ee126c7..3c6bd57 100755
@@ -23,7 +23,7 @@ our @msgs = (
                                                  STRING doing_what),
                                                 'unsigned long', 'done',
                                                 'unsigned long', 'total'] ],
-    [  3, 'scxW',   "suspend", [] ],         
+    [  3, 'scxA',   "suspend", [] ],         
     [  4, 'scxW',   "postcopy", [] ],        
     [  5, 'scxA',   "checkpoint", [] ],      
     [  6, 'scxA',   "switch_qemu_logdirty",  [qw(int domid
Signature · 1 line
1.7.10.4
#26 lore raw

Re: [PATCH 09/19] libxl: suspend: Async libxl__domain_suspend_callback

Quoted 6 lines · Ian Jackson

Mark the suspend callback libxl__domain_suspend_callback as asynchronous in the helper stub generator (libxl_save_msgs_gen.pl). We are going to want to provide an asynchronous version of this function to get rid of the usleeps and waiting loops in the suspend code.

Quoted 6 lines

libxl__domain_suspend_common_callback, the common synchronous core, which used to be provided directly as the callback function for the helper machinery, becomes libxl__domain_suspend_callback_common. It can now take a typesafe parameter.

For now, provide two

two == remus & regular?

Quoted 6 lines

very similar asynchronous wrappers for it. Each
is a simple function which contains only boilerplate, calls the common synchronous core, and returns the asynchronous response.

Essentially, we have just moved (in the case of suspend callbacks) the call site of libxl__srm_callout_callback_complete.

Do you mean libxl__srm_callout_sendreply here?

Quoted 8 lines

It was in the
switch statement in the autogenerated _libxl_save_msgs_callout.c, and is now in the handwritten libxl_dom.c.

No functional change.

Cc Shriram Rajagopalan <[email protected]>
#27 lore raw

Re: [PATCH 09/19] libxl: suspend: Async libxl__domain_suspend_callback

Ian Campbell writes ("Re: [PATCH 09/19] libxl: suspend: Async libxl__domain_suspend_callback"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

For now, provide two

two == remus & regular?

Yes.

Quoted 4 lines
Quoted 2 lines

Essentially, we have just moved (in the case of suspend callbacks) the call site of libxl__srm_callout_callback_complete.

Do you mean libxl__srm_callout_sendreply here?

Yes.

I have updated the commit message in my series.

Thanks,
Ian.

#28 lore raw

Re: [PATCH 09/19] libxl: suspend: Async libxl__domain_suspend_callback

Quoted 8 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 09/19] libxl: suspend: Async libxl__domain_suspend_callback"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

For now, provide two

two == remus & regular?

Yes.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Essentially, we have just moved (in the case of suspend callbacks) the call site of libxl__srm_callout_callback_complete.

Do you mean libxl__srm_callout_sendreply here?

Yes.

I have updated the commit message in my series.

In which case: Acked-by: Ian Campbell <[email protected]>

#29 lore raw

PATCH 10/19 libxl: suspend: Async domain_suspend_callback_common

In reply to Ian Jackson

Make domain_suspend_callback_common do its work and then call dss->callback_common_done, rather than simply returning its answer.

This is preparatory to abolishing the usleeps in this function and replacing them with use of the event machinery.

No functional change.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c      |   46 ++++++++++++++++++++++++++++++------------
 tools/libxl/libxl_internal.h |    4 +++-
 2 files changed, 36 insertions(+), 14 deletions(-)
tools/libxl/libxl_dom.c +33 -13
index 1c0f04f..39f06da 100644
@@ -755,6 +755,10 @@ int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
 
 static void domain_suspend_done(libxl__egc *egc,
                         libxl__domain_suspend_state *dss, int rc);
+static void domain_suspend_callback_common_done(libxl__egc *egc,
+                                libxl__domain_suspend_state *dss, int ok);
+static void remus_domain_suspend_callback_common_done(libxl__egc *egc,
+                                libxl__domain_suspend_state *dss, int ok);
 
 /*----- complicated callback, called by xc_domain_save -----*/
 
@@ -1020,7 +1024,9 @@ int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid)
     return 0;
 }
 
-int libxl__domain_suspend_callback_common(libxl__domain_suspend_state *dss)
+/* calls dss->callback_common_done when done */
+static void domain_suspend_callback_common(libxl__egc *egc,
+                                           libxl__domain_suspend_state *dss)
 {
     STATE_AO_GC(dss->ao);
     unsigned long hvm_s_state = 0, hvm_pvdrv = 0;
@@ -1043,12 +1049,12 @@ int libxl__domain_suspend_callback_common(libxl__domain_suspend_state *dss)
         ret = xc_evtchn_notify(dss->xce, dss->suspend_eventchn);
         if (ret < 0) {
             LOG(ERROR, "xc_evtchn_notify failed ret=%d", ret);
-            return 0;
+            goto err;
         }
         ret = xc_await_suspend(CTX->xch, dss->xce, dss->suspend_eventchn);
         if (ret < 0) {
             LOG(ERROR, "xc_await_suspend failed ret=%d", ret);
-            return 0;
+            goto err;
         }
         dss->guest_responded = 1;
         goto guest_suspended;
@@ -1059,7 +1065,7 @@ int libxl__domain_suspend_callback_common(libxl__domain_suspend_state *dss)
         ret = xc_domain_shutdown(CTX->xch, domid, SHUTDOWN_suspend);
         if (ret < 0) {
             LOGE(ERROR, "xc_domain_shutdown failed");
-            return 0;
+            goto err;
         }
         /* The guest does not (need to) respond to this sort of request. */
         dss->guest_responded = 1;
@@ -1113,7 +1119,7 @@ int libxl__domain_suspend_callback_common(libxl__domain_suspend_state *dss)
          */
         if (!strcmp(state, "suspend")) {
             LOG(ERROR, "guest didn't acknowledge suspend, request cancelled");
-            return 0;
+            goto err;
         }
 
         LOG(DEBUG, "guest acknowledged suspend request");
@@ -1143,17 +1149,19 @@ int libxl__domain_suspend_callback_common(libxl__domain_suspend_state *dss)
     }
 
     LOG(ERROR, "guest did not suspend");
-    return 0;
+ err:
+    dss->callback_common_done(egc, dss, 0);
+    return;
 
  guest_suspended:
     if (dss->hvm) {
         ret = libxl__domain_suspend_device_model(gc, dss);
         if (ret) {
             LOG(ERROR, "libxl__domain_suspend_device_model failed ret=%d", ret);
-            return 0;
+            goto err;
         }
     }
-    return 1;
+    dss->callback_common_done(egc, dss, 1);
 }
 
 static inline char *physmap_path(libxl__gc *gc, uint32_t domid,
@@ -1246,10 +1254,16 @@ static void libxl__domain_suspend_callback(void *data)
     libxl__egc *egc = shs->egc;
     libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
 
-    int ok = libxl__domain_suspend_callback_common(dss);
-    libxl__xc_domain_saverestore_async_callback_done(egc, shs, ok);
+    dss->callback_common_done = domain_suspend_callback_common_done;
+    domain_suspend_callback_common(egc, dss);
 }
 
+static void domain_suspend_callback_common_done(libxl__egc *egc,
+                                libxl__domain_suspend_state *dss, int ok)
+{
+    libxl__xc_domain_saverestore_async_callback_done(egc, &dss->shs, ok);
+}    
+
 /*----- remus callbacks -----*/
 
 static void libxl__remus_domain_suspend_callback(void *data)
@@ -1258,11 +1272,17 @@ static void libxl__remus_domain_suspend_callback(void *data)
     libxl__egc *egc = shs->egc;
     libxl__domain_suspend_state *dss = CONTAINER_OF(shs, *dss, shs);
 
-    /* REMUS TODO: Issue disk and network checkpoint reqs. */
-    int ok = libxl__domain_suspend_callback_common(dss);
-    libxl__xc_domain_saverestore_async_callback_done(egc, shs, ok);
+    dss->callback_common_done = remus_domain_suspend_callback_common_done;
+    domain_suspend_callback_common(egc, dss);
 }
 
+static void remus_domain_suspend_callback_common_done(libxl__egc *egc,
+                                libxl__domain_suspend_state *dss, int ok)
+{
+    /* REMUS TODO: Issue disk and network checkpoint reqs. */
+    libxl__xc_domain_saverestore_async_callback_done(egc, &dss->shs, ok);
+}    
+
 static int libxl__remus_domain_resume_callback(void *data)
 {
     libxl__save_helper_state *shs = data;
tools/libxl/libxl_internal.h +3 -1
index a32d75f..369a1b3 100644
@@ -2425,6 +2425,8 @@ struct libxl__domain_suspend_state {
     int interval; /* checkpoint interval (for Remus) */
     libxl__save_helper_state shs;
     libxl__logdirty_switch logdirty;
+    void (*callback_common_done)(libxl__egc*,
+                                 struct libxl__domain_suspend_state*, int ok);
     /* private for libxl__domain_save_device_model */
     libxl__save_device_model_cb *save_dm_callback;
     libxl__datacopier_state save_dm_datacopier;
@@ -2717,7 +2719,7 @@ _hidden void libxl__xc_domain_save_done(libxl__egc*, void *dss_void,
 void libxl__xc_domain_saverestore_async_callback_done(libxl__egc *egc,
                            libxl__save_helper_state *shs, int return_value);
 
-_hidden int libxl__domain_suspend_callback_common(libxl__domain_suspend_state*);
+
 _hidden void libxl__domain_suspend_common_switch_qemu_logdirty
                                (int domid, unsigned int enable, void *data);
 _hidden int libxl__toolstack_save(uint32_t domid, uint8_t **buf,
Signature · 1 line
1.7.10.4
Acked-by on v1 10/19
#30 lore raw

Re: [PATCH 10/19] libxl: suspend: Async domain_suspend_callback_common

Quoted 10 lines · Ian Jackson

Make domain_suspend_callback_common do its work and then call dss->callback_common_done, rather than simply returning its answer.

This is preparatory to abolishing the usleeps in this function and replacing them with use of the event machinery.

No functional change.

CC Stefano Stabellini <[email protected]>
#31 lore raw

PATCH 11/19 libxl: suspend: Reorg domain_suspend_callback_common

In reply to Ian Jackson

Make domain_suspend_callback_common more callback-oriented:

* Turn the functionality behind the goto labels "err" and "guest_suspended" into functions which can be called just before "return".

* Deindent the "issuing %s suspend request via XenBus control node" branch; it is going to be split up into various functions as the xenstore work becomes callback-based.

No functional change.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |  158 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 103 insertions(+), 55 deletions(-)
tools/libxl/libxl_dom.c +103 -55
index 39f06da..59f6ce3 100644
@@ -1024,6 +1024,16 @@ int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid)
     return 0;
 }
 
+static void domain_suspend_common_wait_guest(libxl__egc *egc,
+                                             libxl__domain_suspend_state *dss);
+static void domain_suspend_common_guest_suspended(libxl__egc *egc,
+                                         libxl__domain_suspend_state *dss);
+static void domain_suspend_common_failed(libxl__egc *egc,
+                                         libxl__domain_suspend_state *dss);
+static void domain_suspend_common_done(libxl__egc *egc,
+                                       libxl__domain_suspend_state *dss,
+                                       bool ok);
+
 /* calls dss->callback_common_done when done */
 static void domain_suspend_callback_common(libxl__egc *egc,
                                            libxl__domain_suspend_state *dss)
@@ -1057,7 +1067,8 @@ static void domain_suspend_callback_common(libxl__egc *egc,
             goto err;
         }
         dss->guest_responded = 1;
-        goto guest_suspended;
+        domain_suspend_common_guest_suspended(egc, dss);
+        return;
     }
 
     if (dss->hvm && (!hvm_pvdrv || hvm_s_state)) {
@@ -1069,63 +1080,81 @@ static void domain_suspend_callback_common(libxl__egc *egc,
         }
         /* The guest does not (need to) respond to this sort of request. */
         dss->guest_responded = 1;
-    } else {
-        LOG(DEBUG, "issuing %s suspend request via XenBus control node",
-            dss->hvm ? "PVHVM" : "PV");
+        domain_suspend_common_wait_guest(egc, dss);
+        return;
+    }
 
-        libxl__domain_pvcontrol_write(gc, XBT_NULL, domid, "suspend");
+    LOG(DEBUG, "issuing %s suspend request via XenBus control node",
+        dss->hvm ? "PVHVM" : "PV");
 
-        LOG(DEBUG, "wait for the guest to acknowledge suspend request");
-        watchdog = 60;
-        while (!strcmp(state, "suspend") && watchdog > 0) {
-            usleep(100000);
+    libxl__domain_pvcontrol_write(gc, XBT_NULL, domid, "suspend");
 
-            state = libxl__domain_pvcontrol_read(gc, XBT_NULL, domid);
-            if (!state) state = "";
+    LOG(DEBUG, "wait for the guest to acknowledge suspend request");
+    watchdog = 60;
+    while (!strcmp(state, "suspend") && watchdog > 0) {
+        usleep(100000);
 
-            watchdog--;
-        }
+        state = libxl__domain_pvcontrol_read(gc, XBT_NULL, domid);
+        if (!state) state = "";
 
-        /*
-         * Guest appears to not be responding. Cancel the suspend
-         * request.
-         *
-         * We re-read the suspend node and clear it within a
-         * transaction in order to handle the case where we race
-         * against the guest catching up and acknowledging the request
-         * at the last minute.
-         */
-        if (!strcmp(state, "suspend")) {
-            LOG(ERROR, "guest didn't acknowledge suspend, cancelling request");
-        retry_transaction:
-            t = xs_transaction_start(CTX->xsh);
-
-            state = libxl__domain_pvcontrol_read(gc, t, domid);
-            if (!state) state = "";
-
-            if (!strcmp(state, "suspend"))
-                libxl__domain_pvcontrol_write(gc, t, domid, "");
-
-            if (!xs_transaction_end(CTX->xsh, t, 0))
-                if (errno == EAGAIN)
-                    goto retry_transaction;
+        watchdog--;
+    }
 
-        }
+    /*
+     * Guest appears to not be responding. Cancel the suspend
+     * request.
+     *
+     * We re-read the suspend node and clear it within a
+     * transaction in order to handle the case where we race
+     * against the guest catching up and acknowledging the request
+     * at the last minute.
+     */
+    if (!strcmp(state, "suspend")) {
+        LOG(ERROR, "guest didn't acknowledge suspend, cancelling request");
+    retry_transaction:
+        t = xs_transaction_start(CTX->xsh);
 
-        /*
-         * Final check for guest acknowledgement. The guest may have
-         * acknowledged while we were cancelling the request in which
-         * case we lost the race while cancelling and should continue.
-         */
-        if (!strcmp(state, "suspend")) {
-            LOG(ERROR, "guest didn't acknowledge suspend, request cancelled");
-            goto err;
-        }
+        state = libxl__domain_pvcontrol_read(gc, t, domid);
+        if (!state) state = "";
+
+        if (!strcmp(state, "suspend"))
+            libxl__domain_pvcontrol_write(gc, t, domid, "");
+
+        if (!xs_transaction_end(CTX->xsh, t, 0))
+            if (errno == EAGAIN)
+                goto retry_transaction;
 
-        LOG(DEBUG, "guest acknowledged suspend request");
-        dss->guest_responded = 1;
     }
 
+    /*
+     * Final check for guest acknowledgement. The guest may have
+     * acknowledged while we were cancelling the request in which
+     * case we lost the race while cancelling and should continue.
+     */
+    if (!strcmp(state, "suspend")) {
+        LOG(ERROR, "guest didn't acknowledge suspend, request cancelled");
+        goto err;
+    }
+
+    LOG(DEBUG, "guest acknowledged suspend request");
+    dss->guest_responded = 1;
+    domain_suspend_common_wait_guest(egc,dss);
+    return;
+
+ err:
+    domain_suspend_common_failed(egc, dss);
+}
+
+static void domain_suspend_common_wait_guest(libxl__egc *egc,
+                                             libxl__domain_suspend_state *dss)
+{
+    STATE_AO_GC(dss->ao);
+    int ret;
+    int watchdog;
+
+    /* Convenience aliases */
+    const uint32_t domid = dss->domid;
+
     LOG(DEBUG, "wait for the guest to suspend");
     watchdog = 60;
     while (watchdog > 0) {
@@ -1141,7 +1170,8 @@ static void domain_suspend_callback_common(libxl__egc *egc,
                 & XEN_DOMINF_shutdownmask;
             if (shutdown_reason == SHUTDOWN_suspend) {
                 LOG(DEBUG, "guest has suspended");
-                goto guest_suspended;
+                domain_suspend_common_guest_suspended(egc, dss);
+                return;
             }
         }
 
@@ -1149,19 +1179,37 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     }
 
     LOG(ERROR, "guest did not suspend");
- err:
-    dss->callback_common_done(egc, dss, 0);
-    return;
+    domain_suspend_common_failed(egc, dss);
+}
+
+static void domain_suspend_common_guest_suspended(libxl__egc *egc,
+                                         libxl__domain_suspend_state *dss)
+{
+    STATE_AO_GC(dss->ao);
+    int ret;
 
- guest_suspended:
     if (dss->hvm) {
         ret = libxl__domain_suspend_device_model(gc, dss);
         if (ret) {
             LOG(ERROR, "libxl__domain_suspend_device_model failed ret=%d", ret);
-            goto err;
+            domain_suspend_common_failed(egc, dss);
+            return;
         }
     }
-    dss->callback_common_done(egc, dss, 1);
+    domain_suspend_common_done(egc, dss, 1);
+}
+
+static void domain_suspend_common_failed(libxl__egc *egc,
+                                         libxl__domain_suspend_state *dss)
+{
+    domain_suspend_common_done(egc, dss, 0);
+}
+
+static void domain_suspend_common_done(libxl__egc *egc,
+                                       libxl__domain_suspend_state *dss,
+                                       bool ok)
+{
+    dss->callback_common_done(egc, dss, ok);
 }
 
 static inline char *physmap_path(libxl__gc *gc, uint32_t domid,
Signature · 1 line
1.7.10.4
Acked-by on v1 11/19
#32 lore raw

Re: [PATCH 11/19] libxl: suspend: Reorg domain_suspend_callback_common

Quoted 6 lines · Ian Jackson

Make domain_suspend_callback_common more callback-oriented:

* Turn the functionality behind the goto labels "err" and "guest_suspended" into functions which can be called just before "return".

Quoted 8 lines

* Deindent the "issuing %s suspend request via XenBus control node" branch; it is going to be split up into various functions as the xenstore work becomes callback-based.

No functional change.

CC Stefano Stabellini <[email protected]>
#33 lore raw

PATCH 12/19 libxl: suspend: New libxl__domain_pvcontrol_xspath

In reply to Ian Jackson

Factor out the pv control node xenstore path calculation into libxl__domain_pvcontrol_xspath.

This xs path calculation was open coded in libxl__domain_pvcontrol_read and _write. This is undesirable because it duplicates the code and because it makes the path inaccessible to other parts of libxl (which are soon going to want it).

No functional change.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl.c          |   21 +++++++++++----------
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 12 insertions(+), 10 deletions(-)
tools/libxl/libxl.c +11 -10
index f91a398..395d18e 100644
@@ -894,17 +894,23 @@ int libxl__domain_pvcontrol_available(libxl__gc *gc, uint32_t domid)
     return !!pvdriver;
 }
 
-char * libxl__domain_pvcontrol_read(libxl__gc *gc, xs_transaction_t t,
-                                    uint32_t domid)
+const char *libxl__domain_pvcontrol_xspath(libxl__gc *gc, uint32_t domid)
 {
-    const char *shutdown_path;
     const char *dom_path;
 
     dom_path = libxl__xs_get_dompath(gc, domid);
     if (!dom_path)
         return NULL;
 
-    shutdown_path = libxl__sprintf(gc, "%s/control/shutdown", dom_path);
+    return GCSPRINTF("%s/control/shutdown", dom_path);
+}
+
+char * libxl__domain_pvcontrol_read(libxl__gc *gc, xs_transaction_t t,
+                                    uint32_t domid)
+{
+    const char *shutdown_path;
+
+    shutdown_path = libxl__domain_pvcontrol_xspath(gc, domid);
     if (!shutdown_path)
         return NULL;
 
@@ -915,13 +921,8 @@ int libxl__domain_pvcontrol_write(libxl__gc *gc, xs_transaction_t t,
                                   uint32_t domid, const char *cmd)
 {
     const char *shutdown_path;
-    const char *dom_path;
-
-    dom_path = libxl__xs_get_dompath(gc, domid);
-    if (!dom_path)
-        return ERROR_FAIL;
 
-    shutdown_path = libxl__sprintf(gc, "%s/control/shutdown", dom_path);
+    shutdown_path = libxl__domain_pvcontrol_xspath(gc, domid);
     if (!shutdown_path)
         return ERROR_FAIL;
 
tools/libxl/libxl_internal.h +1 -0
index 369a1b3..97dbd8e 100644
@@ -979,6 +979,7 @@ _hidden int libxl__domain_resume(libxl__gc *gc, uint32_t domid,
 /* returns 0 or 1, or a libxl error code */
 _hidden int libxl__domain_pvcontrol_available(libxl__gc *gc, uint32_t domid);
 
+_hidden const char *libxl__domain_pvcontrol_xspath(libxl__gc*, uint32_t domid);
 _hidden char * libxl__domain_pvcontrol_read(libxl__gc *gc,
                                             xs_transaction_t t, uint32_t domid);
 _hidden int libxl__domain_pvcontrol_write(libxl__gc *gc, xs_transaction_t t,
Signature · 1 line
1.7.10.4
Acked-by on v1 12/19
#34 lore raw

Re: [PATCH 12/19] libxl: suspend: New libxl__domain_pvcontrol_xspath

Quoted 8 lines · Ian Jackson

Factor out the pv control node xenstore path calculation into libxl__domain_pvcontrol_xspath.

This xs path calculation was open coded in libxl__domain_pvcontrol_read and _write. This is undesirable because it duplicates the code and because it makes the path inaccessible to other parts of libxl (which are soon going to want it).

Quoted 4 lines

No functional change.

CC Stefano Stabellini <[email protected]>
#35 lore raw

PATCH 13/19 libxl: suspend: New domain_suspend_pvcontrol_acked

In reply to Ian Jackson

Factor out domain_suspend_pvcontrol_acked.

This replaces a bunch of open-coded strcmp()s and makes the code clearer. It also eliminates the need to check for state==NULL each time it's read, because we can check for NULL once before the strcmp.

No functional change.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |   17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/libxl/libxl_dom.c +10 -7
index 59f6ce3..a0b3a57 100644
@@ -1034,6 +1034,12 @@ static void domain_suspend_common_done(libxl__egc *egc,
                                        libxl__domain_suspend_state *dss,
                                        bool ok);
 
+static bool domain_suspend_pvcontrol_acked(const char *state) {
+    /* any value other than "suspend", including ENOENT, is OK */
+    if (!state) return 1;
+    return strcmp(state,"suspend");
+}
+
 /* calls dss->callback_common_done when done */
 static void domain_suspend_callback_common(libxl__egc *egc,
                                            libxl__domain_suspend_state *dss)
@@ -1091,11 +1097,10 @@ static void domain_suspend_callback_common(libxl__egc *egc,
 
     LOG(DEBUG, "wait for the guest to acknowledge suspend request");
     watchdog = 60;
-    while (!strcmp(state, "suspend") && watchdog > 0) {
+    while (!domain_suspend_pvcontrol_acked(state) && watchdog > 0) {
         usleep(100000);
 
         state = libxl__domain_pvcontrol_read(gc, XBT_NULL, domid);
-        if (!state) state = "";
 
         watchdog--;
     }
@@ -1109,21 +1114,19 @@ static void domain_suspend_callback_common(libxl__egc *egc,
      * against the guest catching up and acknowledging the request
      * at the last minute.
      */
-    if (!strcmp(state, "suspend")) {
+    if (!domain_suspend_pvcontrol_acked(state)) {
         LOG(ERROR, "guest didn't acknowledge suspend, cancelling request");
     retry_transaction:
         t = xs_transaction_start(CTX->xsh);
 
         state = libxl__domain_pvcontrol_read(gc, t, domid);
-        if (!state) state = "";
 
-        if (!strcmp(state, "suspend"))
+        if (!domain_suspend_pvcontrol_acked(state))
             libxl__domain_pvcontrol_write(gc, t, domid, "");
 
         if (!xs_transaction_end(CTX->xsh, t, 0))
             if (errno == EAGAIN)
                 goto retry_transaction;
-
     }
 
     /*
@@ -1131,7 +1134,7 @@ static void domain_suspend_callback_common(libxl__egc *egc,
      * acknowledged while we were cancelling the request in which
      * case we lost the race while cancelling and should continue.
      */
-    if (!strcmp(state, "suspend")) {
+    if (!domain_suspend_pvcontrol_acked(state)) {
         LOG(ERROR, "guest didn't acknowledge suspend, request cancelled");
         goto err;
     }
Signature · 1 line
1.7.10.4
Acked-by on v1 13/19
#36 lore raw

Re: [PATCH 13/19] libxl: suspend: New domain_suspend_pvcontrol_acked

Quoted 20 lines · Ian Jackson

Factor out domain_suspend_pvcontrol_acked.

This replaces a bunch of open-coded strcmp()s and makes the code clearer. It also eliminates the need to check for state==NULL each time it's read, because we can check for NULL once before the strcmp.

No functional change.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |   17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/libxl/libxl_dom.c +0 -0
index 59f6ce3..a0b3a57 100644

tools/libxl/libxl_dom.c static void domain_suspend_common_done(libxl__egc *egc, · line 1038

Quoted 5 lines
tools/libxl/libxl_dom.c +2 -0
@@ -1034,6 +1034,12 @@ static void domain_suspend_common_done(libxl__egc *egc,
                                        libxl__domain_suspend_state *dss,
                                        bool ok);
 
+static bool domain_suspend_pvcontrol_acked(const char *state) {
+    /* any value other than "suspend", including ENOENT, is OK */

Where ENOENT is indicated by !state I suppose?

Assuming so you can mention that here if you like, either way:

#37 lore raw

Re: [PATCH 13/19] libxl: suspend: New domain_suspend_pvcontrol_acked

Ian Campbell writes ("Re: [PATCH 13/19] libxl: suspend: New domain_suspend_pvcontrol_acked"):

Quoted 8 lines · Ian Jackson

tools/libxl/libxl_dom.c static void domain_suspend_common_done(libxl__egc *egc, · line 1038

Quoted 2 lines
tools/libxl/libxl_dom.c +2 -0
+static bool domain_suspend_pvcontrol_acked(const char *state) {
+    /* any value other than "suspend", including ENOENT, is OK */

Where ENOENT is indicated by !state I suppose?

Assuming so you can mention that here if you like, either way:

Yes, thanks. I have changed it to say:

/* any value other than "suspend", including ENOENT (i.e. !state), is OK */

Ian.

#38 lore raw

PATCH 14/19 libxl: suspend: domain_suspend_callback_common xs errs

In reply to Ian Jackson

In domain_suspend_callback_common, use libxl__xs_transaction_start in a loop, rather than xs_transaction_start and a goto label.

This will improve the error handling, but have no other semantic effect.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |   19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
tools/libxl/libxl_dom.c +11 -8
index a0b3a57..8aceba9 100644
@@ -1050,6 +1050,7 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     char *state = "suspend";
     int watchdog;
     xs_transaction_t t;
+    int rc;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
@@ -1116,17 +1117,19 @@ static void domain_suspend_callback_common(libxl__egc *egc,
      */
     if (!domain_suspend_pvcontrol_acked(state)) {
         LOG(ERROR, "guest didn't acknowledge suspend, cancelling request");
-    retry_transaction:
-        t = xs_transaction_start(CTX->xsh);
+        for (;;) {
+            rc = libxl__xs_transaction_start(gc, &t);
+            if (rc) goto err;
 
-        state = libxl__domain_pvcontrol_read(gc, t, domid);
+            state = libxl__domain_pvcontrol_read(gc, t, domid);
 
-        if (!domain_suspend_pvcontrol_acked(state))
-            libxl__domain_pvcontrol_write(gc, t, domid, "");
+            if (!domain_suspend_pvcontrol_acked(state))
+                libxl__domain_pvcontrol_write(gc, t, domid, "");
 
-        if (!xs_transaction_end(CTX->xsh, t, 0))
-            if (errno == EAGAIN)
-                goto retry_transaction;
+            rc = libxl__xs_transaction_commit(gc, &t);
+            if (!rc) break;
+            if (rc<0) goto err;
+        }
     }
 
     /*
Signature · 1 line
1.7.10.4
Acked-by on v1 14/19
#39 lore raw

Re: [PATCH 14/19] libxl: suspend: domain_suspend_callback_common xs errs

Quoted 8 lines · Ian Jackson

In domain_suspend_callback_common, use libxl__xs_transaction_start in a loop, rather than xs_transaction_start and a goto label.

This will improve the error handling, but have no other semantic effect.

CC Stefano Stabellini <[email protected]>
#40 lore raw

PATCH 15/19 libxl: suspend: Async xenstore pvcontrol wait

In reply to Ian Jackson

When negotiating guest suspend via the xenstore pvcontrol protocol (ie when the guest does NOT support the evtchn fast suspend protocol):

Replace the use of loops and usleep with a call to libxl__xswait.

Also, replace the xenstore transaction loop with one using libxl__xs_transaction_start et al.

There is not intended to be any semantic change, other than to make the algorithm properly asynchronous.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c      |   93 ++++++++++++++++++++++++++----------------
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 59 insertions(+), 35 deletions(-)
tools/libxl/libxl_dom.c +58 -35
index 8aceba9..dde7e33 100644
@@ -1028,6 +1028,8 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss);
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
+static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
+      libxl__xswait_state *xswa, int rc, const char *state);
 static void domain_suspend_common_failed(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
 static void domain_suspend_common_done(libxl__egc *egc,
@@ -1047,10 +1049,6 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     unsigned long hvm_s_state = 0, hvm_pvdrv = 0;
     int ret;
-    char *state = "suspend";
-    int watchdog;
-    xs_transaction_t t;
-    int rc;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
@@ -1096,59 +1094,81 @@ static void domain_suspend_callback_common(libxl__egc *egc,
 
     libxl__domain_pvcontrol_write(gc, XBT_NULL, domid, "suspend");
 
-    LOG(DEBUG, "wait for the guest to acknowledge suspend request");
-    watchdog = 60;
-    while (!domain_suspend_pvcontrol_acked(state) && watchdog > 0) {
-        usleep(100000);
+    dss->pvcontrol.path = libxl__domain_pvcontrol_xspath(gc, domid);
+    if (!dss->pvcontrol.path) goto err;
 
-        state = libxl__domain_pvcontrol_read(gc, XBT_NULL, domid);
+    dss->pvcontrol.ao = ao;
+    dss->pvcontrol.what = "guest acknowledgement of suspend request";
+    dss->pvcontrol.timeout_ms = 60 * 1000;
+    dss->pvcontrol.callback = domain_suspend_common_pvcontrol_suspending;
+    libxl__xswait_start(gc, &dss->pvcontrol);
+    return;
 
-        watchdog--;
-    }
+ err:
+    domain_suspend_common_failed(egc, dss);
+}
 
-    /*
-     * Guest appears to not be responding. Cancel the suspend
-     * request.
-     *
-     * We re-read the suspend node and clear it within a
-     * transaction in order to handle the case where we race
-     * against the guest catching up and acknowledging the request
-     * at the last minute.
-     */
-    if (!domain_suspend_pvcontrol_acked(state)) {
-        LOG(ERROR, "guest didn't acknowledge suspend, cancelling request");
+static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
+      libxl__xswait_state *xswa, int rc, const char *state)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(xswa, *dss, pvcontrol);
+    STATE_AO_GC(dss->ao);
+    xs_transaction_t t = 0;
+
+    if (!rc && !domain_suspend_pvcontrol_acked(state))
+        /* keep waiting */
+        return;
+
+    libxl__xswait_stop(gc, &dss->pvcontrol);
+
+    if (rc == ERROR_TIMEDOUT) {
+        /*
+         * Guest appears to not be responding. Cancel the suspend
+         * request.
+         *
+         * We re-read the suspend node and clear it within a
+         * transaction in order to handle the case where we race
+         * against the guest catching up and acknowledging the request
+         * at the last minute.
+         */
         for (;;) {
             rc = libxl__xs_transaction_start(gc, &t);
             if (rc) goto err;
 
-            state = libxl__domain_pvcontrol_read(gc, t, domid);
+            rc = libxl__xs_read_checked(gc, t, xswa->path, &state);
+            if (rc) goto err;
+
+            if (domain_suspend_pvcontrol_acked(state))
+                break;
 
-            if (!domain_suspend_pvcontrol_acked(state))
-                libxl__domain_pvcontrol_write(gc, t, domid, "");
+            rc = libxl__xs_write_checked(gc, t, xswa->path, "");
+            if (rc) goto err;
 
             rc = libxl__xs_transaction_commit(gc, &t);
-            if (!rc) break;
+            if (!rc) {
+                LOG(ERROR,
+                    "guest didn't acknowledge suspend, cancelling request");
+                goto err;
+            }
             if (rc<0) goto err;
         }
-    }
-
-    /*
-     * Final check for guest acknowledgement. The guest may have
-     * acknowledged while we were cancelling the request in which
-     * case we lost the race while cancelling and should continue.
-     */
-    if (!domain_suspend_pvcontrol_acked(state)) {
-        LOG(ERROR, "guest didn't acknowledge suspend, request cancelled");
+    } else if (rc) {
+        /* some error in xswait's read of xenstore, already logged */
         goto err;
     }
 
+    assert(domain_suspend_pvcontrol_acked(state));
     LOG(DEBUG, "guest acknowledged suspend request");
+
+    libxl__xs_transaction_abort(gc, &t);
     dss->guest_responded = 1;
     domain_suspend_common_wait_guest(egc,dss);
     return;
 
  err:
+    libxl__xs_transaction_abort(gc, &t);
     domain_suspend_common_failed(egc, dss);
+    return;
 }
 
 static void domain_suspend_common_wait_guest(libxl__egc *egc,
@@ -1215,6 +1235,8 @@ static void domain_suspend_common_done(libxl__egc *egc,
                                        libxl__domain_suspend_state *dss,
                                        bool ok)
 {
+    EGC_GC;
+    assert(!libxl__xswait_inuse(&dss->pvcontrol));
     dss->callback_common_done(egc, dss, ok);
 }
 
@@ -1400,6 +1422,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
         &dss->shs.callbacks.save.a;
 
     logdirty_init(&dss->logdirty);
+    libxl__xswait_init(&dss->pvcontrol);
 
     switch (type) {
     case LIBXL_DOMAIN_TYPE_HVM: {
tools/libxl/libxl_internal.h +1 -0
index 97dbd8e..5ac02cb 100644
@@ -2422,6 +2422,7 @@ struct libxl__domain_suspend_state {
     int hvm;
     int xcflags;
     int guest_responded;
+    libxl__xswait_state pvcontrol;
     const char *dm_savefile;
     int interval; /* checkpoint interval (for Remus) */
     libxl__save_helper_state shs;
Signature · 1 line
1.7.10.4
#41 lore raw

Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait

Quoted 123 lines · Ian Jackson

When negotiating guest suspend via the xenstore pvcontrol protocol (ie when the guest does NOT support the evtchn fast suspend protocol):

Replace the use of loops and usleep with a call to libxl__xswait.

Also, replace the xenstore transaction loop with one using libxl__xs_transaction_start et al.

There is not intended to be any semantic change, other than to make the algorithm properly asynchronous.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c      |   93 ++++++++++++++++++++++++++----------------
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 59 insertions(+), 35 deletions(-)
tools/libxl/libxl_dom.c +46 -26
index 8aceba9..dde7e33 100644
@@ -1028,6 +1028,8 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss);
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
+static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
+      libxl__xswait_state *xswa, int rc, const char *state);
 static void domain_suspend_common_failed(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
 static void domain_suspend_common_done(libxl__egc *egc,
@@ -1047,10 +1049,6 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     unsigned long hvm_s_state = 0, hvm_pvdrv = 0;
     int ret;
-    char *state = "suspend";
-    int watchdog;
-    xs_transaction_t t;
-    int rc;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
@@ -1096,59 +1094,81 @@ static void domain_suspend_callback_common(libxl__egc *egc,
 
     libxl__domain_pvcontrol_write(gc, XBT_NULL, domid, "suspend");
 
-    LOG(DEBUG, "wait for the guest to acknowledge suspend request");
-    watchdog = 60;
-    while (!domain_suspend_pvcontrol_acked(state) && watchdog > 0) {
-        usleep(100000);
+    dss->pvcontrol.path = libxl__domain_pvcontrol_xspath(gc, domid);
+    if (!dss->pvcontrol.path) goto err;
 
-        state = libxl__domain_pvcontrol_read(gc, XBT_NULL, domid);
+    dss->pvcontrol.ao = ao;
+    dss->pvcontrol.what = "guest acknowledgement of suspend request";
+    dss->pvcontrol.timeout_ms = 60 * 1000;
+    dss->pvcontrol.callback = domain_suspend_common_pvcontrol_suspending;
+    libxl__xswait_start(gc, &dss->pvcontrol);
+    return;
 
-        watchdog--;
-    }
+ err:
+    domain_suspend_common_failed(egc, dss);
+}
 
-    /*
-     * Guest appears to not be responding. Cancel the suspend
-     * request.
-     *
-     * We re-read the suspend node and clear it within a
-     * transaction in order to handle the case where we race
-     * against the guest catching up and acknowledging the request
-     * at the last minute.
-     */
-    if (!domain_suspend_pvcontrol_acked(state)) {
-        LOG(ERROR, "guest didn't acknowledge suspend, cancelling request");
+static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
+      libxl__xswait_state *xswa, int rc, const char *state)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(xswa, *dss, pvcontrol);
+    STATE_AO_GC(dss->ao);
+    xs_transaction_t t = 0;
+
+    if (!rc && !domain_suspend_pvcontrol_acked(state))
+        /* keep waiting */
+        return;
+
+    libxl__xswait_stop(gc, &dss->pvcontrol);
+
+    if (rc == ERROR_TIMEDOUT) {
+        /*
+         * Guest appears to not be responding. Cancel the suspend
+         * request.
+         *
+         * We re-read the suspend node and clear it within a
+         * transaction in order to handle the case where we race
+         * against the guest catching up and acknowledging the request
+         * at the last minute.
+         */
         for (;;) {
             rc = libxl__xs_transaction_start(gc, &t);
             if (rc) goto err;
 
-            state = libxl__domain_pvcontrol_read(gc, t, domid);
+            rc = libxl__xs_read_checked(gc, t, xswa->path, &state);
+            if (rc) goto err;
+
+            if (domain_suspend_pvcontrol_acked(state))
+                break;
 
-            if (!domain_suspend_pvcontrol_acked(state))
-                libxl__domain_pvcontrol_write(gc, t, domid, "");
+            rc = libxl__xs_write_checked(gc, t, xswa->path, "");
+            if (rc) goto err;
 
             rc = libxl__xs_transaction_commit(gc, &t);
-            if (!rc) break;
+            if (!rc) {
+                LOG(ERROR,
+                    "guest didn't acknowledge suspend, cancelling request");

tools/libxl/libxl_dom.c static void domain_suspend_callback_common(libxl__egc *egc, · line 1138

Quoted 10 lines
tools/libxl/libxl_dom.c +2 -6
+                goto err;
+            }
             if (rc<0) goto err;
         }
-    }
-
-    /*
-     * Final check for guest acknowledgement. The guest may have
-     * acknowledged while we were cancelling the request in which
-     * case we lost the race while cancelling and should continue.

This behaviour has gone completely now?

tools/libxl/libxl_dom.c static void domain_suspend_callback_common(libxl__egc *egc, · line 1163

Quoted 12 lines
tools/libxl/libxl_dom.c +5 -3
-     */
-    if (!domain_suspend_pvcontrol_acked(state)) {
-        LOG(ERROR, "guest didn't acknowledge suspend, request cancelled");
+    } else if (rc) {
+        /* some error in xswait's read of xenstore, already logged */
         goto err;
     }
 
+    assert(domain_suspend_pvcontrol_acked(state));
     LOG(DEBUG, "guest acknowledged suspend request");
+
+    libxl__xs_transaction_abort(gc, &t);

Is this right/proper in the success case?

If rc != TIMEOUT t may not have been started, although it is initialised so I suppose this is intended to be ok.

Quoted 40 lines
tools/libxl/libxl_dom.c +5 -0
     dss->guest_responded = 1;
     domain_suspend_common_wait_guest(egc,dss);
     return;
 
  err:
+    libxl__xs_transaction_abort(gc, &t);
     domain_suspend_common_failed(egc, dss);
+    return;
 }
 
 static void domain_suspend_common_wait_guest(libxl__egc *egc,
@@ -1215,6 +1235,8 @@ static void domain_suspend_common_done(libxl__egc *egc,
                                        libxl__domain_suspend_state *dss,
                                        bool ok)
 {
+    EGC_GC;
+    assert(!libxl__xswait_inuse(&dss->pvcontrol));
     dss->callback_common_done(egc, dss, ok);
 }
 
@@ -1400,6 +1422,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
         &dss->shs.callbacks.save.a;
 
     logdirty_init(&dss->logdirty);
+    libxl__xswait_init(&dss->pvcontrol);
 
     switch (type) {
     case LIBXL_DOMAIN_TYPE_HVM: {
tools/libxl/libxl_internal.h +1 -0
index 97dbd8e..5ac02cb 100644
@@ -2422,6 +2422,7 @@ struct libxl__domain_suspend_state {
     int hvm;
     int xcflags;
     int guest_responded;
+    libxl__xswait_state pvcontrol;
     const char *dm_savefile;
     int interval; /* checkpoint interval (for Remus) */
     libxl__save_helper_state shs;
#42 lore raw

Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait

Ian Campbell writes ("Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

When negotiating guest suspend via the xenstore pvcontrol protocol (ie when the guest does NOT support the evtchn fast suspend protocol):

...

Quoted 6 lines

tools/libxl/libxl_dom.c static void domain_suspend_callback_common(libxl__egc *egc, · line 1138

Quoted 4 lines
tools/libxl/libxl_dom.c +0 -4
-    /*
-     * Final check for guest acknowledgement. The guest may have
-     * acknowledged while we were cancelling the request in which
-     * case we lost the race while cancelling and should continue.

This behaviour has gone completely now?

No, this possibility still exists. This is handled by the rc == ERROR_TIMEDOUT branch in domain_suspend_common_pvcontrol_suspending. The comment there (now) reads:

/*
* Guest appears to not be responding. Cancel the suspend
* request.
*
* We re-read the suspend node and clear it within a
* transaction in order to handle the case where we race
* against the guest catching up and acknowledging the request
* at the last minute.
*/

Quoted 6 lines

tools/libxl/libxl_dom.c static void domain_suspend_callback_common(libxl__egc *egc, · line 1163

Quoted 4 lines
tools/libxl/libxl_dom.c +3 -0
+    assert(domain_suspend_pvcontrol_acked(state));
     LOG(DEBUG, "guest acknowledged suspend request");
+
+    libxl__xs_transaction_abort(gc, &t);

Is this right/proper in the success case?

Yes. Because it has then already been committed. This is exactly the pattern from the usage comment for libxl__xs_transaction_* in libxl_internal.h.

Quoted 2 lines

If rc != TIMEOUT t may not have been started, although it is initialised so I suppose this is intended to be ok.

Yes. Precisely.

Thanks for the careful review :-).

Ian.

#43 lore raw

Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait

Quoted 16 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

When negotiating guest suspend via the xenstore pvcontrol protocol (ie when the guest does NOT support the evtchn fast suspend protocol):

...

Quoted 6 lines

tools/libxl/libxl_dom.c static void domain_suspend_callback_common(libxl__egc *egc, · line 1138

Quoted 4 lines
tools/libxl/libxl_dom.c +0 -4
-    /*
-     * Final check for guest acknowledgement. The guest may have
-     * acknowledged while we were cancelling the request in which
-     * case we lost the race while cancelling and should continue.

This behaviour has gone completely now?

No, this possibility still exists. This is handled by the rc == ERROR_TIMEDOUT branch in domain_suspend_common_pvcontrol_suspending. The comment there (now) reads:

Quoted 9 lines

/*
* Guest appears to not be responding. Cancel the suspend
* request.
*
* We re-read the suspend node and clear it within a
* transaction in order to handle the case where we race
* against the guest catching up and acknowledging the request
* at the last minute.
*/

I was querying it because the original code appears to have both of the above ("Guest appears to not..." and "Final check for..."). IOW even after the final check transaction to clear it checks state again.

I think what you've done is make that "final" check within the transaction loop with:

    if (domain_suspend_pvcontrol_acked(state))
        break;

Would it make sense to include something akin to the "Final check" comment just before this to clarify?

Quoted 11 lines
Quoted 7 lines

tools/libxl/libxl_dom.c static void domain_suspend_callback_common(libxl__egc *egc, · line 1163

Quoted 5 lines
tools/libxl/libxl_dom.c +3 -0
+    assert(domain_suspend_pvcontrol_acked(state));
     LOG(DEBUG, "guest acknowledged suspend request");
+
+    libxl__xs_transaction_abort(gc, &t);

Is this right/proper in the success case?

Yes. Because it has then already been committed. This is exactly the pattern from the usage comment for libxl__xs_transaction_* in libxl_internal.h.

That usage comment doesn't have an abort after a successful commit though i.e. in the "if (!rc) break;" case it drops straight through to the success and return something case.

I think the subtlety is that the transaction loop here differs slightly from the canonical example because of the "final check" break quoted above -- since that exits the transaction successfully without actually needing to complete the transaction.

Assuming aborting a committed transaction is ok then a comment might be worthwhile. Or do the abort just before the exceptional success break, which is a bit unconventional but then so is the check itself.

Ian.

Quoted 8 lines
Quoted 2 lines

If rc != TIMEOUT t may not have been started, although it is initialised so I suppose this is intended to be ok.

Yes. Precisely.

Thanks for the careful review :-).

Ian.

#44 lore raw

Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait

Ian Campbell writes ("Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait"):

Quoted 15 lines · Ian Jackson
Quoted 4 lines

No, this possibility still exists. This is handled by the rc == ERROR_TIMEDOUT branch in domain_suspend_common_pvcontrol_suspending. The comment there (now) reads:

Quoted 9 lines

/*
* Guest appears to not be responding. Cancel the suspend
* request.
*
* We re-read the suspend node and clear it within a
* transaction in order to handle the case where we race
* against the guest catching up and acknowledging the request
* at the last minute.
*/

Quoted 3 lines

I was querying it because the original code appears to have both of the above ("Guest appears to not..." and "Final check for..."). IOW even after the final check transaction to clear it checks state again.

That's just an artefact of the tangled control structure (and confusing comments) in the old code. At the "Final check" it's not actually reading xs again - that happened earlier. It's just coping with one of the two possible exits from the transaction loop. In the old code the last-minute-success path and the timeout-failure path are mixed during and after the loop and need to be distinguished again.

In the new code, the success case is the main path; last-minute-success comes out in the wash, falling through into the main success path. The timeout-failure path goes to err, so there is nothing corresponding to the if following "Final check".

Quoted 7 lines

I think what you've done is make that "final" check within the transaction loop with:

    if (domain_suspend_pvcontrol_acked(state))
        break;

Would it make sense to include something akin to the "Final check" comment just before this to clarify?

In the old code the "Final check" comment is the second part of the explanation "We re-read", earlier.

I think this is only confusing if you look at the confusing old code. If you just look at the new code it's clearly correct. The comment above clearly says that the purpose is to handle last-minute-success. But if you prefer I could add a comment

Quoted 1 line

if (domain_suspend_pvcontrol_acked(state))

+ /* last minute ack */

Quoted 11 lines
        break;
Quoted 5 lines
Quoted 1 line

Is this right/proper in the success case?

Yes. Because it has then already been committed. This is exactly the pattern from the usage comment for libxl__xs_transaction_* in libxl_internal.h.

That usage comment doesn't have an abort after a successful commit though i.e. in the "if (!rc) break;" case it drops straight through to the success and return something case.

Oh I see.

Quoted 8 lines

I think the subtlety is that the transaction loop here differs slightly from the canonical example because of the "final check" break quoted above -- since that exits the transaction successfully without actually needing to complete the transaction.

Assuming aborting a committed transaction is ok then a comment might be worthwhile. Or do the abort just before the exceptional success break, which is a bit unconventional but then so is the check itself.

How about if I add a new patch introducing a comment documenting formally the possible states of *t with the libxl__xs_transaction functions ?

And/or perhaps rename libxl__xs_transaction_abort to libxl__xs_transaction_cleanup ?

Thanks,
Ian.

#45 lore raw

Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait

Quoted 37 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 15/19] libxl: suspend: Async xenstore pvcontrol wait"):

Quoted 15 lines · Ian Jackson
Quoted 4 lines

No, this possibility still exists. This is handled by the rc == ERROR_TIMEDOUT branch in domain_suspend_common_pvcontrol_suspending. The comment there (now) reads:

Quoted 9 lines

/*
* Guest appears to not be responding. Cancel the suspend
* request.
*
* We re-read the suspend node and clear it within a
* transaction in order to handle the case where we race
* against the guest catching up and acknowledging the request
* at the last minute.
*/

Quoted 3 lines

I was querying it because the original code appears to have both of the above ("Guest appears to not..." and "Final check for..."). IOW even after the final check transaction to clear it checks state again.

That's just an artefact of the tangled control structure (and confusing comments) in the old code. At the "Final check" it's not actually reading xs again - that happened earlier. It's just coping with one of the two possible exits from the transaction loop. In the old code the last-minute-success path and the timeout-failure path are mixed during and after the loop and need to be distinguished again.

In the new code, the success case is the main path; last-minute-success comes out in the wash, falling through into the main success path. The timeout-failure path goes to err, so there is nothing corresponding to the if following "Final check".

Quoted 5 lines

I think what you've done is make that "final" check within the transaction loop with:

    if (domain_suspend_pvcontrol_acked(state))
        break;
Quoted 7 lines
Quoted 2 lines

Would it make sense to include something akin to the "Final check" comment just before this to clarify?

In the old code the "Final check" comment is the second part of the explanation "We re-read", earlier.

I think this is only confusing if you look at the confusing old code.

That's quite plausible.

Quoted 3 lines

If you just look at the new code it's clearly correct. The comment above clearly says that the purpose is to handle last-minute-success. But if you prefer I could add a comment

Based on the above I suppose it isn't necessary (nor harmful). As you like.

Quoted 22 lines
Quoted 2 lines

if (domain_suspend_pvcontrol_acked(state))

+ /* last minute ack */

Quoted 11 lines
        break;
Quoted 5 lines
Quoted 1 line

Is this right/proper in the success case?

Yes. Because it has then already been committed. This is exactly the pattern from the usage comment for libxl__xs_transaction_* in libxl_internal.h.

That usage comment doesn't have an abort after a successful commit though i.e. in the "if (!rc) break;" case it drops straight through to the success and return something case.

Oh I see.

Quoted 5 lines

I think the subtlety is that the transaction loop here differs slightly from the canonical example because of the "final check" break quoted above -- since that exits the transaction successfully without actually needing to complete the transaction.

Quoted 7 lines
Quoted 3 lines

Assuming aborting a committed transaction is ok then a comment might be worthwhile. Or do the abort just before the exceptional success break, which is a bit unconventional but then so is the check itself.

How about if I add a new patch introducing a comment documenting formally the possible states of *t with the libxl__xs_transaction functions ?

That would be good.

Quoted 2 lines

And/or perhaps rename libxl__xs_transaction_abort to libxl__xs_transaction_cleanup ?

This might encourage people to think they have to call it unnecessarily when they are following the regular pattern in the comment without the exception which exists in this case.

Ian.

#46 lore raw

PATCH 16/19 libxl: suspend: Abolish usleeps in domain suspend wait

In reply to Ian Jackson

Replace the use of a loop with usleep().

Instead, use a xenstore watch and an event system timeout. xenstore fires watches on @releaseDomain when a domain shuts down.

The logic which checks for the state of the domain is unchanged, and not ideal, but we will leave that for the next patch.

There is not intended to be any semantic change, other than to make the algorithm properly asynchronous and the consequential waiting on xenstore, rather than polling.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c      |   80 ++++++++++++++++++++++++++++++------------
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 60 insertions(+), 22 deletions(-)
tools/libxl/libxl_dom.c +58 -22
index dde7e33..6291115 100644
@@ -1028,8 +1028,14 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss);
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
+
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state);
+static void suspend_common_wait_guest_watch(libxl__egc *egc,
+      libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path);
+static void suspend_common_wait_guest_timeout(libxl__egc *egc,
+      libxl__ev_time *ev, const struct timeval *requested_abs);
+
 static void domain_suspend_common_failed(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
 static void domain_suspend_common_done(libxl__egc *egc,
@@ -1175,36 +1181,59 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss)
 {
     STATE_AO_GC(dss->ao);
+    int rc;
+
+    LOG(DEBUG, "wait for the guest to suspend");
+
+    rc = libxl__ev_xswatch_register(gc, &dss->guest_watch,
+                                    suspend_common_wait_guest_watch,
+                                    "@releaseDomain");
+    if (rc) goto err;
+
+    rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout, 
+                                     suspend_common_wait_guest_timeout,
+                                     60*1000);
+    if (rc) goto err;
+    return;
+
+ err:
+    domain_suspend_common_failed(egc, dss);
+}
+
+static void suspend_common_wait_guest_watch(libxl__egc *egc,
+      libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path)
+{
+    libxl__domain_suspend_state *dss =
+        CONTAINER_OF(xsw, *dss, guest_watch);
+    STATE_AO_GC(dss->ao);
+    xc_domaininfo_t info;
     int ret;
-    int watchdog;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
 
-    LOG(DEBUG, "wait for the guest to suspend");
-    watchdog = 60;
-    while (watchdog > 0) {
-        xc_domaininfo_t info;
-
-        usleep(100000);
-        ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
-        if (ret == 1 && info.domain == domid &&
-            (info.flags & XEN_DOMINF_shutdown)) {
-            int shutdown_reason;
-
-            shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
-                & XEN_DOMINF_shutdownmask;
-            if (shutdown_reason == SHUTDOWN_suspend) {
-                LOG(DEBUG, "guest has suspended");
-                domain_suspend_common_guest_suspended(egc, dss);
-                return;
-            }
+    ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
+    if (ret == 1 && info.domain == domid &&
+        (info.flags & XEN_DOMINF_shutdown)) {
+        int shutdown_reason;
+
+        shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
+            & XEN_DOMINF_shutdownmask;
+        if (shutdown_reason == SHUTDOWN_suspend) {
+            LOG(DEBUG, "guest has suspended");
+            domain_suspend_common_guest_suspended(egc, dss);
+            return;
         }
-
-        watchdog--;
     }
+    /* otherwise, keep waiting */
+}
 
-    LOG(ERROR, "guest did not suspend");
+static void suspend_common_wait_guest_timeout(libxl__egc *egc,
+      libxl__ev_time *ev, const struct timeval *requested_abs)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(ev, *dss, guest_timeout);
+    STATE_AO_GC(dss->ao);
+    LOG(ERROR, "guest did not suspend, timed out");
     domain_suspend_common_failed(egc, dss);
 }
 
@@ -1214,6 +1243,9 @@ static void domain_suspend_common_guest_suspended(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     int ret;
 
+    libxl__ev_xswatch_deregister(gc, &dss->guest_watch);
+    libxl__ev_time_deregister(gc, &dss->guest_timeout);
+
     if (dss->hvm) {
         ret = libxl__domain_suspend_device_model(gc, dss);
         if (ret) {
@@ -1237,6 +1269,8 @@ static void domain_suspend_common_done(libxl__egc *egc,
 {
     EGC_GC;
     assert(!libxl__xswait_inuse(&dss->pvcontrol));
+    libxl__ev_xswatch_deregister(gc, &dss->guest_watch);
+    libxl__ev_time_deregister(gc, &dss->guest_timeout);
     dss->callback_common_done(egc, dss, ok);
 }
 
@@ -1423,6 +1457,8 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 
     logdirty_init(&dss->logdirty);
     libxl__xswait_init(&dss->pvcontrol);
+    libxl__ev_xswatch_init(&dss->guest_watch);
+    libxl__ev_time_init(&dss->guest_timeout);
 
     switch (type) {
     case LIBXL_DOMAIN_TYPE_HVM: {
tools/libxl/libxl_internal.h +2 -0
index 5ac02cb..7ad15b5 100644
@@ -2423,6 +2423,8 @@ struct libxl__domain_suspend_state {
     int xcflags;
     int guest_responded;
     libxl__xswait_state pvcontrol;
+    libxl__ev_xswatch guest_watch;
+    libxl__ev_time guest_timeout;
     const char *dm_savefile;
     int interval; /* checkpoint interval (for Remus) */
     libxl__save_helper_state shs;
Signature · 1 line
1.7.10.4
#47 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Quoted 45 lines · Ian Jackson

Replace the use of a loop with usleep().

Instead, use a xenstore watch and an event system timeout. xenstore fires watches on @releaseDomain when a domain shuts down.

The logic which checks for the state of the domain is unchanged, and not ideal, but we will leave that for the next patch.

There is not intended to be any semantic change, other than to make the algorithm properly asynchronous and the consequential waiting on xenstore, rather than polling.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c      |   80 ++++++++++++++++++++++++++++++------------
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 60 insertions(+), 22 deletions(-)
tools/libxl/libxl_dom.c +8 -0
index dde7e33..6291115 100644
@@ -1028,8 +1028,14 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss);
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
+
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state);
+static void suspend_common_wait_guest_watch(libxl__egc *egc,
+      libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path);
+static void suspend_common_wait_guest_timeout(libxl__egc *egc,
+      libxl__ev_time *ev, const struct timeval *requested_abs);
+
 static void domain_suspend_common_failed(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
 static void domain_suspend_common_done(libxl__egc *egc,
@@ -1175,36 +1181,59 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss)
 {
     STATE_AO_GC(dss->ao);
+    int rc;
+

tools/libxl/libxl_dom.c static void domain_suspend_common_wait_guest(libxl__egc *egc, · line 1195

Quoted 10 lines
tools/libxl/libxl_dom.c +10 -0
+    LOG(DEBUG, "wait for the guest to suspend");
+
+    rc = libxl__ev_xswatch_register(gc, &dss->guest_watch,
+                                    suspend_common_wait_guest_watch,
+                                    "@releaseDomain");
+    if (rc) goto err;
+
+    rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout, 
+                                     suspend_common_wait_guest_timeout,
+                                     60*1000);

This smells a lot like the xswait stuff you introduced towards the beginning of the series.

Is the difference just that you cannot read the "path" @releaseDomain? If so can we paper over that somehow in the helper?

Ian.

#48 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 2 lines · Ian Jackson
Quoted 1 line

Replace the use of a loop with usleep().

...

Quoted 9 lines

tools/libxl/libxl_dom.c static void domain_suspend_common_wait_guest(libxl__egc *egc, · line 1195

Quoted 8 lines
tools/libxl/libxl_dom.c +8 -0
+    rc = libxl__ev_xswatch_register(gc, &dss->guest_watch,
+                                    suspend_common_wait_guest_watch,
+                                    "@releaseDomain");
+    if (rc) goto err;
+
+    rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout, 
+                                     suspend_common_wait_guest_timeout,
+                                     60*1000);
Quoted 4 lines

This smells a lot like the xswait stuff you introduced towards the beginning of the series.

Is the difference just that you cannot read the "path" @releaseDomain?

Precisely.

Quoted 1 line

If so can we paper over that somehow in the helper?

Not without complicating its interface, I think. We would really want to prevent the @releaseDomain "path" being read at all.

I guess we could special-case paths starting with "@", the way xenstore does, and always return ENOENT. I'm not sure whether this wrinkle (which would end up in the docs for the general facility) is worth the saving in the one call site where it would be relevant.

Ian.

Acked-by
#49 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Quoted 20 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 2 lines · Ian Jackson
Quoted 1 line

Replace the use of a loop with usleep().

...

Quoted 9 lines

tools/libxl/libxl_dom.c static void domain_suspend_common_wait_guest(libxl__egc *egc, · line 1195

Quoted 8 lines
tools/libxl/libxl_dom.c +8 -0
+    rc = libxl__ev_xswatch_register(gc, &dss->guest_watch,
+                                    suspend_common_wait_guest_watch,
+                                    "@releaseDomain");
+    if (rc) goto err;
+
+    rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout, 
+                                     suspend_common_wait_guest_timeout,
+                                     60*1000);
Quoted 4 lines

This smells a lot like the xswait stuff you introduced towards the beginning of the series.

Is the difference just that you cannot read the "path" @releaseDomain?

Precisely.

Quoted 7 lines
Quoted 1 line

If so can we paper over that somehow in the helper?

Not without complicating its interface, I think. We would really want to prevent the @releaseDomain "path" being read at all.

I guess we could special-case paths starting with "@", the way xenstore does, and always return ENOENT.

I wonder what xs_read on @releaseDomain actually does -- I'd not be surprised if it was ENOENT. I wonder even more what xs_write on @releaseDomain would do, hopefully "bugger off" but you never know.

Quoted 3 lines

I'm not sure whether this
wrinkle (which would end up in the docs for the general facility) is worth the saving in the one call site where it would be relevant.

I'm not sure either, I'll leave it to your best judgement. If you want to stick with this variant then:

Ian.

#50 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 7 lines · Ian Jackson
Quoted 2 lines

I guess we could special-case paths starting with "@", the way xenstore does, and always return ENOENT.

I wonder what xs_read on @releaseDomain actually does -- I'd not be surprised if it was ENOENT. I wonder even more what xs_write on @releaseDomain would do, hopefully "bugger off" but you never know.

Reading xenstored_core.c suggests both read and write give EINVAL. So making xswait work for it would indeed involve special-casing @ in libxl.

Quoted 7 lines
Quoted 3 lines

I'm not sure whether this
wrinkle (which would end up in the docs for the general facility) is worth the saving in the one call site where it would be relevant.

I'm not sure either, I'll leave it to your best judgement. If you want to stick with this variant then:

I think it's probably best but I don't know if that's just inertia...

Ian.

#51 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Quoted 5 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 4 lines · Ian Jackson
Quoted 2 lines

I guess we could special-case paths starting with "@", the way xenstore does, and always return ENOENT.

Quoted 7 lines
Quoted 3 lines

I wonder what xs_read on @releaseDomain actually does -- I'd not be surprised if it was ENOENT. I wonder even more what xs_write on @releaseDomain would do, hopefully "bugger off" but you never know.

Reading xenstored_core.c suggests both read and write give EINVAL. So making xswait work for it would indeed involve special-casing @ in libxl.

Right.

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

I'm not sure whether this
wrinkle (which would end up in the docs for the general facility) is worth the saving in the one call site where it would be relevant.

I'm not sure either, I'll leave it to your best judgement. If you want to stick with this variant then:

I think it's probably best but I don't know if that's just inertia...

Well, it's not like we'd be stuck with it if we change our minds later.

Ian.

#52 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

In reply to Ian Jackson

Ian Jackson writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 3 lines

Reading xenstored_core.c suggests both read and write give EINVAL. So making xswait work for it would indeed involve special-casing @ in libxl.

I looked at how this would seem in the API and it's very simple. See below.

I think I should probably add this to my series and use it in the later patch.

Ian.

Quoted 1 line

From 41abced84e669449a926ca38a78194a2188c7cff Mon Sep 17 00:00:00 2001

From: Ian Jackson <[email protected]>
Date: Fri, 14 Mar 2014 17:38:38 +0000
Subject: [PATCH] libxl: events: libxl__xswait* support @paths

Special-case paths starting with '@' in libxl__xswait. Attempting to read these from xenstore gives EINVAL. Callers waiting for (say) @releaseDomain will be checking for some condition which can be observed other than by looking at xenstore.

---
 tools/libxl/libxl_aoutils.c  |    8 ++++++--
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 8 insertions(+), 2 deletions(-)
tools/libxl/libxl_aoutils.c +6 -2
index 477717b..1c9eb9e 100644
@@ -69,8 +69,12 @@ void xswait_xswatch_callback(libxl__egc *egc, libxl__ev_xswatch *xsw,
     int rc;
     const char *data;
 
-    rc = libxl__xs_read_checked(gc, XBT_NULL, xswa->path, &data);
-    if (rc) { xswait_report_error(egc, xswa, rc); return; }
+    if (xswa->path[0] == '@') {
+        data = 0;
+    } else {
+        rc = libxl__xs_read_checked(gc, XBT_NULL, xswa->path, &data);
+        if (rc) { xswait_report_error(egc, xswa, rc); return; }
+    }
 
     xswa->callback(egc, xswa, 0, data);
 }
tools/libxl/libxl_internal.h +2 -0
index 644fcee..dc3d34c 100644
@@ -1110,6 +1110,8 @@ typedef struct libxl__xswait_state libxl__xswait_state;
  *     This HAS been logged.
  *     xswait will not continue (but calling libxl__xswait_stop is OK).
  *
+ * xswait.path may start with with '@', in which case no read is done
+ * and the callback will always get data==0.
  */
 typedef void libxl__xswait_callback(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *data);
Signature · 1 line
1.7.10.4
Acked-by
#53 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Quoted 18 lines · Ian Jackson

Ian Jackson writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 3 lines

Reading xenstored_core.c suggests both read and write give EINVAL. So making xswait work for it would indeed involve special-casing @ in libxl.

I looked at how this would seem in the API and it's very simple. See below.

I think I should probably add this to my series and use it in the later patch.

Ian.

From 41abced84e669449a926ca38a78194a2188c7cff Mon Sep 17 00:00:00 2001
From: Ian Jackson <[email protected]>
Date: Fri, 14 Mar 2014 17:38:38 +0000
Subject: [PATCH] libxl: events: libxl__xswait* support @paths

Quoted 6 lines

Special-case paths starting with '@' in libxl__xswait. Attempting to read these from xenstore gives EINVAL. Callers waiting for (say) @releaseDomain will be checking for some condition which can be observed other than by looking at xenstore.

(including if you fold it into the original xswait patch)

#54 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 9 lines · Ian Jackson
Quoted 6 lines

Special-case paths starting with '@' in libxl__xswait. Attempting to read these from xenstore gives EINVAL. Callers waiting for (say) @releaseDomain will be checking for some condition which can be observed other than by looking at xenstore.

Thanks.

Quoted 1 line

(including if you fold it into the original xswait patch)

I think it's marginally clearer separately and there's no point folding it in.

I've done the update to the patch which will use this ("libxl:
suspend: Abolish usleeps in domain suspend wait") and it builds but I want to test it a bit before I formally resend.

But for your edification, it's below.

Ian.

Quoted 1 line

From d337d7cf831360b0e7ed8f672db6b2559d063271 Mon Sep 17 00:00:00 2001

From: Ian Jackson <[email protected]>
Date: Fri, 6 Dec 2013 16:12:44 +0000
Subject: [PATCH 18/21] libxl: suspend: Abolish usleeps in domain suspend wait

Replace the use of a loop with usleep().

Instead, use an xswait xenstore watch with timeout. (xenstore fires watches on @releaseDomain when a domain shuts down.)

The logic which checks for the state of the domain is unchanged, and not ideal, but we will leave that for the next patch.

There is not intended to be any semantic change, other than to make the algorithm properly asynchronous and the consequential waiting be on xenstore, rather than polling.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>

---
v3: Use an xswait instead of separate watch and timeout.
Improve commit message.
Remove some trailing whitespace

---
 tools/libxl/libxl_dom.c |   74 +++++++++++++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 25 deletions(-)
tools/libxl/libxl_dom.c +49 -25
index 66c84d2..7b78c88 100644
@@ -1028,8 +1028,12 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss);
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
+
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state);
+static void suspend_common_wait_guest_watch(libxl__egc *egc,
+      libxl__xswait_state *xswa, int rc, const char *data_dummy);
+
 static void domain_suspend_common_failed(libxl__egc *egc,
                                          libxl__domain_suspend_state *dss);
 static void domain_suspend_common_done(libxl__egc *egc,
@@ -1175,37 +1179,55 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
                                              libxl__domain_suspend_state *dss)
 {
     STATE_AO_GC(dss->ao);
+    int rc;
+
+    LOG(DEBUG, "wait for the guest to suspend");
+
+    dss->guest_wait.ao = ao;
+    dss->guest_wait.what = "guest to suspend";
+    dss->guest_wait.path = "@releaseDomain";
+    dss->guest_wait.timeout_ms = 60*1000;
+    dss->guest_wait.callback = suspend_common_wait_guest_watch;
+    rc = libxl__xswait_start(gc, &dss->guest_wait);
+    if (rc) goto err;
+    return;
+
+ err:
+    domain_suspend_common_failed(egc, dss);
+}
+
+static void suspend_common_wait_guest_watch(libxl__egc *egc,
+      libxl__xswait_state *xswa, int rc, const char *data_dummy)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(xswa, *dss, guest_wait);
+    STATE_AO_GC(dss->ao);
+    xc_domaininfo_t info;
     int ret;
-    int watchdog;
+
+    if (rc) {
+        if (rc == ERROR_TIMEDOUT)
+            LOG(ERROR, "guest did not suspend, timed out");
+        domain_suspend_common_failed(egc, dss);
+        return;
+    }
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
 
-    LOG(DEBUG, "wait for the guest to suspend");
-    watchdog = 60;
-    while (watchdog > 0) {
-        xc_domaininfo_t info;
-
-        usleep(100000);
-        ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
-        if (ret == 1 && info.domain == domid &&
-            (info.flags & XEN_DOMINF_shutdown)) {
-            int shutdown_reason;
-
-            shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
-                & XEN_DOMINF_shutdownmask;
-            if (shutdown_reason == SHUTDOWN_suspend) {
-                LOG(DEBUG, "guest has suspended");
-                domain_suspend_common_guest_suspended(egc, dss);
-                return;
-            }
+    ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
+    if (ret == 1 && info.domain == domid &&
+        (info.flags & XEN_DOMINF_shutdown)) {
+        int shutdown_reason;
+
+        shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
+            & XEN_DOMINF_shutdownmask;
+        if (shutdown_reason == SHUTDOWN_suspend) {
+            LOG(DEBUG, "guest has suspended");
+            domain_suspend_common_guest_suspended(egc, dss);
+            return;
         }
-
-        watchdog--;
     }
-
-    LOG(ERROR, "guest did not suspend");
-    domain_suspend_common_failed(egc, dss);
+    /* otherwise, keep waiting */
 }
 
 static void domain_suspend_common_guest_suspended(libxl__egc *egc,
@@ -1214,6 +1236,8 @@ static void domain_suspend_common_guest_suspended(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     int ret;
 
+    libxl__xswait_stop(gc, &dss->guest_wait);
+
     if (dss->hvm) {
         ret = libxl__domain_suspend_device_model(gc, dss);
         if (ret) {
@@ -1236,7 +1260,7 @@ static void domain_suspend_common_done(libxl__egc *egc,
                                        bool ok)
 {
     EGC_GC;
-    assert(!libxl__xswait_inuse(&dss->guest_wait));
+    libxl__xswait_stop(gc,&dss->guest_wait);
     dss->callback_common_done(egc, dss, ok);
 }
 
Signature · 1 line
1.7.10.4
Acked-by
#55 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Quoted 2 lines · Ian Jackson

I think it's marginally clearer separately and there's no point folding it in.

OK.

[...]

Quoted 13 lines

From d337d7cf831360b0e7ed8f672db6b2559d063271 Mon Sep 17 00:00:00 2001
From: Ian Jackson <[email protected]>
Date: Fri, 6 Dec 2013 16:12:44 +0000
Subject: [PATCH 18/21] libxl: suspend: Abolish usleeps in domain suspend wait

Replace the use of a loop with usleep().

Instead, use an xswait xenstore watch with timeout. (xenstore fires watches on @releaseDomain when a domain shuts down.)

The logic which checks for the state of the domain is unchanged, and not ideal, but we will leave that for the next patch.

Quoted 6 lines

There is not intended to be any semantic change, other than to make the algorithm properly asynchronous and the consequential waiting be on xenstore, rather than polling.

CC Stefano Stabellini <[email protected]>
#56 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 6 lines · Ian Jackson
Quoted 3 lines

I think it's marginally clearer separately and there's no point folding it in.

OK.

So it turns out that doing this makes the final patch quite fiddly and
annoying: it needs to have a separate timeout anyway because it's also waiting for event channels. Making the two paths separate in this way produces an extra callback function etc.

So I'm going to go back to the previous approach. But I guess I might as well leave the patch "libxl: events: libxl__xswait* support @paths" in the series even though nothing uses that right now.

Thanks,
Ian.

#57 lore raw

Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait

Quoted 6 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 16/19] libxl: suspend: Abolish usleeps in domain suspend wait"):

Quoted 5 lines · Ian Jackson
Quoted 3 lines

I think it's marginally clearer separately and there's no point folding it in.

Quoted 6 lines
Quoted 1 line

OK.

So it turns out that doing this makes the final patch quite fiddly and
annoying: it needs to have a separate timeout anyway because it's also waiting for event channels. Making the two paths separate in this way produces an extra callback function etc.

OK, not to worry.

Quoted 3 lines

So I'm going to go back to the previous approach. But I guess I might as well leave the patch "libxl: events: libxl__xswait* support @paths" in the series even though nothing uses that right now.

Agreed.

Ian.

#58 lore raw

PATCH 17/19 libxl: suspend: Fix suspend wait corner cases

In reply to Ian Jackson

When we are waiting for a guest to suspend, this suspend operation would continue to wait (until the timeout) if the guest was destroyed or shut down for another reason, or if xc_domain_getinfolist failed.

Handle these cases correctly, as errors.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |   42 ++++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
tools/libxl/libxl_dom.c +30 -12
index 6291115..88700ee 100644
@@ -1208,24 +1208,42 @@ static void suspend_common_wait_guest_watch(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     xc_domaininfo_t info;
     int ret;
+    int shutdown_reason;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
 
     ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
-    if (ret == 1 && info.domain == domid &&
-        (info.flags & XEN_DOMINF_shutdown)) {
-        int shutdown_reason;
-
-        shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
-            & XEN_DOMINF_shutdownmask;
-        if (shutdown_reason == SHUTDOWN_suspend) {
-            LOG(DEBUG, "guest has suspended");
-            domain_suspend_common_guest_suspended(egc, dss);
-            return;
-        }
+    if (ret < 0) {
+        LOGE(ERROR, "unable to check for status of guest %"PRId32"", domid);
+        goto err;
+        domain_suspend_common_failed(egc, dss);
+    }
+
+    if (!(ret == 1 && info.domain == domid)) {
+        LOGE(ERROR, "guest %"PRId32" we were suspending has been destroyed",
+             domid);
+        goto err;
+    }
+
+    if (!(info.flags & XEN_DOMINF_shutdown))
+        /* keep waiting */
+        return;
+
+    shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
+        & XEN_DOMINF_shutdownmask;
+    if (shutdown_reason != SHUTDOWN_suspend) {
+        LOG(DEBUG, "guest %"PRId32" we were suspending has shut down"
+            " with unexpected reason code %d", domid, shutdown_reason);
+        goto err;
     }
-    /* otherwise, keep waiting */
+
+    LOG(DEBUG, "guest has suspended");
+    domain_suspend_common_guest_suspended(egc, dss);
+    return;
+
+ err:
+    domain_suspend_common_failed(egc, dss);
 }
 
 static void suspend_common_wait_guest_timeout(libxl__egc *egc,
Signature · 1 line
1.7.10.4
#59 lore raw

Re: [PATCH 17/19] libxl: suspend: Fix suspend wait corner cases

Quoted 32 lines · Ian Jackson

When we are waiting for a guest to suspend, this suspend operation would continue to wait (until the timeout) if the guest was destroyed or shut down for another reason, or if xc_domain_getinfolist failed.

Handle these cases correctly, as errors.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |   42 ++++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
tools/libxl/libxl_dom.c +1 -5
index 6291115..88700ee 100644
@@ -1208,24 +1208,42 @@ static void suspend_common_wait_guest_watch(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     xc_domaininfo_t info;
     int ret;
+    int shutdown_reason;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
 
     ret = xc_domain_getinfolist(CTX->xch, domid, 1, &info);
-    if (ret == 1 && info.domain == domid &&
-        (info.flags & XEN_DOMINF_shutdown)) {
-        int shutdown_reason;
-
-        shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)

tools/libxl/libxl_dom.c static void suspend_common_wait_guest_watch(libxl__egc *egc, · line 1220

Quoted 10 lines
tools/libxl/libxl_dom.c +4 -6
-            & XEN_DOMINF_shutdownmask;
-        if (shutdown_reason == SHUTDOWN_suspend) {
-            LOG(DEBUG, "guest has suspended");
-            domain_suspend_common_guest_suspended(egc, dss);
-            return;
-        }
+    if (ret < 0) {
+        LOGE(ERROR, "unable to check for status of guest %"PRId32"", domid);
+        goto err;
+        domain_suspend_common_failed(egc, dss);

You don't want this here.

tools/libxl/libxl_dom.c static void suspend_common_wait_guest_watch(libxl__egc *egc, · line 1225

Quoted 5 lines
tools/libxl/libxl_dom.c +5 -0
+    }
+
+    if (!(ret == 1 && info.domain == domid)) {
+        LOGE(ERROR, "guest %"PRId32" we were suspending has been destroyed",
+             domid);

Is there an (unlikely) race here where a new domain gets created with the same domid? Not that I have any suggestion what to do about that...

Quoted 25 lines
tools/libxl/libxl_dom.c +20 -1
+        goto err;
+    }
+
+    if (!(info.flags & XEN_DOMINF_shutdown))
+        /* keep waiting */
+        return;
+
+    shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift)
+        & XEN_DOMINF_shutdownmask;
+    if (shutdown_reason != SHUTDOWN_suspend) {
+        LOG(DEBUG, "guest %"PRId32" we were suspending has shut down"
+            " with unexpected reason code %d", domid, shutdown_reason);
+        goto err;
     }
-    /* otherwise, keep waiting */
+
+    LOG(DEBUG, "guest has suspended");
+    domain_suspend_common_guest_suspended(egc, dss);
+    return;
+
+ err:
+    domain_suspend_common_failed(egc, dss);
 }
 
 static void suspend_common_wait_guest_timeout(libxl__egc *egc,
#60 lore raw

Re: [PATCH 17/19] libxl: suspend: Fix suspend wait corner cases

Ian Campbell writes ("Re: [PATCH 17/19] libxl: suspend: Fix suspend wait corner cases"):

Quoted 7 lines · Ian Jackson

tools/libxl/libxl_dom.c static void suspend_common_wait_guest_watch(libxl__egc *egc, · line 1220

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    if (ret < 0) {
+        LOGE(ERROR, "unable to check for status of guest %"PRId32"", domid);
+        goto err;
+        domain_suspend_common_failed(egc, dss);

You don't want this here.

Indeed I don't.

Quoted 6 lines

tools/libxl/libxl_dom.c static void suspend_common_wait_guest_watch(libxl__egc *egc, · line 1225

Quoted 3 lines
tools/libxl/libxl_dom.c +3 -0
+    if (!(ret == 1 && info.domain == domid)) {
+        LOGE(ERROR, "guest %"PRId32" we were suspending has been destroyed",
+             domid);

Is there an (unlikely) race here where a new domain gets created with the same domid? Not that I have any suggestion what to do about that...

If domids are reused within the lifetime of the libxc/libxl code managing the domain, the whole edifice is unsafe. AFAICT this is a fundamental problem which cannot be avoided in any toolstack which is capable of concurrently issuing hypercalls (specifically, destroying domains).

Ian.

Acked-by
#61 lore raw

Re: [PATCH 17/19] libxl: suspend: Fix suspend wait corner cases

Quoted 15 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 17/19] libxl: suspend: Fix suspend wait corner cases"):

Quoted 7 lines · Ian Jackson

tools/libxl/libxl_dom.c static void suspend_common_wait_guest_watch(libxl__egc *egc, · line 1220

Quoted 4 lines
tools/libxl/libxl_dom.c +4 -0
+    if (ret < 0) {
+        LOGE(ERROR, "unable to check for status of guest %"PRId32"", domid);
+        goto err;
+        domain_suspend_common_failed(egc, dss);

You don't want this here.

Indeed I don't.

Quoted 4 lines
Quoted 3 lines
tools/libxl/libxl_dom.c +3 -0
+    if (!(ret == 1 && info.domain == domid)) {
+        LOGE(ERROR, "guest %"PRId32" we were suspending has been destroyed",
+             domid);
Quoted 8 lines
Quoted 2 lines

Is there an (unlikely) race here where a new domain gets created with the same domid? Not that I have any suggestion what to do about that...

If domids are reused within the lifetime of the libxc/libxl code managing the domain, the whole edifice is unsafe. AFAICT this is a fundamental problem which cannot be avoided in any toolstack which is capable of concurrently issuing hypercalls (specifically, destroying domains).

Yes. Oh well.

With the unwanted dmain_suspend call removed:

Ian.

#62 lore raw

PATCH 18/19 libxl: suspend: Async evtchn wait

In reply to Ian Jackson

When negotiating guest suspend via the evtchn ("fast") protocol, abolish synchronous wait for domain suspend.

If the guest supports the event channel suspend protocol, we used to sit in a loop waiting for it to suspend.

Instead, use the new libxl event channel event facility. When we see that the event is signaled, we look at the domain to see if it has suspended.

So the suspend operation no longer blocks with the libxl ctx lock held, and instead returns to the event loop. Additionally, domains which signal the event channel themselves, or undergo other state changes, will be handled more correctly.

We end up making a few more hypercalls.

Also, if we encounter errors setting up the suspend event channel (which should not happen), abort the operation rather than falling back to the xenstore protocol.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c      |   78 ++++++++++++++++++++++++++----------------
 tools/libxl/libxl_internal.h |    3 +-
 2 files changed, 50 insertions(+), 31 deletions(-)
tools/libxl/libxl_dom.c +49 -29
index 88700ee..c431a2d 100644
@@ -1031,8 +1031,12 @@ static void domain_suspend_common_guest_suspended(libxl__egc *egc,
 
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state);
+static void domain_suspend_common_wait_guest_evtchn(libxl__egc *egc,
+        libxl__ev_evtchn *evev);
 static void suspend_common_wait_guest_watch(libxl__egc *egc,
       libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path);
+static void suspend_common_wait_guest_check(libxl__egc *egc,
+        libxl__domain_suspend_state *dss);
 static void suspend_common_wait_guest_timeout(libxl__egc *egc,
       libxl__ev_time *ev, const struct timeval *requested_abs);
 
@@ -1054,7 +1058,7 @@ static void domain_suspend_callback_common(libxl__egc *egc,
 {
     STATE_AO_GC(dss->ao);
     unsigned long hvm_s_state = 0, hvm_pvdrv = 0;
-    int ret;
+    int ret, rc;
 
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
@@ -1064,21 +1068,19 @@ static void domain_suspend_callback_common(libxl__egc *egc,
         xc_get_hvm_param(CTX->xch, domid, HVM_PARAM_ACPI_S_STATE, &hvm_s_state);
     }
 
-    if ((hvm_s_state == 0) && (dss->suspend_eventchn >= 0)) {
+    if ((hvm_s_state == 0) && (dss->guest_evtchn.port >= 0)) {
         LOG(DEBUG, "issuing %s suspend request via event channel",
             dss->hvm ? "PVHVM" : "PV");
-        ret = xc_evtchn_notify(dss->xce, dss->suspend_eventchn);
+        ret = xc_evtchn_notify(CTX->xce, dss->guest_evtchn.port);
         if (ret < 0) {
             LOG(ERROR, "xc_evtchn_notify failed ret=%d", ret);
             goto err;
         }
-        ret = xc_await_suspend(CTX->xch, dss->xce, dss->suspend_eventchn);
-        if (ret < 0) {
-            LOG(ERROR, "xc_await_suspend failed ret=%d", ret);
-            goto err;
-        }
-        dss->guest_responded = 1;
-        domain_suspend_common_guest_suspended(egc, dss);
+
+        dss->guest_evtchn.callback = domain_suspend_common_wait_guest_evtchn;
+        rc = libxl__ev_evtchn_wait(gc, &dss->guest_evtchn);
+        if (rc) goto err;
+
         return;
     }
 
@@ -1114,6 +1116,19 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     domain_suspend_common_failed(egc, dss);
 }
 
+static void domain_suspend_common_wait_guest_evtchn(libxl__egc *egc,
+        libxl__ev_evtchn *evev)
+{
+    libxl__domain_suspend_state *dss = CONTAINER_OF(evev, *dss, guest_evtchn);
+    STATE_AO_GC(dss->ao);
+    /* If we should be done waiting, suspend_common_wait_guest_check
+     * will end up calling domain_suspend_common_guest_suspended or
+     * domain_suspend_common_failed, both of which cancel the evtchn
+     * wait.  So re-enable it now. */
+    libxl__ev_evtchn_wait(gc, &dss->guest_evtchn);
+    suspend_common_wait_guest_check(egc, dss);
+}
+
 static void domain_suspend_common_pvcontrol_suspending(libxl__egc *egc,
       libxl__xswait_state *xswa, int rc, const char *state)
 {
@@ -1203,8 +1218,13 @@ static void domain_suspend_common_wait_guest(libxl__egc *egc,
 static void suspend_common_wait_guest_watch(libxl__egc *egc,
       libxl__ev_xswatch *xsw, const char *watch_path, const char *event_path)
 {
-    libxl__domain_suspend_state *dss =
-        CONTAINER_OF(xsw, *dss, guest_watch);
+    libxl__domain_suspend_state *dss = CONTAINER_OF(xsw, *dss, guest_watch);
+    suspend_common_wait_guest_check(egc, dss);
+}
+
+static void suspend_common_wait_guest_check(libxl__egc *egc,
+        libxl__domain_suspend_state *dss)
+{
     STATE_AO_GC(dss->ao);
     xc_domaininfo_t info;
     int ret;
@@ -1261,6 +1281,7 @@ static void domain_suspend_common_guest_suspended(libxl__egc *egc,
     STATE_AO_GC(dss->ao);
     int ret;
 
+    libxl__ev_evtchn_cancel(gc, &dss->guest_evtchn);
     libxl__ev_xswatch_deregister(gc, &dss->guest_watch);
     libxl__ev_time_deregister(gc, &dss->guest_timeout);
 
@@ -1287,6 +1308,7 @@ static void domain_suspend_common_done(libxl__egc *egc,
 {
     EGC_GC;
     assert(!libxl__xswait_inuse(&dss->pvcontrol));
+    libxl__ev_evtchn_cancel(gc, &dss->guest_evtchn);
     libxl__ev_xswatch_deregister(gc, &dss->guest_watch);
     libxl__ev_time_deregister(gc, &dss->guest_timeout);
     dss->callback_common_done(egc, dss, ok);
@@ -1475,6 +1497,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
 
     logdirty_init(&dss->logdirty);
     libxl__xswait_init(&dss->pvcontrol);
+    libxl__ev_evtchn_init(&dss->guest_evtchn);
     libxl__ev_xswatch_init(&dss->guest_watch);
     libxl__ev_time_init(&dss->guest_timeout);
 
@@ -1503,7 +1526,7 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
           | (debug ? XCFLAGS_DEBUG : 0)
           | (dss->hvm ? XCFLAGS_HVM : 0);
 
-    dss->suspend_eventchn = -1;
+    dss->guest_evtchn.port = -1;
     dss->guest_evtchn_lockfd = -1;
     dss->guest_responded = 0;
     dss->dm_savefile = libxl__device_model_savefile(gc, domid);
@@ -1514,20 +1537,17 @@ void libxl__domain_suspend(libxl__egc *egc, libxl__domain_suspend_state *dss)
             dss->xcflags |= XCFLAGS_CHECKPOINT_COMPRESS;
     }
 
-    dss->xce = xc_evtchn_open(NULL, 0);
-    if (dss->xce == NULL)
-        goto out;
-    else
-    {
-        port = xs_suspend_evtchn_port(dss->domid);
+    port = xs_suspend_evtchn_port(dss->domid);
 
-        if (port >= 0) {
-            dss->suspend_eventchn =
-                xc_suspend_evtchn_init_exclusive(CTX->xch, dss->xce,
+    if (port >= 0) {
+        dss->guest_evtchn.port =
+            xc_suspend_evtchn_init_exclusive(CTX->xch, CTX->xce,
                                   dss->domid, port, &dss->guest_evtchn_lockfd);
 
-            if (dss->suspend_eventchn < 0)
-                LOG(WARN, "Suspend event channel initialization failed");
+        if (dss->guest_evtchn.port < 0) {
+            LOG(WARN, "Suspend event channel initialization failed");
+            rc = ERROR_FAIL;
+            goto out;
         }
     }
 
@@ -1686,11 +1706,11 @@ static void domain_suspend_done(libxl__egc *egc,
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
 
-    if (dss->suspend_eventchn > 0)
-        xc_suspend_evtchn_release(CTX->xch, dss->xce, domid,
-                           dss->suspend_eventchn, &dss->guest_evtchn_lockfd);
-    if (dss->xce != NULL)
-        xc_evtchn_close(dss->xce);
+    libxl__ev_evtchn_cancel(gc, &dss->guest_evtchn);
+
+    if (dss->guest_evtchn.port > 0)
+        xc_suspend_evtchn_release(CTX->xch, CTX->xce, domid,
+                           dss->guest_evtchn.port, &dss->guest_evtchn_lockfd);
 
     dss->callback(egc, dss, rc);
 }
tools/libxl/libxl_internal.h +1 -2
index 7ad15b5..ceb5af7 100644
@@ -2416,8 +2416,7 @@ struct libxl__domain_suspend_state {
     int debug;
     const libxl_domain_remus_info *remus;
     /* private */
-    xc_evtchn *xce; /* event channel handle */
-    int suspend_eventchn;
+    libxl__ev_evtchn guest_evtchn;
     int guest_evtchn_lockfd;
     int hvm;
     int xcflags;
Signature · 1 line
1.7.10.4
Acked-by on v1 18/19
#63 lore raw

Re: [PATCH 18/19] libxl: suspend: Async evtchn wait

Quoted 5 lines · Ian Jackson

When negotiating guest suspend via the evtchn ("fast") protocol, abolish synchronous wait for domain suspend.

If the guest supports the event channel suspend protocol, we used to sit in a loop waiting for it to suspend.

^ in xc_await_suspend

(I was looking for a loop being removed here)

Quoted 3 lines

Instead, use the new libxl event channel event facility. When we see that the event is signaled, we look at the domain to see if it has suspended.

I was looking for a timeout case, but I see now that this is in the next patch.

Quoted 5 lines

So the suspend operation no longer blocks with the libxl ctx lock held, and instead returns to the event loop. Additionally, domains which signal the event channel themselves, or undergo other state changes, will be handled more correctly.

Quoted 8 lines

We end up making a few more hypercalls.

Also, if we encounter errors setting up the suspend event channel (which should not happen), abort the operation rather than falling back to the xenstore protocol.

CC Stefano Stabellini <[email protected]>

Neither of the above comments necessitate a change (although you could clarify the commit message if you were so inclined):

author Acked-by Ian Campbell
#64 lore raw

Re: [PATCH 18/19] libxl: suspend: Async evtchn wait

Ian Campbell writes ("Re: [PATCH 18/19] libxl: suspend: Async evtchn wait"):

Quoted 9 lines · Ian Jackson
Quoted 2 lines

When negotiating guest suspend via the evtchn ("fast") protocol, abolish synchronous wait for domain suspend.

[stuff]

Neither of the above comments necessitate a change (although you could clarify the commit message if you were so inclined):

Thanks. Good idea. Here's the new one.

Ian.

libxl: suspend: Async evtchn wait

When negotiating guest suspend via the evtchn ("fast") protocol, abolish synchronous wait for domain suspend.

If the guest supports the event channel suspend protocol, we used to sit in a loop in xc_await_suspend waiting (perhaps indefinitely) for it to suspend.

Instead, use the new libxl event channel event facility. When we see that the event is signaled, we look at the domain to see if it has suspended. (In this patch we do not yet set a timeout; that will come next.)

So the suspend operation no longer blocks with the libxl ctx lock held, and instead returns to the event loop. Additionally, domains which signal the event channel themselves, or undergo other state changes, will be handled more correctly.

We end up making a few more hypercalls.

Also, if we encounter errors setting up the suspend event channel (which should not happen), abort the operation rather than falling back to the xenstore protocol.

---
v4: Improve commit message.

#65 lore raw

Re: [PATCH 18/19] libxl: suspend: Async evtchn wait

Quoted 5 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 18/19] libxl: suspend: Async evtchn wait"):

Quoted 4 lines · Ian Jackson
Quoted 2 lines

When negotiating guest suspend via the evtchn ("fast") protocol, abolish synchronous wait for domain suspend.

Quoted 7 lines
Quoted 5 lines

[stuff]

Neither of the above comments necessitate a change (although you could clarify the commit message if you were so inclined):

Thanks. Good idea. Here's the new one.

Looks good, thanks.

Ian.

Quoted 33 lines

Ian.

libxl: suspend: Async evtchn wait

When negotiating guest suspend via the evtchn ("fast") protocol, abolish synchronous wait for domain suspend.

If the guest supports the event channel suspend protocol, we used to sit in a loop in xc_await_suspend waiting (perhaps indefinitely) for it to suspend.

Instead, use the new libxl event channel event facility. When we see that the event is signaled, we look at the domain to see if it has suspended. (In this patch we do not yet set a timeout; that will come next.)

So the suspend operation no longer blocks with the libxl ctx lock held, and instead returns to the event loop. Additionally, domains which signal the event channel themselves, or undergo other state changes, will be handled more correctly.

We end up making a few more hypercalls.

Also, if we encounter errors setting up the suspend event channel (which should not happen), abort the operation rather than falling back to the xenstore protocol.

---
v4: Improve commit message.

#66 lore raw

PATCH 19/19 libxl: suspend: Apply guest timeout in evtchn case

In reply to Ian Jackson

When negotiating guest suspend via the evtchn ("fast") protocol, the guest may still fail to respond.

So set the timeout. The existing error path will already properly tear down our (event channel) wait.

CC Stefano Stabellini <[email protected]>
CC Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dom.c |    5 +++++
 1 file changed, 5 insertions(+)
tools/libxl/libxl_dom.c +5 -0
index c431a2d..820ec3a 100644
@@ -1081,6 +1081,11 @@ static void domain_suspend_callback_common(libxl__egc *egc,
         rc = libxl__ev_evtchn_wait(gc, &dss->guest_evtchn);
         if (rc) goto err;
 
+        rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout,
+                                         suspend_common_wait_guest_timeout,
+                                         60*1000);
+        if (rc) goto err;
+
         return;
     }
 
Signature · 1 line
1.7.10.4
Acked-by on v1 19/19
#67 lore raw

Re: [PATCH 19/19] libxl: suspend: Apply guest timeout in evtchn case

Quoted 8 lines · Ian Jackson

When negotiating guest suspend via the evtchn ("fast") protocol, the guest may still fail to respond.

So set the timeout. The existing error path will already properly tear down our (event channel) wait.

CC Stefano Stabellini <[email protected]>
#68 lore raw
In reply to Ian Jackson

Ian Jackson

Should the remus netbuf&replicated-disks patchset be rebased on top of this patchset?

Or I can send them as if this asynchronous suspend patchset is not applied.

Thanks,
Lai.

PS: git-am found some trailing whitespaces:

Applying: libxl: init: Provide a gc later in libxl_ctx_alloc
Applying: libxl: init: libxl__poller_init and _get take gc
Applying: libxl: events: const-correct *_inuse, *_isregistered
Applying: libxl: events: Provide libxl__xswait_*
Applying: libxl: events: Use libxl__xswait_* in spawn code
Applying: libxl: events: Provide libxl__ev_evtchn*
Applying: libxc: suspend: Rename, improve xc_suspend_evtchn_init
Applying: libxc: suspend: Fix suspend event channel locking
Applying: libxl: suspend: Async libxl__domain_suspend_callback /home/laijs/work/xen/.git/rebase-apply/patch:87: trailing whitespace.

    [  3, 'scxA',   "suspend", [] ],         

warning: 1 line adds whitespace errors.
Applying: libxl: suspend: Async domain_suspend_callback_common /home/laijs/work/xen/.git/rebase-apply/patch:102: trailing whitespace. }
/home/laijs/work/xen/.git/rebase-apply/patch:123: trailing whitespace. }
warning: 2 lines add whitespace errors.
Applying: libxl: suspend: Reorg domain_suspend_callback_common
Applying: libxl: suspend: New libxl__domain_pvcontrol_xspath
Applying: libxl: suspend: New domain_suspend_pvcontrol_acked
Applying: libxl: suspend: domain_suspend_callback_common xs errs
Applying: libxl: suspend: Async xenstore pvcontrol wait
Applying: libxl: suspend: Abolish usleeps in domain suspend wait /home/laijs/work/xen/.git/rebase-apply/patch:38: trailing whitespace.

    rc = libxl__ev_time_register_rel(gc, &dss->guest_timeout, 

warning: 1 line adds whitespace errors.
Applying: libxl: suspend: Fix suspend wait corner cases
Applying: libxl: suspend: Async evtchn wait
Applying: libxl: suspend: Apply guest timeout in evtchn case

Quoted 46 lines · Ian Jackson

This series removes the usleeps and waiting loops in libxl_dom.c and replaces them with event-callback code.

We want some additional libxl event facilities:
01/19 libxl: init: Provide a gc later in libxl_ctx_alloc 02/19 libxl: init: libxl__poller_init and _get take gc
a 03/19 libxl: events: const-correct *_inuse, *_isregistered
- 04/19 libxl: events: Provide libxl__xswait_*
a 05/19 libxl: events: Use libxl__xswait_* in spawn code
+ 06/19 libxl: events: Provide libxl__ev_evtchn*

We need to clean up some unfortunate code in libxc:
07/19 libxc: suspend: Rename, improve xc_suspend_evtchn_init 08/19 libxc: suspend: Fix suspend event channel locking

We do some shuffling around of the libxl suspend control flow:
- 09/19 libxl: suspend: Async libxl__domain_suspend_callback 10/19 libxl: suspend: Async domain_suspend_callback_common 11/19 libxl: suspend: Reorg domain_suspend_callback_common 12/19 libxl: suspend: New libxl__domain_pvcontrol_xspath 13/19 libxl: suspend: New domain_suspend_pvcontrol_acked
No functional change in those five. These changes are broken down just to make the changes reviewable.

Finally, we can start to work on the event code, removing the bugs, usleeps and loops one at a time:
14/19 libxl: suspend: domain_suspend_callback_common xs errs 15/19 libxl: suspend: Async xenstore pvcontrol wait 16/19 libxl: suspend: Abolish usleeps in domain suspend wait 17/19 libxl: suspend: Fix suspend wait corner cases 18/19 libxl: suspend: Async evtchn wait
19/19 libxl: suspend: Apply guest timeout in evtchn case

Notes:

   +   modified in v2
   -   modified in v2 but only in commit message or comments
   a   acked

I have tested v2 with a Debian pvops kernel (xenstore pvcontrol suspend signalling) and OpenSUSE 11 (event channel suspend signalling). v2.1 is a rebase onto current staging.

I haven't really touched the Remus-specific code here but this series ought to be suitable for the Remus developers to base things on.

#69 lore raw

Lai Jiangshan writes ("Re: [PATCH v2.1 RESEND 00/19] libxl: asynchronous suspend"):

Quoted 2 lines

Should the remus netbuf&replicated-disks patchset be rebased on top of this patchset?

I think that would be best, but I'd wait for an opinion from Ian Campbell before doing the rebase. He's just got back and will hopefully comment shortly...

Quoted 1 line

PS: git-am found some trailing whitespaces:

I'll fix this, thanks.

Ian.