Skip to contentSkip to conversation

toolstack-based approach to pvhvm guest kexec

RFC PATCH Superseded Vitaly Kuznetsov 3 participants 17 messages Oct 7, 2014 – Nov 27, 2014

Subsystems (4)

v3 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v3 cover toolstack-based approach to pvhvm guest kexec

Changes from RFC/WIPv2:

Here is a slightly different approach to memory reassignment. Instead of introducing new (and very doubtful) XENMEM_transfer operation introduce simple XEN_DOMCTL_set_recipient operation and do everything in free_domheap_pages() handler utilizing normal domain destroy path. This is better because:
- The approach is general-enough
- All memory pages are usually being freed when the domain is destroyed
- No special grants handling required
- Better supportability

With regards to PV:
Though XEN_DOMCTL_set_recipient works for both PV and HVM this patchset does not bring PV kexec/kdump support. xc_domain_soft_reset() is limited to work with HVM domains only. The main reason for that is: it is (in theory) possible to save p2m and rebuild them with the new domain but that would only allow us to resume execution from where we stopped. If we want to execute new kernel we need to build the same kernel/initrd/bootstrap_pagetables/... structure we build to boot PV domain initially. That however would destroy the original domain's memory thus making kdump impossible. To make everything work additional support from kexec userspace/linux kernel is required and I'm not sure it makes sense to implement all this stuff in the light of PVH.

Original description:

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.

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

P.S. The patch series can be tested with PVHVM Linux guest with the following modifications:

arch/x86/xen/enlighten.c +20 -0
index c0cb11f..33c5cdd 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_soft_reset);
+	   }
+}
+
+static void xen_pvhvm_crash_shutdown(struct pt_regs *regs)
+{
+	native_machine_crash_shutdown(regs);
+	xen_reboot(SHUTDOWN_soft_reset);
+}
+#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..b5942a8 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_soft_reset 5  /* Soft-reset for kexec.                      */
 
 #endif /* __XEN_PUBLIC_SCHED_H__ */

Vitaly Kuznetsov (8):
xen: introduce SHUTDOWN_soft_reset shutdown reason
libxl: support SHUTDOWN_soft_reset shutdown reason
xen: introduce XEN_DOMCTL_set_recipient
libxc: support XEN_DOMCTL_set_recipient
libxl: add libxl__domain_soft_reset_destroy_old()
libxc: introduce soft reset for HVM domains
libxl: soft reset support
xsm: add XEN_DOMCTL_set_recipient support

 tools/libxc/Makefile                |   1 +
 tools/libxc/xc_domain.c             |  10 +
 tools/libxc/xc_domain_soft_reset.c  | 394 ++++++++++++++++++++++++++++++++++++
 tools/libxc/xenctrl.h               |  14 ++
 tools/libxc/xenguest.h              |  20 ++
 tools/libxl/libxl.c                 |  32 ++-
 tools/libxl/libxl.h                 |   6 +
 tools/libxl/libxl_create.c          | 103 +++++++++-
 tools/libxl/libxl_internal.h        |   8 +
 tools/libxl/libxl_types.idl         |   1 +
 tools/libxl/xl_cmdimpl.c            |  24 ++-
 tools/python/xen/lowlevel/xl/xl.c   |   1 +
 xen/common/domain.c                 |   3 +
 xen/common/domctl.c                 |  35 ++++
 xen/common/page_alloc.c             |  26 ++-
 xen/common/shutdown.c               |   7 +
 xen/include/public/domctl.h         |  19 ++
 xen/include/public/sched.h          |   3 +-
 xen/include/xen/sched.h             |   1 +
 xen/include/xsm/dummy.h             |   6 +
 xen/include/xsm/xsm.h               |   6 +
 xen/xsm/dummy.c                     |   1 +
 xen/xsm/flask/hooks.c               |  17 ++
 xen/xsm/flask/policy/access_vectors |  10 +
 24 files changed, 726 insertions(+), 22 deletions(-)
 create mode 100644 tools/libxc/xc_domain_soft_reset.c
Signature · 1 line
1.9.3
#1 lore raw

RFC PATCH v3 1/8 xen: introduce SHUTDOWN_soft_reset shutdown reason

---
 xen/common/shutdown.c      | 7 +++++++
 xen/include/public/sched.h | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)
xen/common/shutdown.c +7 -0
index 94d4c53..5c3a158 100644
@@ -71,6 +71,13 @@ void hwdom_shutdown(u8 reason)
         break; /* not reached */
     }
 
+    case SHUTDOWN_soft_reset:
+    {
+        printk("Domain 0 did soft reset but it is unsupported, rebooting.\n");
+        machine_restart(0);
+        break; /* not reached */
+    }
+
     default:
     {
         printk("Domain 0 shutdown (unknown reason %u): ", reason);
xen/include/public/sched.h +2 -1
index 4000ac9..800c808 100644
@@ -159,7 +159,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_soft_reset 5  /* Soft reset, rebuild keeping memory content */
+#define SHUTDOWN_MAX        5  /* Maximum valid shutdown reason.             */
 /* ` } */
 
 #endif /* __XEN_PUBLIC_SCHED_H__ */
Signature · 1 line
1.9.3
#2 lore raw

RFC PATCH v3 2/8 libxl: support SHUTDOWN_soft_reset shutdown reason

In reply to Vitaly Kuznetsov

Use letter 't' to indicate a domain in such state.

---
 tools/libxl/libxl_types.idl       | 1 +
 tools/libxl/xl_cmdimpl.c          | 2 +-
 tools/python/xen/lowlevel/xl/xl.c | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl_types.idl +1 -0
index f1fcbc3..8077415 100644
@@ -166,6 +166,7 @@ libxl_shutdown_reason = Enumeration("shutdown_reason", [
     (2, "suspend"),
     (3, "crash"),
     (4, "watchdog"),
+    (5, "soft_reset"),
     ], init_val = "LIBXL_SHUTDOWN_REASON_UNKNOWN")
 
 libxl_vga_interface_type = Enumeration("vga_interface_type", [
tools/libxl/xl_cmdimpl.c +1 -1
index 698b3bc..b40ad50 100644
@@ -3279,7 +3279,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[]= "-rscwt";
     libxl_bitmap nodemap;
     libxl_physinfo physinfo;
 
tools/python/xen/lowlevel/xl/xl.c +1 -0
index 32f982a..7c61160 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_SOFT_RESET);
 
     genwrap__init(m);
 }
Signature · 1 line
1.9.3
#3 lore raw

RFC PATCH v3 3/8 xen: introduce XEN_DOMCTL_set_recipient

In reply to Vitaly Kuznetsov

New operation sets the 'recipient' domain which will recieve all memory pages from a particular domain when these pages are freed.

---
 xen/common/domain.c         |  3 +++
 xen/common/domctl.c         | 27 +++++++++++++++++++++++++++
 xen/common/page_alloc.c     | 26 ++++++++++++++++++++++----
 xen/include/public/domctl.h | 19 +++++++++++++++++++
 xen/include/xen/sched.h     |  1 +
 5 files changed, 72 insertions(+), 4 deletions(-)
xen/common/domain.c +3 -0
index 62514b0..6bceb64 100644
@@ -822,6 +822,9 @@ static void complete_domain_destroy(struct rcu_head *head)
     if ( d->target != NULL )
         put_domain(d->target);
 
+    if ( d->recipient != NULL )
+        put_domain(d->recipient);
+
     evtchn_destroy_final(d);
 
     radix_tree_destroy(&d->pirq_tree, free_pirq_struct);
xen/common/domctl.c +27 -0
index 1ad0729..38cd10a 100644
@@ -1152,6 +1152,33 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     }
     break;
 
