Skip to contentSkip to conversation

x86/dom0: minor fixes and improvements to PVH builder

PATCH In review Roger Pau Monne 4 participants 11 messages Dec 28, 2018 – Jan 7, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover x86/dom0: minor fixes and improvements to PVH builder

Hello,

This series contains an improvement when filling the p2m so that alignment is taken into account when allocating and populating the p2m.

The last patch is optional and adds a verbose mode to dom0 build so more information can be printed.

Thanks, Roger.

Roger Pau Monne (2):
x86/dom0: take alignment into account when populating p2m in PVH mode x86/dom0: add verbose mode and print memory allocation stats

 docs/misc/xen-command-line.markdown |  8 +++-
 xen/arch/x86/dom0_build.c           |  3 ++
 xen/arch/x86/hvm/dom0_build.c       | 64 ++++++++++++++++++++++++++---
 xen/include/asm-x86/setup.h         |  1 +
 4 files changed, 70 insertions(+), 6 deletions(-)
Signature · 7 lines
2.17.2 (Apple Git-113)


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

PATCH v2 1/2 x86/dom0: take alignment into account when populating p2m in PVH mode

Current code that allocates memory and populates the p2m for PVH Dom0 doesn't take the address alignment into account, this can lead to high order allocations that start on a non-aligned address to be broken down into lower order entries on the p2m page tables.

Fix this by taking into account the p2m page sizes and alignment requirements when allocating the memory and populating the p2m.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v1:
- Batch allocations/mappings when possible in order to allocate higher order regions.

---
 xen/arch/x86/hvm/dom0_build.c | 48 +++++++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/dom0_build.c +43 -5
