Skip to contentSkip to conversation

Early Boot Allocation on Power

PATCH In review Shawn Anastasio 4 participants 12 messages Sep 26, 2024 – Dec 16, 2024

lore
Share & tools
lore
#0 lore raw

v5 cover Early Boot Allocation on Power

Hello all,

This series enables the Xen boot time allocator on Power by parsing the available memory regions from the firmware-provided device tree.

Thanks to Oleksii's work on my patches to move ARM's bootfdt code to common, v5 is much smaller. One new patch is included to fix some newly-broken behavior in bootfdt.c regarding handling of DT reserved memory maps.

Thanks,
Shawn

Shawn Anastasio (3):
xen/device-tree: Let DT reserve map entries overlap reserved-memory xen/ppc: Enable bootfdt and boot allocator
xen/ppc: mm-radix: Allocate all paging structures at runtime

 xen/arch/ppc/mm-radix.c           | 238 ++++++++++++++++--------------
 xen/arch/ppc/setup.c              |  20 ++-
 xen/common/device-tree/bootfdt.c  |  37 ++++-
 xen/common/device-tree/bootinfo.c |  11 +-
 xen/include/xen/bootfdt.h         |   3 +-
 5 files changed, 191 insertions(+), 118 deletions(-)

--
2.30.2

#1 lore raw

PATCH v5 1/3 xen/device-tree: Let DT reserve map entries overlap reserved-memory

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")
---
 xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++-----
 xen/common/device-tree/bootinfo.c | 11 +++++++++--
 xen/include/xen/bootfdt.h         |  3 ++-
 3 files changed, 34 insertions(+), 8 deletions(-)
xen/common/device-tree/bootfdt.c +23 -5
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
     {
         device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
         if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) )
+             check_reserved_regions_overlap(start, size, NULL) )
             return -EINVAL;
         /* Some DT may describe empty bank, ignore them */
         if ( !size )
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
     if ( nr_rsvd < 0 )
         panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
     for ( i = 0; i < nr_rsvd; i++ )
     {
+        const struct membanks *overlap = NULL;
         struct membank *bank;
         paddr_t s, sz;
 
         if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
             continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;
+            }
+            else
+                panic("FDT reserve map overlapped with membanks/modules\n");
+        }
+
         if ( reserved_mem->nr_banks < reserved_mem->max_banks )
         {
             bank = &reserved_mem->bank[reserved_mem->nr_banks];
@@ -610,10 +632,6 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
             panic("Cannot allocate reserved memory bank\n");
     }
 
-    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
-    if ( ret )
-        panic("Early FDT parsing failed (%d)\n", ret);
-
     /*
      * On Arm64 setup_directmap_mappings() expects to be called with the lowest
      * bank in memory first. There is no requirement that the DT will provide
xen/common/device-tree/bootinfo.c +9 -2
index f2e6a1145b..c1752bfdc8 100644
@@ -171,7 +171,8 @@ void __init fw_unreserved_regions(paddr_t s, paddr_t e,
  * existing reserved memory regions, otherwise false.
  */
 bool __init check_reserved_regions_overlap(paddr_t region_start,
-                                           paddr_t region_size)
+                                           paddr_t region_size,
+                                           const struct membanks **out_overlapping_membanks)
 {
     const struct membanks *mem_banks[] = {
         bootinfo_get_reserved_mem(),
@@ -190,8 +191,14 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
      * shared memory banks (when static shared memory feature is enabled)
      */
     for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+    {
         if ( meminfo_overlap_check(mem_banks[i], region_start, region_size) )
+        {
+            if ( out_overlapping_membanks )
+                *out_overlapping_membanks = mem_banks[i];
             return true;
+        }
+    }
 
     /* Check if input region is overlapping with bootmodules */
     if ( bootmodules_overlap_check(&bootinfo.modules,
@@ -216,7 +223,7 @@ struct bootmodule __init *add_boot_module(bootmodule_kind kind,
         return NULL;
     }
 
-    if ( check_reserved_regions_overlap(start, size) )
+    if ( check_reserved_regions_overlap(start, size, NULL) )
         return NULL;
 
     for ( i = 0 ; i < mods->nr_mods ; i++ )
xen/include/xen/bootfdt.h +2 -1
index 16fa05f38f..03e1d5fde8 100644
@@ -158,7 +158,8 @@ struct bootinfo {
 extern struct bootinfo bootinfo;
 
 bool check_reserved_regions_overlap(paddr_t region_start,
-                                    paddr_t region_size);
+                                    paddr_t region_size,
+                                    const struct membanks **out_overlapping_membanks);
 
 struct bootmodule *add_boot_module(bootmodule_kind kind,
                                    paddr_t start, paddr_t size, bool domU);
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v5 1/3] xen/device-tree: Let DT reserve map entries overlap reserved-memory

Quoted 10 lines · Shawn Anastasio

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Quoted 7 lines
Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")
---
 xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++-----
 xen/common/device-tree/bootinfo.c | 11 +++++++++--
 xen/include/xen/bootfdt.h         |  3 ++-
 3 files changed, 34 insertions(+), 8 deletions(-)

DT maintainers?

Jan

Quoted 113 lines
xen/common/device-tree/bootfdt.c +23 -5
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
     {
         device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
         if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) )
+             check_reserved_regions_overlap(start, size, NULL) )
             return -EINVAL;
         /* Some DT may describe empty bank, ignore them */
         if ( !size )
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
     if ( nr_rsvd < 0 )
         panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
     for ( i = 0; i < nr_rsvd; i++ )
     {
+        const struct membanks *overlap = NULL;
         struct membank *bank;
         paddr_t s, sz;
 
         if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
             continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;
+            }
+            else
+                panic("FDT reserve map overlapped with membanks/modules\n");
+        }
+
         if ( reserved_mem->nr_banks < reserved_mem->max_banks )
         {
             bank = &reserved_mem->bank[reserved_mem->nr_banks];
@@ -610,10 +632,6 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
             panic("Cannot allocate reserved memory bank\n");
     }
 