+    case XEN_DOMCTL_set_recipient:
+    {
+        struct domain *recipient_dom;
+
+        if ( op->u.recipient.recipient == DOMID_INVALID )
+        {
+            if ( d->recipient )
+            {
+                put_domain(d->recipient);
+            }
+            d->recipient = NULL;
+            break;
+        }
+
+        recipient_dom = get_domain_by_id(op->u.recipient.recipient);
+        if ( recipient_dom == NULL )
+        {
+            ret = -ESRCH;
+            break;
+        }
+        else
+        {
+            d->recipient = recipient_dom;
+        }
+    }
+    break;
+
     default:
         ret = arch_do_domctl(op, d, u_domctl);
         break;
xen/common/page_alloc.c +22 -4
index 7b4092d..abb2ef3 100644
@@ -1707,6 +1707,7 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
 {
     struct domain *d = page_get_owner(pg);
     unsigned int i;
+    unsigned long mfn, gmfn;
     bool_t drop_dom_ref;
 
     ASSERT(!in_irq());
@@ -1764,11 +1765,28 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
+        if ( !d || !d->recipient || d->recipient->is_dying )
+        {
+            if ( unlikely(scrub) )
+                for ( i = 0; i < (1 << order); i++ )
+                    scrub_one_page(&pg[i]);
 
-        free_heap_pages(pg, order);
+            free_heap_pages(pg, order);
+        }
+        else
+        {
+            mfn = page_to_mfn(pg);
+            gmfn = mfn_to_gmfn(d, mfn);
+
+            page_set_owner(pg, NULL);
+            assign_pages(d->recipient, pg, order, 0);
+
+            if ( guest_physmap_add_page(d->recipient, gmfn, mfn, order) )
+            {
+                gdprintk(XENLOG_INFO, "Failed to add page to domain's physmap"
+                         " mfn: %lx\n", mfn);
+            }
+        }
     }
 
     if ( drop_dom_ref )
xen/include/public/domctl.h +19 -0
index cfa39b3..7fcb6db 100644
@@ -965,6 +965,23 @@ struct xen_domctl_vnuma {
 typedef struct xen_domctl_vnuma xen_domctl_vnuma_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vnuma_t);
 
+/*
+ * XEN_DOMCTL_set_recipient - sets the 'recipient' domain which will recieve
+ * all the domain's memory after its death or when and memory page from
+ * domain's heap is being freed. Pages from xen heap belonging to the domain
+ * are not copied. An additional reference link to the destination domain is
+ * being taken to prevent it from dying.
+ * When DOMID_INVALID is supplied as a recipient the source domain returns to
+ * its original state when memory pages are freed normally, reference link to
+ * the destination domain is being dropped.
+ */
+struct xen_domctl_set_recipient {
+    domid_t recipient;
+};
+typedef struct xen_domctl_set_recipient xen_domctl_set_recipient_t;
+DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_recipient_t);
+
+
 struct xen_domctl {
     uint32_t cmd;
 #define XEN_DOMCTL_createdomain                   1
@@ -1038,6 +1055,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_get_vcpu_msrs                 72
 #define XEN_DOMCTL_set_vcpu_msrs                 73
 #define XEN_DOMCTL_setvnumainfo                  74
+#define XEN_DOMCTL_set_recipient                 75
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1099,6 +1117,7 @@ struct xen_domctl {
         struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu;
         struct xen_domctl_gdbsx_domstatus   gdbsx_domstatus;
         struct xen_domctl_vnuma             vnuma;
+        struct xen_domctl_set_recipient     recipient;
         uint8_t                             pad[128];
     } u;
 };
xen/include/xen/sched.h +1 -0
index c5157e6..d00d655 100644
@@ -366,6 +366,7 @@ struct domain
     bool_t           is_privileged;
     /* Which guest this guest has privileges on */
     struct domain   *target;
+    struct domain   *recipient;
     /* Is this guest being debugged by dom0? */
     bool_t           debugger_attached;
     /* Is this guest dying (i.e., a zombie)? */
Signature · 1 line
1.9.3
#4 lore raw

Re: [PATCH RFCv3 3/8] xen: introduce XEN_DOMCTL_set_recipient

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 07.10.14 at 15:10, <[email protected]> wrote:

New operation sets the 'recipient' domain which will recieve all memory pages from a particular domain when these pages are freed.

xen/common/domctl.c +0 -0
@@ -1152,6 +1152,33 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     }
     break;
 

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 1164

Quoted 10 lines
xen/common/domctl.c +10 -0
+    case XEN_DOMCTL_set_recipient:
+    {
+        struct domain *recipient_dom;
+
+        if ( op->u.recipient.recipient == DOMID_INVALID )
+        {
+            if ( d->recipient )
+            {
+                put_domain(d->recipient);
+            }

Please drop pointless braces like these and ...

Quoted 4 lines
xen/common/domctl.c +4 -0
+            d->recipient = NULL;
+            break;
+        }
+

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 1178

Quoted 10 lines
xen/common/domctl.c +10 -0
+        recipient_dom = get_domain_by_id(op->u.recipient.recipient);
+        if ( recipient_dom == NULL )
+        {
+            ret = -ESRCH;
+            break;
+        }
+        else
+        {
+            d->recipient = recipient_dom;
+        }

... the pointless else-s/break-s like here.

Quoted 23 lines
xen/common/page_alloc.c +6 -3
@@ -1707,6 +1707,7 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
 {
     struct domain *d = page_get_owner(pg);
     unsigned int i;
+    unsigned long mfn, gmfn;
     bool_t drop_dom_ref;
 
     ASSERT(!in_irq());
@@ -1764,11 +1765,28 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
+        if ( !d || !d->recipient || d->recipient->is_dying )
+        {
+            if ( unlikely(scrub) )
+                for ( i = 0; i < (1 << order); i++ )
+                    scrub_one_page(&pg[i]);
 

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 1782

Quoted 10 lines
xen/common/page_alloc.c +9 -1
-        free_heap_pages(pg, order);
+            free_heap_pages(pg, order);
+        }
+        else
+        {
+            mfn = page_to_mfn(pg);
+            gmfn = mfn_to_gmfn(d, mfn);
+
+            page_set_owner(pg, NULL);
+            assign_pages(d->recipient, pg, order, 0);

This can fail.

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 1787

Quoted 5 lines
xen/common/page_alloc.c +5 -0
+
+            if ( guest_physmap_add_page(d->recipient, gmfn, mfn, order) )
+            {
+                gdprintk(XENLOG_INFO, "Failed to add page to domain's physmap"
+                         " mfn: %lx\n", mfn);

The current domain/vCPU don't matter here afaict, hence no need for gdprintk(). The source and/or recipient domain do matter though, hence printing either or both would seem desirable. The gMFN may be relevant for diagnostic purposes too. Hence e.g.

printk(XENLOG_G_INFO "Failed to add MFN %lx (GFN %lx) to Dom%d's physmap\n"
mfn, gmfn, d->recipient->domain_id);

What's worse though is that you don't guard against XEN_DOMCTL_set_recipient zapping d->recipient. If that really is necessary to be supported, some synchronization will be needed.

And finally - what's the intended protocol for the tool stack to know that all pages got transferred (and hence the new domain can be launched)?

xen/include/public/domctl.h struct xen_domctl_vnuma { · line 971

Quoted 10 lines
xen/include/public/domctl.h +4 -0
@@ -965,6 +965,23 @@ struct xen_domctl_vnuma {
 typedef struct xen_domctl_vnuma xen_domctl_vnuma_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vnuma_t);
 
+/*
+ * XEN_DOMCTL_set_recipient - sets the 'recipient' domain which will recieve
+ * all the domain's memory after its death or when and memory page from
+ * domain's heap is being freed.

So this specifically allows for this to happen on a non-dying domain. Why, i.e. what's the intended usage scenario? If not really needed, please remove this and verify in the handling that the source domain is dying.

xen/include/xen/sched.h struct domain · line 369

Quoted 7 lines
xen/include/xen/sched.h +1 -0
@@ -366,6 +366,7 @@ struct domain
     bool_t           is_privileged;
     /* Which guest this guest has privileges on */
     struct domain   *target;
+    struct domain   *recipient;

Please add a brief comment similar to the one for "target".

Jan

#5 lore raw

Re: [PATCH RFCv3 3/8] xen: introduce XEN_DOMCTL_set_recipient

"Jan Beulich" <[email protected]> writes:

Thanks for the review!

Quoted 63 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 07.10.14 at 15:10, <[email protected]> wrote:

New operation sets the 'recipient' domain which will recieve all memory pages from a particular domain when these pages are freed.

xen/common/domctl.c +0 -0
@@ -1152,6 +1152,33 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
     }
     break;
 

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 1164

Quoted 10 lines
xen/common/domctl.c +10 -0
+    case XEN_DOMCTL_set_recipient:
+    {
+        struct domain *recipient_dom;
+
+        if ( op->u.recipient.recipient == DOMID_INVALID )
+        {
+            if ( d->recipient )
+            {
+                put_domain(d->recipient);
+            }

Please drop pointless braces like these and ...

Quoted 4 lines
xen/common/domctl.c +4 -0
+            d->recipient = NULL;
+            break;
+        }
+

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 1178

Quoted 10 lines
xen/common/domctl.c +10 -0
+        recipient_dom = get_domain_by_id(op->u.recipient.recipient);
+        if ( recipient_dom == NULL )
+        {
+            ret = -ESRCH;
+            break;
+        }
+        else
+        {
+            d->recipient = recipient_dom;
+        }

... the pointless else-s/break-s like here.

Quoted 23 lines
xen/common/page_alloc.c +6 -3
@@ -1707,6 +1707,7 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
 {
     struct domain *d = page_get_owner(pg);
     unsigned int i;
+    unsigned long mfn, gmfn;
     bool_t drop_dom_ref;
 
     ASSERT(!in_irq());
@@ -1764,11 +1765,28 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
+        if ( !d || !d->recipient || d->recipient->is_dying )
+        {
+            if ( unlikely(scrub) )
+                for ( i = 0; i < (1 << order); i++ )
+                    scrub_one_page(&pg[i]);
 
Quoted 12 lines

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 1782

Quoted 10 lines
xen/common/page_alloc.c +9 -1
-        free_heap_pages(pg, order);
+            free_heap_pages(pg, order);
+        }
+        else
+        {
+            mfn = page_to_mfn(pg);
+            gmfn = mfn_to_gmfn(d, mfn);
+
+            page_set_owner(pg, NULL);
+            assign_pages(d->recipient, pg, order, 0);

This can fail.

.. if the recipient is dying or we're over-allocating. Shouldn't happen (if toolstack does its job right) but I'll add a check.

Quoted 15 lines

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 1787

Quoted 6 lines
xen/common/page_alloc.c +5 -0
+
+            if ( guest_physmap_add_page(d->recipient, gmfn, mfn, order) )
+            {
+                gdprintk(XENLOG_INFO, "Failed to add page to domain's physmap"
+                         " mfn: %lx\n", mfn);

The current domain/vCPU don't matter here afaict, hence no need for gdprintk(). The source and/or recipient domain do matter though, hence printing either or both would seem desirable. The gMFN may be relevant for diagnostic purposes too. Hence e.g.

printk(XENLOG_G_INFO "Failed to add MFN %lx (GFN %lx) to Dom%d's physmap\n"
mfn, gmfn, d->recipient->domain_id);

Quoted 8 lines

What's worse though is that you don't guard against XEN_DOMCTL_set_recipient zapping d->recipient. If that really is necessary to be supported, some synchronization will be needed.

And finally - what's the intended protocol for the tool stack to know that all pages got transferred (and hence the new domain can be launched)?

(hope this answers both questions above)

Now the protocol is:
1) Toolstack queries for all page frames (with xc_get_pfn_type_batch()) for the old domain.
2) Toolstack issues XEN_DOMCTL_set_recipient with the recipient domain
3) Toolstack kills the source domain
4) Toolstack waits for awhile (loop keeps checking while we see new pages arriving + some timeout).
5) Toolstack issues XEN_DOMCTL_set_recipient with DOMID_INVALID resetting recipient.
6) Toolstack checks if all pages were transferred
7) If some pages are missing (e.g. source domain became zombie holding something) request new empty pages instead.
8) Toolstack starts new domain

So we don't actually need XEN_DOMCTL_set_recipient to switch from one recipient to the other, we just need a way to reset it.

Quoted 6 lines
Quoted 6 lines
xen/include/public/domctl.h +0 -0
@@ -965,6 +965,23 @@ struct xen_domctl_vnuma {
 typedef struct xen_domctl_vnuma xen_domctl_vnuma_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vnuma_t);
 
Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_vnuma {

Quoted 4 lines
xen/include/public/domctl.h +4 -0
+/*
+ * XEN_DOMCTL_set_recipient - sets the 'recipient' domain which will recieve
+ * all the domain's memory after its death or when and memory page from
+ * domain's heap is being freed.

So this specifically allows for this to happen on a non-dying domain. Why, i.e. what's the intended usage scenario? If not really needed, please remove this and verify in the handling that the source domain is dying.

Sorry if I didn't get this comment right. We need a way to tell which domain will recieve memory and XEN_DOMCTL_set_recipient sets (and resets) this target domain. We call it from toolstack before we start killing old domain so it is not dying yet. We can't do it hypervistor-side when our source domain exits doing SHUTDOWN_soft_reset as there is no recipient domain created yet.

Quoted 1 line

From a general (hypervisor) point of view we don't actually care if the

domain is dying or not. We just want to recieve all freed pages from this domain (so they go to some other domain instead of trash).

Quoted 10 lines

xen/include/xen/sched.h struct domain · line 369

Quoted 7 lines
xen/include/xen/sched.h +1 -0
@@ -366,6 +366,7 @@ struct domain
     bool_t           is_privileged;
     /* Which guest this guest has privileges on */
     struct domain   *target;
+    struct domain   *recipient;

Please add a brief comment similar to the one for "target".

Thanks again,

Signature · 1 line
  Vitaly
#6 lore raw

Re: [PATCH RFCv3 3/8] xen: introduce XEN_DOMCTL_set_recipient

Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 16:41, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 07.10.14 at 15:10, <[email protected]> wrote:
xen/common/page_alloc.c +5 -3
@@ -1764,11 +1765,28 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
+        if ( !d || !d->recipient || d->recipient->is_dying )
+        {
+            if ( unlikely(scrub) )
+                for ( i = 0; i < (1 << order); i++ )
+                    scrub_one_page(&pg[i]);
 
Quoted 12 lines

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 1782

Quoted 10 lines
xen/common/page_alloc.c +9 -1
-        free_heap_pages(pg, order);
+            free_heap_pages(pg, order);
+        }
+        else
+        {
+            mfn = page_to_mfn(pg);
+            gmfn = mfn_to_gmfn(d, mfn);
+
+            page_set_owner(pg, NULL);
+            assign_pages(d->recipient, pg, order, 0);

This can fail.

Quoted 2 lines

.. if the recipient is dying or we're over-allocating. Shouldn't happen (if toolstack does its job right) but I'll add a check.

You must not rely on the tool stack doing things right (see XSA-77).

Quoted 25 lines
Quoted 8 lines

What's worse though is that you don't guard against XEN_DOMCTL_set_recipient zapping d->recipient. If that really is necessary to be supported, some synchronization will be needed.

And finally - what's the intended protocol for the tool stack to know that all pages got transferred (and hence the new domain can be launched)?

(hope this answers both questions above)

Now the protocol is:
1) Toolstack queries for all page frames (with xc_get_pfn_type_batch()) for the old domain.
2) Toolstack issues XEN_DOMCTL_set_recipient with the recipient domain
3) Toolstack kills the source domain
4) Toolstack waits for awhile (loop keeps checking while we see new pages arriving + some timeout).
5) Toolstack issues XEN_DOMCTL_set_recipient with DOMID_INVALID resetting recipient.
6) Toolstack checks if all pages were transferred
7) If some pages are missing (e.g. source domain became zombie holding something) request new empty pages instead.
8) Toolstack starts new domain

