Skip to contentSkip to conversation

toolstack-based approach to pvhvm guest kexec

PATCH In review Vitaly Kuznetsov 4 participants 10 messages Aug 25, 2014 – Aug 26, 2014

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover toolstack-based approach to pvhvm guest kexec

Here is an early prototype/proof-of-concept of an idea that instead of taking care of all things (event channels, shared/vcpu info, grants,... ) we can rebuild the whole domain performing kexec with toolstack.

This is a 'naive and simple' solution leveraging save/restore path. It was tested with the changes to linux kernel listed below.

Now I'm going to take a look at how we can avoid copying memory and just reassign it. Any ideas, suggestions and pointers are more than welcome!

arch/x86/xen/enlighten.c +24 -0
index c0cb11f..b3b8ff0 100644
@@ -33,6 +33,10 @@
 #include <linux/memblock.h>
 #include <linux/edd.h>
 
+#ifdef CONFIG_KEXEC
+#include <linux/kexec.h>
+#endif
+
 #include <xen/xen.h>
 #include <xen/events.h>
 #include <xen/interface/xen.h>
@@ -1810,6 +1814,22 @@ static struct notifier_block xen_hvm_cpu_notifier = {
        .notifier_call  = xen_hvm_cpu_notify,
 };
 
+#ifdef CONFIG_KEXEC
+static void xen_pvhvm_kexec_shutdown(void)
+{
+       native_machine_shutdown();
+       if (kexec_in_progress) {
+               xen_reboot(SHUTDOWN_kexec);
+       }
+}
+
+static void xen_pvhvm_crash_shutdown(struct pt_regs *regs)
+{
+       native_machine_crash_shutdown(regs);
+       xen_reboot(SHUTDOWN_kexec);
+}
+#endif
+
 static void __init xen_hvm_guest_init(void)
 {
        init_hvm_pv_info();
@@ -1826,6 +1846,10 @@ static void __init xen_hvm_guest_init(void)
        x86_init.irqs.intr_init = xen_init_IRQ;
        xen_hvm_init_time_ops();
        xen_hvm_init_mmu_ops();
+#ifdef CONFIG_KEXEC
+       machine_ops.shutdown = xen_pvhvm_kexec_shutdown;
+       machine_ops.crash_shutdown = xen_pvhvm_crash_shutdown;
+#endif
 }
 
 static bool xen_nopv = false;
