Skip to contentSkip to conversation

xen/arm: don't assign domU static-mem to dom0 as reserved-memory

PATCH Superseded Stefano Stabellini 2 participants 2 messages Nov 9, 2021 – Nov 10, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/arm: don't assign domU static-mem to dom0 as reserved-memory

From: Stefano Stabellini <[email protected]>

DomUs static-mem ranges are added to the reserved_mem array for accounting, but they shouldn't be assigned to dom0 as the other regular reserved-memory ranges in device tree.

In make_memory_nodes, fix the error by skipping banks with xen_domain set to true in the reserved-memory array. Also make sure to use the first valid (!xen_domain) start address for the memory node name.

Fixes 41c031ff437b ("xen/arm: introduce domain on Static Allocation")

---
Changes in v2:
- improve commit message
- improve in-code comment
- update nr_cells appropriately

---
 xen/arch/arm/domain_build.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
xen/arch/arm/domain_build.c +17 -6
index 9e92b640cd..c81a2bdfef 100644
@@ -862,21 +862,25 @@ static int __init make_memory_node(const struct domain *d,
 {
     int res, i;
     int reg_size = addrcells + sizecells;
-    int nr_cells = reg_size * mem->nr_banks;
+    int nr_cells = 0;
     /* Placeholder for memory@ + a 64-bit number + \0 */
     char buf[24];
     __be32 reg[NR_MEM_BANKS * 4 /* Worst case addrcells + sizecells */];
     __be32 *cells;
 
-    BUG_ON(nr_cells >= ARRAY_SIZE(reg));
     if ( mem->nr_banks == 0 )
         return -ENOENT;
 
-    dt_dprintk("Create memory node (reg size %d, nr cells %d)\n",
-               reg_size, nr_cells);
+    /* find first memory range not bound to a Xen domain */
+    for ( i = 0; i < mem->nr_banks && mem->bank[i].xen_domain; i++ )
+        ;
+    if ( i == mem->nr_banks )
+        return 0;
+
+    dt_dprintk("Create memory node\n");
 
     /* ePAPR 3.4 */
-    snprintf(buf, sizeof(buf), "memory@%"PRIx64, mem->bank[0].start);
+    snprintf(buf, sizeof(buf), "memory@%"PRIx64, mem->bank[i].start);
     res = fdt_begin_node(fdt, buf);
     if ( res )
         return res;
@@ -886,17 +890,24 @@ static int __init make_memory_node(const struct domain *d,
         return res;
 
     cells = &reg[0];
-    for ( i = 0 ; i < mem->nr_banks; i++ )
+    for ( ; i < mem->nr_banks; i++ )
     {
         u64 start = mem->bank[i].start;
         u64 size = mem->bank[i].size;
 
+        if ( mem->bank[i].xen_domain )
+            continue;
+
         dt_dprintk("  Bank %d: %#"PRIx64"->%#"PRIx64"\n",
                    i, start, start + size);
 
+        nr_cells += reg_size;
         dt_child_set_range(&cells, addrcells, sizecells, start, size);
     }
 
+    dt_dprintk("(reg size %d, nr cells %d)\n", reg_size, nr_cells);
+    BUG_ON(nr_cells >= ARRAY_SIZE(reg));
+
     res = fdt_property(fdt, "reg", reg, nr_cells * sizeof(*reg));
     if ( res )
         return res;
Signature · 1 line
2.25.1
#1 lore raw

Hi Stefano,

Quoted 29 lines · Stefano Stabellini

From: Stefano Stabellini <[email protected]>

DomUs static-mem ranges are added to the reserved_mem array for accounting, but they shouldn't be assigned to dom0 as the other regular reserved-memory ranges in device tree.

In make_memory_nodes, fix the error by skipping banks with xen_domain set to true in the reserved-memory array. Also make sure to use the first valid (!xen_domain) start address for the memory node name.

Fixes 41c031ff437b ("xen/arm: introduce domain on Static Allocation")

---
Changes in v2:
- improve commit message
- improve in-code comment
- update nr_cells appropriately

---
  xen/arch/arm/domain_build.c | 23 +++++++++++++++++------
  1 file changed, 17 insertions(+), 6 deletions(-)
xen/arch/arm/domain_build.c +0 -0
index 9e92b640cd..c81a2bdfef 100644
@@ -862,21 +862,25 @@ static int __init make_memory_node(const struct domain *d,
  {

xen/arch/arm/domain_build.c static int __init make_memory_node(const struct domain *d, · line 871

Quoted 10 lines
xen/arch/arm/domain_build.c +1 -2
      int res, i;
      int reg_size = addrcells + sizecells;
-    int nr_cells = reg_size * mem->nr_banks;
+    int nr_cells = 0;
      /* Placeholder for memory@ + a 64-bit number + \0 */
      char buf[24];
      __be32 reg[NR_MEM_BANKS * 4 /* Worst case addrcells + sizecells */];
      __be32 *cells;
 
-    BUG_ON(nr_cells >= ARRAY_SIZE(reg));

The goal of this BUG_ON() was to catch potential overrun of the buffer before it is actually used. Now...

Quoted 31 lines
xen/arch/arm/domain_build.c +11 -4
      if ( mem->nr_banks == 0 )
          return -ENOENT;
 
-    dt_dprintk("Create memory node (reg size %d, nr cells %d)\n",
-               reg_size, nr_cells);
+    /* find first memory range not bound to a Xen domain */
+    for ( i = 0; i < mem->nr_banks && mem->bank[i].xen_domain; i++ )
+        ;
+    if ( i == mem->nr_banks )
+        return 0;
+
+    dt_dprintk("Create memory node\n");
 
      /* ePAPR 3.4 */
-    snprintf(buf, sizeof(buf), "memory@%"PRIx64, mem->bank[0].start);
+    snprintf(buf, sizeof(buf), "memory@%"PRIx64, mem->bank[i].start);
      res = fdt_begin_node(fdt, buf);
      if ( res )
          return res;
@@ -886,17 +890,24 @@ static int __init make_memory_node(const struct domain *d,
          return res;
 
      cells = &reg[0];
-    for ( i = 0 ; i < mem->nr_banks; i++ )
+    for ( ; i < mem->nr_banks; i++ )
      {
          u64 start = mem->bank[i].start;
          u64 size = mem->bank[i].size;
 
+        if ( mem->bank[i].xen_domain )
+            continue;

xen/arch/arm/domain_build.c static int __init make_memory_node(const struct domain *d, · line 909

Quoted 10 lines
xen/arch/arm/domain_build.c +4 -0
+
          dt_dprintk("  Bank %d: %#"PRIx64"->%#"PRIx64"\n",
                     i, start, start + size);
 
+        nr_cells += reg_size;
          dt_child_set_range(&cells, addrcells, sizecells, start, size);
      }
 
+    dt_dprintk("(reg size %d, nr cells %d)\n", reg_size, nr_cells);
+    BUG_ON(nr_cells >= ARRAY_SIZE(reg));

... this is moved after we write to the array. Which means the badness already happened and the BUG_ON() becomes pointless.

I would still like to have the BUG_ON() in place. So I would move it in the loop just after nr_cells is updated.

Quoted 5 lines
xen/arch/arm/domain_build.c +1 -0
+
      res = fdt_property(fdt, "reg", reg, nr_cells * sizeof(*reg));
      if ( res )
          return res;
Signature · 1 line
Julien Grall