[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] xen/dt: Skip non-RAM reserved-memory regions in make_memory_node()



Xen creates a second /memory node for the hardware domain from
bootinfo_get_reserved_mem() without checking whether those regions
belong to host RAM. However, /reserved-memory can also describe non-RAM
regions such as on-chip SRAM outside system RAM. For example, Linux's
Documentation/devicetree/bindings/nvmem/rmem.yaml defines nvmem-rmem
nodes under /reserved-memory, and the comment in Linux's
early_init_dt_reserve_memory() notes that a no-map region may not be
part of memory. Listing them in a /memory node causes the hardware
domain to register non-RAM addresses as system RAM, conflicting with
device MMIO mappings.

Skip reserved-memory banks outside the host RAM banks reported by
bootinfo_get_mem() in make_memory_node(). Skip this check when booted
via EFI, where bootinfo_get_mem() only contains usable memory and does
not include reserved regions.

Fixes: 248faa637d2f ("xen/arm: add reserved-memory regions to the dom0 memory 
node")
Signed-off-by: Kuan-Wei Chiu <visitorckw@xxxxxxxxx>
---
Changes in v2:
- Skip the host RAM check when booted via EFI.
- Drop the redundant !bank->size check from is_in_host_ram().
- Extract the bank-filtering logic into skip_memory_bank() and update
the stale comment in make_memory_node().
- Expand the commit message to reference Linux's nvmem-rmem binding,
early_init_dt_reserve_memory() comment, and EFI boot behavior.

 xen/common/device-tree/domain-build.c | 50 +++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/xen/common/device-tree/domain-build.c 
b/xen/common/device-tree/domain-build.c
index 634a26946c..e58c340746 100644
--- a/xen/common/device-tree/domain-build.c
+++ b/xen/common/device-tree/domain-build.c
@@ -2,6 +2,7 @@
 
 #include <xen/bootinfo.h>
 #include <xen/domain-layout.h>
+#include <xen/efi.h>
 #include <xen/fdt-domain-build.h>
 #include <xen/init.h>
 #include <xen/lib.h>
@@ -504,6 +505,42 @@ int __init make_chosen_node(const struct kernel_info 
*kinfo)
     return res;
 }
 
+static bool __init is_in_host_ram(const struct membank *bank)
+{
+    const struct membanks *ram = bootinfo_get_mem();
+    paddr_t start = bank->start;
+    paddr_t end = start + bank->size;
+    unsigned int i;
+
+    if ( end < start )
+        return false;
+
+    for ( i = 0; i < ram->nr_banks; i++ )
+    {
+        paddr_t ram_start = ram->bank[i].start;
+        paddr_t ram_end = ram_start + ram->bank[i].size;
+
+        if ( start < ram_start )
+            return false;
+
+        if ( end <= ram_end )
+            return true;
+
+        if ( start < ram_end )
+            start = ram_end;
+    }
+
+    return false;
+}
+
+static bool __init skip_memory_bank(const struct membank *bank, bool check_ram)
+{
+    if ( bank->type != MEMBANK_DEFAULT )
+        return true;
+
+    return check_ram && !is_in_host_ram(bank);
+}
+
 int __init make_memory_node(const struct kernel_info *kinfo, int addrcells,
                             int sizecells, const struct membanks *mem)
 {
@@ -513,13 +550,20 @@ int __init make_memory_node(const struct kernel_info 
*kinfo, int addrcells,
     int nr_cells = 0;
     __be32 reg[DT_MEM_NODE_REG_RANGE_SIZE];
     __be32 *cells;
+    bool check_ram;
 
     if ( mem->nr_banks == 0 )
         return -ENOENT;
 
-    /* find the first memory range that is reserved for device (or firmware) */
+    /*
+     * When booting via EFI, bootinfo_get_mem() only holds usable memory and
+     * excludes reserved regions, so skip the host RAM check in that case.
+     */
+    check_ram = (mem == bootinfo_get_reserved_mem()) && !efi_enabled(EFI_BOOT);
+
+    /* Find the first memory bank to include in the /memory node. */
     for ( i = 0; i < mem->nr_banks &&
-                 (mem->bank[i].type != MEMBANK_DEFAULT); i++ )
+                 skip_memory_bank(&mem->bank[i], check_ram); i++ )
         ;
 
     if ( i == mem->nr_banks )
@@ -541,7 +585,7 @@ int __init make_memory_node(const struct kernel_info 
*kinfo, int addrcells,
         u64 start = mem->bank[i].start;
         u64 size = mem->bank[i].size;
 
-        if ( mem->bank[i].type != MEMBANK_DEFAULT )
+        if ( skip_memory_bank(&mem->bank[i], check_ram) )
             continue;
 
         nr_cells += reg_size;
-- 
2.56.1.703.gf848ed807c-goog




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.