include/xen/interface/sched.h +1 -0
index 9ce0839..6ccf3c1 100644
@@ -107,5 +107,6 @@ struct sched_watchdog {
 #define SHUTDOWN_suspend    2  /* Clean up, save suspend info, kill.         */
 #define SHUTDOWN_crash      3  /* Tell controller we've crashed.             */
 #define SHUTDOWN_watchdog   4  /* Restart because watchdog time expired.     */
+#define SHUTDOWN_kexec      5  /* Soft-reset for kexec.                      */
 
 #endif /* __XEN_PUBLIC_SCHED_H__ */

Vitaly Kuznetsov (4):
Introduce SHUTDOWN_kexec shutdown reason
libxc: treat domain in SHUTDOWN_kexec state as being already suspended
libxl: add SHUTDOWN_kexec state support
libxl: rebuild guest domains on kexec

 tools/libxc/xc_domain_save.c      |  3 +-
 tools/libxl/libxl.c               |  1 +
 tools/libxl/libxl.h               |  1 +
 tools/libxl/libxl_dom.c           |  6 ++++
 tools/libxl/libxl_internal.h      |  1 +
 tools/libxl/libxl_types.idl       |  1 +
 tools/libxl/xl_cmdimpl.c          | 62 +++++++++++++++++++++++++++++++++++++--
 tools/python/xen/lowlevel/xl/xl.c |  1 +
 xen/include/public/sched.h        |  3 +-
 9 files changed, 75 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/4 Introduce SHUTDOWN_kexec shutdown reason

---
 tools/libxl/libxl_types.idl       | 1 +
 tools/python/xen/lowlevel/xl/xl.c | 1 +
 xen/include/public/sched.h        | 3 ++-
 3 files changed, 4 insertions(+), 1 deletion(-)
tools/libxl/libxl_types.idl +1 -0
index 649ce50..2405c13 100644
@@ -148,6 +148,7 @@ libxl_shutdown_reason = Enumeration("shutdown_reason", [
     (2, "suspend"),
     (3, "crash"),
     (4, "watchdog"),
+    (5, "kexec"),
     ], init_val = "LIBXL_SHUTDOWN_REASON_UNKNOWN")
 
 libxl_vga_interface_type = Enumeration("vga_interface_type", [
tools/python/xen/lowlevel/xl/xl.c +1 -0
index 32f982a..04231f1 100644
@@ -784,6 +784,7 @@ PyMODINIT_FUNC initxl(void)
     _INT_CONST_LIBXL(m, SHUTDOWN_REASON_SUSPEND);
     _INT_CONST_LIBXL(m, SHUTDOWN_REASON_CRASH);
     _INT_CONST_LIBXL(m, SHUTDOWN_REASON_WATCHDOG);
+    _INT_CONST_LIBXL(m, SHUTDOWN_REASON_KEXEC);
 
     genwrap__init(m);
 }
xen/include/public/sched.h +2 -1
index a30b11d..6abf225 100644
@@ -158,7 +158,8 @@ DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t);
 #define SHUTDOWN_suspend    2  /* Clean up, save suspend info, kill.         */
 #define SHUTDOWN_crash      3  /* Tell controller we've crashed.             */
 #define SHUTDOWN_watchdog   4  /* Restart because watchdog time expired.     */
-#define SHUTDOWN_MAX        4  /* Maximum valid shutdown reason.             */
+#define SHUTDOWN_kexec      5  /* Soft-reboot for kexec.                     */
+#define SHUTDOWN_MAX        5  /* Maximum valid shutdown reason.             */
 /* ` } */
 
 #endif /* __XEN_PUBLIC_SCHED_H__ */
Signature · 1 line
1.9.3
#2 lore raw

Re: [PATCH RFC/WIP 1/4] Introduce SHUTDOWN_kexec shutdown reason

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 162

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.08.14 at 15:42, <[email protected]> wrote:
xen/include/public/sched.h +2 -1
@@ -158,7 +158,8 @@ DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t);
 #define SHUTDOWN_suspend    2  /* Clean up, save suspend info, kill.         */
 #define SHUTDOWN_crash      3  /* Tell controller we've crashed.             */
 #define SHUTDOWN_watchdog   4  /* Restart because watchdog time expired.     */
-#define SHUTDOWN_MAX        4  /* Maximum valid shutdown reason.             */
+#define SHUTDOWN_kexec      5  /* Soft-reboot for kexec.                     */
+#define SHUTDOWN_MAX        5  /* Maximum valid shutdown reason.             */

I have to admit that I dislike the term "kexec" in here as being Linux- centric (I admit there being a kexec hypercall, but that's intentionally Linux-centric I would say) - why not SHUTDOWN_soft_reset?

Jan

#3 lore raw

Re: [PATCH RFC/WIP 1/4] Introduce SHUTDOWN_kexec shutdown reason

In reply to Vitaly Kuznetsov

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 161

Quoted 8 lines · Vitaly Kuznetsov
xen/include/public/sched.h +1 -1
@@ -158,7 +158,8 @@ DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t);
 #define SHUTDOWN_suspend    2  /* Clean up, save suspend info, kill.         */
 #define SHUTDOWN_crash      3  /* Tell controller we've crashed.             */
 #define SHUTDOWN_watchdog   4  /* Restart because watchdog time expired.     */
-#define SHUTDOWN_MAX        4  /* Maximum valid shutdown reason.             */
+#define SHUTDOWN_kexec      5  /* Soft-reboot for kexec.                     */

I agree with Jan on the name.

The documentation also needs to be expanded. It should be clear that this hypercall may return in a new domain.

David

#4 lore raw

Re: [PATCH RFC/WIP 1/4] Introduce SHUTDOWN_kexec shutdown reason

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 26.08.14 at 12:16, <[email protected]> wrote:

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 158

Quoted 4 lines · Vitaly Kuznetsov
xen/include/public/sched.h +0 -0
@@ -158,7 +158,8 @@ DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t);
 #define SHUTDOWN_suspend    2  /* Clean up, save suspend info, kill.        

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 159