index 7ea29c443a..f02fb931d1 100644
@@ -91,16 +91,54 @@ static int __init pvh_populate_memory_range(struct domain *d,
                                             unsigned long start,
                                             unsigned long nr_pages)
 {
-    unsigned int order = MAX_ORDER, i = 0;
+    struct {
+        unsigned long align;
+        unsigned int order;
+    } static const __initconst orders[] = {
+        /* NB: must be sorted by decreasing size. */
+        { .align = PFN_DOWN(GB(1)), .order = PAGE_ORDER_1G },
+        { .align = PFN_DOWN(MB(2)), .order = PAGE_ORDER_2M },
+        { .align = PFN_DOWN(KB(4)), .order = PAGE_ORDER_4K },
+    };
+    unsigned int max_order = MAX_ORDER, i = 0;
     struct page_info *page;
     int rc;
 #define MAP_MAX_ITER 64
 
     while ( nr_pages != 0 )
     {
-        unsigned int range_order = get_order_from_pages(nr_pages + 1);
+        unsigned int order, j;
+        unsigned long end;
+
+        for ( j = 0; j < ARRAY_SIZE(orders); j++ )
+            if ( IS_ALIGNED(start, orders[j].align) &&
+                 nr_pages >= (1UL << orders[j].order) )
+                break;
+
+        switch ( j )
+        {
+        case ARRAY_SIZE(orders):
+            printk("Unable to find allocation order for [%#lx,%#lx)\n",
+                   start, start + nr_pages);
+            return -EINVAL;
+
+        case 0:
+            /* Highest order, aim to allocate until the end of the region. */
+            end = (start + nr_pages) & ~(orders[0].align - 1);
+            break;
+
+        default:
+            /*
+             * Aim to allocate until the higher next order alignment or the
+             * end of the region.
+             */
+            end = min(ROUNDUP(start + 1, orders[j - 1].align),
+                      start + nr_pages);
+            break;
+        }
 
-        order = min(range_order ? range_order - 1 : 0, order);
+        order = get_order_from_pages(end - start + 1);
+        order = min(order ? order - 1 : 0, max_order);
         page = alloc_domheap_pages(d, order, dom0_memflags | MEMF_no_scrub);
         if ( page == NULL )
         {
@@ -108,7 +146,7 @@ static int __init pvh_populate_memory_range(struct domain *d,
             {
                 /* Try again without any dom0_memflags. */
                 dom0_memflags = 0;
-                order = MAX_ORDER;
+                max_order = MAX_ORDER;
                 continue;
             }
             if ( order == 0 )
@@ -116,7 +154,7 @@ static int __init pvh_populate_memory_range(struct domain *d,
                 printk("Unable to allocate memory with order 0!\n");
                 return -ENOMEM;
             }
-            order--;
+            max_order = order - 1;
             continue;
         }
 
Signature · 7 lines
2.17.2 (Apple Git-113)


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

PATCH v2 2/2 x86/dom0: add verbose mode and print memory allocation stats

In reply to Roger Pau Monne

Add a verbose option to the dom0 command line, so that dom0 builder can print extra debug information when required.

Use this new verbose mode to print statistics about memory allocations when populating dom0 p2m.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 docs/misc/xen-command-line.markdown |  8 +++++++-
 xen/arch/x86/dom0_build.c           |  3 +++
 xen/arch/x86/hvm/dom0_build.c       | 16 ++++++++++++++++
 xen/include/asm-x86/setup.h         |  1 +
 4 files changed, 27 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +7 -1
index 78b207c0d0..e7973ee509 100644
@@ -637,7 +637,7 @@ trace feature is only enabled in debugging builds of Xen.
 Specify the bit width of the DMA heap.
 
 ### dom0 (x86)
-> `= List of [ pvh | shadow ]`
+> `= List of [ pvh | shadow | verbose ]`
 
 > Sub-options:
 
@@ -654,6 +654,12 @@ Flag that makes a dom0 boot in PVHv2 mode.
 Flag that makes a dom0 use shadow paging. Only works when "pvh" is
 enabled.
 
+> `verbose`
+
+> Default: `false`
+
+Print debug information during dom0 build.
+
 ### dom0-iommu
 > `= List of [ passthrough | strict | map-inclusive ]`
 
xen/arch/x86/dom0_build.c +3 -0
index 54737daf6a..c0bc022a83 100644
@@ -281,6 +281,7 @@ struct vcpu *__init alloc_dom0_vcpu0(struct domain *dom0)
 bool __initdata opt_dom0_shadow;
 #endif
 bool __initdata dom0_pvh;
+bool __initdata dom0_verbose;
 
 /*
  * List of parameters that affect Dom0 creation:
@@ -306,6 +307,8 @@ static int __init parse_dom0_param(const char *s)
         else if ( (val = parse_boolean("shadow", s, ss)) >= 0 )
             opt_dom0_shadow = val;
 #endif
+        else if ( (val = parse_boolean("verbose", s, ss)) >= 0 )
+            dom0_verbose = val;
         else
             rc = -EINVAL;
 
xen/arch/x86/hvm/dom0_build.c +16 -0
index f02fb931d1..72d98fac78 100644
@@ -60,6 +60,18 @@ static struct acpi_madt_interrupt_override __initdata *intsrcovr;
 static unsigned int __initdata acpi_nmi_sources;
 static struct acpi_madt_nmi_source __initdata *nmisrc;
 
+static unsigned int __initdata order_stats[MAX_ORDER + 1] = { };
+
+static void __init print_order_stats(void)
+{
+    unsigned int i;
+
+    printk("Memory allocation stats:\n");
+    for ( i = 0; i < ARRAY_SIZE(order_stats); i++ )
+        if ( order_stats[i] )
+            printk("order: %2u allocations: %u\n", i, order_stats[i]);
+}
+
 static int __init modify_identity_mmio(struct domain *d, unsigned long pfn,
                                        unsigned long nr_pages, const bool map)
 {
@@ -168,6 +180,7 @@ static int __init pvh_populate_memory_range(struct domain *d,
         }
         start += 1UL << order;
         nr_pages -= 1UL << order;
+        order_stats[order]++;
         if ( (++i % MAP_MAX_ITER) == 0 )
             process_pending_softirqs();
     }
@@ -464,6 +477,9 @@ static int __init pvh_setup_p2m(struct domain *d)
             return rc;
     }
 
+    if ( dom0_verbose )
+        print_order_stats();
+
     return 0;
 #undef MB1_PAGES
 }
xen/include/asm-x86/setup.h +1 -0
index 1c8078340d..bb4c38567c 100644
@@ -65,6 +65,7 @@ extern bool opt_dom0_shadow;
 #define opt_dom0_shadow false
 #endif
 extern bool dom0_pvh;
+extern bool dom0_verbose;
 
 #define max_init_domid (0)
 
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by Tested-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] x86/dom0: take alignment into account when populating p2m in PVH mode

In reply to Roger Pau Monne
Quoted 10 lines · Roger Pau Monne

Current code that allocates memory and populates the p2m for PVH Dom0 doesn't take the address alignment into account, this can lead to high order allocations that start on a non-aligned address to be broken down into lower order entries on the p2m page tables.

Fix this by taking into account the p2m page sizes and alignment requirements when allocating the memory and populating the p2m.

Nice.  I now get a layout as follows:

(XEN) *** Building a PVH Dom0 ***
(XEN) pvh_populate_memory_range(d0, 0, a0)
(XEN) guest_physmap_add_page(d0, 00000, 100b00, 7 = 512kB) (XEN) guest_physmap_add_page(d0, 00080, 100bc0, 5 = 128kB) (XEN) pvh_populate_memory_range(d0, 100, 360) (XEN) guest_physmap_add_page(d0, 00100, 100a00, 8 = 1024kB) (XEN) guest_physmap_add_page(d0, 00200, 100800, 9 = 2048kB) (XEN) guest_physmap_add_page(d0, 00400, 100b80, 6 = 256kB) (XEN) guest_physmap_add_page(d0, 00440, 1007e0, 5 = 128kB)

which looks to be correct.

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

#6 lore raw

Re: [PATCH v2 2/2] x86/dom0: add verbose mode and print memory allocation stats

In reply to Roger Pau Monne
Quoted 38 lines · Roger Pau Monne

Add a verbose option to the dom0 command line, so that dom0 builder can print extra debug information when required.

Use this new verbose mode to print statistics about memory allocations when populating dom0 p2m.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 docs/misc/xen-command-line.markdown |  8 +++++++-
 xen/arch/x86/dom0_build.c           |  3 +++
 xen/arch/x86/hvm/dom0_build.c       | 16 ++++++++++++++++
 xen/include/asm-x86/setup.h         |  1 +
 4 files changed, 27 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +1 -1
index 78b207c0d0..e7973ee509 100644
@@ -637,7 +637,7 @@ trace feature is only enabled in debugging builds of Xen.
 Specify the bit width of the DMA heap.
 
 ### dom0 (x86)
-> `= List of [ pvh | shadow ]`
+> `= List of [ pvh | shadow | verbose ]`
 
 > Sub-options:
 

docs/misc/xen-command-line.markdown Flag that makes a dom0 boot in PVHv2 mode. · line 662

Quoted 9 lines
docs/misc/xen-command-line.markdown +6 -0
@@ -654,6 +654,12 @@ Flag that makes a dom0 boot in PVHv2 mode.
 Flag that makes a dom0 use shadow paging. Only works when "pvh" is
 enabled.
 
+> `verbose`
+
+> Default: `false`
+
+Print debug information during dom0 build.
+

Is there plan to expand this to PV as well? A user reading this may specify dom0=verbose in a PV setup but find nothing in the log.

The rest looks correct.

Wei.

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

#7 lore raw

Re: [PATCH v2 2/2] x86/dom0: add verbose mode and print memory allocation stats

Quoted 42 lines · Wei Liu
Quoted 41 lines · Roger Pau Monne

Add a verbose option to the dom0 command line, so that dom0 builder can print extra debug information when required.

Use this new verbose mode to print statistics about memory allocations when populating dom0 p2m.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 docs/misc/xen-command-line.markdown |  8 +++++++-
 xen/arch/x86/dom0_build.c           |  3 +++
 xen/arch/x86/hvm/dom0_build.c       | 16 ++++++++++++++++
 xen/include/asm-x86/setup.h         |  1 +
 4 files changed, 27 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +1 -1
index 78b207c0d0..e7973ee509 100644
@@ -637,7 +637,7 @@ trace feature is only enabled in debugging builds of Xen.
 Specify the bit width of the DMA heap.
 
 ### dom0 (x86)
-> `= List of [ pvh | shadow ]`
+> `= List of [ pvh | shadow | verbose ]`
 
 > Sub-options:
 
@@ -654,6 +654,12 @@ Flag that makes a dom0 boot in PVHv2 mode.
 Flag that makes a dom0 use shadow paging. Only works when "pvh" is
 enabled.
 
Quoted 9 lines

docs/misc/xen-command-line.markdown Flag that makes a dom0 boot in PVHv2 mode. · line 662

Quoted 6 lines
docs/misc/xen-command-line.markdown +6 -0
+> `verbose`
+
+> Default: `false`
+
+Print debug information during dom0 build.
+

Is there plan to expand this to PV as well? A user reading this may specify dom0=verbose in a PV setup but find nothing in the log.

Dom0 PV build is already quite verbose in comparison to PVH. The only user ATM is indeed PVH Dom0 builder, but it could be used by PV also.

I could add something to the documentation, but I'm afraid it would get stale.

Thanks, Roger.

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

#8 lore raw

Re: [PATCH v2 2/2] x86/dom0: add verbose mode and print memory allocation stats

Quoted 53 lines · Roger Pau Monné
Quoted 42 lines · Wei Liu
Quoted 41 lines · Roger Pau Monne

Add a verbose option to the dom0 command line, so that dom0 builder can print extra debug information when required.

Use this new verbose mode to print statistics about memory allocations when populating dom0 p2m.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 docs/misc/xen-command-line.markdown |  8 +++++++-
 xen/arch/x86/dom0_build.c           |  3 +++
 xen/arch/x86/hvm/dom0_build.c       | 16 ++++++++++++++++
 xen/include/asm-x86/setup.h         |  1 +
 4 files changed, 27 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +1 -1
index 78b207c0d0..e7973ee509 100644
@@ -637,7 +637,7 @@ trace feature is only enabled in debugging builds of Xen.
 Specify the bit width of the DMA heap.
 
 ### dom0 (x86)
-> `= List of [ pvh | shadow ]`
+> `= List of [ pvh | shadow | verbose ]`
 
 > Sub-options:
 
@@ -654,6 +654,12 @@ Flag that makes a dom0 boot in PVHv2 mode.
 Flag that makes a dom0 use shadow paging. Only works when "pvh" is
 enabled.
 
Quoted 9 lines

docs/misc/xen-command-line.markdown Flag that makes a dom0 boot in PVHv2 mode. · line 662

Quoted 6 lines
docs/misc/xen-command-line.markdown +6 -0
+> `verbose`
+
+> Default: `false`
+
+Print debug information during dom0 build.
+

Is there plan to expand this to PV as well? A user reading this may specify dom0=verbose in a PV setup but find nothing in the log.

Quoted 5 lines

Dom0 PV build is already quite verbose in comparison to PVH. The only user ATM is indeed PVH Dom0 builder, but it could be used by PV also.

I could add something to the documentation, but I'm afraid it would get stale.

I'm not too fussed.

Wei.

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

#3 lore raw

Re: [PATCH v2 1/2] x86/dom0: take alignment into account when populating p2m in PVH mode

In reply to Roger Pau Monne

xen/arch/x86/hvm/dom0_build.c static int __init pvh_populate_memory_range(struct domain *d, · line 95

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 12:18, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +2 -1
@@ -91,16 +91,54 @@ static int __init pvh_populate_memory_range(struct domain *d,
                                             unsigned long start,
                                             unsigned long nr_pages)
 {
-    unsigned int order = MAX_ORDER, i = 0;
+    struct {
+        unsigned long align;

It's .init.* data only, but anyway - why "long" instead of "int"?

Jan

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

#4 lore raw

Re: [PATCH v2 1/2] x86/dom0: take alignment into account when populating p2m in PVH mode

Quoted 12 lines · Jan Beulich

xen/arch/x86/hvm/dom0_build.c static int __init pvh_populate_memory_range(struct domain *d, · line 95

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 12:18, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +2 -1
@@ -91,16 +91,54 @@ static int __init pvh_populate_memory_range(struct domain *d,
                                             unsigned long start,
                                             unsigned long nr_pages)
 {
-    unsigned int order = MAX_ORDER, i = 0;
+    struct {
+        unsigned long align;

It's .init.* data only, but anyway - why "long" instead of "int"?

A mistake, unsigned int is fine to store the current alignments, will send a patch to fix it.

Thanks, Roger.

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

#9 lore raw

Re: [PATCH v2 2/2] x86/dom0: add verbose mode and print memory allocation stats

In reply to Roger Pau Monne

xen/arch/x86/hvm/dom0_build.c static struct acpi_madt_interrupt_override __initdata *intsrcovr; · line 63

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 12:18, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +1 -0
@@ -60,6 +60,18 @@ static struct acpi_madt_interrupt_override __initdata *intsrcovr;
 static unsigned int __initdata acpi_nmi_sources;
 static struct acpi_madt_nmi_source __initdata *nmisrc;
 
+static unsigned int __initdata order_stats[MAX_ORDER + 1] = { };

Pointless initializer.

xen/arch/x86/hvm/dom0_build.c static struct acpi_madt_interrupt_override __initdata *intsrcovr; · line 69

Quoted 5 lines
xen/arch/x86/hvm/dom0_build.c +5 -0
+static void __init print_order_stats(void)
+{
+    unsigned int i;
+
+    printk("Memory allocation stats:\n");

I think this wants prefixing with "Dom0".

xen/arch/x86/hvm/dom0_build.c static struct acpi_madt_interrupt_override __initdata *intsrcovr; · line 72

Quoted 3 lines
xen/arch/x86/hvm/dom0_build.c +3 -0
+    for ( i = 0; i < ARRAY_SIZE(order_stats); i++ )
+        if ( order_stats[i] )
+            printk("order: %2u allocations: %u\n", i, order_stats[i]);

Why the first of the two colons?

Jan

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

#10 lore raw

Re: [PATCH v2 2/2] x86/dom0: add verbose mode and print memory allocation stats

Quoted 11 lines · Jan Beulich

xen/arch/x86/hvm/dom0_build.c static struct acpi_madt_interrupt_override __initdata *intsrcovr; · line 63

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 12:18, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +1 -0
@@ -60,6 +60,18 @@ static struct acpi_madt_interrupt_override __initdata *intsrcovr;
 static unsigned int __initdata acpi_nmi_sources;
 static struct acpi_madt_nmi_source __initdata *nmisrc;
 
+static unsigned int __initdata order_stats[MAX_ORDER + 1] = { };

Pointless initializer.

Quoted 7 lines

xen/arch/x86/hvm/dom0_build.c static struct acpi_madt_interrupt_override __initdata *intsrcovr; · line 69

Quoted 5 lines
xen/arch/x86/hvm/dom0_build.c +5 -0
+static void __init print_order_stats(void)
+{
+    unsigned int i;
+
+    printk("Memory allocation stats:\n");

I think this wants prefixing with "Dom0".

I will use Dom%u and d->domain_id.

Quoted 5 lines

xen/arch/x86/hvm/dom0_build.c static struct acpi_madt_interrupt_override __initdata *intsrcovr; · line 72

Quoted 3 lines
xen/arch/x86/hvm/dom0_build.c +3 -0
+    for ( i = 0; i < ARRAY_SIZE(order_stats); i++ )
+        if ( order_stats[i] )
+            printk("order: %2u allocations: %u\n", i, order_stats[i]);

Why the first of the two colons?

Because it's printing the order, I will remove it in the next version.

Thanks, Roger.

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