Skip to contentSkip to conversation

Run PV guest in PVH container

RFC PATCH Superseded Wei Liu 11 participants 206 messages Jan 4, 2018 – Jan 12, 2018 Page 2 of 2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#159 lore raw

Re: [PATCH RFC v1 56/74] xen/pvshim: add grant table operations

In reply to Wei Liu

xen/arch/x86/pv/shim.c line 34

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +1 -0
@@ -30,11 +31,17 @@
 #include <asm/guest.h>
 #include <asm/pv/mm.h>
 
+#include <compat/grant_table.h>

Interesting: The event channel patch gave me the impression that it is not intended to deal with 32-bit guests.

Quoted 46 lines
xen/arch/x86/pv/shim.c +42 -0
@@ -360,6 +367,173 @@ void pv_shim_inject_evtchn(unsigned int port)
     }
 }
 
+long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
+                            unsigned int count, bool compat)
+{
+    struct domain *d = current->domain;
+    long rc = 0;
+
+    if ( count != 1 )
+        return -EINVAL;
+
+    switch ( cmd )
+    {
+    case GNTTABOP_setup_table:
+    {
+        struct gnttab_setup_table nat;
+        struct compat_gnttab_setup_table cmp;
+        unsigned int i;
+
+        if ( unlikely(compat ? copy_from_guest(&cmp, uop, 1)
+                             : copy_from_guest(&nat, uop, 1)) ||
+             unlikely(compat ? !compat_handle_okay(cmp.frame_list,
+                                                   cmp.nr_frames)
+                             : !guest_handle_okay(nat.frame_list,
+                                                  nat.nr_frames)) )
+        {
+            rc = -EFAULT;
+            break;
+        }
+        if ( compat )
+#define XLAT_gnttab_setup_table_HNDL_frame_list(d, s)
+                XLAT_gnttab_setup_table(&nat, &cmp);
+#undef XLAT_gnttab_setup_table_HNDL_frame_list
+
+        nat.status = GNTST_okay;
+
+        spin_lock(&grant_lock);
+        if ( !nr_grant_list )
+        {
+            struct gnttab_query_size query_size = {
+                .dom = DOMID_SELF,
+            };
+
+            rc = xen_hypercall_grant_table_op(GNTTABOP_query_size,

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 421

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+                                              &query_size, 1);
+            if ( rc )
+            {
+                spin_unlock(&grant_lock);
+                break;
+            }
+
+            ASSERT(!grant_frames);
+            grant_frames = xzalloc_array(unsigned long,
+                                         query_size.max_nr_frames);

Hmm, such runtime allocations (especially when the amount can be large) are a fundamental problem. I think this needs setting up before the guest is started.

Quoted 67 lines
xen/arch/x86/pv/shim.c +67 -0
+            if ( !grant_frames )
+            {
+                spin_unlock(&grant_lock);
+                rc = -ENOMEM;
+                break;
+            }
+
+            nr_grant_list = query_size.max_nr_frames;
+        }
+
+        if ( nat.nr_frames > nr_grant_list )
+        {
+            spin_unlock(&grant_lock);
+            rc = -EINVAL;
+            break;
+        }
+
+        for ( i = 0; i < nat.nr_frames; i++ )
+        {
+            if ( !grant_frames[i] )
+            {
+                struct xen_add_to_physmap xatp = {
+                    .domid = DOMID_SELF,
+                    .idx = i,
+                    .space = XENMAPSPACE_grant_table,
+                };
+                mfn_t mfn;
+
+                rc = hypervisor_alloc_unused_page(&mfn);
+                if ( rc )
+                {
+                    gprintk(XENLOG_ERR,
+                            "unable to get memory for grant table\n");
+                    break;
+                }
+
+                xatp.gpfn = mfn_x(mfn);
+                rc = xen_hypercall_memory_op(XENMEM_add_to_physmap, &xatp);
+                if ( rc )
+                {
+                    hypervisor_free_unused_page(mfn);
+                    break;
+                }
+
+                BUG_ON(iomem_permit_access(d, mfn_x(mfn), mfn_x(mfn)));
+                grant_frames[i] = mfn_x(mfn);
+            }
+
+            ASSERT(grant_frames[i]);
+            if ( compat )
+            {
+                compat_pfn_t pfn = grant_frames[i];
+
+                if ( __copy_to_compat_offset(cmp.frame_list, i, &pfn, 1) )
+                {
+                    nat.status = GNTST_bad_virt_addr;
+                    rc = -EFAULT;
+                    break;
+                }
+            }
+            else if ( __copy_to_guest_offset(nat.frame_list, i,
+                                             &grant_frames[i], 1) )
+            {
+                nat.status = GNTST_bad_virt_addr;
+                rc = -EFAULT;
+                break;
+            }

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 498

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+        }
+        spin_unlock(&grant_lock);
+
+        if ( compat )
+#define XLAT_gnttab_setup_table_HNDL_frame_list(d, s)
+                XLAT_gnttab_setup_table(&cmp, &nat);
+#undef XLAT_gnttab_setup_table_HNDL_frame_list
+
+        if ( unlikely(compat ? copy_to_guest(uop, &cmp, 1)
+                             : copy_to_guest(uop, &nat, 1)) )

__copy_to_guest()

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 506

Quoted 8 lines
xen/arch/x86/pv/shim.c +8 -0
+        {
+            rc = -EFAULT;
+            break;
+        }
+
+        break;
+    }
+    case GNTTABOP_query_size:

Blank line above such "case" please.

Quoted 5 lines
xen/arch/x86/pv/shim.c +5 -0
+    {
+        struct gnttab_query_size op;
+        int rc;
+
+        if ( unlikely(copy_from_guest(&op, uop, 1)) )

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 521

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+        {
+            rc = -EFAULT;
+            break;
+        }
+
+        rc = xen_hypercall_grant_table_op(GNTTABOP_query_size, &op, count);
+        if ( rc )
+            break;
+
+        if ( copy_to_guest(uop, &op, 1) )

__copy_to_guest() (assuming this coping in and out is necessary in the first place).

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 530

Quoted 9 lines
xen/arch/x86/pv/shim.c +9 -0
+        {
+            rc = -EFAULT;
+            break;
+        }
+
+        break;
+    }
+    default:
+        rc = -ENOSYS;

-EOPNOTSUPP again please. Plus - what about other sub-ops?

xen/common/grant_table.c do_grant_table_op( · line 3335

Quoted 9 lines
xen/common/grant_table.c +5 -0
@@ -3324,6 +3328,12 @@ do_grant_table_op(
     if ( (cmd &= GNTTABOP_CMD_MASK) != GNTTABOP_cache_flush && opaque_in )
         return -EINVAL;
 
+#ifdef CONFIG_X86
+    if ( pv_shim )
+        /* NB: no continuation support for pv-shim ops. */
+        return pv_shim_grant_table_op(cmd, uop, count, false);
+#endif

As for event channels - patch it right into the hypercall table?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#160 lore raw

Re: [PATCH RFC v1 56/74] xen/pvshim: add grant table operations

Quoted 9 lines · Jan Beulich

xen/arch/x86/pv/shim.c line 34

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +1 -0
@@ -30,11 +31,17 @@
 #include <asm/guest.h>
 #include <asm/pv/mm.h>
 
+#include <compat/grant_table.h>

Interesting: The event channel patch gave me the impression that it is not intended to deal with 32-bit guests.

AFAICT the event channel didn't need any explicit compat stuff. That's not the case with grant tables however...

Quoted 52 lines
Quoted 52 lines
xen/arch/x86/pv/shim.c +48 -0
@@ -360,6 +367,173 @@ void pv_shim_inject_evtchn(unsigned int port)
     }
 }
 
+long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
+                            unsigned int count, bool compat)
+{
+    struct domain *d = current->domain;
+    long rc = 0;
+
+    if ( count != 1 )
+        return -EINVAL;
+
+    switch ( cmd )
+    {
+    case GNTTABOP_setup_table:
+    {
+        struct gnttab_setup_table nat;
+        struct compat_gnttab_setup_table cmp;
+        unsigned int i;
+
+        if ( unlikely(compat ? copy_from_guest(&cmp, uop, 1)
+                             : copy_from_guest(&nat, uop, 1)) ||
+             unlikely(compat ? !compat_handle_okay(cmp.frame_list,
+                                                   cmp.nr_frames)
+                             : !guest_handle_okay(nat.frame_list,
+                                                  nat.nr_frames)) )
+        {
+            rc = -EFAULT;
+            break;
+        }
+        if ( compat )
+#define XLAT_gnttab_setup_table_HNDL_frame_list(d, s)
+                XLAT_gnttab_setup_table(&nat, &cmp);
+#undef XLAT_gnttab_setup_table_HNDL_frame_list
+
+        nat.status = GNTST_okay;
+
+        spin_lock(&grant_lock);
+        if ( !nr_grant_list )
+        {
+            struct gnttab_query_size query_size = {
+                .dom = DOMID_SELF,
+            };
+
+            rc = xen_hypercall_grant_table_op(GNTTABOP_query_size,
+                                              &query_size, 1);
+            if ( rc )
+            {
+                spin_unlock(&grant_lock);
+                break;
+            }
Quoted 8 lines

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 421

Quoted 4 lines
xen/arch/x86/pv/shim.c +4 -0
+
+            ASSERT(!grant_frames);
+            grant_frames = xzalloc_array(unsigned long,
+                                         query_size.max_nr_frames);

Hmm, such runtime allocations (especially when the amount can be large) are a fundamental problem. I think this needs setting up before the guest is started.

The shim already sets some memory apart for it's own usage. It could be moved to some shim-start function, but it will likely have to be freed and allocated again on migration, since the number of grant table frames can change when migrating from one host to another.

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+    {
+        struct gnttab_query_size op;
+        int rc;
+
+        if ( unlikely(copy_from_guest(&op, uop, 1)) )
+        {
+            rc = -EFAULT;
+            break;
+        }
+
Quoted 8 lines

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 521

Quoted 5 lines
xen/arch/x86/pv/shim.c +5 -0
+        rc = xen_hypercall_grant_table_op(GNTTABOP_query_size, &op, count);
+        if ( rc )
+            break;
+
+        if ( copy_to_guest(uop, &op, 1) )

__copy_to_guest() (assuming this coping in and out is necessary in the first place).

I guess this could be bypassed by just using uop instead of op in the hypercall?

Quoted 11 lines

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 530

Quoted 9 lines
xen/arch/x86/pv/shim.c +9 -0
+        {
+            rc = -EFAULT;
+            break;
+        }
+
+        break;
+    }
+    default:
+        rc = -ENOSYS;

-EOPNOTSUPP again please. Plus - what about other sub-ops?

They are not yet implemented. I think this is bare minimum needed to boot a PV DomU, we can expand this later on.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#161 lore raw

Re: [PATCH RFC v1 56/74] xen/pvshim: add grant table operations

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 19:34, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +10 -0
+    {
+        struct gnttab_query_size op;
+        int rc;
+
+        if ( unlikely(copy_from_guest(&op, uop, 1)) )
+        {
+            rc = -EFAULT;
+            break;
+        }
+
Quoted 8 lines

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 521

Quoted 5 lines
xen/arch/x86/pv/shim.c +5 -0
+        rc = xen_hypercall_grant_table_op(GNTTABOP_query_size, &op, count);
+        if ( rc )
+            break;
+
+        if ( copy_to_guest(uop, &op, 1) )

__copy_to_guest() (assuming this coping in and out is necessary in the first place).

Quoted 2 lines

I guess this could be bypassed by just using uop instead of op in the hypercall?

That's my impression, but you doing the copying made me assume you might have found a case where things don't work without copying.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#162 lore raw

Re: [PATCH RFC v1 56/74] xen/pvshim: add grant table operations

Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 19:34, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +5 -0
+    {
+        struct gnttab_query_size op;
+        int rc;
+
+        if ( unlikely(copy_from_guest(&op, uop, 1)) )

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 521

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+        {
+            rc = -EFAULT;
+            break;
+        }
+
+        rc = xen_hypercall_grant_table_op(GNTTABOP_query_size, &op, count);
+        if ( rc )
+            break;
+
+        if ( copy_to_guest(uop, &op, 1) )

__copy_to_guest() (assuming this coping in and out is necessary in the first place).

Quoted 6 lines
Quoted 2 lines

I guess this could be bypassed by just using uop instead of op in the hypercall?

That's my impression, but you doing the copying made me assume you might have found a case where things don't work without copying.

No, just didn't realize I could do it that way. gnttab_query_size doesn't require any compat code AFAICT, so it should work fine.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#163 lore raw

RFC PATCH 57/74 x86/pv-shim: shadow PV console's page for L2 DomU

In reply to Wei Liu

From: Sergey Dyasli <[email protected]>

---
 xen/arch/x86/pv/shim.c      |  31 ++++++++--
 xen/drivers/char/Makefile   |   1 +
 xen/drivers/char/console.c  |   4 ++
 xen/drivers/char/consoled.c | 145 ++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/consoled.h  |  27 +++++++++
 5 files changed, 204 insertions(+), 4 deletions(-)
 create mode 100644 xen/drivers/char/consoled.c
 create mode 100644 xen/include/xen/consoled.h
xen/arch/x86/pv/shim.c +27 -4
index 98c1e31e8f..74b24bf950 100644
@@ -25,6 +25,8 @@
 #include <xen/iocap.h>
 #include <xen/shutdown.h>
 #include <xen/types.h>
+#include <xen/consoled.h>
+#include <xen/pv_console.h>
 
 #include <asm/apic.h>
 #include <asm/dom0_build.h>
@@ -125,13 +127,28 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
 })
     SET_AND_MAP_PARAM(HVM_PARAM_STORE_PFN, si->store_mfn, store_va);
     SET_AND_MAP_PARAM(HVM_PARAM_STORE_EVTCHN, si->store_evtchn, 0);
+    SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 0);
     if ( !pv_console )
-    {
         SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_PFN, si->console.domU.mfn,
                           console_va);
-        SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 0);
-    }
 #undef SET_AND_MAP_PARAM
+    else
+    {
+        /* Allocate a new page for DomU's PV console */
+        void *page = alloc_xenheap_pages(0, MEMF_bits(32));
+        uint64_t console_mfn;
+
+        ASSERT(page);
+        clear_page(page);
+        console_mfn = virt_to_mfn(page);
+        si->console.domU.mfn = console_mfn;
+        share_xen_page_with_guest(mfn_to_page(console_mfn), d,
+                                  XENSHARE_writable);
+        replace_va(d, l4start, console_va, console_mfn);
+        dom0_update_physmap(d, (console_va - va_start) >> PAGE_SHIFT,
+                            console_mfn, vphysmap);
+        consoled_set_ring_addr(page);
+    }
 }
 
 void pv_shim_shutdown(uint8_t reason)
@@ -334,7 +351,13 @@ case EVTCHNOP_##cmd: {                                                  \
         if ( copy_from_guest(&send, arg, 1) != 0 )
             return -EFAULT;
 
-        rc = xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
+        if ( pv_console && send.port == pv_console_evtchn() )
+        {
+            consoled_guest_rx();
+            rc = 0;
+        }
+        else
+            rc = xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
 
         break;
     }