-    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
-    if ( ret )
-        panic("Early FDT parsing failed (%d)\n", ret);
-
     /*
      * On Arm64 setup_directmap_mappings() expects to be called with the lowest
      * bank in memory first. There is no requirement that the DT will provide
xen/common/device-tree/bootinfo.c +9 -2
index f2e6a1145b..c1752bfdc8 100644
@@ -171,7 +171,8 @@ void __init fw_unreserved_regions(paddr_t s, paddr_t e,
  * existing reserved memory regions, otherwise false.
  */
 bool __init check_reserved_regions_overlap(paddr_t region_start,
-                                           paddr_t region_size)
+                                           paddr_t region_size,
+                                           const struct membanks **out_overlapping_membanks)
 {
     const struct membanks *mem_banks[] = {
         bootinfo_get_reserved_mem(),
@@ -190,8 +191,14 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
      * shared memory banks (when static shared memory feature is enabled)
      */
     for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+    {
         if ( meminfo_overlap_check(mem_banks[i], region_start, region_size) )
+        {
+            if ( out_overlapping_membanks )
+                *out_overlapping_membanks = mem_banks[i];
             return true;
+        }
+    }
 
     /* Check if input region is overlapping with bootmodules */
     if ( bootmodules_overlap_check(&bootinfo.modules,
@@ -216,7 +223,7 @@ struct bootmodule __init *add_boot_module(bootmodule_kind kind,
         return NULL;
     }
 
-    if ( check_reserved_regions_overlap(start, size) )
+    if ( check_reserved_regions_overlap(start, size, NULL) )
         return NULL;
 
     for ( i = 0 ; i < mods->nr_mods ; i++ )
xen/include/xen/bootfdt.h +2 -1
index 16fa05f38f..03e1d5fde8 100644
@@ -158,7 +158,8 @@ struct bootinfo {
 extern struct bootinfo bootinfo;
 
 bool check_reserved_regions_overlap(paddr_t region_start,
-                                    paddr_t region_size);
+                                    paddr_t region_size,
+                                    const struct membanks **out_overlapping_membanks);
 
 struct bootmodule *add_boot_module(bootmodule_kind kind,
                                    paddr_t start, paddr_t size, bool domU);
#3 lore raw

Re: [PATCH v5 1/3] xen/device-tree: Let DT reserve map entries overlap reserved-memory

In reply to Shawn Anastasio
Quoted 34 lines · Shawn Anastasio

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")
---
 xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++-----
 xen/common/device-tree/bootinfo.c | 11 +++++++++--
 xen/include/xen/bootfdt.h         |  3 ++-
 3 files changed, 34 insertions(+), 8 deletions(-)
xen/common/device-tree/bootfdt.c +1 -1
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
     {
         device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
         if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) )
+             check_reserved_regions_overlap(start, size, NULL) )
             return -EINVAL;
         /* Some DT may describe empty bank, ignore them */
         if ( !size )

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 593

Quoted 4 lines
xen/common/device-tree/bootfdt.c +1 -0
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
     if ( nr_rsvd < 0 )
         panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);

This should be moved before fdt_num_mem_rsv so that the program flow makes sense. In your case nr_rsvd is not used immediately after.

Quoted 14 lines
xen/common/device-tree/bootfdt.c +6 -0
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
     for ( i = 0; i < nr_rsvd; i++ )
     {
+        const struct membanks *overlap = NULL;
         struct membank *bank;
         paddr_t s, sz;
 
         if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
             continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 617

Quoted 10 lines
xen/common/device-tree/bootfdt.c +10 -0
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;

I think this is incorrect. Imagine this scenario:
/memreserve/ 0x40000000 0x40000000;
and /reserved-memory/foo with:
reg = <0x0 0x7FFFF000 0x0 0x1000>;

You would ignore the entire region described with /memreserve/ even though it overlaps just the last page.

The problem you're describing is about regions that match 1:1 in /memreserve/ and /reserved-memory/. Therefore I think you should check that the overlapped regions match exactly.

~Michal

#4 lore raw

Re: [PATCH v5 1/3] xen/device-tree: Let DT reserve map entries overlap reserved-memory

Hi All,

Quoted 73 lines · Michal Orzel
Quoted 34 lines · Shawn Anastasio

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")
---
  xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++-----
  xen/common/device-tree/bootinfo.c | 11 +++++++++--
  xen/include/xen/bootfdt.h         |  3 ++-
  3 files changed, 34 insertions(+), 8 deletions(-)
xen/common/device-tree/bootfdt.c +1 -1
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
      {
          device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
          if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) )
+             check_reserved_regions_overlap(start, size, NULL) )
              return -EINVAL;
          /* Some DT may describe empty bank, ignore them */
          if ( !size )

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 593

Quoted 4 lines
xen/common/device-tree/bootfdt.c +1 -0
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
      if ( nr_rsvd < 0 )
          panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);

This should be moved before fdt_num_mem_rsv so that the program flow makes sense. In your case nr_rsvd is not used immediately after.

Quoted 14 lines
xen/common/device-tree/bootfdt.c +6 -0
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
      for ( i = 0; i < nr_rsvd; i++ )
      {
+        const struct membanks *overlap = NULL;
          struct membank *bank;
          paddr_t s, sz;
 
          if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
              continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 617

Quoted 10 lines
xen/common/device-tree/bootfdt.c +10 -0
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;

I think this is incorrect. Imagine this scenario:
/memreserve/ 0x40000000 0x40000000;
and /reserved-memory/foo with:
reg = <0x0 0x7FFFF000 0x0 0x1000>;

Quoted 5 lines

You would ignore the entire region described with /memreserve/ even though it overlaps just the last page.

The problem you're describing is about regions that match 1:1 in /memreserve/ and /reserved-memory/. Therefore I think you should check that the overlapped regions match exactly.

I've also discovered an issue with Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") - the bootloader adds Initrd in
FDT reserved map which then conflicts with Initrd module (ARM64).

This patch, as is, doesn't fix an issue for me:

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) Region: [0x00000084000040, 0x00000086152ac6) overlapping with mod[2]: [0x00000084000040, 0x00000086152ac6) (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FDT reserve map overlapped with membanks/modules
(XEN) ****************************************

So I did fast try of Michal Orzel suggestion and it seems working for me. And if it's working for PPC - may be that's it (feel free to incorporate). Diff below.

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) RAM: 0000000048000000 - 00000000bfffffff
(XEN) RAM: 0000000480000000 - 00000004ffffffff
(XEN) RAM: 0000000600000000 - 00000006ffffffff
(XEN)
(XEN) MODULE[0]: 0000000048080000 - 00000000481ec000 Xen (XEN) MODULE[1]: 0000000048000000 - 000000004801e080 Device Tree (XEN) MODULE[2]: 0000000084000040 - 0000000086152ac6 Ramdisk (XEN) MODULE[3]: 0000000048300000 - 000000004a300000 Kernel (XEN) MODULE[4]: 0000000048070000 - 0000000048080000 XSM

(XEN)  RESVD[0]: 0000000060000000 - 000000007fffffff
(XEN)  RESVD[1]: 00000000b0000000 - 00000000bfffffff
(XEN)  RESVD[2]: 00000000a0000000 - 00000000afffffff

...
(XEN) *** LOADING DOMAIN 0 ***
(XEN) Loading d0 kernel from boot module @ 0000000048300000 (XEN) Loading ramdisk from boot module @ 0000000084000040 (XEN) Allocating 1:1 mappings totalling 256MB for dom0:
(XEN) BANK[0] 0x00000050000000-0x00000060000000 (256MB)
...

---

xen/common/device-tree/bootinfo.c +78 -0
index f2e6a1145b7c..10e997eeca8d 100644
@@ -124,6 +124,30 @@ static bool __init meminfo_overlap_check(const struct membanks *mem,
     return false;
 }
 
+static bool __init meminfo_is_exist(const struct membanks *mem,
+                                         paddr_t region_start,
+                                         paddr_t region_size)
+{
+    paddr_t bank_start = INVALID_PADDR, bank_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, bank_num = mem->nr_banks;
+
+    for ( i = 0; i < bank_num; i++ )
+    {
+        bank_start = mem->bank[i].start;
+        bank_end = bank_start + mem->bank[i].size;
+
+        if ( INVALID_PADDR == bank_start || region_end <= bank_start ||
+             region_start >= bank_end )
+            continue;
+
+        if ( region_start == bank_start && region_end  == bank_end)
+            return true;
+    }
+
+    return false;
+}
+
 /*
  * TODO: '*_end' could be 0 if the module/region is at the end of the physical
  * address space. This is for now not handled as it requires more rework.
@@ -154,6 +178,29 @@ static bool __init bootmodules_overlap_check(struct bootmodules *bootmodules,
     return false;
 }
 
+static bool __init bootmodules_is_exist(struct bootmodules *bootmodules,
+                                             paddr_t region_start,
+                                             paddr_t region_size)
+{
+    paddr_t mod_start = INVALID_PADDR, mod_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, mod_num = bootmodules->nr_mods;
+
+    for ( i = 0; i < mod_num; i++ )
+    {
+        mod_start = bootmodules->module[i].start;
+        mod_end = mod_start + bootmodules->module[i].size;
+
+        if ( region_end <= mod_start || region_start >= mod_end )
+            continue;
+
+        if (region_start == mod_start && region_end == mod_end)
+            return true;
+    }
+
+    return false;
+}
+
 void __init fw_unreserved_regions(paddr_t s, paddr_t e,
                                   void (*cb)(paddr_t ps, paddr_t pe),
                                   unsigned int first)
@@ -201,6 +248,37 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
     return false;
 }
 
+bool __init check_reserved_regions_is_exist(paddr_t region_start,
+                                            paddr_t region_size)
+{
+    const struct membanks *mem_banks[] = {
+        bootinfo_get_reserved_mem(),
+#ifdef CONFIG_ACPI
+        bootinfo_get_acpi(),
+#endif
+#ifdef CONFIG_STATIC_SHM
+        bootinfo_get_shmem(),
+#endif
+    };
+    unsigned int i;
+
+    /*
+     * Check if input region is overlapping with reserved memory banks or
+     * ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static
+     * shared memory banks (when static shared memory feature is enabled)
+     */
+    for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+        if ( meminfo_is_exist(mem_banks[i], region_start, region_size) )
+            return true;
+
+    /* Check if input region is overlapping with bootmodules */
+    if ( bootmodules_is_exist(&bootinfo.modules,
+                                   region_start, region_size) )
+        return true;
+
+    return false;
+}
+
 struct bootmodule __init *add_boot_module(bootmodule_kind kind,
                                           paddr_t start, paddr_t size,
                                           bool domU)