Quoted 1 line
xen/include/public/sched.h +0 -0
 #define SHUTDOWN_crash      3  /* Tell controller we've crashed.            

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 160

Quoted 1 line
xen/include/public/sched.h +0 -0
 #define SHUTDOWN_watchdog   4  /* Restart because watchdog time expired.    

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 161

Quoted 1 line
xen/include/public/sched.h +0 -1
-#define SHUTDOWN_MAX        4  /* Maximum valid shutdown reason.             
Quoted 8 lines

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 161

Quoted 1 line
xen/include/public/sched.h +1 -0
+#define SHUTDOWN_kexec      5  /* Soft-reboot for kexec.                     

*/

I agree with Jan on the name.

The documentation also needs to be expanded. It should be clear that this hypercall may return in a new domain.

Perhaps s/may/will/ ?

Jan

#5 lore raw

Re: [PATCH RFC/WIP 1/4] Introduce SHUTDOWN_kexec shutdown reason

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 26.08.14 at 12:16, <[email protected]> wrote:

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 158

Quoted 4 lines · Vitaly Kuznetsov
xen/include/public/sched.h +0 -0
@@ -158,7 +158,8 @@ DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t);
 #define SHUTDOWN_suspend    2  /* Clean up, save suspend info, kill.        

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 159

Quoted 1 line
xen/include/public/sched.h +0 -0
 #define SHUTDOWN_crash      3  /* Tell controller we've crashed.            

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 160

Quoted 1 line
xen/include/public/sched.h +0 -0
 #define SHUTDOWN_watchdog   4  /* Restart because watchdog time expired.    

*/

xen/include/public/sched.h DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t); · line 161

Quoted 1 line
xen/include/public/sched.h +0 -1
-#define SHUTDOWN_MAX        4  /* Maximum valid shutdown reason.             

*/

Quoted 1 line
xen/include/public/sched.h +1 -0
+#define SHUTDOWN_kexec      5  /* Soft-reboot for kexec.                     
Quoted 8 lines
Quoted 6 lines

*/

I agree with Jan on the name.

The documentation also needs to be expanded. It should be clear that this hypercall may return in a new domain.

Perhaps s/may/will/ ?

Yes, but only on success.

David

#6 lore raw

PATCH 2/4 libxc: treat domain in SHUTDOWN_kexec state as being already suspended

In reply to Vitaly Kuznetsov
---
 tools/libxc/xc_domain_save.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libxc/xc_domain_save.c +2 -1