xen/drivers/char/Makefile +1 -0
index 9d48d0f2dc..0d48b16e8d 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_HAS_EHCI) += ehci-dbgp.o
 obj-$(CONFIG_ARM) += arm-uart.o
 obj-y += serial.o
 obj-$(CONFIG_XEN_GUEST) += xen_pv_console.o
+obj-$(CONFIG_PV_SHIM) += consoled.o
xen/drivers/char/console.c +4 -0
index 354e020d19..3c615a255c 100644
@@ -15,6 +15,7 @@
 #include <xen/init.h>
 #include <xen/event.h>
 #include <xen/console.h>
+#include <xen/consoled.h>
 #include <xen/serial.h>
 #include <xen/pv_console.h>
 #include <xen/softirq.h>
@@ -408,6 +409,9 @@ static void __serial_rx(char c, struct cpu_user_regs *regs)
         serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
     /* Always notify the guest: prevents receive path from getting stuck. */
     send_global_virq(VIRQ_CONSOLE);
+
+    if ( pv_shim && pv_console )
+        consoled_guest_tx(c);
 }
 
 static void serial_rx(char c, struct cpu_user_regs *regs)
xen/drivers/char/consoled.c new file +145 -0

145 lines not on this page · raw message

xen/include/xen/consoled.h new file +27 -0
new file mode 100644
index 0000000000..fd5d220a8a
@@ -0,0 +1,27 @@
+#ifndef __XEN_CONSOLED_H__
+#define __XEN_CONSOLED_H__
+
+#include <public/io/console.h>
+
+#ifdef CONFIG_PV_SHIM
+
+void consoled_set_ring_addr(struct xencons_interface *ring);
+struct xencons_interface *consoled_get_ring_addr(void);
+size_t consoled_guest_rx(void);
+size_t consoled_guest_tx(char c);
+
+#else
+
+size_t consoled_guest_tx(char c) { return 0; }
+
+#endif /* !CONFIG_PV_SHIM */
+#endif /* __XEN_CONSOLED_H__ */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#164 lore raw

Re: [PATCH RFC v1 57/74] x86/pv-shim: shadow PV console's page for L2 DomU

Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +7 -3
@@ -125,13 +127,28 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
 })
     SET_AND_MAP_PARAM(HVM_PARAM_STORE_PFN, si->store_mfn, store_va);
     SET_AND_MAP_PARAM(HVM_PARAM_STORE_EVTCHN, si->store_evtchn, 0);
+    SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 0);
     if ( !pv_console )
-    {
         SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_PFN, si->console.domU.mfn,
                           console_va);
-        SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 0);
-    }
 #undef SET_AND_MAP_PARAM
+    else
+    {
+        /* Allocate a new page for DomU's PV console */
+        void *page = alloc_xenheap_pages(0, MEMF_bits(32));
+        uint64_t console_mfn;
+

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 150

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+        ASSERT(page);
+        clear_page(page);
+        console_mfn = virt_to_mfn(page);
+        si->console.domU.mfn = console_mfn;
+        share_xen_page_with_guest(mfn_to_page(console_mfn), d,
+                                  XENSHARE_writable);
+        replace_va(d, l4start, console_va, console_mfn);
+        dom0_update_physmap(d, (console_va - va_start) >> PAGE_SHIFT,
+                            console_mfn, vphysmap);
+        consoled_set_ring_addr(page);

This looks to be a fair part of SET_AND_MAP_PARAM(), so I think this wants breaking out as a separate macro.

Quoted 10 lines
xen/drivers/char/consoled.c new file +10 -0
+size_t consoled_guest_rx(void)
+{
+    size_t recv = 0, idx = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    spin_lock(&rx_lock);
+

xen/drivers/char/consoled.c line 70

Quoted 10 lines
xen/drivers/char/consoled.c new file +10 -0
+    cons = cons_ring->out_cons;
+    prod = ACCESS_ONCE(cons_ring->out_prod);
+    ASSERT((prod - cons) <= sizeof(cons_ring->out));
+
+    /* Is the ring empty? */
+    if ( cons == prod )
+        goto out;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();

I think this need to move up ahead of the if(). In the comment perhaps s/Update/Latch/?

Quoted 8 lines
xen/drivers/char/consoled.c new file +8 -0
+size_t consoled_guest_tx(char c)
+{
+    size_t sent = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+

xen/drivers/char/consoled.c line 121

Quoted 10 lines
xen/drivers/char/consoled.c new file +10 -0
+    cons = ACCESS_ONCE(cons_ring->in_cons);
+    prod = cons_ring->in_prod;
+    ASSERT((prod - cons) <= sizeof(cons_ring->in));
+
+    /* Is the ring out of space? */
+    if ( sizeof(cons_ring->in) - (prod - cons) == 0 )
+        goto notify;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();

Same here.

Quoted 8 lines
xen/include/xen/consoled.h new file +5 -0
@@ -0,0 +1,27 @@
+#ifndef __XEN_CONSOLED_H__
+#define __XEN_CONSOLED_H__
+
+#include <public/io/console.h>
+

xen/include/xen/consoled.h line 15

Quoted 10 lines
xen/include/xen/consoled.h new file +10 -0
+#ifdef CONFIG_PV_SHIM
+
+void consoled_set_ring_addr(struct xencons_interface *ring);
+struct xencons_interface *consoled_get_ring_addr(void);
+size_t consoled_guest_rx(void);
+size_t consoled_guest_tx(char c);
+
+#else
+
+size_t consoled_guest_tx(char c) { return 0; }

static inline

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#165 lore raw

Re: [PATCH RFC v1 57/74] x86/pv-shim: shadow PV console's page for L2 DomU

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/drivers/char/consoled.c new file +15 -0
+size_t consoled_guest_rx(void)
+{
+    size_t recv = 0, idx = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    spin_lock(&rx_lock);
+
+    cons = cons_ring->out_cons;
+    prod = ACCESS_ONCE(cons_ring->out_prod);
+    ASSERT((prod - cons) <= sizeof(cons_ring->out));
+
+    /* Is the ring empty? */
Quoted 8 lines

xen/drivers/char/consoled.c line 70

Quoted 5 lines
xen/drivers/char/consoled.c new file +5 -0
+    if ( cons == prod )
+        goto out;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();

I think this need to move up ahead of the if(). In the comment perhaps s/Update/Latch/?

The read/write memory barriers here are between read/write accesses to ring->out_prod and ring->out array. So there is no need to move them. (the same goes for the input ring)

Signature · 6 lines
Thanks,
Sergey
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#166 lore raw

Re: [PATCH RFC v1 57/74] x86/pv-shim: shadow PV console's page for L2 DomU

Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 16:43, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/drivers/char/consoled.c new file +15 -0
+size_t consoled_guest_rx(void)
+{
+    size_t recv = 0, idx = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    spin_lock(&rx_lock);
+
+    cons = cons_ring->out_cons;
+    prod = ACCESS_ONCE(cons_ring->out_prod);
+    ASSERT((prod - cons) <= sizeof(cons_ring->out));
+
+    /* Is the ring empty? */
Quoted 8 lines

xen/drivers/char/consoled.c line 70

Quoted 5 lines
xen/drivers/char/consoled.c new file +5 -0
+    if ( cons == prod )
+        goto out;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();

I think this need to move up ahead of the if(). In the comment perhaps s/Update/Latch/?

Quoted 3 lines

The read/write memory barriers here are between read/write accesses to ring->out_prod and ring->out array. So there is no need to move them. (the same goes for the input ring)

And there is no multiple-read issue here?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#167 lore raw

Re: [PATCH RFC v1 57/74] x86/pv-shim: shadow PV console's page for L2 DomU

Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 16:43, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/drivers/char/consoled.c new file +10 -0
+size_t consoled_guest_rx(void)
+{
+    size_t recv = 0, idx = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    spin_lock(&rx_lock);
+

xen/drivers/char/consoled.c line 70

Quoted 10 lines
xen/drivers/char/consoled.c new file +10 -0
+    cons = cons_ring->out_cons;
+    prod = ACCESS_ONCE(cons_ring->out_prod);
+    ASSERT((prod - cons) <= sizeof(cons_ring->out));
+
+    /* Is the ring empty? */
+    if ( cons == prod )
+        goto out;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();

I think this need to move up ahead of the if(). In the comment perhaps s/Update/Latch/?

Quoted 5 lines
Quoted 3 lines

The read/write memory barriers here are between read/write accesses to ring->out_prod and ring->out array. So there is no need to move them. (the same goes for the input ring)

And there is no multiple-read issue here?

As Andrew has kindly explained to me, there is an issue indeed. So I moved smp_rmb() to be right after cons and prod read, and updated the comment to say:

"Latch pointers before accessing the ring. Included compiler barrier also ensures that pointers are really read only once into local variables."

Signature · 6 lines
Thanks,
Sergey
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#168 lore raw

Re: [PATCH RFC v1 57/74] x86/pv-shim: shadow PV console's page for L2 DomU

Quoted 34 lines · Sergey Dyasli
Quoted 33 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 16:43, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/drivers/char/consoled.c new file +15 -0
+size_t consoled_guest_rx(void)
+{
+    size_t recv = 0, idx = 0;
+    XENCONS_RING_IDX cons, prod;
+
+    if ( !cons_ring )
+        return 0;
+
+    spin_lock(&rx_lock);
+
+    cons = cons_ring->out_cons;
+    prod = ACCESS_ONCE(cons_ring->out_prod);
+    ASSERT((prod - cons) <= sizeof(cons_ring->out));
+
+    /* Is the ring empty? */
Quoted 8 lines

xen/drivers/char/consoled.c line 70

Quoted 5 lines
xen/drivers/char/consoled.c new file +5 -0
+    if ( cons == prod )
+        goto out;
+
+    /* Update pointers before accessing the ring */
+    smp_rmb();

I think this need to move up ahead of the if(). In the comment perhaps s/Update/Latch/?

The read/write memory barriers here are between read/write accesses to ring->out_prod and ring->out array. So there is no need to move them. (the same goes for the input ring)

Quoted 9 lines
Quoted 1 line

And there is no multiple-read issue here?

As Andrew has kindly explained to me, there is an issue indeed. So I moved smp_rmb() to be right after cons and prod read, and updated the comment to say:

"Latch pointers before accessing the ring. Included compiler barrier also ensures that pointers are really read only once into local variables."

There is an incompatibility in this (also vixen's) serial output. The NetBSD installer includes NUL characters in its output and does not display properly due to the call to puts. putc demonstrably works, though it would be better to add a length oriented function to serial.c.

--Sarah

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#169 lore raw

RFC PATCH 58/74 xen/pvshim: add migration support

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

---
 xen/arch/x86/guest/xen.c          |  29 +++++++
 xen/arch/x86/mm.c                 |   3 +-
 xen/arch/x86/pv/shim.c            | 163 +++++++++++++++++++++++++++++++++++++-
 xen/common/domain.c               |  11 ++-
 xen/common/schedule.c             |   3 +-
 xen/drivers/char/xen_pv_console.c |   2 +-
 xen/include/asm-x86/guest/xen.h   |   5 ++
 xen/include/asm-x86/pv/shim.h     |   5 +-
 xen/include/xen/sched.h           |   2 +-
 9 files changed, 206 insertions(+), 17 deletions(-)
xen/arch/x86/guest/xen.c +29 -0
index 653a7366ab..54c997b9e0 100644
@@ -361,6 +361,35 @@ uint32_t hypervisor_cpuid_base(void)
     return xen_cpuid_base;
 }
 
+void ap_resume(void *unused)
+{
+    map_vcpuinfo();
+    init_evtchn();
+}
+
+void hypervisor_resume(void)
+{
+    /* Reset shared info page. */
+    map_shared_info();
+
+    /*
+     * Reset vcpu_info. Just clean the mapped bitmap and try to map the vcpu
+     * area again. On failure to map (when it was previously mapped) panic
+     * since it's impossible to safely shut down running guest vCPUs in order
+     * to meet the new XEN_LEGACY_MAX_VCPUS requirement.
+     */
+    memset(vcpu_info_mapped, 0, sizeof(vcpu_info_mapped));
+    if ( map_vcpuinfo() && nr_cpu_ids > XEN_LEGACY_MAX_VCPUS )
+        panic("unable to remap vCPU info and vCPUs > legacy limit");
+
+    /* Setup event channel upcall vector. */
+    init_evtchn();
+    smp_call_function(ap_resume, NULL, 1);
+
+    if ( pv_console )
+        pv_console_init();
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/mm.c +1 -2
index 4332d3bb39..e726c62064 100644
@@ -466,8 +466,7 @@ void share_xen_page_with_guest(
     spin_unlock(&d->page_alloc_lock);
 }
 
-int __init unshare_xen_page_with_guest(struct page_info *page,
-                                       struct domain *d)
+int unshare_xen_page_with_guest(struct page_info *page, struct domain *d)
 {
     if ( page_get_owner(page) != d || !is_xen_heap_page(page) )
         return -EINVAL;
xen/arch/x86/pv/shim.c +160 -3

170 lines not on this page · raw message

xen/common/domain.c +5 -6
index d653a0b0bb..bc2ceb2d36 100644
@@ -701,15 +701,12 @@ void __domain_crash_synchronous(void)
 }
 
 
-void domain_shutdown(struct domain *d, u8 reason)
+int domain_shutdown(struct domain *d, u8 reason)
 {
     struct vcpu *v;
 
     if ( pv_shim )
-    {
-        pv_shim_shutdown(reason);
-        return;
-    }
+        return pv_shim_shutdown(reason);
 
     spin_lock(&d->shutdown_lock);
 
@@ -723,7 +720,7 @@ void domain_shutdown(struct domain *d, u8 reason)
     if ( d->is_shutting_down )
     {
         spin_unlock(&d->shutdown_lock);
-        return;
+        return 0;
     }
 
     d->is_shutting_down = 1;
@@ -745,6 +742,8 @@ void domain_shutdown(struct domain *d, u8 reason)
     __domain_finalise_shutdown(d);
 
     spin_unlock(&d->shutdown_lock);
+
+    return 0;
 }
 
 void domain_resume(struct domain *d)
xen/common/schedule.c +1 -2
index 88279213e8..b7884263f2 100644
@@ -1149,11 +1149,10 @@ ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&sched_shutdown, arg, 1) )
             break;
 
-        ret = 0;
         TRACE_3D(TRC_SCHED_SHUTDOWN,
                  current->domain->domain_id, current->vcpu_id,
                  sched_shutdown.reason);
-        domain_shutdown(current->domain, (u8)sched_shutdown.reason);
+        ret = domain_shutdown(current->domain, (u8)sched_shutdown.reason);
 
         break;
     }
xen/drivers/char/xen_pv_console.c +1 -1
index fb5a7893be..d2a28478ac 100644
@@ -37,7 +37,7 @@ static DEFINE_SPINLOCK(tx_lock);
 
 bool pv_console;
 
-void __init pv_console_init(void)
+void pv_console_init(void)
 {
     struct evtchn_unmask unmask;
     long r;
xen/include/asm-x86/guest/xen.h +5 -0
index 94f781c30f..c244569e83 100644
@@ -40,6 +40,7 @@ void hypervisor_fixup_e820(struct e820map *e820);
 void hypervisor_init_memory(void);
 const unsigned long *hypervisor_reserved_pages(unsigned int *size);
 uint32_t hypervisor_cpuid_base(void);
+void hypervisor_resume(void);
 
 #else
 
@@ -91,6 +92,10 @@ static inline uint32_t hypervisor_cpuid_base(void)
     ASSERT_UNREACHABLE();
     return 0;
 };
+static inline void hypervisor_resume(void)
+{
+    ASSERT_UNREACHABLE();
+};
 
 #endif /* CONFIG_XEN_GUEST */
 #endif /* __X86_GUEST_XEN_H__ */
xen/include/asm-x86/pv/shim.h +3 -2
index 47bc4267af..0207348a85 100644
@@ -35,7 +35,7 @@ void pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
                        unsigned long va_start, unsigned long store_va,
                        unsigned long console_va, unsigned long vphysmap,
                        start_info_t *si);
-void pv_shim_shutdown(uint8_t reason);
+int pv_shim_shutdown(uint8_t reason);
 long pv_shim_event_channel_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg);
 void pv_shim_inject_evtchn(unsigned int port);
 long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
@@ -53,9 +53,10 @@ static inline void pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
 {
     ASSERT_UNREACHABLE();
 }
-static inline void pv_shim_shutdown(uint8_t reason)
+static inline int pv_shim_shutdown(uint8_t reason)
 {
     ASSERT_UNREACHABLE();
+    return 0;
 }
 static inline long pv_shim_event_channel_op(int cmd,
                                             XEN_GUEST_HANDLE_PARAM(void) arg)
xen/include/xen/sched.h +1 -1
index ac65d0c0df..70db377eae 100644
@@ -605,7 +605,7 @@ static inline struct domain *rcu_lock_current_domain(void)
 struct domain *get_domain_by_id(domid_t dom);
 void domain_destroy(struct domain *d);
 int domain_kill(struct domain *d);
-void domain_shutdown(struct domain *d, u8 reason);
+int domain_shutdown(struct domain *d, u8 reason);
 void domain_resume(struct domain *d);
 void domain_pause_for_debugger(void);
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#170 lore raw

Re: [PATCH RFC v1 58/74] xen/pvshim: add migration support

xen/arch/x86/guest/xen.c uint32_t hypervisor_cpuid_base(void) · line 381

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/guest/xen.c +12 -0
+void hypervisor_resume(void)
+{
+    /* Reset shared info page. */
+    map_shared_info();
+
+    /*
+     * Reset vcpu_info. Just clean the mapped bitmap and try to map the vcpu
+     * area again. On failure to map (when it was previously mapped) panic
+     * since it's impossible to safely shut down running guest vCPUs in order
+     * to meet the new XEN_LEGACY_MAX_VCPUS requirement.
+     */
+    memset(vcpu_info_mapped, 0, sizeof(vcpu_info_mapped));

bitmap_zero() would seem the more natural function to use here.

xen/arch/x86/mm.c void share_xen_page_with_guest( · line 470

Quoted 10 lines
xen/arch/x86/mm.c +1 -2
@@ -466,8 +466,7 @@ void share_xen_page_with_guest(
     spin_unlock(&d->page_alloc_lock);
 }
 
-int __init unshare_xen_page_with_guest(struct page_info *page,
-                                       struct domain *d)
+int unshare_xen_page_with_guest(struct page_info *page, struct domain *d)
 {

The function is - afaict - not generally safe to use in its current shape; I've recently made an attempt at making it generic, but iirc I wasn't able to make it fully race free. Therefore, to prevent use in the wrong context, please retain the __init here when not building a shim.

Quoted 7 lines
xen/arch/x86/pv/shim.c +0 -1
@@ -151,10 +151,167 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
     }
 }
 
-void pv_shim_shutdown(uint8_t reason)

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 161

Quoted 10 lines
xen/arch/x86/pv/shim.c +7 -2
+static void write_start_info(struct domain *d)
 {
-    /* XXX: handle suspend */
-    xen_hypercall_shutdown(reason);
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    start_info_t *si = map_domain_page(_mfn(is_pv_32bit_domain(d) ? regs->edx
+                                                                  : regs->rdx));
+    uint64_t param;
+
+    BUG_ON(!si);

map_domain_page() can't fail, so this is pointless.

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 167

Quoted 5 lines
xen/arch/x86/pv/shim.c +5 -0
+    snprintf(si->magic, sizeof(si->magic), "xen-3.0-x86_%s",
+             is_pv_32bit_domain(d) ? "32p" : "64");
+    si->nr_pages = d->tot_pages;
+    si->shared_info = virt_to_maddr(d->shared_info);
+    si->flags = (xen_processor_pmbits << 8) & SIF_PM_MASK;

This appears to be pointless (and irritating) in the context of the shim.

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 177

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+    BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_STORE_PFN, &si->store_mfn));
+    BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_STORE_EVTCHN, &param));
+    si->store_evtchn = param;
+    BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_EVTCHN, &param));
+    si->console.domU.evtchn = param;
+    if ( !pv_console )
+        BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN,
+                                           &si->console.domU.mfn));
+    else
+        si->console.domU.mfn = virt_to_mfn(consoled_get_ring_addr());