xen/include/xen/bootfdt.h +2 -0
index 16fa05f38f38..b8db1335be6c 100644
@@ -159,6 +159,8 @@ extern struct bootinfo bootinfo;
 
 bool check_reserved_regions_overlap(paddr_t region_start,
                                     paddr_t region_size);
+bool check_reserved_regions_is_exist(paddr_t region_start,
+                                     paddr_t region_size);
 
 struct bootmodule *add_boot_module(bootmodule_kind kind,
                                    paddr_t start, paddr_t size, bool domU);
#5 lore raw

Re: [PATCH v5 1/3] xen/device-tree: Let DT reserve map entries overlap reserved-memory

Quoted 234 lines · Grygorii Strashko

Hi All,

Quoted 73 lines · Michal Orzel
Quoted 34 lines · Shawn Anastasio

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")

---
  xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++----- xen/common/device-tree/bootinfo.c | 11 +++++++++-- xen/include/xen/bootfdt.h         |  3 ++-

  3 files changed, 34 insertions(+), 8 deletions(-)
xen/common/device-tree/bootfdt.c +0 -0
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
       {
           device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);

          if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) ) +             check_reserved_regions_overlap(start, size, NULL) ) return -EINVAL;
          /* Some DT may describe empty bank, ignore them */ if ( !size )

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 593

Quoted 4 lines
xen/common/device-tree/bootfdt.c +1 -0
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
       if ( nr_rsvd < 0 )
           panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);

This should be moved before fdt_num_mem_rsv so that the program flow makes sense. In your case nr_rsvd is not used immediately after.

Quoted 14 lines
xen/common/device-tree/bootfdt.c +6 -0
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
       for ( i = 0; i < nr_rsvd; i++ )
       {
+        const struct membanks *overlap = NULL;
           struct membank *bank;
           paddr_t s, sz;
 
           if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
               continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 617

Quoted 10 lines
xen/common/device-tree/bootfdt.c +10 -0
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;

I think this is incorrect. Imagine this scenario:
/memreserve/ 0x40000000 0x40000000;
and /reserved-memory/foo with:
reg = <0x0 0x7FFFF000 0x0 0x1000>;

Quoted 5 lines

You would ignore the entire region described with /memreserve/ even though it overlaps just the last page.

The problem you're describing is about regions that match 1:1 in /memreserve/ and /reserved-memory/. Therefore I think you should check that the overlapped regions match exactly.

I've also discovered an issue with Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") - the bootloader adds Initrd in
FDT reserved map which then conflicts with Initrd module (ARM64).

This patch, as is, doesn't fix an issue for me:

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) Region: [0x00000084000040, 0x00000086152ac6) overlapping with mod[2]: [0x00000084000040, 0x00000086152ac6) (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FDT reserve map overlapped with membanks/modules
(XEN) ****************************************

So I did fast try of Michal Orzel suggestion and it seems working for me. And if it's working for PPC - may be that's it (feel free to incorporate). Diff below.

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) RAM: 0000000048000000 - 00000000bfffffff
(XEN) RAM: 0000000480000000 - 00000004ffffffff
(XEN) RAM: 0000000600000000 - 00000006ffffffff
(XEN)
(XEN) MODULE[0]: 0000000048080000 - 00000000481ec000 Xen (XEN) MODULE[1]: 0000000048000000 - 000000004801e080 Device Tree (XEN) MODULE[2]: 0000000084000040 - 0000000086152ac6 Ramdisk (XEN) MODULE[3]: 0000000048300000 - 000000004a300000 Kernel (XEN) MODULE[4]: 0000000048070000 - 0000000048080000 XSM (XEN)  RESVD[0]: 0000000060000000 - 000000007fffffff (XEN)  RESVD[1]: 00000000b0000000 - 00000000bfffffff (XEN)  RESVD[2]: 00000000a0000000 - 00000000afffffff
...
(XEN) *** LOADING DOMAIN 0 ***
(XEN) Loading d0 kernel from boot module @ 0000000048300000 (XEN) Loading ramdisk from boot module @ 0000000084000040 (XEN) Allocating 1:1 mappings totalling 256MB for dom0:
(XEN) BANK[0] 0x00000050000000-0x00000060000000 (256MB)
...

---

xen/common/device-tree/bootinfo.c +78 -0
index f2e6a1145b7c..10e997eeca8d 100644
@@ -124,6 +124,30 @@ static bool __init meminfo_overlap_check(const struct membanks *mem,
      return false;
  }
 
+static bool __init meminfo_is_exist(const struct membanks *mem,
+                                         paddr_t region_start,
+                                         paddr_t region_size)
+{
+    paddr_t bank_start = INVALID_PADDR, bank_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, bank_num = mem->nr_banks;
+
+    for ( i = 0; i < bank_num; i++ )
+    {
+        bank_start = mem->bank[i].start;
+        bank_end = bank_start + mem->bank[i].size;
+
+        if ( INVALID_PADDR == bank_start || region_end <= bank_start ||
+             region_start >= bank_end )
+            continue;
+
+        if ( region_start == bank_start && region_end  == bank_end)
+            return true;
+    }
+
+    return false;
+}
+
  /*
   * TODO: '*_end' could be 0 if the module/region is at the end of the physical
   * address space. This is for now not handled as it requires more rework.
@@ -154,6 +178,29 @@ static bool __init bootmodules_overlap_check(struct bootmodules *bootmodules,
      return false;
  }
 
+static bool __init bootmodules_is_exist(struct bootmodules *bootmodules,
+                                             paddr_t region_start,
+                                             paddr_t region_size)
+{
+    paddr_t mod_start = INVALID_PADDR, mod_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, mod_num = bootmodules->nr_mods;
+
+    for ( i = 0; i < mod_num; i++ )
+    {
+        mod_start = bootmodules->module[i].start;
+        mod_end = mod_start + bootmodules->module[i].size;
+
+        if ( region_end <= mod_start || region_start >= mod_end )
+            continue;
+
+        if (region_start == mod_start && region_end == mod_end)
+            return true;
+    }
+
+    return false;
+}
+
  void __init fw_unreserved_regions(paddr_t s, paddr_t e,
                                    void (*cb)(paddr_t ps, paddr_t pe),
                                    unsigned int first)
@@ -201,6 +248,37 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
      return false;
  }
 
+bool __init check_reserved_regions_is_exist(paddr_t region_start,
+                                            paddr_t region_size)
+{
+    const struct membanks *mem_banks[] = {
+        bootinfo_get_reserved_mem(),
+#ifdef CONFIG_ACPI
+        bootinfo_get_acpi(),
+#endif
+#ifdef CONFIG_STATIC_SHM
+        bootinfo_get_shmem(),
+#endif
+    };
+    unsigned int i;
+
+    /*
+     * Check if input region is overlapping with reserved memory banks or
+     * ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static
+     * shared memory banks (when static shared memory feature is enabled)
+     */
+    for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+        if ( meminfo_is_exist(mem_banks[i], region_start, region_size) )
+            return true;
+
+    /* Check if input region is overlapping with bootmodules */
+    if ( bootmodules_is_exist(&bootinfo.modules,
+                                   region_start, region_size) )
+        return true;
+
+    return false;
+}
+
  struct bootmodule __init *add_boot_module(bootmodule_kind kind,
                                            paddr_t start, paddr_t size,
                                            bool domU)
xen/include/xen/bootfdt.h +0 -0
index 16fa05f38f38..b8db1335be6c 100644

xen/include/xen/bootfdt.h extern struct bootinfo bootinfo; · line 166

Quoted 8 lines
xen/include/xen/bootfdt.h +2 -0
@@ -159,6 +159,8 @@ extern struct bootinfo bootinfo;
 
  bool check_reserved_regions_overlap(paddr_t region_start,
                                      paddr_t region_size);