index 42c4752..5423bfa 100644
@@ -366,7 +366,8 @@ static int suspend_and_state(int (*suspend)(void*), void* data,
     }
 
     if ( (xc_domain_getinfo(xch, dom, 1, info) != 1) ||
-         !info->shutdown || (info->shutdown_reason != SHUTDOWN_suspend) )
+         !info->shutdown || ((info->shutdown_reason != SHUTDOWN_suspend) &&
+                             (info->shutdown_reason != SHUTDOWN_kexec)) )
     {
         ERROR("Domain not in suspended state");
         return -1;
Signature · 1 line
1.9.3
#7 lore raw

Re: [PATCH RFC/WIP 2/4] libxc: treat domain in SHUTDOWN_kexec state as being already suspended

Quoted 10 lines · Vitaly Kuznetsov
---
 tools/libxc/xc_domain_save.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libxc/xc_domain_save.c +0 -0
index 42c4752..5423bfa 100644

tools/libxc/xc_domain_save.c static int suspend_and_state(int (*suspend)(void*), void* data, · line 370

Quoted 6 lines
tools/libxc/xc_domain_save.c +2 -1
@@ -366,7 +366,8 @@ static int suspend_and_state(int (*suspend)(void*), void* data,
     }
 
     if ( (xc_domain_getinfo(xch, dom, 1, info) != 1) ||
-         !info->shutdown || (info->shutdown_reason != SHUTDOWN_suspend) )
+         !info->shutdown || ((info->shutdown_reason != SHUTDOWN_suspend) &&
+                             (info->shutdown_reason != SHUTDOWN_kexec)) )

This will break PV unless you mandate that SHUTDOWN_kexec follows the same principle as SHUTDOWN_suspend, i.e. stashing the start info mfn in rdx.

Best alter the comment in public/sched.h when adding SHUTDOWN_kexec.

~Andrew

#8 lore raw

PATCH 3/4 libxl: add SHUTDOWN_kexec state support

In reply to Vitaly Kuznetsov

Add kexec to domain suspend state and treat a domain as already suspended and successfully responded to suspend request if the flag is set.

---
 tools/libxl/libxl.c          | 1 +
 tools/libxl/libxl.h          | 1 +
 tools/libxl/libxl_dom.c      | 6 ++++++
 tools/libxl/libxl_internal.h | 1 +
 4 files changed, 9 insertions(+)
tools/libxl/libxl.c +1 -0
index 2d29ad2..0ba3285 100644
@@ -786,6 +786,7 @@ int libxl_domain_suspend(libxl_ctx *ctx, uint32_t domid, int fd, int flags,
     dss->type = type;
     dss->live = flags & LIBXL_SUSPEND_LIVE;
     dss->debug = flags & LIBXL_SUSPEND_DEBUG;
+    dss->kexec = flags & LIBXL_SUSPEND_KEXEC;
 
     libxl__domain_suspend(egc, dss);
     return AO_INPROGRESS;
tools/libxl/libxl.h +1 -0
index 06bbca6..c8185f9 100644
@@ -689,6 +689,7 @@ int libxl_domain_suspend(libxl_ctx *ctx, uint32_t domid, int fd,
                          LIBXL_EXTERNAL_CALLERS_ONLY;
 #define LIBXL_SUSPEND_DEBUG 1
 #define LIBXL_SUSPEND_LIVE 2
+#define LIBXL_SUSPEND_KEXEC 4
 
 /* @param suspend_cancel [from xenctrl.h:xc_domain_resume( @param fast )]
  *   If this parameter is true, use co-operative resume. The guest
tools/libxl/libxl_dom.c +6 -0
index 69e6088..b2f3a09 100644
@@ -1038,6 +1038,12 @@ int libxl__domain_suspend_common_callback(void *user)
     /* Convenience aliases */
     const uint32_t domid = dss->domid;
 
+    if (dss->kexec) {
+        /* No suspend required if we're in SHUTDOWN_kexec */
+        dss->guest_responded = 1;
+        goto guest_suspended;
+    }
+
     if (dss->hvm) {
         xc_get_hvm_param(CTX->xch, domid, HVM_PARAM_CALLBACK_IRQ, &hvm_pvdrv);
         xc_get_hvm_param(CTX->xch, domid, HVM_PARAM_ACPI_S_STATE, &hvm_s_state);
tools/libxl/libxl_internal.h +1 -0
index 9d17586..cdef241 100644
@@ -2314,6 +2314,7 @@ struct libxl__domain_suspend_state {
     libxl_domain_type type;
     int live;
     int debug;
+    int kexec;
     const libxl_domain_remus_info *remus;
     /* private */
     xc_evtchn *xce; /* event channel handle */
Signature · 1 line
1.9.3
#9 lore raw

PATCH 4/4 libxl: rebuild guest domains on kexec

In reply to Vitaly Kuznetsov

When a PVHVM linux guest performs kexec there are lots of things which require taking care of:
- shared info, vcpu_info
- grants
- event channels
- ...
Instead of taking care of all these things we can rebuild the domain performing kexec from scratch doing so-called soft-reboot. Here is a naive and simple implementation which leverages save/restore (migrate) path. It actually copies all the memory through a pipe so it is not very fast and the host is required to have free memory to host two copies of the guest doing kexec.

The idea was suggested by David Vrabel, Jan Beulich, and Konrad Rzeszutek Wilk.

---
 tools/libxl/xl_cmdimpl.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +60 -2
index 4fc46eb..a9bce17 100644
@@ -1774,7 +1774,8 @@ static void reload_domain_config(uint32_t domid,
 }
 
 /* Returns 1 if domain should be restarted,
- * 2 if domain should be renamed then restarted, or 0
+ * 2 if domain should be renamed then restarted,
+ * 3 if domain performed kexec/kdump, or 0
  * Can update r_domid if domain is destroyed etc */
 static int handle_domain_death(uint32_t *r_domid,
                                libxl_event *event,
@@ -1801,6 +1802,9 @@ static int handle_domain_death(uint32_t *r_domid,
     case LIBXL_SHUTDOWN_REASON_WATCHDOG:
         action = d_config->on_watchdog;
         break;
+    case LIBXL_SHUTDOWN_REASON_KEXEC:
+        LOG("Domain kexec-ed.");
+        return 3;
     default:
         LOG("Unknown shutdown reason code %d. Destroying domain.",
             event->u.domain_shutdown.shutdown_reason);
@@ -2038,6 +2042,19 @@ static void evdisable_disk_ejects(libxl_evgen_disk_eject **diskws,
     }
 }
 
+static void domain_kexec_finish(libxl_ctx *ctx_cb, int rc, void *domid_arg)
+{
+    uint32_t domid = *(uint32_t *)domid_arg;
+    if (!rc) {
+        /* successful kexec, destroy the domain */
+        LOG("Domain kexec finished, destroying");
+        libxl_domain_destroy(ctx_cb, domid, 0);
+    }
+    else
+        LOG("Domain kexec failed, manual intervention required");
+    free(domid_arg);
+}
+
 static uint32_t create_domain(struct domain_create *dom_info)
 {
     uint32_t domid = INVALID_DOMID;
@@ -2066,6 +2083,9 @@ static uint32_t create_domain(struct domain_create *dom_info)
     int restore_fd = -1;
     const libxl_asyncprogress_how *autoconnect_console_how;
     struct save_file_header hdr;
+    int pipefd[2];
+
+    pipefd[0] = pipefd[1] = -1;
 
     int restoring = (restore_file || (migrate_fd >= 0));
 
@@ -2250,6 +2270,12 @@ start:
          * restore/migrate-receive it again.
          */
         restoring = 0;
+        if (pipefd[0] != -1) {
+            /* we were doing kexec, close pipes */
+            close(pipefd[0]);
+            close(pipefd[1]);
+            pipefd[0] = pipefd[1] = -1;
+        }
     }else{
         ret = libxl_domain_create_new(ctx, &d_config, &domid,
                                       0, autoconnect_console_how);
@@ -2339,6 +2365,10 @@ start:
     }
     while (1) {
         libxl_event *event;
+        libxl_asyncop_how ao_how;
+        uint32_t domid_old;
+        void *domid_arg;
+
         ret = domain_wait_event(domid, &event);
         if (ret) goto out;
 
@@ -2351,6 +2381,33 @@ start:
             switch (handle_domain_death(&domid, event,
                                         (uint8_t **)&config_data, &config_len,
                                         &d_config)) {
+            case 3:
+                domid_old = domid;
+                if (!preserve_domain(&domid, event, &d_config)) {
+                    ret = -1;
+                    goto out;
+                }
+                if (libxl_pipe(ctx, pipefd)) {
+                    ret = -1;
+                    goto out;
+                }
+
+                domid_arg = (void *)malloc(sizeof(domid_old));
+                if (!domid_arg) {
+                    ret = -1;
+                    goto out;
+                }
+                *(uint32_t *)domid_arg = domid_old;
+
+                ao_how.callback = domain_kexec_finish;
+                ao_how.u.for_callback = domid_arg;
+
+                ret = libxl_domain_suspend(ctx, domid_old, pipefd[1], LIBXL_SUSPEND_KEXEC, &ao_how);
+
+                restoring = 1;
+                restore_fd = pipefd[0];
+
+                goto restart;
             case 2:
                 if (!preserve_domain(&domid, event, &d_config)) {
                     /* If we fail then exit leaving the old domain in place. */
@@ -2359,6 +2416,7 @@ start:
                 }
 
                 /* Otherwise fall through and restart. */
+restart:
             case 1:
                 libxl_event_free(ctx, event);
                 libxl_evdisable_domain_death(ctx, deathw);
@@ -3287,7 +3345,7 @@ static void list_domains(int verbose, int context, int claim, int numa,
                          const libxl_dominfo *info, int nb_domain)
 {
     int i;
-    static const char shutdown_reason_letters[]= "-rscw";
+    static const char shutdown_reason_letters[]= "-rscwk";
     libxl_bitmap nodemap;
     libxl_physinfo physinfo;
 
Signature · 1 line
1.9.3