Generally we prefer to avoid side effects in BUG_ON()s, so perhaps better

    if ( pv_console )
        si->console.domU.mfn = virt_to_mfn(consoled_get_ring_addr());
    else if ( xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN,
                                           &si->console.domU.mfn) )
        BUG();

Considering it, I think the same (mostly cosmetic) issues exists earlier in the series.

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 190

Quoted 6 lines
xen/arch/x86/pv/shim.c +6 -0
+int pv_shim_shutdown(uint8_t reason)
+{
+    long rc;
+
+    if ( reason == SHUTDOWN_suspend )
+    {

Reduce indentation of almost the entire function body by one level by doing

    if ( reason != SHUTDOWN_suspend )
        /* Forward to L0. */
        return xen_hypercall_shutdown(reason);

?

Quoted 41 lines
xen/arch/x86/pv/shim.c +41 -0
+        struct domain *d = current->domain;
+        struct vcpu *v;
+        unsigned int i;
+        uint64_t old_store_pfn, old_console_pfn = 0, store_pfn, console_pfn;
+        uint64_t store_evtchn, console_evtchn;
+
+        BUG_ON(current->vcpu_id != 0);
+
+        BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_STORE_PFN,
+                                           &old_store_pfn));
+        if ( !pv_console )
+            BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN,
+                                               &old_console_pfn));
+
+        /* Pause the other vcpus before starting the migration. */
+        for_each_vcpu(d, v)
+            if ( v != current )
+                vcpu_pause_by_systemcontroller(v);
+
+        rc = xen_hypercall_shutdown(SHUTDOWN_suspend);
+        if ( rc )
+        {
+            for_each_vcpu(d, v)
+                if ( v != current )
+                    vcpu_unpause_by_systemcontroller(v);
+
+            return rc;
+        }
+
+        /* Resume the shim itself first. */
+        hypervisor_resume();
+
+        /*
+         * ATM there's nothing Xen can do if the console/store pfn changes,
+         * because Xen won't have a page_info struct for it.
+         */
+        BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_STORE_PFN,
+                                           &store_pfn));
+        BUG_ON(old_store_pfn != store_pfn);
+        if ( !pv_console )
+        {

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 241

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+            BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN,
+                                               &console_pfn));
+            BUG_ON(old_console_pfn != console_pfn);
+        }
+
+        /* Update domain id. */
+        d->domain_id = get_dom0_domid();
+
+        /* Clean the iomem range. */
+        BUG_ON(iomem_deny_access(d, 0, ~0UL));

Does this rangeset change across migration?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#171 lore raw

Re: [PATCH RFC v1 58/74] xen/pvshim: add migration support

Quoted 46 lines · Jan Beulich
Quoted 46 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +45 -0
+        struct domain *d = current->domain;
+        struct vcpu *v;
+        unsigned int i;
+        uint64_t old_store_pfn, old_console_pfn = 0, store_pfn, console_pfn;
+        uint64_t store_evtchn, console_evtchn;
+
+        BUG_ON(current->vcpu_id != 0);
+
+        BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_STORE_PFN,
+                                           &old_store_pfn));
+        if ( !pv_console )
+            BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN,
+                                               &old_console_pfn));
+
+        /* Pause the other vcpus before starting the migration. */
+        for_each_vcpu(d, v)
+            if ( v != current )
+                vcpu_pause_by_systemcontroller(v);
+
+        rc = xen_hypercall_shutdown(SHUTDOWN_suspend);
+        if ( rc )
+        {
+            for_each_vcpu(d, v)
+                if ( v != current )
+                    vcpu_unpause_by_systemcontroller(v);
+
+            return rc;
+        }
+
+        /* Resume the shim itself first. */
+        hypervisor_resume();
+
+        /*
+         * ATM there's nothing Xen can do if the console/store pfn changes,
+         * because Xen won't have a page_info struct for it.
+         */
+        BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_STORE_PFN,
+                                           &store_pfn));
+        BUG_ON(old_store_pfn != store_pfn);
+        if ( !pv_console )
+        {
+            BUG_ON(xen_hypercall_hvm_get_param(HVM_PARAM_CONSOLE_PFN,
+                                               &console_pfn));
+            BUG_ON(old_console_pfn != console_pfn);
+        }
Quoted 8 lines

xen/arch/x86/pv/shim.c void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start, · line 241

Quoted 6 lines
xen/arch/x86/pv/shim.c +6 -0
+
+        /* Update domain id. */
+        d->domain_id = get_dom0_domid();
+
+        /* Clean the iomem range. */
+        BUG_ON(iomem_deny_access(d, 0, ~0UL));

Does this rangeset change across migration?

Likely, the allowed iomem ranges for the DomU change depending on what hypervisor_alloc_unused_page returns. Those are mainly used to map grant table frames.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#172 lore raw

RFC PATCH 59/74 xen/pvshim: add shim_mem cmdline parameter

In reply to Wei Liu

From: Sergey Dyasli <[email protected]>

---
 docs/misc/xen-command-line.markdown | 16 +++++++++++++
 xen/arch/x86/dom0_build.c           | 18 ++++++++++++++-
 xen/arch/x86/pv/shim.c              | 46 +++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/pv/shim.h       |  7 ++++++
 4 files changed, 86 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +16 -0