+bool check_reserved_regions_is_exist(paddr_t region_start,
+                                     paddr_t region_size);
 
  struct bootmodule *add_boot_module(bootmodule_kind kind,
                                     paddr_t start, paddr_t size, bool domU);

Sorry missed part of the diff:

xen/common/device-tree/bootfdt.c +7 -4
index 927f59c64b0d..18816b6f08f1 100644
@@ -586,6 +586,10 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
     add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
     nr_rsvd = fdt_num_mem_rsv(fdt);
     if ( nr_rsvd < 0 )
         panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
@@ -598,6 +602,9 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
         if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
             continue;
 
+        if (check_reserved_regions_is_exist(s, sz))
+            continue;
+
         if ( reserved_mem->nr_banks < reserved_mem->max_banks )
         {
             bank = &reserved_mem->bank[reserved_mem->nr_banks];
@@ -610,10 +617,6 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
             panic("Cannot allocate reserved memory bank\n");
     }
 
-    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
-    if ( ret )
-        panic("Early FDT parsing failed (%d)\n", ret);
-
     /*
      * On Arm64 setup_directmap_mappings() expects to be called with the lowest
      * bank in memory first. There is no requirement that the DT will provide
#6 lore raw

Re: [PATCH v5 1/3] xen/device-tree: Let DT reserve map entries overlap reserved-memory

In reply to Grygorii Strashko
Quoted 238 lines · Grygorii Strashko

Hi All,

Quoted 73 lines · Michal Orzel
Quoted 34 lines · Shawn Anastasio

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")
---
  xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++-----
  xen/common/device-tree/bootinfo.c | 11 +++++++++--
  xen/include/xen/bootfdt.h         |  3 ++-
  3 files changed, 34 insertions(+), 8 deletions(-)
xen/common/device-tree/bootfdt.c +1 -1
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
      {
          device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
          if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) )
+             check_reserved_regions_overlap(start, size, NULL) )
              return -EINVAL;
          /* Some DT may describe empty bank, ignore them */
          if ( !size )

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 593

Quoted 4 lines
xen/common/device-tree/bootfdt.c +1 -0
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
      if ( nr_rsvd < 0 )
          panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);

This should be moved before fdt_num_mem_rsv so that the program flow makes sense. In your case nr_rsvd is not used immediately after.

Quoted 14 lines
xen/common/device-tree/bootfdt.c +6 -0
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
      for ( i = 0; i < nr_rsvd; i++ )
      {
+        const struct membanks *overlap = NULL;
          struct membank *bank;
          paddr_t s, sz;
 
          if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
              continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 617

Quoted 10 lines
xen/common/device-tree/bootfdt.c +10 -0
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;

I think this is incorrect. Imagine this scenario:
/memreserve/ 0x40000000 0x40000000;
and /reserved-memory/foo with:
reg = <0x0 0x7FFFF000 0x0 0x1000>;

Quoted 5 lines

You would ignore the entire region described with /memreserve/ even though it overlaps just the last page.

The problem you're describing is about regions that match 1:1 in /memreserve/ and /reserved-memory/. Therefore I think you should check that the overlapped regions match exactly.

I've also discovered an issue with Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") - the bootloader adds Initrd in
FDT reserved map which then conflicts with Initrd module (ARM64).

This patch, as is, doesn't fix an issue for me:

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) Region: [0x00000084000040, 0x00000086152ac6) overlapping with mod[2]: [0x00000084000040, 0x00000086152ac6) (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FDT reserve map overlapped with membanks/modules
(XEN) ****************************************

So I did fast try of Michal Orzel suggestion and it seems working for me. And if it's working for PPC - may be that's it (feel free to incorporate). Diff below.

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) RAM: 0000000048000000 - 00000000bfffffff
(XEN) RAM: 0000000480000000 - 00000004ffffffff
(XEN) RAM: 0000000600000000 - 00000006ffffffff
(XEN)
(XEN) MODULE[0]: 0000000048080000 - 00000000481ec000 Xen (XEN) MODULE[1]: 0000000048000000 - 000000004801e080 Device Tree (XEN) MODULE[2]: 0000000084000040 - 0000000086152ac6 Ramdisk (XEN) MODULE[3]: 0000000048300000 - 000000004a300000 Kernel (XEN) MODULE[4]: 0000000048070000 - 0000000048080000 XSM

(XEN)  RESVD[0]: 0000000060000000 - 000000007fffffff
(XEN)  RESVD[1]: 00000000b0000000 - 00000000bfffffff
(XEN)  RESVD[2]: 00000000a0000000 - 00000000afffffff

...
(XEN) *** LOADING DOMAIN 0 ***
(XEN) Loading d0 kernel from boot module @ 0000000048300000 (XEN) Loading ramdisk from boot module @ 0000000084000040 (XEN) Allocating 1:1 mappings totalling 256MB for dom0:
(XEN) BANK[0] 0x00000050000000-0x00000060000000 (256MB)
...

---

xen/common/device-tree/bootinfo.c +78 -0
index f2e6a1145b7c..10e997eeca8d 100644
@@ -124,6 +124,30 @@ static bool __init meminfo_overlap_check(const struct membanks *mem,
      return false;
  }
 
+static bool __init meminfo_is_exist(const struct membanks *mem,
+                                         paddr_t region_start,
+                                         paddr_t region_size)
+{
+    paddr_t bank_start = INVALID_PADDR, bank_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, bank_num = mem->nr_banks;
+
+    for ( i = 0; i < bank_num; i++ )
+    {
+        bank_start = mem->bank[i].start;
+        bank_end = bank_start + mem->bank[i].size;
+
+        if ( INVALID_PADDR == bank_start || region_end <= bank_start ||
+             region_start >= bank_end )
+            continue;
+
+        if ( region_start == bank_start && region_end  == bank_end)
+            return true;
+    }
+
+    return false;
+}
+
  /*
   * TODO: '*_end' could be 0 if the module/region is at the end of the physical
   * address space. This is for now not handled as it requires more rework.
@@ -154,6 +178,29 @@ static bool __init bootmodules_overlap_check(struct bootmodules *bootmodules,
      return false;
  }
 
+static bool __init bootmodules_is_exist(struct bootmodules *bootmodules,
+                                             paddr_t region_start,
+                                             paddr_t region_size)
+{
+    paddr_t mod_start = INVALID_PADDR, mod_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, mod_num = bootmodules->nr_mods;
+
+    for ( i = 0; i < mod_num; i++ )
+    {
+        mod_start = bootmodules->module[i].start;
+        mod_end = mod_start + bootmodules->module[i].size;
+
+        if ( region_end <= mod_start || region_start >= mod_end )
+            continue;
+
+        if (region_start == mod_start && region_end == mod_end)
+            return true;
+    }
+
+    return false;
+}
+
  void __init fw_unreserved_regions(paddr_t s, paddr_t e,
                                    void (*cb)(paddr_t ps, paddr_t pe),
                                    unsigned int first)
@@ -201,6 +248,37 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
      return false;
  }
 
+bool __init check_reserved_regions_is_exist(paddr_t region_start,
+                                            paddr_t region_size)
+{
+    const struct membanks *mem_banks[] = {
+        bootinfo_get_reserved_mem(),
+#ifdef CONFIG_ACPI
+        bootinfo_get_acpi(),
+#endif
+#ifdef CONFIG_STATIC_SHM
+        bootinfo_get_shmem(),
+#endif
+    };
+    unsigned int i;
+
+    /*
+     * Check if input region is overlapping with reserved memory banks or
+     * ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static
+     * shared memory banks (when static shared memory feature is enabled)
+     */
+    for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+        if ( meminfo_is_exist(mem_banks[i], region_start, region_size) )
+            return true;
+
+    /* Check if input region is overlapping with bootmodules */
+    if ( bootmodules_is_exist(&bootinfo.modules,
+                                   region_start, region_size) )
+        return true;
+
+    return false;
+}
+
  struct bootmodule __init *add_boot_module(bootmodule_kind kind,
                                            paddr_t start, paddr_t size,
                                            bool domU)
xen/include/xen/bootfdt.h +0 -0
index 16fa05f38f38..b8db1335be6c 100644