Quoted 2 lines

So we don't actually need XEN_DOMCTL_set_recipient to switch from one recipient to the other, we just need a way to reset it.

No, this doesn't address either question. For the first one, you again assume the tool stack behaves correctly, which is wrong nowadays. And for the second you give a description, but that's not really a dependable protocol. Nor do I really follow why resetting the recipient is necessary. Can't the tools e.g. wait for the final death of the domain if there's no other notification?

Quoted 23 lines
Quoted 6 lines
Quoted 6 lines
xen/include/public/domctl.h +0 -0
@@ -965,6 +965,23 @@ struct xen_domctl_vnuma {
 typedef struct xen_domctl_vnuma xen_domctl_vnuma_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vnuma_t);
 
Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_vnuma {

Quoted 4 lines
xen/include/public/domctl.h +4 -0
+/*
+ * XEN_DOMCTL_set_recipient - sets the 'recipient' domain which will recieve
+ * all the domain's memory after its death or when and memory page from
+ * domain's heap is being freed.

So this specifically allows for this to happen on a non-dying domain. Why, i.e. what's the intended usage scenario? If not really needed, please remove this and verify in the handling that the source domain is dying.

Sorry if I didn't get this comment right. We need a way to tell which domain will recieve memory and XEN_DOMCTL_set_recipient sets (and resets) this target domain. We call it from toolstack before we start killing old domain so it is not dying yet. We can't do it hypervistor-side when our source domain exits doing SHUTDOWN_soft_reset as there is no recipient domain created yet.

Quoted 3 lines

From a general (hypervisor) point of view we don't actually care if the domain is dying or not. We just want to recieve all freed pages from this domain (so they go to some other domain instead of trash).

We do care I think, primarily because we want a correct GMFN <-> MFN mapping. Seeing multiple pages for the same GMFN is for example going to cause the whole process in its current form to fail. And considering the need for such a correct mapping - is it always the case that the mapping gets updated after a page got removed from a guest? (I can see that the mapping doesn't change anymore for a dying guest, but you explicitly say that you want/need this to work before the guest is actually marked dying.)

Jan

#7 lore raw

Re: [PATCH RFCv3 3/8] xen: introduce XEN_DOMCTL_set_recipient

Quoted 62 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 16:41, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 07.10.14 at 15:10, <[email protected]> wrote:
xen/common/page_alloc.c +5 -3
@@ -1764,11 +1765,28 @@ void free_domheap_pages(struct page_info *pg, unsigned int order)
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
+        if ( !d || !d->recipient || d->recipient->is_dying )
+        {
+            if ( unlikely(scrub) )
+                for ( i = 0; i < (1 << order); i++ )
+                    scrub_one_page(&pg[i]);
 
Quoted 12 lines

xen/common/page_alloc.c void free_domheap_pages(struct page_info *pg, unsigned int order) · line 1782

Quoted 10 lines
xen/common/page_alloc.c +9 -1
-        free_heap_pages(pg, order);
+            free_heap_pages(pg, order);
+        }
+        else
+        {
+            mfn = page_to_mfn(pg);
+            gmfn = mfn_to_gmfn(d, mfn);
+
+            page_set_owner(pg, NULL);
+            assign_pages(d->recipient, pg, order, 0);

This can fail.

Quoted 2 lines

.. if the recipient is dying or we're over-allocating. Shouldn't happen (if toolstack does its job right) but I'll add a check.

You must not rely on the tool stack doing things right (see XSA-77).

Quoted 25 lines
Quoted 8 lines

What's worse though is that you don't guard against XEN_DOMCTL_set_recipient zapping d->recipient. If that really is necessary to be supported, some synchronization will be needed.

And finally - what's the intended protocol for the tool stack to know that all pages got transferred (and hence the new domain can be launched)?

(hope this answers both questions above)

Now the protocol is:
1) Toolstack queries for all page frames (with xc_get_pfn_type_batch()) for the old domain.
2) Toolstack issues XEN_DOMCTL_set_recipient with the recipient domain
3) Toolstack kills the source domain
4) Toolstack waits for awhile (loop keeps checking while we see new pages arriving + some timeout).
5) Toolstack issues XEN_DOMCTL_set_recipient with DOMID_INVALID resetting recipient.
6) Toolstack checks if all pages were transferred
7) If some pages are missing (e.g. source domain became zombie holding something) request new empty pages instead.
8) Toolstack starts new domain

Quoted 2 lines

So we don't actually need XEN_DOMCTL_set_recipient to switch from one recipient to the other, we just need a way to reset it.

Quoted 6 lines

No, this doesn't address either question. For the first one, you again assume the tool stack behaves correctly, which is wrong nowadays. And for the second you give a description, but that's not really a dependable protocol. Nor do I really follow why resetting the recipient is necessary. Can't the tools e.g. wait for the final death of the domain if there's no other notification?

Yes, it's possible and should happen in all normal cases. However my idea was that it's possible to start new domain even if the old one fails to die holding several (presumably special) pages and we're fine with replacing those with empty pages. In case we go for 'always wait for the original domain to die' solution resetting XEN_DOMCTL_set_recipient is not necessary (it is necessary in case we don't as we can recieve a page after we already requested a new one instead).

Quoted 24 lines
Quoted 24 lines
Quoted 7 lines
Quoted 7 lines
xen/include/public/domctl.h +0 -0
@@ -965,6 +965,23 @@ struct xen_domctl_vnuma {
 typedef struct xen_domctl_vnuma xen_domctl_vnuma_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vnuma_t);
 
Quoted 9 lines

xen/include/public/domctl.h struct xen_domctl_vnuma {

Quoted 4 lines
xen/include/public/domctl.h +4 -0
+/*
+ * XEN_DOMCTL_set_recipient - sets the 'recipient' domain which will recieve
+ * all the domain's memory after its death or when and memory page from
+ * domain's heap is being freed.

So this specifically allows for this to happen on a non-dying domain. Why, i.e. what's the intended usage scenario? If not really needed, please remove this and verify in the handling that the source domain is dying.

Sorry if I didn't get this comment right. We need a way to tell which domain will recieve memory and XEN_DOMCTL_set_recipient sets (and resets) this target domain. We call it from toolstack before we start killing old domain so it is not dying yet. We can't do it hypervistor-side when our source domain exits doing SHUTDOWN_soft_reset as there is no recipient domain created yet.

Quoted 7 lines
Quoted 3 lines

From a general (hypervisor) point of view we don't actually care if the domain is dying or not. We just want to recieve all freed pages from this domain (so they go to some other domain instead of trash).

We do care I think, primarily because we want a correct GMFN <-> MFN mapping. Seeing multiple pages for the same GMFN is for example going to cause the whole process in its current form to fail.

Can adding a check that nothing is mapped to the GMFN before mapping new MFN there be a solution?

Quoted 5 lines

And considering the need for such a correct mapping - is it always the case that the mapping gets updated after a page got removed from a guest? (I can see that the mapping doesn't change anymore for a dying guest, but you explicitly say that you want/need this to work before the guest is actually marked dying.)

Actual reassignment here happens for a dying guest only as XEN_DOMCTL_set_recipient does nothing. If you think it's safer to depend on the fact that dying flag is always set we can couple XEN_DOMCTL_set_recipient with XEN_DOMCTL_destroydomain in one call. It is possible if we go for 'always wait for the original domain to die' solution (so no reset is required).

Quoted 2 lines

Jan

Signature · 1 line
  Vitaly
#8 lore raw

Re: [PATCH RFCv3 3/8] xen: introduce XEN_DOMCTL_set_recipient

Quoted 73 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 18:10, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 16:41, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich

Review void free_domheap_pages(struct page_info *pg,

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 07.10.14 at 15:10, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -1764,11 +1765,28 @@ void free_domheap_pages(struct page_info *pg, 

unsigned int order)

Quoted 58 lines
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
xen/common/page_alloc.c +14 -4
             scrub = 1;
         }
 
-        if ( unlikely(scrub) )
-            for ( i = 0; i < (1 << order); i++ )
-                scrub_one_page(&pg[i]);
+        if ( !d || !d->recipient || d->recipient->is_dying )
+        {
+            if ( unlikely(scrub) )
+                for ( i = 0; i < (1 << order); i++ )
+                    scrub_one_page(&pg[i]);
 
-        free_heap_pages(pg, order);
+            free_heap_pages(pg, order);
+        }
+        else
+        {
+            mfn = page_to_mfn(pg);
+            gmfn = mfn_to_gmfn(d, mfn);
+
+            page_set_owner(pg, NULL);
+            assign_pages(d->recipient, pg, order, 0);
Quoted 4 lines
Quoted 1 line

This can fail.

.. if the recipient is dying or we're over-allocating. Shouldn't happen (if toolstack does its job right) but I'll add a check.

You must not rely on the tool stack doing things right (see XSA-77).

Quoted 25 lines
Quoted 8 lines

What's worse though is that you don't guard against XEN_DOMCTL_set_recipient zapping d->recipient. If that really is necessary to be supported, some synchronization will be needed.

And finally - what's the intended protocol for the tool stack to know that all pages got transferred (and hence the new domain can be launched)?

(hope this answers both questions above)

Now the protocol is:
1) Toolstack queries for all page frames (with xc_get_pfn_type_batch()) for the old domain.
2) Toolstack issues XEN_DOMCTL_set_recipient with the recipient domain
3) Toolstack kills the source domain
4) Toolstack waits for awhile (loop keeps checking while we see new pages arriving + some timeout).
5) Toolstack issues XEN_DOMCTL_set_recipient with DOMID_INVALID resetting recipient.
6) Toolstack checks if all pages were transferred
7) If some pages are missing (e.g. source domain became zombie holding something) request new empty pages instead.
8) Toolstack starts new domain

Quoted 2 lines

So we don't actually need XEN_DOMCTL_set_recipient to switch from one recipient to the other, we just need a way to reset it.

Quoted 6 lines

No, this doesn't address either question. For the first one, you again assume the tool stack behaves correctly, which is wrong nowadays. And for the second you give a description, but that's not really a dependable protocol. Nor do I really follow why resetting the recipient is necessary. Can't the tools e.g. wait for the final death of the domain if there's no other notification?

Quoted 8 lines

Yes, it's possible and should happen in all normal cases. However my idea was that it's possible to start new domain even if the old one fails to die holding several (presumably special) pages and we're fine with replacing those with empty pages. In case we go for 'always wait for the original domain to die' solution resetting XEN_DOMCTL_set_recipient is not necessary (it is necessary in case we don't as we can recieve a page after we already requested a new one instead).

I think this "always wait" is imo the only reasonable solution: How would replacing some pages with empty ones fit the kexec/kdump purposes? However, it may not be necessary to wait for the domain to completely disappear, it may be sufficient to wait for its d->tot_pages to become zero.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

From a general (hypervisor) point of view we don't actually care if the domain is dying or not. We just want to recieve all freed pages from this domain (so they go to some other domain instead of trash).

We do care I think, primarily because we want a correct GMFN <-> MFN mapping. Seeing multiple pages for the same GMFN is for example going to cause the whole process in its current form to fail.

Can adding a check that nothing is mapped to the GMFN before mapping new MFN there be a solution?

Not checking for this is presumably the better approach - the new domain would get what the old one had last at a given GMFN. What it may not get are pages that intermediately got moved around. But again, all that is relevant only if the old domain can still alter its memory layout while the transfer is already in progress, which (as said before) I think should be avoided.

Quoted 6 lines
Quoted 5 lines

And considering the need for such a correct mapping - is it always the case that the mapping gets updated after a page got removed from a guest? (I can see that the mapping doesn't change anymore for a dying guest, but you explicitly say that you want/need this to work before the guest is actually marked dying.)

Quoted 6 lines

Actual reassignment here happens for a dying guest only as XEN_DOMCTL_set_recipient does nothing. If you think it's safer to depend on the fact that dying flag is always set we can couple XEN_DOMCTL_set_recipient with XEN_DOMCTL_destroydomain in one call. It is possible if we go for 'always wait for the original domain to die' solution (so no reset is required).

I indeed think that's the safer and more correct route.

Jan

#9 lore raw

RFC PATCH v3 4/8 libxc: support XEN_DOMCTL_set_recipient

In reply to Vitaly Kuznetsov

Introduce new xc_domain_set_recipient() function to support XEN_DOMCTL_set_recipient.

---
 tools/libxc/xc_domain.c | 10 ++++++++++
 tools/libxc/xenctrl.h   | 14 ++++++++++++++
 2 files changed, 24 insertions(+)
tools/libxc/xc_domain.c +10 -0
index 14f4666..2c69087 100644
@@ -92,6 +92,16 @@ int xc_domain_destroy(xc_interface *xch,
     return ret;
 }
 
+int xc_domain_set_recipient(xc_interface *xch,
+                            uint32_t domid, uint32_t recipient)
+{
+    DECLARE_DOMCTL;
+    domctl.cmd = XEN_DOMCTL_set_recipient;
+    domctl.domain = (domid_t)domid;
+    domctl.u.recipient.recipient = (domid_t)recipient;
+    return do_domctl(xch, &domctl);
+}
+
 int xc_domain_shutdown(xc_interface *xch,
                        uint32_t domid,
                        int reason)
tools/libxc/xenctrl.h +14 -0
index 514b241..e59c718 100644
@@ -551,6 +551,20 @@ int xc_domain_unpause(xc_interface *xch,
 int xc_domain_destroy(xc_interface *xch,
                       uint32_t domid);
 
+/**
+ * This function sets a 'recipient' domain for a domain (when the source domain
+ * releases memory it is being reassigned to the recipient domain instead of
+ * being freed). The destination domain is supposed to have enough max_mem.
+ * Supplying DOMID_INVALID as a recipient cleans things up.
+ *
+ * @parm xch a handle to an open hypervisor interface
+ * @parm domid the source domain id
+ * @parm recipient the destrination domain id
+ * @return 0 on success, -1 on failure
+ */
+int xc_domain_set_recipient(xc_interface *xch,
+                            uint32_t domid, uint32_t recipient);
+
 
 /**
  * This function resumes a suspended domain. The domain should have
Signature · 1 line
1.9.3
#10 lore raw

RFC PATCH v3 5/8 libxl: add libxl__domain_soft_reset_destroy_old()

In reply to Vitaly Kuznetsov

New libxl__domain_soft_reset_destroy_old() is an internal-only version of libxl_domain_destroy() which follows the same domain destroy path with the only difference: xc_domain_destroy() is being avoided so the domain is not actually being destroyed.

Add soft_reset flag to libxl__domain_destroy_state structure to support the change.

The original libxl_domain_destroy() function could be easily modified to support new flag but I'm trying to avoid that as it is part of public API.

---
 tools/libxl/libxl.c          | 32 +++++++++++++++++++++++++++-----
 tools/libxl/libxl_internal.h |  4 ++++
 2 files changed, 31 insertions(+), 5 deletions(-)
tools/libxl/libxl.c +27 -5
index 77672d0..a38019b 100644
@@ -1351,6 +1351,23 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid,
     return AO_INPROGRESS;
 }
 
+int libxl__domain_soft_reset_destroy_old(libxl_ctx *ctx, uint32_t domid,
+                                         const libxl_asyncop_how *ao_how)
+{
+    AO_CREATE(ctx, domid, ao_how);
+    libxl__domain_destroy_state *dds;
+
+    GCNEW(dds);
+    dds->ao = ao;
+    dds->domid = domid;
+    dds->callback = domain_destroy_cb;
+    dds->soft_reset = 1;
+    libxl__domain_destroy(egc, dds);
+
+    return AO_INPROGRESS;
+}
+
+
 static void domain_destroy_cb(libxl__egc *egc, libxl__domain_destroy_state *dds,
                               int rc)
 {
@@ -1526,6 +1543,7 @@ static void devices_destroy_cb(libxl__egc *egc,
 {
     STATE_AO_GC(drs->ao);
     libxl__destroy_domid_state *dis = CONTAINER_OF(drs, *dis, drs);
+    libxl__domain_destroy_state *dds = CONTAINER_OF(dis, *dds, domain);
     libxl_ctx *ctx = CTX;
     uint32_t domid = dis->domid;
     char *dom_path;
@@ -1564,11 +1582,15 @@ static void devices_destroy_cb(libxl__egc *egc,
     }
     libxl__userdata_destroyall(gc, domid);
 
-    rc = xc_domain_destroy(ctx->xch, domid);
-    if (rc < 0) {
-        LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
-        rc = ERROR_FAIL;
-        goto out;
+    if (!dds->soft_reset)
+    {
+        rc = xc_domain_destroy(ctx->xch, domid);
+        if (rc < 0) {
+            LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
+                                "xc_domain_destroy failed for %d", domid);
+            rc = ERROR_FAIL;
+            goto out;
+        }
     }
     rc = 0;
 
tools/libxl/libxl_internal.h +4 -0
index f61673c..b43f2e3 100644
@@ -2762,6 +2762,7 @@ struct libxl__domain_destroy_state {
     int stubdom_finished;
     libxl__destroy_domid_state domain;
     int domain_finished;
+    int soft_reset;
 };
 
 /*
@@ -2925,6 +2926,9 @@ _hidden void libxl__domain_save_device_model(libxl__egc *egc,
 
 _hidden const char *libxl__device_model_savefile(libxl__gc *gc, uint32_t domid);
 
+_hidden int libxl__domain_soft_reset_destroy_old(libxl_ctx *ctx, uint32_t domid,
+                                                 const libxl_asyncop_how *ao_how);
+
 
 /*
  * Convenience macros.
Signature · 1 line
1.9.3
#11 lore raw

RFC PATCH v3 6/8 libxc: introduce soft reset for HVM domains

In reply to Vitaly Kuznetsov

Add new xc_domain_soft_reset() function which performs so-called 'soft reset' for an HVM domain. It is being performed in the following way:
- Save HVM context and all HVM params;
- Set a recipient domain (an empty one with the same config) with XEN_DOMCTL_set_recipient;
- The source domain is destroyed;
- If everything goes well all memory gets reassigned to the new domain;
- If we have some leftovers left (that means something went wrong during destroy) just copy all remaining pages;
- Restore HVM context, HVM params, seed grant table

After that the domain resumes execution from where SHUTDOWN_soft_reset was called.

---
 tools/libxc/Makefile               |   1 +
 tools/libxc/xc_domain_soft_reset.c | 394 +++++++++++++++++++++++++++++++++++++
 tools/libxc/xenguest.h             |  20 ++
 3 files changed, 415 insertions(+)
 create mode 100644 tools/libxc/xc_domain_soft_reset.c
tools/libxc/Makefile +1 -0
index 3b04027..b5d4b60 100644
@@ -49,6 +49,7 @@ GUEST_SRCS-y += xc_offline_page.c xc_compression.c
 else
 GUEST_SRCS-y += xc_nomigrate.c
 endif
+GUEST_SRCS-y += xc_domain_soft_reset.c
 
 vpath %.c ../../xen/common/libelf
 CFLAGS += -I../../xen/common/libelf
tools/libxc/xc_domain_soft_reset.c new file +394 -0

394 lines not on this page · raw message

tools/libxc/xenguest.h +20 -0
index 40bbac8..770cd10 100644
@@ -131,6 +131,26 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
  * of the new domain is automatically appended to the filename,
  * separated by a ".".
  */
+
+/**
+ * This function does soft reset for a domain. During soft reset all
+ * source domain's memory is being reassigned to the destination domain,
+ * HVM context and HVM params are being copied.
+ *
+ * @parm xch a handle to an open hypervisor interface
+ * @parm source_dom the id of the source domain
+ * @parm dest_dom the id of the destination domain
+ * @parm console_domid the id of the domain handling console
+ * @parm console_mfn returned with the mfn of the console page
+ * @parm store_domid the id of the domain handling store
+ * @parm store_mfn returned with the mfn of the store page
+ * @return 0 on success, -1 on failure
+ */
+int xc_domain_soft_reset(xc_interface *xch, uint32_t source_dom,
+                         uint32_t dest_dom, domid_t console_domid,
+                         unsigned long *console_mfn, domid_t store_domid,
+                         unsigned long *store_mfn);
+
 #define XC_DEVICE_MODEL_RESTORE_FILE "/var/lib/xen/qemu-resume"
 
 /**
Signature · 1 line
1.9.3
#12 lore raw

RFC PATCH v3 7/8 libxl: soft reset support

In reply to Vitaly Kuznetsov

Perform soft reset when a domain did SHUTDOWN_soft_reset. Migrate the content with xc_domain_soft_reset(), reload dm and toolstack.

---
 tools/libxl/libxl.h          |   6 +++
 tools/libxl/libxl_create.c   | 103 +++++++++++++++++++++++++++++++++++++++----
 tools/libxl/libxl_internal.h |   4 ++
 tools/libxl/xl_cmdimpl.c     |  22 ++++++++-
 4 files changed, 124 insertions(+), 11 deletions(-)
tools/libxl/libxl.h +6 -0
index bc68cac..24b43c5 100644
@@ -877,6 +877,12 @@ int static inline libxl_domain_create_restore_0x040200(
 
 #endif
 
+int libxl_domain_soft_reset(libxl_ctx *ctx, libxl_domain_config *d_config,
+                            uint32_t *domid, uint32_t domid_old,
+                            const libxl_asyncop_how *ao_how,
+                            const libxl_asyncprogress_how *aop_console_how)
+                            LIBXL_EXTERNAL_CALLERS_ONLY;
+
   /* A progress report will be made via ao_console_how, of type
    * domain_create_console_available, when the domain's primary
    * console is available and can be connected to.
tools/libxl/libxl_create.c +94 -9

183 lines not on this page · raw message

tools/libxl/libxl_internal.h +4 -0
index b43f2e3..14b38f5 100644
@@ -2862,6 +2862,7 @@ struct libxl__domain_create_state {
     libxl_domain_config *guest_config;
     libxl_domain_config guest_config_saved; /* vanilla config */
     int restore_fd;
+    uint32_t domid_soft_reset;
     libxl__domain_create_cb *callback;
     libxl_asyncprogress_how aop_console_how;
     /* private to domain_create */
@@ -2916,6 +2917,9 @@ _hidden void libxl__xc_domain_restore(libxl__egc *egc,
  * If rc!=0, retval and errnoval are undefined. */
 _hidden void libxl__xc_domain_restore_done(libxl__egc *egc, void *dcs_void,
                                            int rc, int retval, int errnoval);
+/* calls libxl__xc_domain_restore_done when done */
+_hidden void libxl__xc_domain_soft_reset(libxl__egc *egc,
+                                         libxl__domain_create_state *dcs);
 
 /* Each time the dm needs to be saved, we must call suspend and then save */
 _hidden int libxl__domain_suspend_device_model(libxl__gc *gc,
tools/libxl/xl_cmdimpl.c +20 -2
index b40ad50..4307edf 100644
@@ -1818,7 +1818,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 soft reset, or 0
  * Can update r_domid if domain is destroyed etc */
 static int handle_domain_death(uint32_t *r_domid,
                                libxl_event *event,
@@ -1844,6 +1845,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_SOFT_RESET:
+        LOG("Domain performed soft reset.");
+        return 3;
     default:
         LOG("Unknown shutdown reason code %d. Destroying domain.",
             event->u.domain_shutdown.shutdown_reason);
@@ -2067,6 +2071,7 @@ static void evdisable_disk_ejects(libxl_evgen_disk_eject **diskws,
 static uint32_t create_domain(struct domain_create *dom_info)
 {
     uint32_t domid = INVALID_DOMID;
+    uint32_t domid_old = INVALID_DOMID;
 
     libxl_domain_config d_config;
 
@@ -2292,7 +2297,18 @@ start:
          * restore/migrate-receive it again.
          */
         restoring = 0;
-    }else{
+    } else if (domid_old != INVALID_DOMID) {
+        /* Do soft reset */
+        d_config.b_info.nodemap.size = 0;
+        ret = libxl_domain_soft_reset(ctx, &d_config,
+                                      &domid, domid_old,
+                                      0, 0);
+
+        if ( ret ) {
+            goto error_out;
+        }
+        domid_old = INVALID_DOMID;
+    } else {
         ret = libxl_domain_create_new(ctx, &d_config, &domid,
                                       0, autoconnect_console_how);
     }
@@ -2356,6 +2372,8 @@ start:
                 event->u.domain_shutdown.shutdown_reason,
                 event->u.domain_shutdown.shutdown_reason);
             switch (handle_domain_death(&domid, event, &d_config)) {
+            case 3:
+                domid_old = domid;
             case 2:
                 if (!preserve_domain(&domid, event, &d_config)) {
                     /* If we fail then exit leaving the old domain in place. */
Signature · 1 line
1.9.3
#13 lore raw

RFC PATCH v3 8/8 xsm: add XEN_DOMCTL_set_recipient support

In reply to Vitaly Kuznetsov
---
 xen/common/domctl.c                 | 14 +++++++++++---
 xen/include/xsm/dummy.h             |  6 ++++++
 xen/include/xsm/xsm.h               |  6 ++++++
 xen/xsm/dummy.c                     |  1 +
 xen/xsm/flask/hooks.c               | 17 +++++++++++++++++
 xen/xsm/flask/policy/access_vectors | 10 ++++++++++
 6 files changed, 51 insertions(+), 3 deletions(-)
xen/common/domctl.c +11 -3
index 38cd10a..b456ff2 100644
@@ -1158,6 +1158,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
         if ( op->u.recipient.recipient == DOMID_INVALID )
         {
+            ret = xsm_set_recipient(XSM_HOOK, d, NULL);
+            if ( ret )
+                break;
+
             if ( d->recipient )
             {
                 put_domain(d->recipient);
@@ -1172,10 +1176,14 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
             ret = -ESRCH;
             break;
         }
-        else
-        {
-            d->recipient = recipient_dom;
+
+        ret = xsm_set_recipient(XSM_HOOK, d, recipient_dom);
+        if ( ret ) {
+            put_domain(recipient_dom);
+            break;
         }
+
+        d->recipient = recipient_dom;
     }
     break;
 
xen/include/xsm/dummy.h +6 -0
index df55e70..c037b78 100644
@@ -113,6 +113,12 @@ static XSM_INLINE int xsm_set_target(XSM_DEFAULT_ARG struct domain *d, struct do
     return xsm_default_action(action, current->domain, NULL);
 }
 
+static XSM_INLINE int xsm_set_recipient(XSM_DEFAULT_ARG struct domain *d, struct domain *e)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, NULL);
+}
+
 static XSM_INLINE int xsm_domctl(XSM_DEFAULT_ARG struct domain *d, int cmd)
 {
     XSM_ASSERT_ACTION(XSM_OTHER);
xen/include/xsm/xsm.h +6 -0
index 6c1c079..818654d 100644
@@ -58,6 +58,7 @@ struct xsm_operations {
     int (*domctl_scheduler_op) (struct domain *d, int op);
     int (*sysctl_scheduler_op) (int op);
     int (*set_target) (struct domain *d, struct domain *e);
+    int (*set_recipient) (struct domain *d, struct domain *e);
     int (*domctl) (struct domain *d, int cmd);
     int (*sysctl) (int cmd);
     int (*readconsole) (uint32_t clear);
@@ -210,6 +211,11 @@ static inline int xsm_set_target (xsm_default_t def, struct domain *d, struct do
     return xsm_ops->set_target(d, e);
 }
 
+static inline int xsm_set_recipient (xsm_default_t def, struct domain *d, struct domain *r)
+{
+    return xsm_ops->set_recipient(d, r);
+}
+
 static inline int xsm_domctl (xsm_default_t def, struct domain *d, int cmd)
 {
     return xsm_ops->domctl(d, cmd);
xen/xsm/dummy.c +1 -0
index 0826a8b..e38d9c6 100644
@@ -35,6 +35,7 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, domctl_scheduler_op);
     set_to_dummy_if_null(ops, sysctl_scheduler_op);
     set_to_dummy_if_null(ops, set_target);
+    set_to_dummy_if_null(ops, set_recipient);
     set_to_dummy_if_null(ops, domctl);
     set_to_dummy_if_null(ops, sysctl);
     set_to_dummy_if_null(ops, readconsole);
xen/xsm/flask/hooks.c +17 -0
index df05566..ae16795 100644
@@ -565,6 +565,21 @@ static int flask_set_target(struct domain *d, struct domain *t)
     return rc;
 }
 
+static int flask_set_recipient(struct domain *d, struct domain *r)
+{
+    int rc;
+    struct domain_security_struct *dsec, *rsec;
+    dsec = d->ssid;
+    rsec = r->ssid;
+
+    rc = current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_AS_SOURCE);
+    if ( rc )
+        return rc;
+    if ( r )
+        rc = current_has_perm(r, SECCLASS_DOMAIN2, DOMAIN2__SET_AS_RECIPIENT);
+    return rc;
+}
+
 static int flask_domctl(struct domain *d, int cmd)
 {
     switch ( cmd )
@@ -577,6 +592,7 @@ static int flask_domctl(struct domain *d, int cmd)
     case XEN_DOMCTL_iomem_permission:
     case XEN_DOMCTL_memory_mapping:
     case XEN_DOMCTL_set_target:
+    case XEN_DOMCTL_set_recipient:
 #ifdef CONFIG_X86
     /* These have individual XSM hooks (arch/x86/domctl.c) */
     case XEN_DOMCTL_shadow_op:
@@ -1493,6 +1509,7 @@ static struct xsm_operations flask_ops = {
     .domctl_scheduler_op = flask_domctl_scheduler_op,
     .sysctl_scheduler_op = flask_sysctl_scheduler_op,
     .set_target = flask_set_target,
+    .set_recipient = flask_set_recipient,
     .domctl = flask_domctl,
     .sysctl = flask_sysctl,
     .readconsole = flask_readconsole,
xen/xsm/flask/policy/access_vectors +10 -0
index d279841..5604281 100644
@@ -132,6 +132,8 @@ class domain
 #  target = the new target domain
 # see also the domain2 make_priv_for and set_as_target checks
     set_target
+# XEN_DOMCTL_set_recipient
+    set_recipient
 # SCHEDOP_remote_shutdown
     shutdown
 # XEN_DOMCTL_set{,_machine}_address_size
@@ -184,6 +186,14 @@ class domain2
 #  source = the domain making the hypercall
 #  target = the new target domain
     set_as_target
+# checked in XEN_DOMCTL_set_recipient:
+#  source = the domain making the hypercall
+#  target = the new source domain
+    set_as_source
+# checked in XEN_DOMCTL_set_recipient:
+#  source = the domain making the hypercall
+#  target = the new recipient domain
+    set_as_recipient
 # XEN_DOMCTL_set_cpuid
     set_cpuid
 # XEN_DOMCTL_gettscinfo
Signature · 1 line
1.9.3
#14 lore raw
In reply to Vitaly Kuznetsov
Quoted 10 lines · Vitaly Kuznetsov

Changes from RFC/WIPv2:

Here is a slightly different approach to memory reassignment. Instead of introducing new (and very doubtful) XENMEM_transfer operation introduce simple XEN_DOMCTL_set_recipient operation and do everything in free_domheap_pages() handler utilizing normal domain destroy path. This is better because:
- The approach is general-enough
- All memory pages are usually being freed when the domain is destroyed
- No special grants handling required
- Better supportability

I like this idea, but this really isn't my area of expertise so you'll have to wait to see what Jan and Tim say.

Quoted 11 lines

With regards to PV:
Though XEN_DOMCTL_set_recipient works for both PV and HVM this patchset does not bring PV kexec/kdump support. xc_domain_soft_reset() is limited to work with HVM domains only. The main reason for that is: it is (in theory) possible to save p2m and rebuild them with the new domain but that would only allow us to resume execution from where we stopped. If we want to execute new kernel we need to build the same kernel/initrd/bootstrap_pagetables/... structure we build to boot PV domain initially. That however would destroy the original domain's memory thus making kdump impossible. To make everything work additional support from kexec userspace/linux kernel is required and I'm not sure it makes sense to implement all this stuff in the light of PVH.

I'm fine with limiting this to HVM guests.

David

#15 lore raw
In reply to Vitaly Kuznetsov
Quoted 11 lines · Vitaly Kuznetsov

Changes from RFC/WIPv2:

Here is a slightly different approach to memory reassignment. Instead of introducing new (and very doubtful) XENMEM_transfer operation introduce simple XEN_DOMCTL_set_recipient operation and do everything in free_domheap_pages() handler utilizing normal domain destroy path. This is better because:
- The approach is general-enough
- All memory pages are usually being freed when the domain is destroyed
- No special grants handling required
- Better supportability

Quoted 11 lines

With regards to PV:
Though XEN_DOMCTL_set_recipient works for both PV and HVM this patchset does not bring PV kexec/kdump support. xc_domain_soft_reset() is limited to work with HVM domains only. The main reason for that is: it is (in theory) possible to save p2m and rebuild them with the new domain but that would only allow us to resume execution from where we stopped. If we want to execute new kernel we need to build the same kernel/initrd/bootstrap_pagetables/... structure we build to boot PV domain initially. That however would destroy the original domain's memory thus making kdump impossible. To make everything work additional support from kexec userspace/linux kernel is required and I'm not sure it makes sense to implement all this stuff in the light of PVH.

I don't want to create noise but I didn't get many comments on this RFC (the only one belongs to David). In case there are no comments on RFC (design) level I can rebase and send first non-RFC.

I understand 4.5 release is the main focus atm though I would greatly appreciate some feedback.

Thanks,

Quoted 114 lines

Original description:

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.

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

P.S. The patch series can be tested with PVHVM Linux guest with the following modifications:

arch/x86/xen/enlighten.c +24 -0
index c0cb11f..33c5cdd 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_soft_reset);
+	   }
+}
+
+static void xen_pvhvm_crash_shutdown(struct pt_regs *regs)
+{
+	native_machine_crash_shutdown(regs);
+	xen_reboot(SHUTDOWN_soft_reset);
+}
+#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..b5942a8 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_soft_reset 5  /* Soft-reset for kexec.                      */
 
 #endif /* __XEN_PUBLIC_SCHED_H__ */

Vitaly Kuznetsov (8):
xen: introduce SHUTDOWN_soft_reset shutdown reason
libxl: support SHUTDOWN_soft_reset shutdown reason
xen: introduce XEN_DOMCTL_set_recipient
libxc: support XEN_DOMCTL_set_recipient
libxl: add libxl__domain_soft_reset_destroy_old()
libxc: introduce soft reset for HVM domains
libxl: soft reset support
xsm: add XEN_DOMCTL_set_recipient support

 tools/libxc/Makefile                |   1 +
 tools/libxc/xc_domain.c             |  10 +
 tools/libxc/xc_domain_soft_reset.c  | 394 ++++++++++++++++++++++++++++++++++++
 tools/libxc/xenctrl.h               |  14 ++
 tools/libxc/xenguest.h              |  20 ++
 tools/libxl/libxl.c                 |  32 ++-
 tools/libxl/libxl.h                 |   6 +
 tools/libxl/libxl_create.c          | 103 +++++++++-
 tools/libxl/libxl_internal.h        |   8 +
 tools/libxl/libxl_types.idl         |   1 +
 tools/libxl/xl_cmdimpl.c            |  24 ++-
 tools/python/xen/lowlevel/xl/xl.c   |   1 +
 xen/common/domain.c                 |   3 +
 xen/common/domctl.c                 |  35 ++++
 xen/common/page_alloc.c             |  26 ++-
 xen/common/shutdown.c               |   7 +
 xen/include/public/domctl.h         |  19 ++
 xen/include/public/sched.h          |   3 +-
 xen/include/xen/sched.h             |   1 +
 xen/include/xsm/dummy.h             |   6 +
 xen/include/xsm/xsm.h               |   6 +
 xen/xsm/dummy.c                     |   1 +
 xen/xsm/flask/hooks.c               |  17 ++
 xen/xsm/flask/policy/access_vectors |  10 +
 24 files changed, 726 insertions(+), 22 deletions(-)
 create mode 100644 tools/libxc/xc_domain_soft_reset.c
Signature · 1 line
  Vitaly
#16 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.11.14 at 14:21, <[email protected]> wrote:

I don't want to create noise but I didn't get many comments on this RFC (the only one belongs to David). In case there are no comments on RFC (design) level I can rebase and send first non-RFC.

Afaic, I will likely have time to look at the hypervisor parts of this only when 4.5 has (at least mostly) settled.

Jan