index 3a1a9c1fba..9f51710a46 100644
@@ -686,6 +686,8 @@ any dom0 autoballooning feature present in your toolstack. See the
 _xl.conf(5)_ man page or [Xen Best
 Practices](http://wiki.xen.org/wiki/Xen_Best_Practices#Xen_dom0_dedicated_memory_and_preventing_dom0_memory_ballooning).
 
+This option doesn't have effect if pv-shim mode is enabled.
+
 ### dom0\_nodes
 
 > `= List of [ <integer> | relaxed | strict ]`
@@ -1456,6 +1458,20 @@ guest compatibly inside an HVM container.
 In this mode, the kernel and initrd passed as modules to the hypervisor are
 constructed into a plain unprivileged PV domain.
 
+### shim\_mem (x86)
+> `= List of ( min:<size> | max:<size> | <size> )`
+
+Set the amount of memory that xen-shim reserves for itself. Only has effect
+if pv-shim mode is enabled.
+
+* `min:<size>` specifies the minimum amount of memory. Ignored if greater
+   than max. Default: 10M.
+* `max:<size>` specifies the maximum amount of memory. Default: 128M.
+* `<size>` specifies the exact amount of memory. Overrides both min and max.
+
+By default, 1/16th of total HVM container's memory is reserved for xen-shim
+with minimum amount being 10MB and maximum amount 128MB.
+
 ### rcu-idle-timer-period-ms
 > `= <integer>`
 
xen/arch/x86/dom0_build.c +17 -1
index 1c5853690a..1b0b89fdeb 100644
@@ -51,6 +51,13 @@ static long __init parse_amt(const char *s, const char **ps)
 
 static int __init parse_dom0_mem(const char *s)
 {
+    /* xen-shim uses shim_mem parameter instead of dom0_mem */
+    if ( pv_shim )
+    {
+        printk("Ignoring dom0_mem param in pv-shim mode\n");
+        return 0;
+    }
+
     do {
         if ( !strncmp(s, "min:", 4) )
             dom0_min_nrpages = parse_amt(s+4, &s);
@@ -284,7 +291,16 @@ unsigned long __init dom0_compute_nr_pages(
          * maximum of 128MB.
          */
         if ( nr_pages == 0 )
-            nr_pages = -min(avail / 16, 128UL << (20 - PAGE_SHIFT));
+        {
+            uint64_t rsvd = min(avail / 16, 128UL << (20 - PAGE_SHIFT));
+            if ( pv_shim )
+            {
+                rsvd = pv_shim_mem(avail);
+                printk("Reserved %lu pages for xen-shim\n", rsvd);
+
+            }
+            nr_pages = -rsvd;
+        }
 
         /* Negative specification means "all memory - specified amount". */
         if ( (long)nr_pages  < 0 ) nr_pages  += avail;
xen/arch/x86/pv/shim.c +46 -0

52 lines not on this page · raw message

xen/include/asm-x86/pv/shim.h +7 -0
index 0207348a85..00906f884b 100644
@@ -41,6 +41,7 @@ void pv_shim_inject_evtchn(unsigned int port);
 long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
                             unsigned int count, bool compat);
 domid_t get_dom0_domid(void);
+uint64_t pv_shim_mem(uint64_t avail);
 
 #else
 
@@ -80,6 +81,12 @@ static inline domid_t get_dom0_domid(void)
     return 0;
 }
 
+static inline uint64_t pv_shim_mem(uint64_t avail)
+{
+    ASSERT_UNREACHABLE();
+    return 0;
+}
+
 #endif
 
 #endif /* __X86_PV_SHIM_H__ */
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 59/74
#173 lore raw

Re: [PATCH RFC v1 59/74] xen/pvshim: add shim_mem cmdline parameter

xen/arch/x86/dom0_build.c unsigned long __init dom0_compute_nr_pages( · line 295

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/dom0_build.c +2 -1
@@ -284,7 +291,16 @@ unsigned long __init dom0_compute_nr_pages(
          * maximum of 128MB.
          */
         if ( nr_pages == 0 )
-            nr_pages = -min(avail / 16, 128UL << (20 - PAGE_SHIFT));
+        {
+            uint64_t rsvd = min(avail / 16, 128UL << (20 - PAGE_SHIFT));

"unsigned long" and blank line following it.

Quoted 13 lines
xen/arch/x86/pv/shim.c +7 -0
@@ -40,6 +40,52 @@ bool pv_shim;
 boolean_param("pv-shim", pv_shim);
 #endif
 
+/*
+ * By default, 1/16th of total HVM container's memory is reserved for xen-shim
+ * with minimum amount being 10MB and maximum amount 128MB. Some users may wish
+ * to tune this constants for better memory utilization. This can be achieved
+ * using the following xen-shim's command line option:
+ *
+ * shim_mem=[min:<min_amt>,][max:<max_amt>,][<amt>]

xen/arch/x86/pv/shim.c bool pv_shim; · line 59

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+ *
+ * <min_amt>: The minimum amount of memory that should be allocated for xen-shim
+ *            (ignored if greater than max)
+ * <max_amt>: The maximum amount of memory that should be allocated for xen-shim
+ * <amt>:     The precise amount of memory to allocate for xen-shim
+ *            (overrides both min and max)
+ */
+static uint64_t __initdata shim_nrpages;
+static uint64_t __initdata shim_min_nrpages = 10UL << (20 - PAGE_SHIFT);
+static uint64_t __initdata shim_max_nrpages = 128UL << (20 - PAGE_SHIFT);

"unsigned long" at the very least. The Dom0 variant of the code also allows for negative values (and hence uses "long"), but it looks like you don't mean to support such here.

xen/arch/x86/pv/shim.c bool pv_shim; · line 67

Quoted 7 lines
xen/arch/x86/pv/shim.c +7 -0
+static int __init parse_shim_mem(const char *s)
+{
+    do {
+        if ( !strncmp(s, "min:", 4) )
+            shim_min_nrpages = parse_size_and_unit(s+4, &s) >> PAGE_SHIFT;
+        else if ( !strncmp(s, "max:", 4) )
+            shim_max_nrpages = parse_size_and_unit(s+4, &s) >> PAGE_SHIFT;

Blanks around + (twice) please.

xen/arch/x86/pv/shim.c bool pv_shim; · line 76

Quoted 9 lines
xen/arch/x86/pv/shim.c +9 -0
+        else
+            shim_nrpages = parse_size_and_unit(s, &s) >> PAGE_SHIFT;
+    } while ( *s++ == ',' );
+
+    return s[-1] ? -EINVAL : 0;
+}
+custom_param("shim_mem", parse_shim_mem);
+
+uint64_t pv_shim_mem(uint64_t avail)

unsigned long (twice) and __init.

With that (or the promise to clean it up after initial commit)

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#174 lore raw

RFC PATCH 60/74 xen/pvshim: set max_pages to the value of tot_pages

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

So that the guest is not able to deplete the memory pool of the shim itself by trying to balloon up.

---
 xen/arch/x86/pv/shim.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/x86/pv/shim.c +6 -0
index c24adacbc7..46f77362a7 100644
@@ -195,6 +195,12 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
                             console_mfn, vphysmap);
         consoled_set_ring_addr(page);
     }
+
+    /*
+     * Set the max pages to the current number of pages to prevent the
+     * guest from depleting the shim memory pool.
+     */
+    d->max_pages = d->tot_pages;
 }
 
 static void write_start_info(struct domain *d)
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 60/74
#175 lore raw

Re: [PATCH RFC v1 60/74] xen/pvshim: set max_pages to the value of tot_pages

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:

From: Roger Pau Monne <[email protected]>

So that the guest is not able to deplete the memory pool of the shim itself by trying to balloon up.

Could perhaps be consider folding into the previous patch.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#176 lore raw

RFC PATCH 61/74 xen/pvshim: support vCPU hotplug

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

---
 xen/arch/x86/pv/shim.c        | 48 +++++++++++++++++++++++++++++++++++++++++++
 xen/common/domain.c           | 37 +++++++++++++++++++++++----------
 xen/include/asm-x86/pv/shim.h | 12 +++++++++++
 xen/include/xen/domain.h      |  1 +
 4 files changed, 87 insertions(+), 11 deletions(-)
xen/arch/x86/pv/shim.c +48 -0
index 46f77362a7..29f343b871 100644
@@ -766,6 +766,54 @@ long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
     return rc;
 }
 
+long pv_shim_cpu_up(void *data)
+{
+    struct vcpu *v = data;
+    long rc;
+
+    BUG_ON(smp_processor_id() != 0);
+
+    if ( !cpu_online(v->vcpu_id) )
+    {
+        rc = cpu_up_helper((void *)(unsigned long)v->vcpu_id);
+        if ( rc )
+        {
+            gprintk(XENLOG_ERR, "Failed to bring up CPU#%u: %ld\n",
+                    v->vcpu_id, rc);
+            return rc;
+        }
+    }
+
+    return vcpu_up(v);
+}
+
+long pv_shim_cpu_down(void *data)
+{
+    struct vcpu *v = data;
+    long rc;
+
+    BUG_ON(smp_processor_id() != 0);
+
+    if ( !test_and_set_bit(_VPF_down, &v->pause_flags) )
+        vcpu_sleep_sync(v);
+
+    if ( cpu_online(v->vcpu_id) )
+    {
+        rc = cpu_down_helper((void *)(unsigned long)v->vcpu_id);
+        if ( rc )
+            gprintk(XENLOG_ERR, "Failed to bring down CPU#%u: %ld\n",
+                    v->vcpu_id, rc);
+        /*
+         * NB: do not propagate errors from cpu_down_helper failing. The shim
+         * is going to run with extra CPUs, but that's not going to prevent
+         * normal operation. OTOH most guests are not prepared to handle an
+         * error on VCPUOP_down failing, and will likely panic.
+         */
+    }
+
+    return 0;
+}
+
 domid_t get_dom0_domid(void)
 {
     uint32_t eax, ebx, ecx, edx;
xen/common/domain.c +26 -11
index bc2ceb2d36..c30e98b24e 100644
@@ -1281,6 +1281,23 @@ int default_initialise_vcpu(struct vcpu *v, XEN_GUEST_HANDLE_PARAM(void) arg)
     return rc;
 }
 
+int vcpu_up(struct vcpu *v)
+{
+    bool wake = false;
+    int rc = 0;
+
+    domain_lock(v->domain);
+    if ( !v->is_initialised )
+        rc = -EINVAL;
+    else
+        wake = test_and_clear_bit(_VPF_down, &v->pause_flags);
+    domain_unlock(v->domain);
+    if ( wake )
+        vcpu_wake(v);
+
+    return rc;
+}
+
 long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     struct domain *d = current->domain;
@@ -1303,22 +1320,20 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         break;
 
-    case VCPUOP_up: {
-        bool_t wake = 0;
-        domain_lock(d);
-        if ( !v->is_initialised )
-            rc = -EINVAL;
+    case VCPUOP_up:
+        if ( pv_shim )
+            rc = continue_hypercall_on_cpu(0, pv_shim_cpu_up, v);
         else
-            wake = test_and_clear_bit(_VPF_down, &v->pause_flags);
-        domain_unlock(d);
-        if ( wake )
-            vcpu_wake(v);
+            rc = vcpu_up(v);
+
         break;
-    }
 
     case VCPUOP_down:
-        if ( !test_and_set_bit(_VPF_down, &v->pause_flags) )
+        if ( pv_shim )
+            rc = continue_hypercall_on_cpu(0, pv_shim_cpu_down, v);
+        else if ( !test_and_set_bit(_VPF_down, &v->pause_flags) )
             vcpu_sleep_nosync(v);
+
         break;
 
     case VCPUOP_is_up:
xen/include/asm-x86/pv/shim.h +12 -0
index 00906f884b..d107a617a7 100644
@@ -40,6 +40,8 @@ long pv_shim_event_channel_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg);
 void pv_shim_inject_evtchn(unsigned int port);
 long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
                             unsigned int count, bool compat);
+long pv_shim_cpu_up(void *data);
+long pv_shim_cpu_down(void *data);
 domid_t get_dom0_domid(void);
 uint64_t pv_shim_mem(uint64_t avail);
 
@@ -76,6 +78,16 @@ static inline long pv_shim_grant_table_op(unsigned int cmd,
     ASSERT_UNREACHABLE();
     return 0;
 }
+static inline long pv_shim_cpu_up(void *data)
+{
+    ASSERT_UNREACHABLE();
+    return 0;
+}
+static inline long pv_shim_cpu_down(void *data)
+{
+    ASSERT_UNREACHABLE();
+    return 0;
+}
 static inline domid_t get_dom0_domid(void)
 {
     return 0;
xen/include/xen/domain.h +1 -0
index 347f264047..eb62f1dab1 100644
@@ -17,6 +17,7 @@ struct vcpu *alloc_vcpu(
     struct domain *d, unsigned int vcpu_id, unsigned int cpu_id);
 struct vcpu *alloc_dom0_vcpu0(struct domain *dom0);
 int vcpu_reset(struct vcpu *);
+int vcpu_up(struct vcpu *v);
 
 struct xen_domctl_getdomaininfo;
 void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info);
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#177 lore raw

Re: [PATCH RFC v1 61/74] xen/pvshim: support vCPU hotplug

xen/common/domain.c long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1310

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/common/domain.c +0 -5
@@ -1303,22 +1320,20 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         break;
 
-    case VCPUOP_up: {
-        bool_t wake = 0;
-        domain_lock(d);
-        if ( !v->is_initialised )
-            rc = -EINVAL;

Shouldn't this check remain here? I realize this will complicate locking (luckily the domain lock is a recursive one, so it shouldn't be too bad), but I don't think pv_shim_cpu_up() can tolerate failing because of vcpu_up() failing.

I also think that the use of "long" for return types and values isn't really warranted here, and there's also no visible to me reason to special case CPU0 here. But for simplicity reasons I can see why you've chosen that option; otoh the locking issue above that you'll need to solve might be easier to deal with if you didn't switch CPUs for hypercall processing (without dropping the use of continue_hypercall_on_cpu()).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#178 lore raw

Re: [PATCH RFC v1 61/74] xen/pvshim: support vCPU hotplug

Quoted 16 lines · Jan Beulich

xen/common/domain.c long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1310

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/common/domain.c +0 -5
@@ -1303,22 +1320,20 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         break;
 
-    case VCPUOP_up: {
-        bool_t wake = 0;
-        domain_lock(d);
-        if ( !v->is_initialised )
-            rc = -EINVAL;

Shouldn't this check remain here? I realize this will complicate locking (luckily the domain lock is a recursive one, so it shouldn't be too bad), but I don't think pv_shim_cpu_up() can tolerate failing because of vcpu_up() failing.

Quoted 7 lines

I also think that the use of "long" for return types and values isn't really warranted here, and there's also no visible to me reason to special case CPU0 here. But for simplicity reasons I can see why you've chosen that option; otoh the locking issue above that you'll need to solve might be easier to deal with if you didn't switch CPUs for hypercall processing (without dropping the use of continue_hypercall_on_cpu()).

Right, I'm not sure why bringing a CPU up is required to happen on CPU0, but that's what the current code in arch_do_sysctl does.

I'm not sure I'm following the last part of your reply, if for CPU bringup there's no need to switch to CPU0, why would I want to keep the continue_hypercall_on_cpu for in that case?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#179 lore raw

Re: [PATCH RFC v1 61/74] xen/pvshim: support vCPU hotplug

Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 10.01.18 at 14:07, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich

Review long do_vcpu_op(int cmd, unsigned int vcpuid,

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -1303,22 +1320,20 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, 

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 14 lines

xen/common/domain.c long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1310

Quoted 8 lines
xen/common/domain.c +0 -5
         break;
 
-    case VCPUOP_up: {
-        bool_t wake = 0;
-        domain_lock(d);
-        if ( !v->is_initialised )
-            rc = -EINVAL;

Shouldn't this check remain here? I realize this will complicate locking (luckily the domain lock is a recursive one, so it shouldn't be too bad), but I don't think pv_shim_cpu_up() can tolerate failing because of vcpu_up() failing.

Quoted 7 lines

I also think that the use of "long" for return types and values isn't really warranted here, and there's also no visible to me reason to special case CPU0 here. But for simplicity reasons I can see why you've chosen that option; otoh the locking issue above that you'll need to solve might be easier to deal with if you didn't switch CPUs for hypercall processing (without dropping the use of continue_hypercall_on_cpu()).

Quoted 2 lines

Right, I'm not sure why bringing a CPU up is required to happen on CPU0, but that's what the current code in arch_do_sysctl does.

In the bare metal hypervisor we likely will need to do some work to remove this CPU0 restriction.

Quoted 3 lines

I'm not sure I'm following the last part of your reply, if for CPU bringup there's no need to switch to CPU0, why would I want to keep the continue_hypercall_on_cpu for in that case?

For onlining you may indeed get away without. But in the offlining case you don't want to offline the CPU you're running on.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#180 lore raw

Re: [PATCH RFC v1 61/74] xen/pvshim: support vCPU hotplug

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -1303,22 +1320,20 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         break;
 
Quoted 10 lines

xen/common/domain.c long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1310

Quoted 5 lines
xen/common/domain.c +0 -5
-    case VCPUOP_up: {
-        bool_t wake = 0;
-        domain_lock(d);
-        if ( !v->is_initialised )
-            rc = -EINVAL;

Shouldn't this check remain here? I realize this will complicate locking (luckily the domain lock is a recursive one, so it shouldn't be too bad), but I don't think pv_shim_cpu_up() can tolerate failing because of vcpu_up() failing.

I guess you mean that it's unfortunate to fail in pv_shim_cpu_up after having brought up the physical CPU if it turns out that !v->is_initialised. I can add a check at the top of pv_shim_cpu_up for !v->is_initialised and change vcpu_up slightly.

Regarding the usage of long, continue_hypercall_on_cpu requires a function that returns long, so I would rather keep things as they are now for simplicity.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#181 lore raw

RFC PATCH 62/74 xen/pvshim: memory hotplug

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

---
 xen/arch/x86/pv/shim.c        | 110 ++++++++++++++++++++++++++++++++++++++++++
 xen/common/memory.c           |  14 ++++++
 xen/include/asm-x86/pv/shim.h |  10 ++++
 3 files changed, 134 insertions(+)
xen/arch/x86/pv/shim.c +110 -0
index 29f343b871..eb34467833 100644
@@ -90,6 +90,9 @@ static unsigned int nr_grant_list;
 static unsigned long *grant_frames;
 static DEFINE_SPINLOCK(grant_lock);
 
+static PAGE_LIST_HEAD(balloon);
+static DEFINE_SPINLOCK(balloon_lock);
+
 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER| \
                  _PAGE_GUEST_KERNEL)
 #define COMPAT_L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
@@ -814,6 +817,113 @@ long pv_shim_cpu_down(void *data)
     return 0;
 }
 