xen/include/xen/bootfdt.h extern struct bootinfo bootinfo; · line 166

Quoted 8 lines
xen/include/xen/bootfdt.h +2 -0
@@ -159,6 +159,8 @@ extern struct bootinfo bootinfo;
 
  bool check_reserved_regions_overlap(paddr_t region_start,
                                      paddr_t region_size);
+bool check_reserved_regions_is_exist(paddr_t region_start,
+                                     paddr_t region_size);
 
  struct bootmodule *add_boot_module(bootmodule_kind kind,
                                     paddr_t start, paddr_t size, bool domU);

I don't think there is a need for introduction of that many functions. For a simple exact matching case we can opencode the logic a bit. On top of Shawn patch, the minimal version would look as follows:

xen/common/device-tree/bootfdt.c +29 -15
index d35b2629e5a1..759c790888f9 100644
@@ -586,14 +586,14 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
     add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
 
-    nr_rsvd = fdt_num_mem_rsv(fdt);
-    if ( nr_rsvd < 0 )
-        panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
-
     ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
     if ( ret )
         panic("Early FDT parsing failed (%d)\n", ret);
 
+    nr_rsvd = fdt_num_mem_rsv(fdt);
+    if ( nr_rsvd < 0 )
+        panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
+
     for ( i = 0; i < nr_rsvd; i++ )
     {
         const struct membanks *overlap = NULL;
@@ -605,19 +605,33 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
         if ( check_reserved_regions_overlap(s, sz, &overlap) )
         {
-            if ( overlap == bootinfo_get_reserved_mem() )
+            unsigned int j;
+            bool match = false;
+
+            if ( overlap != reserved_mem )
+                panic("FDT reserve map overlapped with membanks/modules\n");
+
+            /*
+             * Some valid device trees, such as those generated by OpenPOWER
+             * skiboot firmware, expose all reserved memory regions in the
+             * FDT memory reservation block (here) AND in the
+             * reserved-memory node which has already been parsed. Thus, any
+             * matching overlaps in the mem_reserved banks should be ignored.
+             */
+            for ( j = 0; j < overlap->nr_banks; j++ )
             {
-                /*
-                 * Some valid device trees, such as those generated by OpenPOWER
-                 * skiboot firmware, expose all reserved memory regions in the
-                 * FDT memory reservation block (here) AND in the
-                 * reserved-memory node which has already been parsed. Thus, any
-                 * overlaps in the mem_reserved banks should be ignored.
-                 */
-                 continue;
+                if ( (overlap->bank[j].start == s) &&
+                     (overlap->bank[j].size == sz) )
+                {
+                    match = true;
+                    break;
+                }
             }
-            else
-                panic("FDT reserve map overlapped with membanks/modules\n");
+
+            if ( match )
+                continue;
+
+            panic("FDT reserve map partially overlaps with /reserved-memory\n");
         }
 
         if ( reserved_mem->nr_banks < reserved_mem->max_banks )

Let's wait for Shawn test and other DT maintainers opinion.

~Michal

#7 lore raw

Re: [PATCH v5 1/3] xen/device-tree: Let DT reserve map entries overlap reserved-memory

Quoted 277 lines · Michal Orzel
Quoted 238 lines · Grygorii Strashko

Hi All,

Quoted 73 lines · Michal Orzel
Quoted 34 lines · Shawn Anastasio

Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") changes the way reserve map regions are tracked, and as a result broke bootfdt's ability to handle device trees in which the reserve map and the reserved-memory node contain the same entries as each other, as is the case on PPC when booted by skiboot.

Fix this behavior by moving the reserve map check to after the DT has been parsed and by explicitly allowing overlap with entries created by reserved-memory nodes.

Fixes 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem")
---
   xen/common/device-tree/bootfdt.c  | 28 +++++++++++++++++++++++-----
   xen/common/device-tree/bootinfo.c | 11 +++++++++--
   xen/include/xen/bootfdt.h         |  3 ++-
   3 files changed, 34 insertions(+), 8 deletions(-)
xen/common/device-tree/bootfdt.c +1 -1
index 911a630e7d..2a51ee44a3 100644
@@ -177,7 +177,7 @@ static int __init device_tree_get_meminfo(const void *fdt, int node,
       {
           device_tree_get_reg(&cell, address_cells, size_cells, &start, &size);
           if ( mem == bootinfo_get_reserved_mem() &&
-             check_reserved_regions_overlap(start, size) )
+             check_reserved_regions_overlap(start, size, NULL) )
               return -EINVAL;
           /* Some DT may describe empty bank, ignore them */
           if ( !size )

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 593

Quoted 4 lines
xen/common/device-tree/bootfdt.c +1 -0
@@ -590,14 +590,36 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
       if ( nr_rsvd < 0 )
           panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
 
+    ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);

This should be moved before fdt_num_mem_rsv so that the program flow makes sense. In your case nr_rsvd is not used immediately after.

Quoted 14 lines
xen/common/device-tree/bootfdt.c +6 -0
+    if ( ret )
+        panic("Early FDT parsing failed (%d)\n", ret);
+
       for ( i = 0; i < nr_rsvd; i++ )
       {
+        const struct membanks *overlap = NULL;
           struct membank *bank;
           paddr_t s, sz;
 
           if ( fdt_get_mem_rsv_paddr(device_tree_flattened, i, &s, &sz) < 0 )
               continue;
 
+        if ( check_reserved_regions_overlap(s, sz, &overlap) )
+        {

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 617

Quoted 10 lines
xen/common/device-tree/bootfdt.c +10 -0
+            if ( overlap == bootinfo_get_reserved_mem() )
+            {
+                /*
+                 * Some valid device trees, such as those generated by OpenPOWER
+                 * skiboot firmware, expose all reserved memory regions in the
+                 * FDT memory reservation block (here) AND in the
+                 * reserved-memory node which has already been parsed. Thus, any
+                 * overlaps in the mem_reserved banks should be ignored.
+                 */
+                 continue;

I think this is incorrect. Imagine this scenario:
/memreserve/ 0x40000000 0x40000000;
and /reserved-memory/foo with:
reg = <0x0 0x7FFFF000 0x0 0x1000>;

Quoted 5 lines

You would ignore the entire region described with /memreserve/ even though it overlaps just the last page.

The problem you're describing is about regions that match 1:1 in /memreserve/ and /reserved-memory/. Therefore I think you should check that the overlapped regions match exactly.

I've also discovered an issue with Commit 53dc37829c31 ("xen/arm: Add DT reserve map regions to bootinfo.reserved_mem") - the bootloader adds Initrd in
FDT reserved map which then conflicts with Initrd module (ARM64).

This patch, as is, doesn't fix an issue for me:

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) Region: [0x00000084000040, 0x00000086152ac6) overlapping with mod[2]: [0x00000084000040, 0x00000086152ac6) (XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) FDT reserve map overlapped with membanks/modules
(XEN) ****************************************

So I did fast try of Michal Orzel suggestion and it seems working for me. And if it's working for PPC - may be that's it (feel free to incorporate). Diff below.

(XEN) Checking for initrd in /chosen
(XEN) Initrd 0000000084000040-0000000086152ac6
(XEN) RAM: 0000000048000000 - 00000000bfffffff
(XEN) RAM: 0000000480000000 - 00000004ffffffff
(XEN) RAM: 0000000600000000 - 00000006ffffffff
(XEN)
(XEN) MODULE[0]: 0000000048080000 - 00000000481ec000 Xen (XEN) MODULE[1]: 0000000048000000 - 000000004801e080 Device Tree (XEN) MODULE[2]: 0000000084000040 - 0000000086152ac6 Ramdisk (XEN) MODULE[3]: 0000000048300000 - 000000004a300000 Kernel (XEN) MODULE[4]: 0000000048070000 - 0000000048080000 XSM

(XEN)  RESVD[0]: 0000000060000000 - 000000007fffffff
(XEN)  RESVD[1]: 00000000b0000000 - 00000000bfffffff
(XEN)  RESVD[2]: 00000000a0000000 - 00000000afffffff

...
(XEN) *** LOADING DOMAIN 0 ***
(XEN) Loading d0 kernel from boot module @ 0000000048300000 (XEN) Loading ramdisk from boot module @ 0000000084000040 (XEN) Allocating 1:1 mappings totalling 256MB for dom0:
(XEN) BANK[0] 0x00000050000000-0x00000060000000 (256MB)
...

---

xen/common/device-tree/bootinfo.c +78 -0
index f2e6a1145b7c..10e997eeca8d 100644
@@ -124,6 +124,30 @@ static bool __init meminfo_overlap_check(const struct membanks *mem,
       return false;
   }
 
+static bool __init meminfo_is_exist(const struct membanks *mem,
+                                         paddr_t region_start,
+                                         paddr_t region_size)
+{
+    paddr_t bank_start = INVALID_PADDR, bank_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, bank_num = mem->nr_banks;
+
+    for ( i = 0; i < bank_num; i++ )
+    {
+        bank_start = mem->bank[i].start;
+        bank_end = bank_start + mem->bank[i].size;
+
+        if ( INVALID_PADDR == bank_start || region_end <= bank_start ||
+             region_start >= bank_end )
+            continue;
+
+        if ( region_start == bank_start && region_end  == bank_end)
+            return true;
+    }
+
+    return false;
+}
+
   /*
    * TODO: '*_end' could be 0 if the module/region is at the end of the physical
    * address space. This is for now not handled as it requires more rework.
@@ -154,6 +178,29 @@ static bool __init bootmodules_overlap_check(struct bootmodules *bootmodules,
       return false;
   }
 
+static bool __init bootmodules_is_exist(struct bootmodules *bootmodules,
+                                             paddr_t region_start,
+                                             paddr_t region_size)
+{
+    paddr_t mod_start = INVALID_PADDR, mod_end = 0;
+    paddr_t region_end = region_start + region_size;
+    unsigned int i, mod_num = bootmodules->nr_mods;
+
+    for ( i = 0; i < mod_num; i++ )
+    {
+        mod_start = bootmodules->module[i].start;
+        mod_end = mod_start + bootmodules->module[i].size;
+
+        if ( region_end <= mod_start || region_start >= mod_end )
+            continue;
+
+        if (region_start == mod_start && region_end == mod_end)
+            return true;
+    }
+
+    return false;
+}
+
   void __init fw_unreserved_regions(paddr_t s, paddr_t e,
                                     void (*cb)(paddr_t ps, paddr_t pe),
                                     unsigned int first)
@@ -201,6 +248,37 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
       return false;
   }
 
+bool __init check_reserved_regions_is_exist(paddr_t region_start,
+                                            paddr_t region_size)
+{
+    const struct membanks *mem_banks[] = {
+        bootinfo_get_reserved_mem(),
+#ifdef CONFIG_ACPI
+        bootinfo_get_acpi(),
+#endif
+#ifdef CONFIG_STATIC_SHM
+        bootinfo_get_shmem(),
+#endif
+    };
+    unsigned int i;
+
+    /*
+     * Check if input region is overlapping with reserved memory banks or
+     * ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static
+     * shared memory banks (when static shared memory feature is enabled)
+     */
+    for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+        if ( meminfo_is_exist(mem_banks[i], region_start, region_size) )
+            return true;
+
+    /* Check if input region is overlapping with bootmodules */
+    if ( bootmodules_is_exist(&bootinfo.modules,
+                                   region_start, region_size) )
+        return true;
+
+    return false;
+}
+
   struct bootmodule __init *add_boot_module(bootmodule_kind kind,
                                             paddr_t start, paddr_t size,
                                             bool domU)
xen/include/xen/bootfdt.h +0 -0
index 16fa05f38f38..b8db1335be6c 100644

xen/include/xen/bootfdt.h extern struct bootinfo bootinfo; · line 166

Quoted 8 lines
xen/include/xen/bootfdt.h +2 -0
@@ -159,6 +159,8 @@ extern struct bootinfo bootinfo;
 
   bool check_reserved_regions_overlap(paddr_t region_start,
                                       paddr_t region_size);
+bool check_reserved_regions_is_exist(paddr_t region_start,
+                                     paddr_t region_size);
 
   struct bootmodule *add_boot_module(bootmodule_kind kind,
                                      paddr_t start, paddr_t size, bool domU);

I don't think there is a need for introduction of that many functions. For a simple exact matching case we can opencode the logic a bit. On top of Shawn patch, the minimal version would look as follows:

xen/common/device-tree/bootfdt.c +4 -4
index d35b2629e5a1..759c790888f9 100644
@@ -586,14 +586,14 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
      add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
 
-    nr_rsvd = fdt_num_mem_rsv(fdt);
-    if ( nr_rsvd < 0 )
-        panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
-
      ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
      if ( ret )
          panic("Early FDT parsing failed (%d)\n", ret);
 
+    nr_rsvd = fdt_num_mem_rsv(fdt);
+    if ( nr_rsvd < 0 )
+        panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
+
      for ( i = 0; i < nr_rsvd; i++ )
      {
          const struct membanks *overlap = NULL;

xen/common/device-tree/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 612

Quoted 9 lines
xen/common/device-tree/bootfdt.c +5 -1
@@ -605,19 +605,33 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
          if ( check_reserved_regions_overlap(s, sz, &overlap) )
          {
-            if ( overlap == bootinfo_get_reserved_mem() )
+            unsigned int j;
+            bool match = false;
+
+            if ( overlap != reserved_mem )
+                panic("FDT reserve map overlapped with membanks/modules\n");

No it will not work this way - as overlap I observe is with Initrd which is *Module* (Not reserved memory). That's why i've had to add new functions.

Quoted 40 lines
xen/common/device-tree/bootfdt.c +20 -10
+
+            /*
+             * Some valid device trees, such as those generated by OpenPOWER
+             * skiboot firmware, expose all reserved memory regions in the
+             * FDT memory reservation block (here) AND in the
+             * reserved-memory node which has already been parsed. Thus, any
+             * matching overlaps in the mem_reserved banks should be ignored.
+             */
+            for ( j = 0; j < overlap->nr_banks; j++ )
              {
-                /*
-                 * Some valid device trees, such as those generated by OpenPOWER
-                 * skiboot firmware, expose all reserved memory regions in the
-                 * FDT memory reservation block (here) AND in the
-                 * reserved-memory node which has already been parsed. Thus, any
-                 * overlaps in the mem_reserved banks should be ignored.
-                 */
-                 continue;
+                if ( (overlap->bank[j].start == s) &&
+                     (overlap->bank[j].size == sz) )
+                {
+                    match = true;
+                    break;
+                }
              }
-            else
-                panic("FDT reserve map overlapped with membanks/modules\n");
+
+            if ( match )
+                continue;
+
+            panic("FDT reserve map partially overlaps with /reserved-memory\n");
          }
 
          if ( reserved_mem->nr_banks < reserved_mem->max_banks )

Let's wait for Shawn test and other DT maintainers opinion.

~Michal

#8 lore raw

PATCH v5 2/3 xen/ppc: Enable bootfdt and boot allocator

In reply to Shawn Anastasio

Enable usage of bootfdt for populating the boot info struct from the firmware-provided device tree. Also enable the Xen boot page allocator.

Additionally, modify bootfdt.c's boot_fdt_info() to tolerate the scenario in which the FDT overlaps a reserved memory region, as is the case on PPC when booted directly from skiboot. Since this means that Xen can now boot without a BOOTMOD_FDT present in bootinfo, clarify this fact in a comment above BOOTMOD_FDT's definition.

---
Changes in v5:
- Drop setup.c's unnecessary boot_fdt variable per Julien's suggestion

 xen/arch/ppc/setup.c             | 20 +++++++++++++++++++-
 xen/common/device-tree/bootfdt.c | 11 +++++++++--
 2 files changed, 28 insertions(+), 3 deletions(-)
xen/arch/ppc/setup.c +19 -1
index 5fb5ab64e3..17bc1aed34 100644
@@ -1,4 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <xen/bootfdt.h>
+#include <xen/device_tree.h>
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/mm.h>
@@ -9,6 +11,7 @@
 #include <asm/early_printk.h>
 #include <asm/mm.h>
 #include <asm/processor.h>
+#include <asm/setup.h>
 
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
@@ -26,6 +29,8 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
                                unsigned long r5, unsigned long r6,
                                unsigned long r7)
 {
+    const struct bootmodule *xen_bootmodule;
+
     if ( r5 )
     {
         /* Unsupported OpenFirmware boot protocol */
@@ -34,11 +39,24 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
     else
     {
         /* kexec boot protocol */
-        boot_opal_init((void *)r3);
+        device_tree_flattened = (void *)r3;
+        boot_opal_init(device_tree_flattened);
     }
 
     setup_exceptions();
 
+    boot_fdt_info(device_tree_flattened, r3);
+
+    /*
+     * Xen relocates itself at the ppc64 entrypoint, so we need to manually mark
+     * the kernel module.
+     */
+    xen_bootmodule = add_boot_module(BOOTMOD_XEN, __pa(_start),
+                                     PAGE_ALIGN(__pa(_end)), false);
+    BUG_ON(!xen_bootmodule);
+
+    populate_boot_allocator();
+
     setup_initial_pagetables();
 
     init_constructors();
xen/common/device-tree/bootfdt.c +9 -2
index 2a51ee44a3..5facd683b8 100644
@@ -584,8 +584,6 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
     if ( ret < 0 )
         panic("No valid device tree\n");
 
-    add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
-
     nr_rsvd = fdt_num_mem_rsv(fdt);
     if ( nr_rsvd < 0 )
         panic("Parsing FDT memory reserve map failed (%d)\n", nr_rsvd);
@@ -632,6 +630,15 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
             panic("Cannot allocate reserved memory bank\n");
     }
 
+    /*
+     * Add module for the FDT itself after the device tree has been parsed. This
+     * is required on ppc64le where the device tree passed to Xen may have been
+     * allocated by skiboot, in which case it will exist within a reserved
+     * region and this call will fail. This is fine, however, since either way
+     * the allocator will know not to step on the device tree.
+     */
+    (void)add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
+
     /*
      * On Arm64 setup_directmap_mappings() expects to be called with the lowest
      * bank in memory first. There is no requirement that the DT will provide

--
2.30.2

#9 lore raw

PATCH v5 3/3 xen/ppc: mm-radix: Allocate all paging structures at runtime

In reply to Shawn Anastasio

In the initial mm-radix implementation, the in-memory partition and process tables required to configure the MMU, as well as the page tables themselves were all allocated statically since the boot allocator was not yet available.

Now that it is, allocate these structures at runtime and bump the size of the Process Table to its maximum supported value (on POWER9).

---
Changes in v5:
- Add more clarification comments to min/max mfn variable decls.

Changes in v4:
- use mfn_add in initial_page_alloc()
- zero pages returned by initial_page_alloc()

 xen/arch/ppc/mm-radix.c | 238 ++++++++++++++++++++++------------------
 1 file changed, 130 insertions(+), 108 deletions(-)
xen/arch/ppc/mm-radix.c +130 -108
index 24232f3907..e3bf41675b 100644
@@ -22,69 +22,112 @@ void enable_mmu(void);
 #define radix_dprintk(...)
 #endif
 
-#define INITIAL_LVL1_PD_COUNT      1
-#define INITIAL_LVL2_LVL3_PD_COUNT 2
-#define INITIAL_LVL4_PT_COUNT      256
-
-static size_t __initdata initial_lvl1_pd_pool_used;
-static struct lvl1_pd initial_lvl1_pd_pool[INITIAL_LVL1_PD_COUNT];
-
-static size_t __initdata initial_lvl2_lvl3_pd_pool_used;
-static struct lvl2_pd initial_lvl2_lvl3_pd_pool[INITIAL_LVL2_LVL3_PD_COUNT];
-
-static size_t __initdata initial_lvl4_pt_pool_used;
-static struct lvl4_pt initial_lvl4_pt_pool[INITIAL_LVL4_PT_COUNT];
-
-/* Only reserve minimum Partition and Process tables  */
 #define PATB_SIZE_LOG2 16 /* Only supported partition table size on POWER9 */
 #define PATB_SIZE      (1UL << PATB_SIZE_LOG2)
-#define PRTB_SIZE_LOG2 12
+#define PRTB_SIZE_LOG2 24 /* Maximum process table size on POWER9 */
 #define PRTB_SIZE      (1UL << PRTB_SIZE_LOG2)
 
-static struct patb_entry
-    __aligned(PATB_SIZE) initial_patb[PATB_SIZE / sizeof(struct patb_entry)];
+static struct patb_entry *initial_patb;
+static struct prtb_entry *initial_prtb;
 
-static struct prtb_entry
-    __aligned(PRTB_SIZE) initial_prtb[PRTB_SIZE / sizeof(struct prtb_entry)];
+/*
+ * The highest and lowest mfns returned by initial_page_alloc. In order to
+ * bootstrap the xen's initial page tables, the mfns allocated to store the
+ * paging structurs needs to be tracked so that they can be included in the
+ * mapping. This is most simply accomplished by keeping track of the minimum
+ * and maximum mfn numbers so that the whole range from min->max can be mapped.
+ */
+static mfn_t __initdata min_alloc_mfn = {-1};
+static mfn_t __initdata max_alloc_mfn = {0};
 
-static __init struct lvl1_pd *lvl1_pd_pool_alloc(void)
+/*
+ * A thin wrapper for alloc_boot_pages that keeps track of the maximum and
+ * minimum mfns that have been allocated. This information is used by
+ * setup_initial_mapping to include the allocated pages in the initial
+ * page mapping.
+ *
+ * Additionally, allocated pages are zeroed before return.
+ */
+static mfn_t __init initial_page_alloc(unsigned long nr_pfns,
+                                       unsigned long pfn_align)
 {
-    if ( initial_lvl1_pd_pool_used >= INITIAL_LVL1_PD_COUNT )
-    {
-        early_printk("Ran out of space for LVL1 PD!\n");
-        die();
-    }
+    mfn_t mfn_first, mfn_last;
 
-    return &initial_lvl1_pd_pool[initial_lvl1_pd_pool_used++];
-}
+    mfn_first = alloc_boot_pages(nr_pfns, pfn_align);
+    mfn_last = mfn_add(mfn_first, nr_pfns - 1);
 
-static __init struct lvl2_pd *lvl2_pd_pool_alloc(void)
-{
-    if ( initial_lvl2_lvl3_pd_pool_used >= INITIAL_LVL2_LVL3_PD_COUNT )
-    {
-        early_printk("Ran out of space for LVL2/3 PD!\n");
-        die();
-    }
+    min_alloc_mfn = _mfn(min(mfn_x(min_alloc_mfn), mfn_x(mfn_first)));
+    max_alloc_mfn = _mfn(max(mfn_x(max_alloc_mfn), mfn_x(mfn_last)));
+
+    memset(__va(mfn_to_maddr(mfn_first)), 0, nr_pfns << PAGE_SHIFT);
 
-    return &initial_lvl2_lvl3_pd_pool[initial_lvl2_lvl3_pd_pool_used++];
+    return mfn_first;
 }
 
-static __init struct lvl3_pd *lvl3_pd_pool_alloc(void)
+static __init void *initial_pd_pt_alloc(void)
 {
-    BUILD_BUG_ON(sizeof(struct lvl3_pd) != sizeof(struct lvl2_pd));
+    BUILD_BUG_ON(sizeof(struct lvl1_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl2_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl3_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl4_pt) > PAGE_SIZE);
 
-    return (struct lvl3_pd *) lvl2_pd_pool_alloc();
+    return __va(mfn_to_maddr(initial_page_alloc(1, 1)));
 }
 
-static __init struct lvl4_pt *lvl4_pt_pool_alloc(void)
+static void map_page_initial(struct lvl1_pd *lvl1, vaddr_t virt, paddr_t phys,
+                             unsigned long flags)
 {
-    if ( initial_lvl4_pt_pool_used >= INITIAL_LVL4_PT_COUNT )
+    struct lvl2_pd *lvl2;
+    struct lvl3_pd *lvl3;
+    struct lvl4_pt *lvl4;
+    pde_t *pde;
+    pte_t *pte;
+
+    /* Allocate LVL 2 PD if necessary */
+    pde = pt_entry(lvl1, virt);
+    if ( !pde_is_valid(*pde) )
     {
-        early_printk("Ran out of space for LVL4 PT!\n");
-        die();
+        lvl2 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl2), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_2);
     }
+    else
+        lvl2 = __va(pde_to_paddr(*pde));
 
-    return &initial_lvl4_pt_pool[initial_lvl4_pt_pool_used++];
+    /* Allocate LVL 3 PD if necessary */
+    pde = pt_entry(lvl2, virt);
+    if ( !pde_is_valid(*pde) )
+    {
+        lvl3 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl3), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_3);
+    }
+    else
+        lvl3 = __va(pde_to_paddr(*pde));
+
+    /* Allocate LVL 4 PT if necessary */
+    pde = pt_entry(lvl3, virt);
+    if ( !pde_is_valid(*pde) )
+    {
+        lvl4 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl4), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_4);
+    }
+    else
+        lvl4 = __va(pde_to_paddr(*pde));
+
+    /* Finally, create PTE in LVL 4 PT */
+    pte = pt_entry(lvl4, virt);
+    if ( !pte_is_valid(*pte) )
+    {
+        radix_dprintk("%016lx being mapped to %016lx\n", phys, virt);
+        *pte = paddr_to_pte(phys, flags);
+    }
+    else
+    {
+        early_printk("BUG: Tried to create PTE for already-mapped page!");
+        die();
+    }
 }
 
 static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
@@ -93,6 +136,7 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
                                          paddr_t phys_base)
 {
     uint64_t page_addr;
+    mfn_t previous_max_alloc_mfn;
 
     if ( map_start & ~PAGE_MASK )
     {
@@ -106,81 +150,47 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
         die();
     }
 
+    /* Identity map Xen itself */
     for ( page_addr = map_start; page_addr < map_end; page_addr += PAGE_SIZE )
     {
-        struct lvl2_pd *lvl2;
-        struct lvl3_pd *lvl3;
-        struct lvl4_pt *lvl4;
-        pde_t *pde;
-        pte_t *pte;
-
-        /* Allocate LVL 2 PD if necessary */
-        pde = pt_entry(lvl1, page_addr);
-        if ( !pde_is_valid(*pde) )
-        {
-            lvl2 = lvl2_pd_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl2), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_2);
-        }
-        else
-            lvl2 = __va(pde_to_paddr(*pde));
-
-        /* Allocate LVL 3 PD if necessary */
-        pde = pt_entry(lvl2, page_addr);
-        if ( !pde_is_valid(*pde) )
-        {
-            lvl3 = lvl3_pd_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl3), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_3);
-        }
-        else
-            lvl3 = __va(pde_to_paddr(*pde));
+        unsigned long flags;
 