+static unsigned long batch_memory_op(int cmd, struct page_list_head *list)
+{
+    struct xen_memory_reservation xmr = {
+        .domid = DOMID_SELF,
+    };
+    unsigned long pfns[64];
+    struct page_info *pg;
+    unsigned long done = 0;
+
+    set_xen_guest_handle(xmr.extent_start, pfns);
+    page_list_for_each ( pg, list )
+    {
+        pfns[xmr.nr_extents++] = page_to_mfn(pg);
+        if ( xmr.nr_extents == ARRAY_SIZE(pfns) || !page_list_next(pg, list) )
+        {
+            long nr = xen_hypercall_memory_op(cmd, &xmr);
+
+            done += nr > 0 ? nr : 0;
+            if ( nr != xmr.nr_extents )
+                break;
+            xmr.nr_extents = 0;
+        }
+    }
+
+    return done;
+}
+
+void pv_shim_online_memory(unsigned int nr, unsigned int order)
+{
+    struct page_info *page, *tmp;
+    PAGE_LIST_HEAD(list);
+
+    spin_lock(&balloon_lock);
+    page_list_for_each_safe ( page, tmp, &balloon )
+    {
+            if ( page->v.free.order != order )
+                continue;
+
+            page_list_del(page, &balloon);
+            page_list_add_tail(page, &list);
+            if ( !--nr )
+                break;
+    }
+    spin_unlock(&balloon_lock);
+
+    if ( nr )
+        gprintk(XENLOG_WARNING,
+                "failed to allocate %u extents of order %u for onlining\n",
+                nr, order);
+
+    nr = batch_memory_op(XENMEM_populate_physmap, &list);
+    while ( nr-- )
+    {
+        BUG_ON((page = page_list_remove_head(&list)) == NULL);
+        free_domheap_pages(page, order);
+    }
+
+    if ( !page_list_empty(&list) )
+    {
+        gprintk(XENLOG_WARNING,
+                "failed to online some of the memory regions\n");
+        spin_lock(&balloon_lock);
+        while ( (page = page_list_remove_head(&list)) != NULL )
+            page_list_add_tail(page, &balloon);
+        spin_unlock(&balloon_lock);
+    }
+}
+
+void pv_shim_offline_memory(unsigned int nr, unsigned int order)
+{
+    struct page_info *page;
+    PAGE_LIST_HEAD(list);
+
+    while ( nr-- )
+    {
+        page = alloc_domheap_pages(NULL, order, 0);
+        if ( !page )
+            break;
+
+        page_list_add_tail(page, &list);
+        page->v.free.order = order;
+    }
+
+    if ( nr + 1 )
+        gprintk(XENLOG_WARNING,
+                "failed to reserve %u extents of order %u for offlining\n",
+                nr + 1, order);
+
+
+    nr = batch_memory_op(XENMEM_decrease_reservation, &list);
+    spin_lock(&balloon_lock);
+    while ( nr-- )
+    {
+        BUG_ON((page = page_list_remove_head(&list)) == NULL);
+        page_list_add_tail(page, &balloon);
+    }
+    spin_unlock(&balloon_lock);
+
+    if ( !page_list_empty(&list) )
+    {
+        gprintk(XENLOG_WARNING,
+                "failed to offline some of the memory regions\n");
+        while ( (page = page_list_remove_head(&list)) != NULL )
+            free_domheap_pages(page, order);
+    }
+}
+
 domid_t get_dom0_domid(void)
 {
     uint32_t eax, ebx, ecx, edx;
xen/common/memory.c +14 -0
index 5a1508a292..f06df8c8cf 100644
@@ -29,6 +29,10 @@
 #include <public/memory.h>
 #include <xsm/xsm.h>
 
+#ifdef CONFIG_X86
+#include <asm/pv/shim.h>
+#endif
+
 struct memop_args {
     /* INPUT */
     struct domain *domain;     /* Domain to be affected. */
@@ -993,6 +997,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             return start_extent;
         }
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op != XENMEM_decrease_reservation && !args.nr_done )
+            pv_shim_online_memory(args.nr_extents, args.extent_order);
+#endif
+
         switch ( op )
         {
         case XENMEM_increase_reservation:
@@ -1015,6 +1024,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                 __HYPERVISOR_memory_op, "lh",
                 op | (rc << MEMOP_EXTENT_SHIFT), arg);
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op == XENMEM_decrease_reservation )
+            pv_shim_offline_memory(args.nr_extents, args.extent_order);
+#endif
+
         break;
 
     case XENMEM_exchange:
xen/include/asm-x86/pv/shim.h +10 -0
index d107a617a7..7174f6fc07 100644
@@ -42,6 +42,8 @@ long pv_shim_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop,
                             unsigned int count, bool compat);
 long pv_shim_cpu_up(void *data);
 long pv_shim_cpu_down(void *data);
+void pv_shim_online_memory(unsigned int nr, unsigned int order);
+void pv_shim_offline_memory(unsigned int nr, unsigned int order);
 domid_t get_dom0_domid(void);
 uint64_t pv_shim_mem(uint64_t avail);
 
@@ -88,6 +90,14 @@ static inline long pv_shim_cpu_down(void *data)
     ASSERT_UNREACHABLE();
     return 0;
 }
+static inline void pv_shim_online_memory(unsigned int nr, unsigned int order)
+{
+    ASSERT_UNREACHABLE();
+}
+static inline void pv_shim_offline_memory(unsigned int nr, unsigned int order)
+{
+    ASSERT_UNREACHABLE();
+}
 static inline domid_t get_dom0_domid(void)
 {
     return 0;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#182 lore raw

Re: [PATCH RFC v1 62/74] xen/pvshim: memory hotplug

xen/arch/x86/pv/shim.c long pv_shim_cpu_down(void *data) · line 820

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +1 -0
@@ -814,6 +817,113 @@ long pv_shim_cpu_down(void *data)
     return 0;
 }
 
+static unsigned long batch_memory_op(int cmd, struct page_list_head *list)

unsigned int cmd, const struct ...

xen/arch/x86/pv/shim.c long pv_shim_cpu_down(void *data) · line 826

Quoted 6 lines
xen/arch/x86/pv/shim.c +6 -0
+{
+    struct xen_memory_reservation xmr = {
+        .domid = DOMID_SELF,
+    };
+    unsigned long pfns[64];
+    struct page_info *pg;

As long as you don't modify the list, this too can be const.

xen/arch/x86/pv/shim.c long pv_shim_cpu_down(void *data) · line 856

Quoted 10 lines
xen/arch/x86/pv/shim.c +10 -0
+void pv_shim_online_memory(unsigned int nr, unsigned int order)
+{
+    struct page_info *page, *tmp;
+    PAGE_LIST_HEAD(list);
+
+    spin_lock(&balloon_lock);
+    page_list_for_each_safe ( page, tmp, &balloon )
+    {
+            if ( page->v.free.order != order )
+                continue;

Since guests (afaik) only ever balloon order-0 pages, this is fine for now. But it's insufficient in general - there's no point failing a request when there's no exact match available, but a higher order one is (which could be split).

xen/arch/x86/pv/shim.c long pv_shim_cpu_down(void *data) · line 870

Quoted 13 lines
xen/arch/x86/pv/shim.c +13 -0
+            page_list_del(page, &balloon);
+            page_list_add_tail(page, &list);
+            if ( !--nr )
+                break;
+    }
+    spin_unlock(&balloon_lock);
+
+    if ( nr )
+        gprintk(XENLOG_WARNING,
+                "failed to allocate %u extents of order %u for onlining\n",
+                nr, order);
+
+    nr = batch_memory_op(XENMEM_populate_physmap, &list);

You need to pass order into the function (and use it there).

xen/arch/x86/pv/shim.c long pv_shim_cpu_down(void *data) · line 883

Quoted 13 lines
xen/arch/x86/pv/shim.c +13 -0
+    while ( nr-- )
+    {
+        BUG_ON((page = page_list_remove_head(&list)) == NULL);
+        free_domheap_pages(page, order);
+    }
+
+    if ( !page_list_empty(&list) )
+    {
+        gprintk(XENLOG_WARNING,
+                "failed to online some of the memory regions\n");
+        spin_lock(&balloon_lock);
+        while ( (page = page_list_remove_head(&list)) != NULL )
+            page_list_add_tail(page, &balloon);

page_list_splice()?

Quoted 13 lines
xen/common/memory.c +5 -0
@@ -993,6 +997,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             return start_extent;
         }
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op != XENMEM_decrease_reservation && !args.nr_done )
+            pv_shim_online_memory(args.nr_extents, args.extent_order);
+#endif
+
         switch ( op )
         {
         case XENMEM_increase_reservation:

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1030

Quoted 7 lines
xen/common/memory.c +4 -0
@@ -1015,6 +1024,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                 __HYPERVISOR_memory_op, "lh",
                 op | (rc << MEMOP_EXTENT_SHIFT), arg);
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op == XENMEM_decrease_reservation )
+            pv_shim_offline_memory(args.nr_extents, args.extent_order);
+#endif

Looking at both of these changes - is it somewhere being made sure that shim containers won't boot in PoD mode?

For the latter change - is this correct when the operation has been preempted? I think you want to offline only the delta between start and args.nr_done.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#183 lore raw

Re: [PATCH RFC v1 62/74] xen/pvshim: memory hotplug

Quoted 16 lines · Jan Beulich

xen/arch/x86/pv/shim.c long pv_shim_cpu_down(void *data) · line 856

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +10 -0
+void pv_shim_online_memory(unsigned int nr, unsigned int order)
+{
+    struct page_info *page, *tmp;
+    PAGE_LIST_HEAD(list);
+
+    spin_lock(&balloon_lock);
+    page_list_for_each_safe ( page, tmp, &balloon )
+    {
+            if ( page->v.free.order != order )
+                continue;

Since guests (afaik) only ever balloon order-0 pages, this is fine for now. But it's insufficient in general - there's no point failing a request when there's no exact match available, but a higher order one is (which could be split).

Yes, that's right. Using order != 0 is likely to not work properly given the lack of support for splitting higher order chunks.

I haven't implemented this because as you say there's no guest making use of ti anyway. Let me add a TODO here.

Quoted 24 lines
Quoted 13 lines
xen/common/memory.c +5 -0
@@ -993,6 +997,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             return start_extent;
         }
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op != XENMEM_decrease_reservation && !args.nr_done )
+            pv_shim_online_memory(args.nr_extents, args.extent_order);
+#endif
+
         switch ( op )
         {
         case XENMEM_increase_reservation:

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1030

Quoted 7 lines
xen/common/memory.c +4 -0
@@ -1015,6 +1024,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                 __HYPERVISOR_memory_op, "lh",
                 op | (rc << MEMOP_EXTENT_SHIFT), arg);
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op == XENMEM_decrease_reservation )
+            pv_shim_offline_memory(args.nr_extents, args.extent_order);
+#endif

Looking at both of these changes - is it somewhere being made sure that shim containers won't boot in PoD mode?

Quoted 3 lines

For the latter change - is this correct when the operation has been preempted? I think you want to offline only the delta between start and args.nr_done.

AFAICT this function will only be called once, even when preempted. On the online case it's only called when args.nr_done == 0, and in the offline case it's only called after the work has been completely done.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#184 lore raw

Re: [PATCH RFC v1 62/74] xen/pvshim: memory hotplug

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 10.01.18 at 14:36, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1030

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:
xen/common/memory.c +4 -0
@@ -1015,6 +1024,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                 __HYPERVISOR_memory_op, "lh",
                 op | (rc << MEMOP_EXTENT_SHIFT), arg);
 
+#ifdef CONFIG_X86
+        if ( pv_shim && op == XENMEM_decrease_reservation )
+            pv_shim_offline_memory(args.nr_extents, args.extent_order);
+#endif

Looking at both of these changes - is it somewhere being made sure that shim containers won't boot in PoD mode?

Quoted 7 lines
Quoted 3 lines

For the latter change - is this correct when the operation has been preempted? I think you want to offline only the delta between start and args.nr_done.

AFAICT this function will only be called once, even when preempted. On the online case it's only called when args.nr_done == 0, and in the offline case it's only called after the work has been completely done.

No, and that's the point of my earlier comment: You call the function solely based on the value of op, not considering at all whether you were preempted. Or am I overlooking anything?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#185 lore raw

RFC PATCH 63/74 xen/shim: modify shim_mem parameter behaviour

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

shim_mem will now account for both the memory used by the hypervisor loaded in memory and the free memory slack given to the shim for runtime usage.

From experimental testing it seems like the total amount of MiB used by the shim (giving it ~1MB of free memory for runtime) is:

memory/113 + 20

---
 docs/misc/xen-command-line.markdown | 13 +++++++------
 xen/arch/x86/dom0_build.c           | 14 +++-----------
 xen/arch/x86/pv/shim.c              | 30 +++++++++++++++++++-----------
 3 files changed, 29 insertions(+), 28 deletions(-)
docs/misc/xen-command-line.markdown +7 -6
index 9f51710a46..68ec52b5c2 100644
@@ -1461,16 +1461,17 @@ constructed into a plain unprivileged PV domain.
 ### shim\_mem (x86)
 > `= List of ( min:<size> | max:<size> | <size> )`
 
-Set the amount of memory that xen-shim reserves for itself. Only has effect
-if pv-shim mode is enabled.
+Set the amount of memory that xen-shim uses. Only has effect if pv-shim mode is
+enabled. Note that this value accounts for the memory used by the shim itself
+plus the free memory slack given to the shim for runtime allocations.
 
 * `min:<size>` specifies the minimum amount of memory. Ignored if greater
-   than max. Default: 10M.
-* `max:<size>` specifies the maximum amount of memory. Default: 128M.
+   than max.
+* `max:<size>` specifies the maximum amount of memory.
 * `<size>` specifies the exact amount of memory. Overrides both min and max.
 
-By default, 1/16th of total HVM container's memory is reserved for xen-shim
-with minimum amount being 10MB and maximum amount 128MB.
+By default, the amount of free memory slack given to the shim for runtime usage
+is 1MB.
 
 ### rcu-idle-timer-period-ms
 > `= <integer>`