-        /* Allocate LVL 4 PT if necessary */
-        pde = pt_entry(lvl3, page_addr);
-        if ( !pde_is_valid(*pde) )
+        if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
         {
-            lvl4 = lvl4_pt_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl4), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_4);
+            radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
+            flags = PTE_XEN_RX;
         }
-        else
-            lvl4 = __va(pde_to_paddr(*pde));
-
-        /* Finally, create PTE in LVL 4 PT */
-        pte = pt_entry(lvl4, page_addr);
-        if ( !pte_is_valid(*pte) )
+        else if ( is_kernel_rodata(page_addr) )
         {
-            unsigned long paddr = (page_addr - map_start) + phys_base;
-            unsigned long flags;
-
-            radix_dprintk("%016lx being mapped to %016lx\n", paddr, page_addr);
-            if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
-            {
-                radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
-                flags = PTE_XEN_RX;
-            }
-            else if ( is_kernel_rodata(page_addr) )
-            {
-                radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
-                flags = PTE_XEN_RO;
-            }
-            else
-            {
-                radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
-                flags = PTE_XEN_RW;
-            }
-
-            *pte = paddr_to_pte(paddr, flags);
-            radix_dprintk("%016lx is the result of PTE map\n",
-                paddr_to_pte(paddr, flags).pte);
+            radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
+            flags = PTE_XEN_RO;
         }
         else
         {
-            early_printk("BUG: Tried to create PTE for already-mapped page!");
-            die();
+            radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
+            flags = PTE_XEN_RW;
         }
+
+        map_page_initial(lvl1, page_addr, (page_addr - map_start) + phys_base, flags);
     }
+
+    previous_max_alloc_mfn = max_alloc_mfn;
+
+    /*
+     * Identity map all pages we've allocated for paging structures. This act
+     * itself will allocate more pages, so continue until we've mapped from
+     * `max_alloc_mfn` down to `min_alloc_mfn`. This assumes that the heap grows
+     * downwards, which matches the behavior of alloc_boot_pages.
+     */
+    for ( page_addr = (vaddr_t)__va(mfn_to_maddr(max_alloc_mfn));
+          mfn_to_maddr(min_alloc_mfn) <= __pa(page_addr);
+          page_addr -= PAGE_SIZE)
+    {
+        map_page_initial(lvl1, page_addr, __pa(page_addr), PTE_XEN_RW);
+    }
+
+    if ( mfn_x(previous_max_alloc_mfn) != mfn_x(max_alloc_mfn) )
+        panic("Early page heap unexpectedly grew upwards\n");
 }
 
 static void __init setup_partition_table(struct lvl1_pd *root)
@@ -209,9 +219,21 @@ static void __init setup_process_table(struct lvl1_pd *root)
 
 void __init setup_initial_pagetables(void)
 {
-    struct lvl1_pd *root = lvl1_pd_pool_alloc();
+    struct lvl1_pd *root;
     unsigned long lpcr;
+    mfn_t patb_mfn, prtb_mfn;
+
+    /* Allocate mfns for in-memory tables using the boot allocator */
+    prtb_mfn = initial_page_alloc(PRTB_SIZE / PAGE_SIZE,
+                                  1 << (PRTB_SIZE_LOG2 - PAGE_SHIFT));
+    patb_mfn = initial_page_alloc(PATB_SIZE / PAGE_SIZE,
+                                  1 << (PATB_SIZE_LOG2 - PAGE_SHIFT));
+
+    initial_patb = __va(mfn_to_maddr(patb_mfn));
+    initial_prtb = __va(mfn_to_maddr(prtb_mfn));
 
+    /* Allocate and create page tables */
+    root = initial_pd_pt_alloc();
     setup_initial_mapping(root, (vaddr_t)_start, (vaddr_t)_end, __pa(_start));
 
     /* Enable Radix mode in LPCR */

--
2.30.2

Acked-by on v5 3/3
#10 lore raw

Re: [PATCH v5 3/3] xen/ppc: mm-radix: Allocate all paging structures at runtime

Quoted 9 lines · Shawn Anastasio

In the initial mm-radix implementation, the in-memory partition and process tables required to configure the MMU, as well as the page tables themselves were all allocated statically since the boot allocator was not yet available.

Now that it is, allocate these structures at runtime and bump the size of the Process Table to its maximum supported value (on POWER9).

On the assumption that the (theoretical only?) case of there being gaps in [min_alloc_mfn,max_alloc_mfn] if deemed okay, with it being just more memory being mapped in such a case:

Jan

#11 lore raw

Re: [PATCH v5 3/3] xen/ppc: mm-radix: Allocate all paging structures at runtime

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 8 lines · Shawn Anastasio

In the initial mm-radix implementation, the in-memory partition and process tables required to configure the MMU, as well as the page tables themselves were all allocated statically since the boot allocator was not yet available.

Now that it is, allocate these structures at runtime and bump the size of the Process Table to its maximum supported value (on POWER9).

Quoted 6 lines
Quoted 1 line

On the assumption that the (theoretical only?) case of there being gaps in [min_alloc_mfn,max_alloc_mfn] if deemed okay, with it being just more memory being mapped in such a case:

Sorry for the late response on this. Your assumption is correct -- in the case that there are gaps in the allocated region, it will just result in more memory than is strictly necessary being mapped, and given the constraints here I'm fine with that tradeoff.

Quoted 1 line

Jan

Thanks,
Shawn