xen/arch/x86/dom0_build.c +3 -11
index 1b0b89fdeb..30347bcc67 100644
@@ -290,17 +290,9 @@ unsigned long __init dom0_compute_nr_pages(
          * for things like DMA buffers. This reservation is clamped to a
          * maximum of 128MB.
          */
-        if ( nr_pages == 0 )
-        {
-            uint64_t rsvd = min(avail / 16, 128UL << (20 - PAGE_SHIFT));
-            if ( pv_shim )
-            {
-                rsvd = pv_shim_mem(avail);
-                printk("Reserved %lu pages for xen-shim\n", rsvd);
-
-            }
-            nr_pages = -rsvd;
-        }
+        if ( !nr_pages )
+            nr_pages = -(pv_shim ? pv_shim_mem(avail)
+                                 : min(avail / 16, 128UL << (20 - PAGE_SHIFT)));
 
         /* Negative specification means "all memory - specified amount". */
         if ( (long)nr_pages  < 0 ) nr_pages  += avail;
xen/arch/x86/pv/shim.c +19 -11
index eb34467833..120c3b55b0 100644
@@ -41,9 +41,8 @@ boolean_param("pv-shim", pv_shim);
 #endif
 
 /*
- * By default, 1/16th of total HVM container's memory is reserved for xen-shim
- * with minimum amount being 10MB and maximum amount 128MB. Some users may wish
- * to tune this constants for better memory utilization. This can be achieved
+ * By default give the shim 1MB of free memory slack. Some users may wish to
+ * tune this constants for better memory utilization. This can be achieved
  * using the following xen-shim's command line option:
  *
  * shim_mem=[min:<min_amt>,][max:<max_amt>,][<amt>]
@@ -55,8 +54,8 @@ boolean_param("pv-shim", pv_shim);
  *            (overrides both min and max)
  */
 static uint64_t __initdata shim_nrpages;
-static uint64_t __initdata shim_min_nrpages = 10UL << (20 - PAGE_SHIFT);
-static uint64_t __initdata shim_max_nrpages = 128UL << (20 - PAGE_SHIFT);
+static uint64_t __initdata shim_min_nrpages;
+static uint64_t __initdata shim_max_nrpages;
 
 static int __init parse_shim_mem(const char *s)
 {
@@ -75,15 +74,24 @@ custom_param("shim_mem", parse_shim_mem);
 
 uint64_t pv_shim_mem(uint64_t avail)
 {
-    uint64_t rsvd = min(avail / 16, shim_max_nrpages);
+    if ( !shim_nrpages )
+    {
+        shim_nrpages = max(shim_min_nrpages,
+                           total_pages - avail + (1UL << (20 - PAGE_SHIFT)));
+        if ( shim_max_nrpages )
+            shim_max_nrpages = min(shim_nrpages, shim_max_nrpages);
+    }
+
+    if ( total_pages - avail > shim_nrpages )
+        panic("pages used by shim > shim_nrpages (%#lx > %#lx)",
+              total_pages - avail, shim_nrpages);
 
-    if ( shim_nrpages )
-        return shim_nrpages;
+    shim_nrpages -= total_pages - avail;
 
-    if ( shim_min_nrpages <= shim_max_nrpages )
-        rsvd = max(rsvd, shim_min_nrpages);
+    printk("shim used pages %#lx reserving %#lx free pages\n",
+           total_pages - avail, shim_nrpages);
 
-    return rsvd;
+    return shim_nrpages;
 }
 
 static unsigned int nr_grant_list;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#186 lore raw

Re: [PATCH RFC v1 63/74] xen/shim: modify shim_mem parameter behaviour

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:

From: Roger Pau Monne <[email protected]>

shim_mem will now account for both the memory used by the hypervisor loaded in memory and the free memory slack given to the shim for runtime usage.

From experimental testing it seems like the total amount of MiB used by the shim (giving it ~1MB of free memory for runtime) is:

memory/113 + 20

How can this be independent of e.g. the client's vCPU count? For the moment we may be better off leaving out this change, with people caring much about memory utilization making use of the command line options.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#187 lore raw

RFC PATCH 64/74 xen/pvshim: use default position for the m2p mappings

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

When running a 32bit kernel as Dom0 on a 64bit hypervisor the hypervisor will try to shrink the hypervisor hole to the minimum needed, and thus requires the Dom0 to use XENMEM_machphys_mapping in order to fetch the position of the start of the hypervisor virtual mappings.

Disable this feature when running as a PV shim, since some DomU kernels don't implemented XENMEM_machphys_mapping and break if the m2p doesn't begin at the default address.

NB: support for the XENMEM_machphys_mapping was added in Linux by commit 7e7750.

---
 xen/arch/x86/pv/dom0_build.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/pv/dom0_build.c +2 -1
index e152fe3a9e..f5a793b1d2 100644
@@ -398,7 +398,8 @@ int __init dom0_construct_pv(struct domain *d,
     if ( parms.pae == XEN_PAE_EXTCR3 )
             set_bit(VMASST_TYPE_pae_extended_cr3, &d->vm_assist);
 
-    if ( (parms.virt_hv_start_low != UNSET_ADDR) && elf_32bit(&elf) )
+    if ( !pv_shim && (parms.virt_hv_start_low != UNSET_ADDR) &&
+         elf_32bit(&elf) )
     {
         unsigned long mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
         value = (parms.virt_hv_start_low + mask) & ~mask;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 64/74
#188 lore raw

Re: [PATCH RFC v1 64/74] xen/pvshim: use default position for the m2p mappings

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:

From: Roger Pau Monne <[email protected]>

When running a 32bit kernel as Dom0 on a 64bit hypervisor the hypervisor will try to shrink the hypervisor hole to the minimum needed, and thus requires the Dom0 to use XENMEM_machphys_mapping in order to fetch the position of the start of the hypervisor virtual mappings.

Quoted 8 lines

Disable this feature when running as a PV shim, since some DomU kernels don't implemented XENMEM_machphys_mapping and break if the m2p doesn't begin at the default address.

NB: support for the XENMEM_machphys_mapping was added in Linux by commit 7e7750.

If we really mean to support 32-bit clients

Otherwise I'd prefer to leave this out.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#189 lore raw

RFC PATCH 65/74 xen/shim: crash instead of reboot in shim mode

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

All guest shutdown operations are forwarded to L0, so the only native calls to machine_restart happen from crash related paths inside the hypervisor, hence switch the reboot code to instead issue a crash shutdown.

---
 xen/arch/x86/shutdown.c    | 7 +++++++
 xen/drivers/char/console.c | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/x86/shutdown.c +7 -0
index 689f6f137d..d2d14ae5f8 100644
@@ -642,6 +642,13 @@ void machine_restart(unsigned int delay_millisecs)
             break;
 
         case BOOT_XEN:
+            if ( pv_shim )
+                /*
+                 * When running in PV shim mode guest shutdown calls are
+                 * forwarded to L0, hence the only way to get here is if a
+                 * shim crash happens.
+                 */
+                xen_hypercall_shutdown(SHUTDOWN_crash);
             xen_hypercall_shutdown(SHUTDOWN_reboot);
             break;
         }
xen/drivers/char/console.c +1 -1
index 3c615a255c..5c37dfc3f6 100644
@@ -1230,7 +1230,7 @@ void panic(const char *fmt, ...)
     if ( opt_noreboot )
         printk("Manual reset required ('noreboot' specified)\n");
     else
-        printk("Reboot in five seconds...\n");
+        printk("%s in five seconds...\n", pv_shim ? "Crash" : "Reboot");
 
     spin_unlock_irqrestore(&lock, flags);
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 65/74
#190 lore raw

Re: [PATCH RFC v1 65/74] xen/shim: crash instead of reboot in shim mode

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:

From: Roger Pau Monne <[email protected]>

All guest shutdown operations are forwarded to L0, so the only native calls to machine_restart happen from crash related paths inside the hypervisor, hence switch the reboot code to instead issue a crash shutdown.

preferably with ...

Quoted 4 lines
xen/arch/x86/shutdown.c +0 -0
@@ -642,6 +642,13 @@ void machine_restart(unsigned int delay_millisecs)
             break;

xen/arch/x86/shutdown.c void machine_restart(unsigned int delay_millisecs) · line 652

Quoted 10 lines
xen/arch/x86/shutdown.c +7 -0
 
         case BOOT_XEN:
+            if ( pv_shim )
+                /*
+                 * When running in PV shim mode guest shutdown calls are
+                 * forwarded to L0, hence the only way to get here is if a
+                 * shim crash happens.
+                 */
+                xen_hypercall_shutdown(SHUTDOWN_crash);
             xen_hypercall_shutdown(SHUTDOWN_reboot);

... this made

             xen_hypercall_shutdown(pv_shim ? SHUTDOWN_crash : SHUTDOWN_reboot);

(suitably line wrapped).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#191 lore raw

RFC PATCH 66/74 xen/shim: allow DomU to have as many vcpus as available

In reply to Wei Liu

From: Roger Pau Monne <[email protected]>

Since the shim VCPUOP_{up/down} hypercall is wired to the plug/unplug of CPUs to the shim itself, start the shim DomU with only the BSP online, and let the guest bring up other CPUs as it needs them.

---
 xen/arch/x86/dom0_build.c    | 32 +++++++++++++++++++++++++++++---
 xen/arch/x86/pv/dom0_build.c |  3 ++-
 xen/arch/x86/setup.c         | 28 ++++++++++++++++++----------
 3 files changed, 49 insertions(+), 14 deletions(-)
xen/arch/x86/dom0_build.c +29 -3
index 30347bcc67..d54155b1a4 100644
@@ -138,9 +138,18 @@ struct vcpu *__init dom0_setup_vcpu(struct domain *d,
 
     if ( v )
     {
-        if ( !d->is_pinned && !dom0_affinity_relaxed )
-            cpumask_copy(v->cpu_hard_affinity, &dom0_cpus);
-        cpumask_copy(v->cpu_soft_affinity, &dom0_cpus);
+        if ( pv_shim )
+        {
+
+            cpumask_setall(v->cpu_hard_affinity);
+            cpumask_setall(v->cpu_soft_affinity);
+        }
+        else
+        {
+            if ( !d->is_pinned && !dom0_affinity_relaxed )
+                cpumask_copy(v->cpu_hard_affinity, &dom0_cpus);
+            cpumask_copy(v->cpu_soft_affinity, &dom0_cpus);
+        }
     }
 
     return v;
@@ -153,6 +162,23 @@ unsigned int __init dom0_max_vcpus(void)
     unsigned int i, max_vcpus, limit;
     nodeid_t node;
 
+    if ( pv_shim )
+    {
+        nodes_setall(dom0_nodes);
+
+        /*
+         * When booting in shim mode APs are not started until the guest brings
+         * other vCPUs up.
+         */
+        cpumask_set_cpu(0, &dom0_cpus);
+
+        /*
+         * On PV shim mode allow the guest to have as many CPUs as available.
+         */
+        return nr_cpu_ids;
+    }
+
+
     for ( i = 0; i < dom0_nr_pxms; ++i )
         if ( (node = pxm_to_node(dom0_pxms[i])) != NUMA_NO_NODE )
             node_set(node, dom0_nodes);
xen/arch/x86/pv/dom0_build.c +2 -1
index f5a793b1d2..2bee988f54 100644
@@ -695,7 +695,8 @@ int __init dom0_construct_pv(struct domain *d,
     for ( i = 0; i < XEN_LEGACY_MAX_VCPUS; i++ )
         shared_info(d, vcpu_info[i].evtchn_upcall_mask) = 1;
 
-    printk("Dom0 has maximum %u VCPUs\n", d->max_vcpus);
+    printk("%s has maximum %u VCPUs\n", pv_shim ? "DomU" : "Dom0",
+           d->max_vcpus);
 
     cpu = v->processor;
     for ( i = 1; i < d->max_vcpus; i++ )
xen/arch/x86/setup.c +18 -10
index 34d746395b..aa6703d542 100644
@@ -1580,20 +1580,28 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     do_presmp_initcalls();
 
-    for_each_present_cpu ( i )
+    if ( !pv_shim )
     {
-        /* Set up cpu_to_node[]. */
-        srat_detect_node(i);
-        /* Set up node_to_cpumask based on cpu_to_node[]. */
-        numa_add_cpu(i);        
-
-        if ( (num_online_cpus() < max_cpus) && !cpu_online(i) )
+        for_each_present_cpu ( i )
         {
-            int ret = cpu_up(i);
-            if ( ret != 0 )
-                printk("Failed to bring up CPU %u (error %d)\n", i, ret);
+            /* Set up cpu_to_node[]. */
+            srat_detect_node(i);
+            /* Set up node_to_cpumask based on cpu_to_node[]. */
+            numa_add_cpu(i);
+
+            if ( (num_online_cpus() < max_cpus) && !cpu_online(i) )
+            {
+                int ret = cpu_up(i);
+                if ( ret != 0 )
+                    printk("Failed to bring up CPU %u (error %d)\n", i, ret);
+            }
         }
     }
+    /*
+     * NB: when running as a PV shim VCPUOP_up/down is wired to the shim
+     * physical cpu_add/remove functions, so launch the guest with only
+     * the BSP online and let it bring up the other CPUs as required.
+     */
 
     printk("Brought up %ld CPUs\n", (long)num_online_cpus());
     smp_cpus_done();
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#192 lore raw

Re: [PATCH RFC v1 66/74] xen/shim: allow DomU to have as many vcpus as available

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:

From: Roger Pau Monne <[email protected]>

Since the shim VCPUOP_{up/down} hypercall is wired to the plug/unplug of CPUs to the shim itself, start the shim DomU with only the BSP online, and let the guest bring up other CPUs as it needs them.

What are the ramifications of not making this change? Shouldn't the shim's pCPU count (pCPU as viewed from its own perspective) simply always match its client's vCPU count?

Quoted 7 lines
xen/arch/x86/dom0_build.c +3 -0
@@ -153,6 +162,23 @@ unsigned int __init dom0_max_vcpus(void)
     unsigned int i, max_vcpus, limit;
     nodeid_t node;
 
+    if ( pv_shim )
+    {
+        nodes_setall(dom0_nodes);

xen/arch/x86/dom0_build.c unsigned int __init dom0_max_vcpus(void) · line 177

Quoted 10 lines
xen/arch/x86/dom0_build.c +10 -0
+
+        /*
+         * When booting in shim mode APs are not started until the guest brings
+         * other vCPUs up.
+         */
+        cpumask_set_cpu(0, &dom0_cpus);
+
+        /*
+         * On PV shim mode allow the guest to have as many CPUs as available.
+         */

Style (single line comment).

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 698

Quoted 8 lines
xen/arch/x86/pv/dom0_build.c +1 -1
@@ -695,7 +695,8 @@ int __init dom0_construct_pv(struct domain *d,
     for ( i = 0; i < XEN_LEGACY_MAX_VCPUS; i++ )
         shared_info(d, vcpu_info[i].evtchn_upcall_mask) = 1;
 
-    printk("Dom0 has maximum %u VCPUs\n", d->max_vcpus);
+    printk("%s has maximum %u VCPUs\n", pv_shim ? "DomU" : "Dom0",

"Dom%c ..." perhaps?

Quoted 28 lines
xen/arch/x86/setup.c +10 -10
@@ -1580,20 +1580,28 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     do_presmp_initcalls();
 
-    for_each_present_cpu ( i )
+    if ( !pv_shim )
     {
-        /* Set up cpu_to_node[]. */
-        srat_detect_node(i);
-        /* Set up node_to_cpumask based on cpu_to_node[]. */
-        numa_add_cpu(i);        
-
-        if ( (num_online_cpus() < max_cpus) && !cpu_online(i) )
+        for_each_present_cpu ( i )
         {
-            int ret = cpu_up(i);
-            if ( ret != 0 )
-                printk("Failed to bring up CPU %u (error %d)\n", i, ret);
+            /* Set up cpu_to_node[]. */
+            srat_detect_node(i);
+            /* Set up node_to_cpumask based on cpu_to_node[]. */
+            numa_add_cpu(i);
+
+            if ( (num_online_cpus() < max_cpus) && !cpu_online(i) )
+            {
+                int ret = cpu_up(i);

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1604

Quoted 10 lines
xen/arch/x86/setup.c +8 -0
+                if ( ret != 0 )
+                    printk("Failed to bring up CPU %u (error %d)\n", i, ret);
+            }
         }
     }
+    /*
+     * NB: when running as a PV shim VCPUOP_up/down is wired to the shim
+     * physical cpu_add/remove functions, so launch the guest with only
+     * the BSP online and let it bring up the other CPUs as required.
+     */

I think this comment would better go immediately ahead of the if() you introduce.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#193 lore raw

Re: [PATCH RFC v1 66/74] xen/shim: allow DomU to have as many vcpus as available

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 14:06, <[email protected]> wrote:

From: Roger Pau Monne <[email protected]>

Since the shim VCPUOP_{up/down} hypercall is wired to the plug/unplug of CPUs to the shim itself, start the shim DomU with only the BSP online, and let the guest bring up other CPUs as it needs them.

Quoted 5 lines
Quoted 1 line

What are the ramifications of not making this change? Shouldn't the shim's pCPU count (pCPU as viewed from its own perspective) simply always match its client's vCPU count?

Yes, that's the point of this change. By default Dom0 will get a many vCPUs as online pCPUs. ÇIn the shim case the number of online pCPUs is always 1, and thus we need to set max_vcpus to match the number of possible pCPUs.

Quoted 10 lines

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 698

Quoted 8 lines
xen/arch/x86/pv/dom0_build.c +1 -1
@@ -695,7 +695,8 @@ int __init dom0_construct_pv(struct domain *d,
     for ( i = 0; i < XEN_LEGACY_MAX_VCPUS; i++ )
         shared_info(d, vcpu_info[i].evtchn_upcall_mask) = 1;
 
-    printk("Dom0 has maximum %u VCPUs\n", d->max_vcpus);
+    printk("%s has maximum %u VCPUs\n", pv_shim ? "DomU" : "Dom0",

"Dom%c ..." perhaps?

We could even use Dom%u and d->domain_id. That would remove the dependency on pv_shim.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#194 lore raw

RFC PATCH 67/74 libxl: libxl__build_hvm: Introduce separate b_info parameter

In reply to Wei Liu

From: Ian Jackson <[email protected]>

When running pv-in-pvh, we are going to want to pass this function an exciting config which is a mixture of the user's main domain configuration, and some PVH configuration which we make up.

To this end, have libxl__build_hvm take, and honour, a separate parameter for config->b_info. Because it already has a convenience alias, the change is trivial.

We add the obvious extra parameter at every call site.

No functional change.

---
 tools/libxl/libxl_create.c   | 4 ++--
 tools/libxl/libxl_dom.c      | 2 +-
 tools/libxl/libxl_internal.h | 1 +
 3 files changed, 4 insertions(+), 3 deletions(-)
tools/libxl/libxl_create.c +2 -2
index f15fb215c2..a837a7f5be 100644
@@ -438,7 +438,7 @@ int libxl__domain_build(libxl__gc *gc,
 
     switch (info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:
-        ret = libxl__build_hvm(gc, domid, d_config, state);
+        ret = libxl__build_hvm(gc, domid, d_config, &d_config->b_info, state);
         if (ret)
             goto out;
 
@@ -499,7 +499,7 @@ int libxl__domain_build(libxl__gc *gc,
 
         break;
     case LIBXL_DOMAIN_TYPE_PVH:
-        ret = libxl__build_hvm(gc, domid, d_config, state);
+        ret = libxl__build_hvm(gc, domid, d_config, &d_config->b_info, state);
         if (ret)
             goto out;
 
tools/libxl/libxl_dom.c +1 -1
index f04eec7c79..14a9a09958 100644
@@ -1172,12 +1172,12 @@ out:
 
 int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
               libxl_domain_config *d_config,
+              libxl_domain_build_info *info,
               libxl__domain_build_state *state)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     int rc;
     uint64_t mmio_start, lowmem_end, highmem_end, mem_size;
-    libxl_domain_build_info *const info = &d_config->b_info;
     struct xc_dom_image *dom = NULL;
     bool device_model = info->type == LIBXL_DOMAIN_TYPE_HVM ? true : false;
 
tools/libxl/libxl_internal.h +1 -0
index ef1b2e2ca1..15a3c33697 100644
@@ -1160,6 +1160,7 @@ _hidden int libxl__build_pv(libxl__gc *gc, uint32_t domid,
              libxl_domain_build_info *info, libxl__domain_build_state *state);
 _hidden int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
               libxl_domain_config *d_config,
+              libxl_domain_build_info *info,
               libxl__domain_build_state *state);
 
 _hidden int libxl__qemu_traditional_cmd(libxl__gc *gc, uint32_t domid,
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#195 lore raw

RFC PATCH 68/74 libxl__domain_build_info_setdefault_pvhhvm: introduce

In reply to Wei Liu

From: Ian Jackson <[email protected]>

We are going to want to make bits of a pvh config, specifically the build_info, internally in libxl. This code is part of the default-setting etc. and will need to be called there.

---
 tools/libxl/libxl_create.c   | 9 +++++++++
 tools/libxl/libxl_internal.h | 2 ++
 2 files changed, 11 insertions(+)
tools/libxl/libxl_create.c +9 -0
index a837a7f5be..6d910e4a09 100644
@@ -396,6 +396,15 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
         return ERROR_INVAL;
     }
 
+    rc = libxl__domain_build_info_setdefault_pvhhvm(gc, b_info);
+    if (rc) return rc;
+
+    return 0;
+}
+
+int libxl__domain_build_info_setdefault_pvhhvm(libxl__gc *gc,
+                                        libxl_domain_build_info *b_info)
+{
     /* Configuration fields shared between PVH and HVM. */
     if (b_info->type != LIBXL_DOMAIN_TYPE_PV) {
         if (libxl__timer_mode_is_default(&b_info->timer_mode))
tools/libxl/libxl_internal.h +2 -0
index 15a3c33697..174cf35d97 100644
@@ -1254,6 +1254,8 @@ _hidden int libxl__domain_create_info_setdefault(libxl__gc *gc,
                                         libxl_domain_create_info *c_info);
 _hidden int libxl__domain_build_info_setdefault(libxl__gc *gc,
                                         libxl_domain_build_info *b_info);
+_hidden int libxl__domain_build_info_setdefault_pvhhvm(libxl__gc *gc,
+                                        libxl_domain_build_info *b_info);
 _hidden void libxl__rdm_setdefault(libxl__gc *gc,
                                    libxl_domain_build_info *b_info);
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#196 lore raw

RFC PATCH 69/74 libxl_bitmap_copy_alloc: copy 0, NULL as 0, NULL

In reply to Wei Liu

From: Ian Jackson <[email protected]>

We shouldn't allocate when it's both unnecessary, and not in accordance with the thing we're copying.

One effect is to make a copied libxl__domain_build_info more like the original, which is going to be helpful for the pv shim mode.

---
 tools/libxl/libxl_utils.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/libxl/libxl_utils.c +6 -2
index 507ee56c7c..63b1a2cac1 100644
@@ -659,9 +659,13 @@ void libxl_bitmap_copy_alloc(libxl_ctx *ctx,
 {
     GC_INIT(ctx);
 
-    dptr->map = libxl__calloc(NOGC, sptr->size, sizeof(*sptr->map));
     dptr->size = sptr->size;
-    memcpy(dptr->map, sptr->map, sptr->size * sizeof(*sptr->map));
+    if (sptr->map) {
+        dptr->map = libxl__calloc(NOGC, sptr->size, sizeof(*sptr->map));
+        memcpy(dptr->map, sptr->map, sptr->size * sizeof(*sptr->map));
+    } else {
+        dptr->map = NULL;
+    }
 
     GC_FREE;
 }
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#197 lore raw

RFC PATCH 70/74 libxl: pvshim: Check state->shim_path before domain type

In reply to Wei Liu

From: Ian Jackson <[email protected]>

This will make it possible to use the shim when the actual application-requested domain type is PV.

Code elsewhere is responsible for setting state->shim_path non-NULL iff the shim is required.

With this patch, in the current context, setting LIBXL_PVSHIM_PATH will affect non-PVH guests now. So we increase the scope of that bodge (which we are about to abolish).

---
 tools/libxl/libxl_dom.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/libxl_dom.c +5 -3
index 14a9a09958..bf509905a1 100644
@@ -1198,9 +1198,11 @@ int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
      * If PVH and no shim override, use the pv cmdline.
      * If not PVH, use info->cmdline.
      */
-    dom = xc_dom_allocate(ctx->xch, info->type == LIBXL_DOMAIN_TYPE_PVH ?
-                          (state->shim_path ? state->shim_cmdline : state->pv_cmdline) :
-                          info->cmdline, NULL);
+    dom = xc_dom_allocate(ctx->xch,
+              state->shim_path                    ? state->shim_cmdline :
+              info->type == LIBXL_DOMAIN_TYPE_PVH ? state->pv_cmdline   :
+              info->cmdline,
+                          NULL);
     if (!dom) {
         LOGE(ERROR, "xc_dom_allocate failed");
         rc = ERROR_NOMEM;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#198 lore raw

RFC PATCH 71/74 libxl: pvshim: Provide first-class config settings to enable shim mode

In reply to Wei Liu

From: Ian Jackson <[email protected]>

** NOTE: This patch does not currently work! **

** NOTE: I intend to change the config names from "pvhshim" to "pvshim" **

This is API-compatible because old callers are supposed to call libxl_*_init to initialise the struct; and the updated function clears these members.

It is ABI-compatible because the new fields make this member of the guest type union larger but only within the existing size of that union.

For now, our config defaults are:
* shim is disabled
* if enabled, path is "xen-shim" in the xen firmware directory
* if enabled, cmdline is the one we are currently debugging with

The debugging arguments will be rationalised in a moment.

---
 tools/libxl/libxl.h          |  8 ++++++++
 tools/libxl/libxl_create.c   | 48 +++++++++++++++++++++++++++++++++++++++++---
 tools/libxl/libxl_dom.c      | 10 ---------
 tools/libxl/libxl_internal.h |  2 ++
 tools/libxl/libxl_types.idl  |  3 +++
 5 files changed, 58 insertions(+), 13 deletions(-)
tools/libxl/libxl.h +8 -0
index 5e9aed739d..81dfcc80ad 100644
@@ -1101,6 +1101,14 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
  */
 #define LIBXL_HAVE_SET_PARAMETERS 1
 
+/*
+ * LIBXL_HAVE_PV_SHIM
+ *
+ * If this is defined, libxl_domain_build_info's pv type information
+ * contains members pvhshim, pvhshim_path, pvhshim_cmdline.
+ */
+#define LIBXL_HAVE_PV_SHIM 1
+
 typedef char **libxl_string_list;
 void libxl_string_list_dispose(libxl_string_list *sl);
 int libxl_string_list_length(const libxl_string_list *sl);
tools/libxl/libxl_create.c +45 -3

72 lines not on this page · raw message

tools/libxl/libxl_dom.c +0 -10
index bf509905a1..3b6c457ec0 100644
@@ -1183,16 +1183,6 @@ int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
 
     xc_dom_loginit(ctx->xch);
 
-    /* FIXME */
-#define LIBXL_PVSHIM_PATH "LIBXL_PVSHIM_PATH"
-#define LIBXL_PVSHIM_CMDLINE "LIBXL_PVSHIM_CMDLINE"
-    state->shim_path = getenv(LIBXL_PVSHIM_PATH);
-    if (state->shim_path) {
-        state->shim_cmdline = getenv(LIBXL_PVSHIM_CMDLINE);
-        LOG(WARN, "LIBXL_PVSHIM_PATH detected, using pv shim %s cmd %s",
-            state->shim_path, state->shim_cmdline);
-    }
-
     /* 
      * If PVH and we have a shim override, use the shim cmdline.
      * If PVH and no shim override, use the pv cmdline.
tools/libxl/libxl_internal.h +2 -0
index 174cf35d97..2897e7c3bb 100644
@@ -118,6 +118,8 @@
 #define TAP_DEVICE_SUFFIX "-emu"
 #define DOMID_XS_PATH "domid"
 #define INVALID_DOMID ~0
+#define PVSHIM_BASENAME "xen-shim"
+#define PVSHIM_CMDLINE "pv-shim console=xen,pv sched=null loglvl=all guest_loglvl=all apic_verbosity=debug e820-verbose"
 
 /* Size macros. */
 #define __AC(X,Y)   (X##Y)
tools/libxl/libxl_types.idl +3 -0
index a239324341..a6ebea0178 100644
@@ -591,6 +591,9 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                       ("features", string, {'const': True}),
                                       # Use host's E820 for PCI passthrough.
                                       ("e820_host", libxl_defbool),
+                                      ("pvhshim", libxl_defbool),
+                                      ("pvhshim_path", string),
+                                      ("pvhshim_cmdline", string),
                                       ])),
                  ("pvh", None),
                  ("invalid", None),
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#199 lore raw

RFC PATCH 72/74 libxl: pvshim: Introduce pvhshim_extra

In reply to Wei Liu

From: Ian Jackson <[email protected]>

And move the debugging options from the default config into a doc comment in libxl_types.idl.

---
 tools/libxl/libxl.h          | 2 +-
 tools/libxl/libxl_create.c   | 5 ++++-
 tools/libxl/libxl_internal.h | 2 +-
 tools/libxl/libxl_types.idl  | 1 +
 4 files changed, 7 insertions(+), 3 deletions(-)
tools/libxl/libxl.h +1 -1
index 81dfcc80ad..7e40155079 100644
@@ -1105,7 +1105,7 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
  * LIBXL_HAVE_PV_SHIM
  *
  * If this is defined, libxl_domain_build_info's pv type information
- * contains members pvhshim, pvhshim_path, pvhshim_cmdline.
+ * contains members pvhshim, pvhshim_path, pvhshim_cmdline, pvhshim_extra.
  */
 #define LIBXL_HAVE_PV_SHIM 1
 
tools/libxl/libxl_create.c +4 -1
index cd98522b9b..94b2c90c5e 100644
@@ -519,7 +519,10 @@ int libxl__domain_build(libxl__gc *gc,
             if (ret) goto out;
 
             state->shim_path = info->u.pv.pvhshim_path;
-            state->shim_cmdline = info->u.pv.pvhshim_cmdline;
+            state->shim_cmdline = GCSPRINTF("%s%s%s",
+                                            info->u.pv.pvhshim_cmdline,
+                                            info->u.pv.pvhshim_extra ? " " : "",
+                                            info->u.pv.pvhshim_extra ? info->u.pv.pvhshim_extra : "");
             ret = libxl__build_hvm(gc, domid,
                                    d_config, &shim_info,
                                    state);
tools/libxl/libxl_internal.h +1 -1
index 2897e7c3bb..040d9c6b30 100644
@@ -119,7 +119,7 @@
 #define DOMID_XS_PATH "domid"
 #define INVALID_DOMID ~0
 #define PVSHIM_BASENAME "xen-shim"
-#define PVSHIM_CMDLINE "pv-shim console=xen,pv sched=null loglvl=all guest_loglvl=all apic_verbosity=debug e820-verbose"
+#define PVSHIM_CMDLINE "pv-shim console=xen,pv sched=null"
 
 /* Size macros. */
 #define __AC(X,Y)   (X##Y)
tools/libxl/libxl_types.idl +1 -0
index a6ebea0178..7ff807acf3 100644
@@ -594,6 +594,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                       ("pvhshim", libxl_defbool),
                                       ("pvhshim_path", string),
                                       ("pvhshim_cmdline", string),
+                                      ("pvhshim_extra", string), # eg "loglvl=all guest_loglvl=all apic_verbosity=debug e820-verbose"
                                       ])),
                  ("pvh", None),
                  ("invalid", None),
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#200 lore raw

RFC PATCH 73/74 xl: pvshim: Provide and document xl config

In reply to Wei Liu

From: Ian Jackson <[email protected]>

** NOTE: I intend to change the config names from "pvhshim" to "pvshim" **

---
 docs/man/xl.cfg.pod.5.in | 28 ++++++++++++++++++++++++++++
 tools/xl/xl_parse.c      | 11 +++++++++++
 2 files changed, 39 insertions(+)
docs/man/xl.cfg.pod.5.in +28 -0
index b7b91d8627..e9f29c2424 100644
@@ -470,6 +470,34 @@ version of pvgrub can be selected.
 Note that xl expects to find the pvgrub32.bin and pvgrub64.bin binaries in
 F<@XENFIRMWAREDIR@>.
 
+=item B<pvhshim=BOOLEAN>
+
+Whether to boot this guest as a PV guest within a PVH container
+(ie, using processor hardware extensions to
+separate its address space.)
+
+Default is false.
+
+=item B<pvhshim_path="PATH">
+
+The PVH shim is a specially-built firmware-like executable
+constructed from the hypervisor source tree.
+This option specifies to use a non-default shim.
+Ignored if pvhsim is false.
+
+=item B<pvhshim_cmdline="STRING">
+
+Command line for the shim.
+Default is "pv-shim console=xen,pv sched=null".
+Ignored if pvhsim is false.
+
+=item B<pvhshim_extra="STRING">
+
+Extra command line arguments for the shim.
+If supplied, appended to the value for pvhshim_cmdline.
+Default is empty.
+Ignored if pvhsim is false.
+
 =back
 
 =head4 HVM guest options
tools/xl/xl_parse.c +11 -0
index 9a692d5ae6..bdd3ad8127 100644
@@ -1402,6 +1402,17 @@ void parse_config_data(const char *config_source,
             exit(1);
         }
 
+        xlu_cfg_get_defbool(config, "pvhshim", &b_info->u.pv.pvhshim, 0);
+        if (!xlu_cfg_get_string(config, "pvhshim_path", &buf, 0))
+            xlu_cfg_replace_string(config, "pvhshim_path",
+                                   &b_info->u.pv.pvhshim_path, 0);
+        if (!xlu_cfg_get_string(config, "pvhshim_cmdline", &buf, 0))
+            xlu_cfg_replace_string(config, "pvhshim_cmdline",
+                                   &b_info->u.pv.pvhshim_cmdline, 0);
+        if (!xlu_cfg_get_string(config, "pvhshim_extra", &buf, 0))
+            xlu_cfg_replace_string(config, "pvhshim_extra",
+                                   &b_info->u.pv.pvhshim_extra, 0);
+
         break;
     }
     default:
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#201 lore raw

RFC PATCH 74/74 libxl: pvshim: Set video_memkb to ~0

In reply to Wei Liu

From: Ian Jackson <[email protected]>

This is how ordinary pvh guests get created right now. This is probably a bug. But I want this pv shim mode to build the guest like pvh does.

---
 tools/libxl/libxl_create.c | 1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl_create.c +1 -0
index 94b2c90c5e..a1f9c9957b 100644
@@ -517,6 +517,7 @@ int libxl__domain_build(libxl__gc *gc,
                                               LIBXL_DOMAIN_TYPE_PVH);
             ret = libxl__domain_build_info_setdefault_pvhhvm(gc, &shim_info);
             if (ret) goto out;
+            shim_info.video_memkb = ~(uint64_t)0; /* bodge ?! */
 
             state->shim_path = info->u.pv.pvhshim_path;
             state->shim_cmdline = GCSPRINTF("%s%s%s",
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#202 lore raw
In reply to Wei Liu

Wei Liu writes ("[Xen-devel] [PATCH RFC v1 00/74] Run PV guest in PVH container"):

Quoted 3 lines

1. ARM build and some Clang build are broken by this series.
2. The host will see a lot over-allocation messages, nothing too harmful and will be fixed once toolstack is ready.

My revised toolstack part of this, which I passed to Wei on Friday, uses a different approach. The caller must specify type=pvh and some additional pvshim options (minimally, they must set a new defbool to true).

Since we are intending to backport something like this, I thought I would write down my API/ABI compatibility analysis, and our discussions as I remember them.

I'm talking here about the libxl ABI/API; xl's config file handling is easier and I won't analyse it here.

Xen 4.10
========

Backport strategy:
------------------

The ABI/API changes at the libxl layer are a few new fields in b_info.u.pvh. That substructure is empty in earlier 4.10. It's part of a union, so the overall struct size and layout does not increase.

Old callers with new libxl on 4.10:
-----------------------------------

If the caller is creating a guest other than a PVH one there is no change to the ABI.

If the caller that is creating a PVH guest uses the proper libxl_*init* functions those new fields will be initialised to the default values. The default is to disable the new mode (this is right because we don't want to turn existing PVH guests into PV-in-PVH ones). With the patch as I passed to Wei, the extra string parameters are initialised unconditionally. This is not desirable in the backport.

=> code change: the backport should set the default non-NULL
values only if the pvhshim boolean is true after defaulting

When the caller disposes a domain config, the libxl dispose function will free any of these fields.

If the caller did not call libxl_*init*, nor initialise the struct with memset, but _does_ call libxl_*dispose*, the dispose will read uninitialised memory and crash. However, that is not a supported approach and such a caller would have to manually initialise all the myriad fields instead of calling *init* so it seems unlikely.

If the caller is examining existing guests: The returned struct will have additional information, for any PVH guests (including PV-in-PVH ones) which will be ignored by the caller. The caller may misunderstand and misreport the guest type. The additional information may come from the heap, but *dispose* will free it, so there should be no leak.

Overall: this is safe, with the code change I propose.

New callers with old libxl on 4.10:
-----------------------------------

If the caller is creating a guest other than a PVH one there is no change to the ABI.

When a caller creates a PVH non-shim guest, it will probably not set any of these fields. Things will work properly.

If a caller tries to create a shim guest, the attempt to do so will be ignored and the guest will be created as PV. Probably, the guest will not boot. Additionally, if the caller filled in pvshim cmdline or path information, it will probably expect *dispose* to free those values - and the result will be a memory leak.

If the caller is examining existing guests, PV and HVM guests will work fine. If the caller is examining existing PVH guests, the library will not initialise the new fields. The result may include an uninitialised read by the caller.

Overall: this is not safe and should be prevented.

=> code change: the 4.10 backport should use symbol versioning or
another technique to prevent expecting callers whose source code understands *shim* guests from using libxl versions which don't.

Xen 4.9 and 4.8
===============

Backport strategy:
------------------

These have some kind of PVH toolstack support but only half-baked and specified with "device_model_version=none" which breaks some PV device availability etc.

We have considered three backport strategies:

(i) Make the shim run in HVM mode and use that. My understanding from experts in the relevant area is that this is quite awkward:
a lot of work, and risks producing a program which does not work well.

(ii) Have libxl callers specify the new mode as a variant on the PV
guest type. Inside libxl this is a real pain because it involves touching every place the domain type is considered, to decide whether this new mode should be like PV or like HVM. This is essentially redoing the work done for PVH in 4.10, but afresh and therefore with new bugs.

(iii) Backport the first-class PVH guest type patches to libxl from
4.10. This is a large backport but these patches have been around for a while and we regard them as stable. Doing this means we provide users of 4.8 and 4.9 with the same bugs as are in 4.10 (which we think are fairly few) rather than doing a lot of work to write an essentially equivalent amount of new code with a lot of new bugs.

From the above it should be evident that we think strategy (iii) is the best answer. Howwever, there are some wrinkles.

Some of the libxl pvh patches move various parameters for specifying guest kernels from the pv-specific part of the domain config struct to the "build info", ie from d_config.b_info.u.pv to d_config.b_info.

We cannot do that in 4.8 and 4.9 because doing so would increase the size of d_config.b_info. b_info is not the last field in d_config, so adding fields to it would move other fields in b_info which is total ABI break.

Accordingly, we will have to provide copies of the kernel related parameters in the pvh struct.

Updated callers that want to create shim guests (or, indeed PVH guests) will have to specify the kernel in the new fields.

We should provide a macro to access the "right" field for each of the kernel fields. We should provide that macro in 4.10 and unstable too, so that the API is forward-compatible.

=> code change: define the macro just discussed

Old callers with new libxl on <=4.9:
------------------------------------

If the caller is creating a guest there is no change to the ABI.

If the caller is examining existing guests: If any PVH guests have been created already, the caller will receive a domain type that it is not expecting. It may crash or produce an error, but undefined behaviour seems unlikely. And this can only happen when old higher-level programs are operating on a host that already has new guests. Furthermore, if the caller properly handles the unexpected guest type, and calls *dispose*, there is even no memory leak.

New callers with old libxl on <=4.9:
------------------------------------

If the caller is creating a guest other than a PVH one there is no change to the ABI.

If the caller is creating a PVH guest, it will call *init* but that call will not initialise the PVH type. (*init* does not return errors.) The caller may attempt boolean setting, string replacement, and so on, on the undefined data.

=> code change: the <=4.9 backport should use symbol versioning or
another technique to prevent callers whose source code understands *PVH* guests from using libxl versions which don't.

If the caller is examining guests, for non-PVH guests there is no problem. For PVH guests, libxl will return wrong information but there will be no serious problem.

Xen 4.7 and earlier:
====================

These lack appropriate hypervisor support for PVHv2 guests. It may be possible to make the shim run in HVM - but see above.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#203 lore raw

Ian Jackson writes ("Re: [Xen-devel] [PATCH RFC v1 00/74] Run PV guest in PVH container"):

Quoted 2 lines

Xen 4.10
========

...

Quoted 2 lines

=> code change: the backport should set the default non-NULL
values only if the pvhshim boolean is true after defaulting

I discover, looking at the code, that this is already true.

Quoted 20 lines

New callers with old libxl on 4.10:
-----------------------------------

If the caller is creating a guest other than a PVH one there is no change to the ABI.

When a caller creates a PVH non-shim guest, it will probably not set any of these fields. Things will work properly.

If a caller tries to create a shim guest, the attempt to do so will be ignored and the guest will be created as PV. Probably, the guest will not boot. Additionally, if the caller filled in pvshim cmdline or path information, it will probably expect *dispose* to free those values - and the result will be a memory leak.

If the caller is examining existing guests, PV and HVM guests will work fine. If the caller is examining existing PVH guests, the library will not initialise the new fields. The result may include an uninitialised read by the caller.

Quoted 5 lines

Overall: this is not safe and should be prevented.

=> code change: the 4.10 backport should use symbol versioning or
another technique to prevent expecting callers whose source code understands *shim* guests from using libxl versions which don't.

This is only _really_ needed to avoid trouble when:
* New tool has been installed
* New libxl has not been installed
* PVH guests (including shim guests) have been created by tool A
* Tool B has not been updated, and is used to examine guests

We do not have any symbol versioning in libxl in 4.10 and it is hard to think of another way to make this work without changes to the tool source code. I don't want to invent something ad-hoc.

So I propose to skip this.

Wei: My conclusion is that the libxl tools branch (including George's "libxl: introduce hack" patch, as I I sent you previously, is suitable for simple rebase to 4.10.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#204 lore raw
In reply to Wei Liu
Quoted 65 lines · Wei Liu

Hi all

This is a patch series to run PV guest inside a PVH container. The series is still in a very RFC state. We're aware that some code is not very clean yet and in the process of cleaning things up.

The series can be found at:

https://xenbits.xen.org/git-http/people/liuw/xen.git wip.pvshim-rfc-v1

The basic idea can be found at page 15 of the slides at [0].

This is a mitigation against one of the CPU vulnerabilities disclosed recently. This series makes it possible to continue running untrusted PV guests. Please refer to XSA-254 [1] for more information.

Given the embargo lifted and vulnerabilities disclosed we opt to develop openly on xen-devel. Feedback and testing is very welcome.

The series is split into three parts: The first part is for the host that runs the shim, the second part is for the shim itself, the third part is for toolstack patches (not yet fully working). See the markers in the list of patches.

Instructions on using the PV shim:

1. Git clone the branch and configure as one normally would.
2. A xen-shim binary would be built and installed into Xen's firmware
directory, along side hvmloader and co.
3. Use the hacky way currently provided in the first part of the series to
boot a PV guest inside a PVH container:
a. Append type='pvh' in your PV guest config file;
b. Export two environment variables so that libxl knows where to find
the shim and what to add to the shim's command line option.
# export LIBXL_PVSHIM_PATH=$PATH_TO_XEN_SHIM
# export LIBXL_PVSHIM_CMDLINE="pv-shim console=xen,pv loglvl=all guest_loglvl=all apic_verbosity=debug e820-verbose sched=null"
4. xl create -c guest.cfg

You should be able to see some Xen messages first and then guest kernel messages (the console= shim paramter is required).

Known issues:

1. ARM build and some Clang build are broken by this series.
2. The host will see a lot over-allocation messages, nothing too harmful and will be fixed once toolstack is ready.

Wei.

[0] https://www.slideshare.net/xen_com_mgr/xpdds17-keynote-towards-a-configurable-and-slimmer-x86-hypervisor-wei-liu-citrix
[1] https://xenbits.xen.org/xsa/advisory-254.html

# Patches for the host:

448f56a363 x86/svm: Offer CPUID Faulting to AMD HVM guests as well 6a78c9ae33 x86: Common cpuid faulting support
05844fec44 x86/upcall: inject a spurious event after setting upcall vector fc7a48dd74 tools/libxc: initialise hvm loader elf log fd to get more logging 522c9cbaf0 tools/libxc: remove extraneous newline in xc_dom_load_acpi bd6b572b32 tools/libelf: fix elf notes check for PVH guest 449b932b0c tools/libxc: Multi modules support
cc6dbdc0c1 libxl: Introduce hack to allow PVH mode to add a shim

# Patches for the shim:

[snip]

Quoted 1 line

7dbc3f25f6 xen/x86: report domain id on cpuid

This is a host (L0) patch, isn't it?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#205 lore raw
Quoted 64 lines · George Dunlap
Quoted 63 lines · Wei Liu

Hi all

This is a patch series to run PV guest inside a PVH container. The series is still in a very RFC state. We're aware that some code is not very clean yet and in the process of cleaning things up.

The series can be found at:

https://xenbits.xen.org/git-http/people/liuw/xen.git wip.pvshim-rfc-v1

The basic idea can be found at page 15 of the slides at [0].

This is a mitigation against one of the CPU vulnerabilities disclosed recently. This series makes it possible to continue running untrusted PV guests. Please refer to XSA-254 [1] for more information.

Given the embargo lifted and vulnerabilities disclosed we opt to develop openly on xen-devel. Feedback and testing is very welcome.

The series is split into three parts: The first part is for the host that runs the shim, the second part is for the shim itself, the third part is for toolstack patches (not yet fully working). See the markers in the list of patches.

Instructions on using the PV shim:

1. Git clone the branch and configure as one normally would.
2. A xen-shim binary would be built and installed into Xen's firmware
directory, along side hvmloader and co.
3. Use the hacky way currently provided in the first part of the series to
boot a PV guest inside a PVH container:
a. Append type='pvh' in your PV guest config file;
b. Export two environment variables so that libxl knows where to find
the shim and what to add to the shim's command line option.
# export LIBXL_PVSHIM_PATH=$PATH_TO_XEN_SHIM
# export LIBXL_PVSHIM_CMDLINE="pv-shim console=xen,pv loglvl=all guest_loglvl=all apic_verbosity=debug e820-verbose sched=null"
4. xl create -c guest.cfg

You should be able to see some Xen messages first and then guest kernel messages (the console= shim paramter is required).

Known issues:

1. ARM build and some Clang build are broken by this series.
2. The host will see a lot over-allocation messages, nothing too harmful and will be fixed once toolstack is ready.

Wei.

[0] https://www.slideshare.net/xen_com_mgr/xpdds17-keynote-towards-a-configurable-and-slimmer-x86-hypervisor-wei-liu-citrix
[1] https://xenbits.xen.org/xsa/advisory-254.html

# Patches for the host:

448f56a363 x86/svm: Offer CPUID Faulting to AMD HVM guests as well 6a78c9ae33 x86: Common cpuid faulting support
05844fec44 x86/upcall: inject a spurious event after setting upcall vector fc7a48dd74 tools/libxc: initialise hvm loader elf log fd to get more logging 522c9cbaf0 tools/libxc: remove extraneous newline in xc_dom_load_acpi bd6b572b32 tools/libelf: fix elf notes check for PVH guest 449b932b0c tools/libxc: Multi modules support
cc6dbdc0c1 libxl: Introduce hack to allow PVH mode to add a shim

Quoted 6 lines
Quoted 2 lines

# Patches for the shim:

[snip]

Quoted 1 line

7dbc3f25f6 xen/x86: report domain id on cpuid

This is a host (L0) patch, isn't it?

Yes it is.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel