Skip to contentSkip to conversation

Follow-up static shared memory PART I

PATCH In review Penny Zheng 3 participants 23 messages Dec 6, 2023 – Dec 11, 2023

lore
Share & tools
lore
#0 lore raw

v5 cover Follow-up static shared memory PART I

There are some unsolving issues on current 4.18 static shared memory feature[1], including:
- In order to avoid keeping growing 'membank', having the shared memory info in separate structures is preferred.
- Missing implementation on having the host address optional in "xen,shared-mem" property
- Removing static shared memory from extended regions
- Missing reference release on foreign superpage
- Fix duplicated /reserved-memory node on Dom0
- Missing static shm node declaration on guest /memory node
- Missing "xen,offset" feature, which is introduced in Linux DOC[2]

All above objects have been divided into two parts to complete. And this patch serie is PART I.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://www.kernel.org/doc/Documentation/devicetree/bindings/reserved-memory/xen%2Cshared-memory.txt

Penny Zheng (11):
xen/arm: remove stale addr_cells/size_cells in assign_shared_memory xen/arm: avoid repetitive checking in process_shm_node xen/arm: re-define a set of data structures for static shared memory
region
xen/arm: introduce allocate_domheap_memory and guest_physmap_memory xen/arm: use paddr_assigned to indicate whether host address is
provided
xen/arm: support static shared memory when host address not provided xen/arm: remove shm holes for extended regions
xen/p2m: put reference for superpage
xen/docs: refine docs about static shared memory
xen/arm: fix duplicate /reserved-memory node in Dom0 xen/arm: create another /memory node for static shm

 docs/misc/arm/device-tree/booting.txt   |  52 +-
 xen/arch/arm/dom0less-build.c           |  11 +-
 xen/arch/arm/domain_build.c             | 177 +++++-
 xen/arch/arm/include/asm/domain_build.h |   7 +
 xen/arch/arm/include/asm/kernel.h       |  11 +-
 xen/arch/arm/include/asm/setup.h        |  69 ++-
 xen/arch/arm/include/asm/static-shmem.h |  48 +-
 xen/arch/arm/mmu/p2m.c                  |  58 +-
 xen/arch/arm/static-shmem.c             | 742 ++++++++++++++++++------
 9 files changed, 929 insertions(+), 246 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 1/11 xen/arm: remove stale addr_cells/size_cells in assign_shared_memory

Function parameters {addr_cells,size_cells} are stale parameters in assign_shared_memory, so we shall remove them.

---
v1 -> v2:
- new commit
---
v2 -> v3:
rebase and no change
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/static-shmem.c | 2 --
 1 file changed, 2 deletions(-)
xen/arch/arm/static-shmem.c +0 -2
index 9097bc8b15..cb268cd2ed 100644
@@ -90,7 +90,6 @@ static mfn_t __init acquire_shared_memory_bank(struct domain *d,
 }
 
 static int __init assign_shared_memory(struct domain *d,
-                                       uint32_t addr_cells, uint32_t size_cells,
                                        paddr_t pbase, paddr_t psize,
                                        paddr_t gbase)
 {
@@ -252,7 +251,6 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
              * specified, so they should be assigned to dom_io.
              */
             ret = assign_shared_memory(owner_dom_io ? dom_io : d,
-                                       addr_cells, size_cells,
                                        pbase, psize, gbase);
             if ( ret )
                 return ret;
Signature · 1 line
2.25.1
#2 lore raw

PATCH v5 2/11 xen/arm: avoid repetitive checking in process_shm_node

In reply to Penny Zheng

Putting overlap and overflow checking in the loop is causing repetitive operation, so this commit extracts both checking outside the loop.

---
v6:
new commit

---
 xen/arch/arm/static-shmem.c | 39 +++++++++++++++----------------------
 1 file changed, 16 insertions(+), 23 deletions(-)
xen/arch/arm/static-shmem.c +16 -23
index cb268cd2ed..1a1a9386e4 100644
@@ -349,7 +349,7 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
 {
     const struct fdt_property *prop, *prop_id, *prop_role;
     const __be32 *cell;
-    paddr_t paddr, gaddr, size;
+    paddr_t paddr, gaddr, size, end;
     struct meminfo *mem = &bootinfo.reserved_mem;
     unsigned int i;
     int len;
@@ -422,6 +422,13 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
         return -EINVAL;
     }
 
+    end = paddr + size;
+    if ( end <= paddr )
+    {
+        printk("fdt: static shared memory region %s overflow\n", shm_id);
+        return -EINVAL;
+    }
+
     for ( i = 0; i < mem->nr_banks; i++ )
     {
         /*
@@ -441,30 +448,13 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
                 return -EINVAL;
             }
         }
+        else if ( strcmp(shm_id, mem->bank[i].shm_id) != 0 )
+            continue;
         else
         {
-            paddr_t end = paddr + size;
-            paddr_t bank_end = mem->bank[i].start + mem->bank[i].size;
-
-            if ( (end <= paddr) || (bank_end <= mem->bank[i].start) )
-            {
-                printk("fdt: static shared memory region %s overflow\n", shm_id);
-                return -EINVAL;
-            }
-
-            if ( check_reserved_regions_overlap(paddr, size) )
-                return -EINVAL;
-            else
-            {
-                if ( strcmp(shm_id, mem->bank[i].shm_id) != 0 )
-                    continue;
-                else
-                {
-                    printk("fdt: different shared memory region could not share the same shm ID %s\n",
-                           shm_id);
-                    return -EINVAL;
-                }
-            }
+            printk("fdt: different shared memory region could not share the same shm ID %s\n",
+                   shm_id);
+            return -EINVAL;
         }
     }
 
@@ -472,6 +462,9 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
     {
         if ( i < NR_MEM_BANKS )
         {
+            if ( check_reserved_regions_overlap(paddr, size) )
+                return -EINVAL;
+
             /* Static shared memory shall be reserved from any other use. */
             safe_strcpy(mem->bank[mem->nr_banks].shm_id, shm_id);
             mem->bank[mem->nr_banks].start = paddr;
Signature · 1 line
2.25.1
Reviewed-by on v5 2/11
#3 lore raw

Re: [PATCH v5 02/11] xen/arm: avoid repetitive checking in process_shm_node

Hi Penny,

Quoted 6 lines · Penny Zheng

Putting overlap and overflow checking in the loop is causing repetitive operation, so this commit extracts both checking outside the loop.

In general the patch looks good to me:

That said, there are 2 things I realized during review.

Quoted 36 lines

---
v6:
new commit

---
 xen/arch/arm/static-shmem.c | 39 +++++++++++++++----------------------
 1 file changed, 16 insertions(+), 23 deletions(-)
xen/arch/arm/static-shmem.c +8 -1
index cb268cd2ed..1a1a9386e4 100644
@@ -349,7 +349,7 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
 {
     const struct fdt_property *prop, *prop_id, *prop_role;
     const __be32 *cell;
-    paddr_t paddr, gaddr, size;
+    paddr_t paddr, gaddr, size, end;
     struct meminfo *mem = &bootinfo.reserved_mem;
     unsigned int i;
     int len;
@@ -422,6 +422,13 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
         return -EINVAL;
     }
 
+    end = paddr + size;
+    if ( end <= paddr )
+    {
+        printk("fdt: static shared memory region %s overflow\n", shm_id);
+        return -EINVAL;
+    }
+
     for ( i = 0; i < mem->nr_banks; i++ )
     {
         /*
@@ -441,30 +448,13 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
                 return -EINVAL;

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 449

Quoted 10 lines
xen/arch/arm/static-shmem.c +2 -4
             }
         }
+        else if ( strcmp(shm_id, mem->bank[i].shm_id) != 0 )
+            continue;
         else
         {
-            paddr_t end = paddr + size;
-            paddr_t bank_end = mem->bank[i].start + mem->bank[i].size;
-
-            if ( (end <= paddr) || (bank_end <= mem->bank[i].start) )

You are iterating over reserved memory regions in general, so apart from shmem regions there might be truly /reserved ones. It appears that we don't have overflow check in device_tree_get_meminfo, so this second check was the only place to detect that (protected by a feature, so not very useful) :) This is just an observation and I agree to drop it. We should be checking for an overflow in device_tree_get_meminfo.

The second observation I made is that we don't seem to assign and check the return code from device_tree_for_each_node.
This means, that any error while parsing the early fdt (e.g. static shm issues) does not stop Xen from booting, which might result in strange behavior later on. If others agree, I'm ok to send a fix for that.

~Michal

#4 lore raw

Re: [PATCH v5 02/11] xen/arm: avoid repetitive checking in process_shm_node

Hi Michal

Quoted 5 lines · Michal Orzel

Hi Penny,

Quoted 2 lines · Penny Zheng
Quoted 7 lines
Quoted 4 lines

Putting overlap and overflow checking in the loop is causing repetitive operation, so this commit extracts both checking outside the loop.

In general the patch looks good to me:

Thx~

Quoted 44 lines

That said, there are 2 things I realized during review.

Quoted 42 lines

---
v6:
new commit

---
  xen/arch/arm/static-shmem.c | 39 +++++++++++++++----------------------
  1 file changed, 16 insertions(+), 23 deletions(-)
xen/arch/arm/static-shmem.c +10 -1
index cb268cd2ed..1a1a9386e4 100644
@@ -349,7 +349,7 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
  {
      const struct fdt_property *prop, *prop_id, *prop_role;
      const __be32 *cell;
-    paddr_t paddr, gaddr, size;
+    paddr_t paddr, gaddr, size, end;
      struct meminfo *mem = &bootinfo.reserved_mem;
      unsigned int i;
      int len;
@@ -422,6 +422,13 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
          return -EINVAL;
      }
 
+    end = paddr + size;
+    if ( end <= paddr )
+    {
+        printk("fdt: static shared memory region %s overflow\n", shm_id);
+        return -EINVAL;
+    }
+
      for ( i = 0; i < mem->nr_banks; i++ )
      {
          /*
@@ -441,30 +448,13 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
                  return -EINVAL;
              }
          }
+        else if ( strcmp(shm_id, mem->bank[i].shm_id) != 0 )
+            continue;
          else
          {
Quoted 9 lines

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 449

Quoted 4 lines
xen/arch/arm/static-shmem.c +0 -4
-            paddr_t end = paddr + size;
-            paddr_t bank_end = mem->bank[i].start + mem->bank[i].size;
-
-            if ( (end <= paddr) || (bank_end <= mem->bank[i].start) )

You are iterating over reserved memory regions in general, so apart from shmem regions there might be truly /reserved ones. It appears that we don't have overflow check in device_tree_get_meminfo, so this second check was the only place to detect that (protected by a feature, so not very useful) :) This is just an observation and I agree to drop it. We should be checking for an overflow in device_tree_get_meminfo.

True~

Quoted 5 lines

The second observation I made is that we don't seem to assign and check the return code from device_tree_for_each_node.
This means, that any error while parsing the early fdt (e.g. static shm issues) does not stop Xen from booting, which might result in strange behavior later on. If others agree, I'm ok to send a fix for that.

~Michal

Penny Zheng
Many thanks

#5 lore raw

PATCH v5 3/11 xen/arm: re-define a set of data structures for static shared memory region

In reply to Penny Zheng

This commit introduces a set of separate data structures to deal with static shared memory at different stages.

In boot-time host device tree parsing, we introduce a new structure "struct shm_node" and a new field "shminfo" in bootinfo to describe and store parsed shm info.

In acquire_nr_borrower_domain, it is better to use SHMID as unique identifier to iterate "shminfo", other than address and size.

In the last, a new anonymized structure "shminfo", which is a array of compound structure that contains SHMID and a "struct membank membank" describing shared memory regions in guest address space, is created in "kinfo" when dealing with domain information.

---
v1 -> v2:
- As the original "struct shm_membank" was making reserving memory more complex and actually memory information could be still got from host Device\
Tree when dealing with domain construction, we introduce a new simple structure "struct shm_node" in bootinfo to only store SHMID and "nr_borrowers"
- Further restrict the scope of the local variable
"struct meminfo *mem = &bootinfo.reserved_mem"
- Introduce a new local global data "shm_data" in bootfdt.c. In which, reserved memory bank is recorded together with the shm node, to assist doing shm node verification.
- Define a set of local variables that point to "shm_data.shm_nodes[i].membank->start", etc, to make the code more readable.
- Use SHMID to iterate "shminfo" to find requested shm node, as we no longer store host memory bank info in shm node.
- A new anonymized structure, which is a array of compound structure that contains SHMID and a "struct membank membank", describing shared memory region in guest, is introduced in "kinfo".
---
v2 -> v3:
- rebase and no changes
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
- With all shm-related functions classified into static-shmem.c, there is no need to import local global data "shm_data".

---
 xen/arch/arm/dom0less-build.c           |   3 +-
 xen/arch/arm/domain_build.c             |   3 +-
 xen/arch/arm/include/asm/kernel.h       |   9 +-
 xen/arch/arm/include/asm/setup.h        |  24 +++++-
 xen/arch/arm/include/asm/static-shmem.h |   4 +-
 xen/arch/arm/static-shmem.c             | 104 ++++++++++++++----------
 6 files changed, 92 insertions(+), 55 deletions(-)
xen/arch/arm/dom0less-build.c +1 -2
index fb63ec6fd1..ac096fa3fa 100644
@@ -645,8 +645,7 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
     if ( ret )
         goto err;
 
-    ret = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells,
-                                &kinfo->shm_mem);
+    ret = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
     if ( ret )
         goto err;
 
xen/arch/arm/domain_build.c +1 -2
index 613b2885ce..64ae944431 100644
@@ -1767,8 +1767,7 @@ static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
                 return res;
         }
 
-        res = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells,
-                                    &kinfo->shm_mem);
+        res = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
         if ( res )
             return res;
     }
xen/arch/arm/include/asm/kernel.h +8 -1
index 0a23e86c2d..db3d8232fa 100644
@@ -39,7 +39,14 @@ struct kernel_info {
     void *fdt; /* flat device tree */
     paddr_t unassigned_mem; /* RAM not (yet) assigned to a bank */
     struct meminfo mem;
-    struct meminfo shm_mem;
+    /* Static shared memory banks */
+    struct {
+        unsigned int nr_banks;
+        struct {
+            char shm_id[MAX_SHM_ID_LENGTH];
+            struct membank membank;
+        } bank[NR_MEM_BANKS];
+    } shminfo;
 
     /* kernel entry point */
     paddr_t entry;
xen/arch/arm/include/asm/setup.h +20 -4
index d15a88d2e0..3a2b35ea46 100644
@@ -50,10 +50,6 @@ struct membank {
     paddr_t start;
     paddr_t size;
     enum membank_type type;
-#ifdef CONFIG_STATIC_SHM
-    char shm_id[MAX_SHM_ID_LENGTH];
-    unsigned int nr_shm_borrowers;
-#endif
 };
 
 struct meminfo {
@@ -95,6 +91,17 @@ struct bootcmdlines {
     struct bootcmdline cmdline[MAX_MODULES];
 };
 
+#ifdef CONFIG_STATIC_SHM
+/*
+ * struct shm_node represents a static shared memory node shared between
+ * multiple domains, identified by the unique SHMID("xen,shm-id").
+ */
+struct shm_node {
+    char shm_id[MAX_SHM_ID_LENGTH];
+    unsigned int nr_shm_borrowers;
+};
+#endif
+
 struct bootinfo {
     struct meminfo mem;
     /* The reserved regions are only used when booting using Device-Tree */
@@ -105,6 +112,15 @@ struct bootinfo {
     struct meminfo acpi;
 #endif
     bool static_heap;
+#ifdef CONFIG_STATIC_SHM
+    struct {
+        unsigned int nr_nodes;
+        struct {
+            struct shm_node node;
+            const struct membank *membank;
+        } shm_nodes[NR_MEM_BANKS];
+    } shminfo;
+#endif
 };
 
 struct map_range_data
xen/arch/arm/include/asm/static-shmem.h +2 -2
index 1536ff18b8..66a3f4c146 100644
@@ -8,7 +8,7 @@
 #ifdef CONFIG_STATIC_SHM
 
 int make_resv_memory_node(const struct domain *d, void *fdt, int addrcells,
-                          int sizecells, const struct meminfo *mem);
+                          int sizecells, const struct kernel_info *kinfo);
 
 int process_shm(struct domain *d, struct kernel_info *kinfo,
                 const struct dt_device_node *node);
@@ -28,7 +28,7 @@ int process_shm_node(const void *fdt, int node, uint32_t address_cells,
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
                                         int addrcells, int sizecells,
-                                        const struct meminfo *mem)
+                                        const struct kernel_info *kinfo)
 {
     return 0;
 }
xen/arch/arm/static-shmem.c +60 -44

228 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v5 03/11] xen/arm: re-define a set of data structures for static shared memory region

Hi Penny,

Quoted 9 lines · Penny Zheng

This commit introduces a set of separate data structures to deal with static shared memory at different stages.

In boot-time host device tree parsing, we introduce a new structure "struct shm_node" and a new field "shminfo" in bootinfo to describe and store parsed shm info.

Quoted 7 lines

In acquire_nr_borrower_domain, it is better to use SHMID as unique identifier to iterate "shminfo", other than address and size.

In the last, a new anonymized structure "shminfo", which is a array of compound structure that contains SHMID and a "struct membank membank" describing shared memory regions in guest address space, is created in "kinfo" when dealing with domain information.

This commit msg describes what the patch does but not why.

Quoted 74 lines

---
v1 -> v2:
- As the original "struct shm_membank" was making reserving memory more complex and actually memory information could be still got from host Device\
Tree when dealing with domain construction, we introduce a new simple structure "struct shm_node" in bootinfo to only store SHMID and "nr_borrowers"
- Further restrict the scope of the local variable
"struct meminfo *mem = &bootinfo.reserved_mem"
- Introduce a new local global data "shm_data" in bootfdt.c. In which, reserved memory bank is recorded together with the shm node, to assist doing shm node verification.
- Define a set of local variables that point to "shm_data.shm_nodes[i].membank->start", etc, to make the code more readable.
- Use SHMID to iterate "shminfo" to find requested shm node, as we no longer store host memory bank info in shm node.
- A new anonymized structure, which is a array of compound structure that contains SHMID and a "struct membank membank", describing shared memory region in guest, is introduced in "kinfo".
---
v2 -> v3:
- rebase and no changes
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
- With all shm-related functions classified into static-shmem.c, there is no need to import local global data "shm_data".

---
 xen/arch/arm/dom0less-build.c           |   3 +-
 xen/arch/arm/domain_build.c             |   3 +-
 xen/arch/arm/include/asm/kernel.h       |   9 +-
 xen/arch/arm/include/asm/setup.h        |  24 +++++-
 xen/arch/arm/include/asm/static-shmem.h |   4 +-
 xen/arch/arm/static-shmem.c             | 104 ++++++++++++++----------
 6 files changed, 92 insertions(+), 55 deletions(-)
xen/arch/arm/dom0less-build.c +1 -2
index fb63ec6fd1..ac096fa3fa 100644
@@ -645,8 +645,7 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
     if ( ret )
         goto err;
 
-    ret = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells,
-                                &kinfo->shm_mem);
+    ret = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
     if ( ret )
         goto err;
 
xen/arch/arm/domain_build.c +1 -2
index 613b2885ce..64ae944431 100644
@@ -1767,8 +1767,7 @@ static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
                 return res;
         }
 
-        res = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells,
-                                    &kinfo->shm_mem);
+        res = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
         if ( res )
             return res;
     }
xen/arch/arm/include/asm/kernel.h +0 -0
index 0a23e86c2d..db3d8232fa 100644
@@ -39,7 +39,14 @@ struct kernel_info {
     void *fdt; /* flat device tree */
     paddr_t unassigned_mem; /* RAM not (yet) assigned to a bank */

xen/arch/arm/include/asm/kernel.h struct kernel_info { · line 49

Quoted 10 lines
xen/arch/arm/include/asm/kernel.h +8 -1
     struct meminfo mem;
-    struct meminfo shm_mem;
+    /* Static shared memory banks */
+    struct {
+        unsigned int nr_banks;
+        struct {
+            char shm_id[MAX_SHM_ID_LENGTH];
+            struct membank membank;
+        } bank[NR_MEM_BANKS];
+    } shminfo;

AFAICT, the only user of this structure is static-shmem.c so why not protecting it with #ifdef?

Quoted 38 lines
xen/arch/arm/include/asm/kernel.h +0 -0
     /* kernel entry point */
     paddr_t entry;
xen/arch/arm/include/asm/setup.h +11 -4
index d15a88d2e0..3a2b35ea46 100644
@@ -50,10 +50,6 @@ struct membank {
     paddr_t start;
     paddr_t size;
     enum membank_type type;
-#ifdef CONFIG_STATIC_SHM
-    char shm_id[MAX_SHM_ID_LENGTH];
-    unsigned int nr_shm_borrowers;
-#endif
 };
 
 struct meminfo {
@@ -95,6 +91,17 @@ struct bootcmdlines {
     struct bootcmdline cmdline[MAX_MODULES];
 };
 
+#ifdef CONFIG_STATIC_SHM
+/*
+ * struct shm_node represents a static shared memory node shared between
+ * multiple domains, identified by the unique SHMID("xen,shm-id").
+ */
+struct shm_node {
+    char shm_id[MAX_SHM_ID_LENGTH];
+    unsigned int nr_shm_borrowers;
+};
+#endif
+
 struct bootinfo {
     struct meminfo mem;
     /* The reserved regions are only used when booting using Device-Tree */
@@ -105,6 +112,15 @@ struct bootinfo {
     struct meminfo acpi;

xen/arch/arm/include/asm/setup.h struct bootinfo { · line 122

Quoted 10 lines
xen/arch/arm/include/asm/setup.h +8 -0
 #endif
     bool static_heap;
+#ifdef CONFIG_STATIC_SHM
+    struct {
+        unsigned int nr_nodes;
+        struct {
+            struct shm_node node;
+            const struct membank *membank;
+        } shm_nodes[NR_MEM_BANKS];
+    } shminfo;

I find it a bit confusing to have 2 structures named exactly the same (here and in kinfo). Something like shminfo_nodes would be better.

Also, correct me if I'm wrong. The reason for this structure is to avoid keeping membank growing?

Quoted 31 lines
xen/arch/arm/include/asm/setup.h +1 -0
+#endif
 };
 
 struct map_range_data
xen/arch/arm/include/asm/static-shmem.h +2 -2
index 1536ff18b8..66a3f4c146 100644
@@ -8,7 +8,7 @@
 #ifdef CONFIG_STATIC_SHM
 
 int make_resv_memory_node(const struct domain *d, void *fdt, int addrcells,
-                          int sizecells, const struct meminfo *mem);
+                          int sizecells, const struct kernel_info *kinfo);
 
 int process_shm(struct domain *d, struct kernel_info *kinfo,
                 const struct dt_device_node *node);
@@ -28,7 +28,7 @@ int process_shm_node(const void *fdt, int node, uint32_t address_cells,
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
                                         int addrcells, int sizecells,
-                                        const struct meminfo *mem)
+                                        const struct kernel_info *kinfo)
 {
     return 0;
 }
xen/arch/arm/static-shmem.c +0 -0
index 1a1a9386e4..6a3d8a54bd 100644

xen/arch/arm/static-shmem.c line 10

Quoted 7 lines
xen/arch/arm/static-shmem.c +1 -2
@@ -6,28 +6,25 @@
 #include <asm/domain_build.h>
 #include <asm/static-shmem.h>
 
-static int __init acquire_nr_borrower_domain(struct domain *d,
-                                             paddr_t pbase, paddr_t psize,
+static int __init acquire_nr_borrower_domain(const char *shm_id,
                                              unsigned long *nr_borrowers)

Why is nr_borrowers unsigned long but nr_shm_borrowers is unsigned int?

xen/arch/arm/static-shmem.c line 12

Quoted 3 lines
xen/arch/arm/static-shmem.c +1 -1
 {
-    unsigned int bank;
+    struct shm_node *shm_node;

Can be const

Quoted 145 lines
xen/arch/arm/static-shmem.c +33 -29
+    unsigned int i;
 
-    /* Iterate reserved memory to find requested shm bank. */
-    for ( bank = 0 ; bank < bootinfo.reserved_mem.nr_banks; bank++ )
+    /* Iterate to find requested static shared memory node. */
+    for ( i = 0; i < bootinfo.shminfo.nr_nodes; i++ )
     {
-        paddr_t bank_start = bootinfo.reserved_mem.bank[bank].start;
-        paddr_t bank_size = bootinfo.reserved_mem.bank[bank].size;
+        shm_node = &bootinfo.shminfo.shm_nodes[i].node;
 
-        if ( (pbase == bank_start) && (psize == bank_size) )
-            break;
+        if ( strcmp(shm_id, shm_node->shm_id) == 0 )
+        {
+            *nr_borrowers = shm_node->nr_shm_borrowers;
+            return 0;
+        }
     }
 
-    if ( bank == bootinfo.reserved_mem.nr_banks )
-        return -ENOENT;
-
-    *nr_borrowers = bootinfo.reserved_mem.bank[bank].nr_shm_borrowers;
-
-    return 0;
+    return -ENOENT;
 }
 
 /*
@@ -91,7 +88,7 @@ static mfn_t __init acquire_shared_memory_bank(struct domain *d,
 
 static int __init assign_shared_memory(struct domain *d,
                                        paddr_t pbase, paddr_t psize,
-                                       paddr_t gbase)
+                                       paddr_t gbase, const char *shm_id)
 {
     mfn_t smfn;
     int ret = 0;
@@ -125,7 +122,7 @@ static int __init assign_shared_memory(struct domain *d,
      * Get the right amount of references per page, which is the number of
      * borrower domains.
      */
-    ret = acquire_nr_borrower_domain(d, pbase, psize, &nr_borrowers);
+    ret = acquire_nr_borrower_domain(shm_id, &nr_borrowers);
     if ( ret )
         return ret;
 
@@ -161,13 +158,16 @@ static int __init append_shm_bank_to_domain(struct kernel_info *kinfo,
                                             paddr_t start, paddr_t size,
                                             const char *shm_id)
 {
-    if ( kinfo->shm_mem.nr_banks >= NR_MEM_BANKS )
+    unsigned int nr_banks = kinfo->shminfo.nr_banks;
+    struct membank *membank = &kinfo->shminfo.bank[nr_banks].membank;
+
+    if ( nr_banks >= NR_MEM_BANKS )
         return -ENOMEM;
 
-    kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].start = start;
-    kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].size = size;
-    safe_strcpy(kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].shm_id, shm_id);
-    kinfo->shm_mem.nr_banks++;
+    membank->start = start;
+    membank->size = size;
+    safe_strcpy(kinfo->shminfo.bank[nr_banks].shm_id, shm_id);
+    kinfo->shminfo.nr_banks++;
 
     return 0;
 }
@@ -251,7 +251,7 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
              * specified, so they should be assigned to dom_io.
              */
             ret = assign_shared_memory(owner_dom_io ? dom_io : d,
-                                       pbase, psize, gbase);
+                                       pbase, psize, gbase, shm_id);
             if ( ret )
                 return ret;
         }
@@ -279,12 +279,12 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
 
 static int __init make_shm_memory_node(const struct domain *d, void *fdt,
                                        int addrcells, int sizecells,
-                                       const struct meminfo *mem)
+                                       const struct kernel_info *kinfo)
 {
     unsigned int i = 0;
     int res = 0;
 
-    if ( mem->nr_banks == 0 )
+    if ( kinfo->shminfo.nr_banks == 0 )
         return -ENOENT;
 
     /*
@@ -294,17 +294,17 @@ static int __init make_shm_memory_node(const struct domain *d, void *fdt,
      */
     dt_dprintk("Create xen-shmem node\n");
 
-    for ( ; i < mem->nr_banks; i++ )
+    for ( ; i < kinfo->shminfo.nr_banks; i++ )
     {
-        uint64_t start = mem->bank[i].start;
-        uint64_t size = mem->bank[i].size;
+        uint64_t start = kinfo->shminfo.bank[i].membank.start;
+        uint64_t size = kinfo->shminfo.bank[i].membank.size;
         const char compat[] = "xen,shared-memory-v1";
         /* Worst case addrcells + sizecells */
         __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS];
         __be32 *cells;
         unsigned int len = (addrcells + sizecells) * sizeof(__be32);
 
-        res = domain_fdt_begin_node(fdt, "xen-shmem", mem->bank[i].start);
+        res = domain_fdt_begin_node(fdt, "xen-shmem", start);
         if ( res )
             return res;
 
@@ -322,7 +322,7 @@ static int __init make_shm_memory_node(const struct domain *d, void *fdt,
         dt_dprintk("Shared memory bank %u: %#"PRIx64"->%#"PRIx64"\n",
                    i, start, start + size);
 
-        res = fdt_property_string(fdt, "xen,id", mem->bank[i].shm_id);
+        res = fdt_property_string(fdt, "xen,id", kinfo->shminfo.bank[i].shm_id);
         if ( res )
             return res;
 
@@ -350,7 +350,6 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
     const struct fdt_property *prop, *prop_id, *prop_role;
     const __be32 *cell;
     paddr_t paddr, gaddr, size, end;
-    struct meminfo *mem = &bootinfo.reserved_mem;
     unsigned int i;
     int len;
     bool owner = false;
@@ -429,17 +428,21 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
         return -EINVAL;
     }
 
-    for ( i = 0; i < mem->nr_banks; i++ )
+    for ( i = 0; i < bootinfo.shminfo.nr_nodes; i++ )
     {
+        paddr_t bank_start = bootinfo.shminfo.shm_nodes[i].membank->start;
+        paddr_t bank_size = bootinfo.shminfo.shm_nodes[i].membank->size;
+        const char *bank_id = bootinfo.shminfo.shm_nodes[i].node.shm_id;
+
         /*

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 445

Quoted 10 lines
xen/arch/arm/static-shmem.c +2 -2
          * Meet the following check:
          * 1) The shm ID matches and the region exactly match
          * 2) The shm ID doesn't match and the region doesn't overlap
          * with an existing one
          */
-        if ( paddr == mem->bank[i].start && size == mem->bank[i].size )
+        if ( paddr == bank_start && size == bank_size )
         {
-            if ( strncmp(shm_id, mem->bank[i].shm_id, MAX_SHM_ID_LENGTH) == 0 )
+            if ( strncmp(shm_id, bank_id, MAX_SHM_ID_LENGTH) == 0 )

In following else if you still use mem->bank[i].shm_id which results in a build failure. You should use bank_id there as well.

~Michal

#7 lore raw

PATCH v5 4/11 xen/arm: introduce allocate_domheap_memory and guest_physmap_memory

In reply to Penny Zheng

We split the code of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory.

One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping.

We also define a set of MACRO helpers to access common fields in data structure of "meminfo" type, e.g. "struct meminfo" is one of them, and later new "struct shm_meminfo" is also one of them.
This kind of structures must have the following characteristics:
- an array of "struct membank"
- a member called "nr_banks" indicating current array size
- a field indicating the maximum array size
When introducing a new data structure, according callbacks with function type "retrieve_fn" shall be defined for using MACRO helpers.
This commit defines callback "retrieve_meminfo" for data structure "struct meminfo".

---
v1 -> v2:
- define a set of MACRO helpers to access common fields in data structure of "meminfo" type. "struct meminfo" is one of them, and according callback "retrieve_meminfo" is also introduced here.
- typo of changing 1ULL to 1UL
---
v2 -> v3
- rebase and no changes
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/domain_build.c      | 119 +++++++++++++++++++++++++------
 xen/arch/arm/include/asm/setup.h |  33 +++++++++
 2 files changed, 129 insertions(+), 23 deletions(-)
xen/arch/arm/domain_build.c +96 -23
index 64ae944431..a8bc78baa5 100644
@@ -51,6 +51,28 @@ boolean_param("ext_regions", opt_ext_regions);
 static u64 __initdata dom0_mem;
 static bool __initdata dom0_mem_set;
 
+#ifdef CONFIG_DOM0LESS_BOOT
+static void __init retrieve_meminfo(void *mem, unsigned int *max_mem_banks,
+                                    struct membank **bank,
+                                    unsigned int **nr_banks)
+{
+    struct meminfo *meminfo = (struct meminfo *)mem;
+
+    if ( max_mem_banks )
+        *max_mem_banks = NR_MEM_BANKS;
+
+    if ( nr_banks )
+        *nr_banks = &(meminfo->nr_banks);
+
+    if ( bank )
+        *bank = meminfo->bank;
+}
+
+retrieve_fn __initdata retrievers[MAX_MEMINFO_TYPE] = {
+    [NORMAL_MEMINFO] = retrieve_meminfo,
+};
+#endif
+
 static int __init parse_dom0_mem(const char *s)
 {
     dom0_mem_set = true;
@@ -415,32 +437,20 @@ static void __init allocate_memory_11(struct domain *d,
 }
 
 #ifdef CONFIG_DOM0LESS_BOOT
-bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo,
-                                 gfn_t sgfn, paddr_t tot_size)
+static bool __init allocate_domheap_memory(struct domain *d,
+                                           paddr_t tot_size,
+                                           void *mem, enum meminfo_type type)
 {
-    int res;
     struct page_info *pg;
-    struct membank *bank;
     unsigned int max_order = ~0;
-
-    /*
-     * allocate_bank_memory can be called with a tot_size of zero for
-     * the second memory bank. It is not an error and we can safely
-     * avoid creating a zero-size memory bank.
-     */
-    if ( tot_size == 0 )
-        return true;
-
-    bank = &kinfo->mem.bank[kinfo->mem.nr_banks];
-    bank->start = gfn_to_gaddr(sgfn);
-    bank->size = tot_size;
+    unsigned int *nr_banks = GET_NR_BANKS(mem, type);
 
     while ( tot_size > 0 )
     {
         unsigned int order = get_allocation_size(tot_size);
+        struct membank *membank;
 
         order = min(max_order, order);
-
         pg = alloc_domheap_pages(d, order, 0);
         if ( !pg )
         {
@@ -460,15 +470,78 @@ bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo,
             continue;
         }
 
-        res = guest_physmap_add_page(d, sgfn, page_to_mfn(pg), order);
-        if ( res )
-        {
-            dprintk(XENLOG_ERR, "Failed map pages to DOMU: %d", res);
+        if ( *nr_banks == MAX_MEM_BANKS(type) )
             return false;
-        }
+
+        membank = GET_MEMBANK(mem, type, *nr_banks);
+        membank->start = mfn_to_maddr(page_to_mfn(pg));
+        membank->size = 1ULL << (PAGE_SHIFT + order);
+        (*nr_banks)++;
+        tot_size -= membank->size;
+    }
+
+    return true;
+}
+
+static int __init guest_physmap_memory(struct domain *d,
+                                       void *mem, enum meminfo_type type,
+                                       gfn_t sgfn)
+{
+    unsigned int i;
+    int res;
+    unsigned int *nr_banks = GET_NR_BANKS(mem, type);
+
+    for ( i = 0; i < *nr_banks; i++ )
+    {
+        struct membank *membank = GET_MEMBANK(mem, type, i);
+        paddr_t start = membank->start;
+        paddr_t size = membank->size;
+        unsigned int order = get_order_from_bytes(size);
+
+        /* Size must be power of two */
+        BUG_ON(!size || (size & (size - 1)));
+        res = guest_physmap_add_page(d, sgfn, maddr_to_mfn(start), order);
+        if ( res )
+            return res;
 
         sgfn = gfn_add(sgfn, 1UL << order);
-        tot_size -= (1ULL << (PAGE_SHIFT + order));
+    }
+
+    return 0;
+}
+
+bool __init allocate_bank_memory(struct domain *d,
+                                 struct kernel_info *kinfo,
+                                 gfn_t sgfn,
+                                 paddr_t total_size)
+{
+    struct membank *bank;
+    struct meminfo host = { 0 };
+
+    /*
+     * allocate_bank_memory can be called with a total_size of zero for
+     * the second memory bank. It is not an error and we can safely
+     * avoid creating a zero-size memory bank.
+     */
+    if ( total_size == 0 )
+        return true;
+
+    bank = &kinfo->mem.bank[kinfo->mem.nr_banks];
+    bank->start = gfn_to_gaddr(sgfn);
+    bank->size = total_size;
+
+    if ( !allocate_domheap_memory(d, total_size, (void *)&host, NORMAL_MEMINFO) )
+    {
+        printk(XENLOG_ERR "Failed to allocate (%"PRIpaddr"MB) pages to %pd\n",
+               total_size >> 20, d);
+        return false;
+    }
+
+    if ( guest_physmap_memory(d, (void *)&host, NORMAL_MEMINFO, sgfn) )
+    {
+        printk(XENLOG_ERR "Failed to map (%"PRIpaddr"MB) pages to %pd\n",
+               total_size >> 20, d);
+        return false;
     }
 
     kinfo->mem.nr_banks++;
xen/arch/arm/include/asm/setup.h +33 -0
index 3a2b35ea46..bc5f08be97 100644
@@ -57,6 +57,39 @@ struct meminfo {
     struct membank bank[NR_MEM_BANKS];
 };
 
+enum meminfo_type {
+    NORMAL_MEMINFO,
+    MAX_MEMINFO_TYPE,
+};
+
+/*
+ * Define a set of MACRO helpers to access meminfo_type, like "struct meminfo"
+ * as type of NORMAL_MEMINFO, etc.
+ * This kind of structure must have a array of "struct membank",
+ * a member called nr_banks indicating the current array size, and also a field
+ * indicating the maximum array size.
+ */
+typedef void (*retrieve_fn)(void *, unsigned int *, struct membank **,
+                            unsigned int **);
+
+#define MAX_MEM_BANKS(type) ({                              \
+    unsigned int _max_mem_banks;                            \
+    retrievers[type](NULL, &_max_mem_banks, NULL, NULL);    \
+    _max_mem_banks;                                         \
+})
+
+#define GET_MEMBANK(mem, type, index) ({                    \
+    struct membank *_bank;                                  \
+    retrievers[type]((void *)(mem), NULL, &_bank, NULL);    \
+    &(_bank[index]);                                        \
+})
+
+#define GET_NR_BANKS(mem, type) ({                          \
+    unsigned int *_nr_banks;                                \
+    retrievers[type]((void *)mem, NULL, NULL, &_nr_banks);  \
+    _nr_banks;                                              \
+})
+
 /*
  * The domU flag is set for kernels and ramdisks of "xen,domain" nodes.
  * The purpose of the domU flag is to avoid getting confused in
Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH v5 04/11] xen/arm: introduce allocate_domheap_memory and guest_physmap_memory

Hi Penny,

Quoted 9 lines · Penny Zheng

We split the code of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory.

One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping.

Quoted 11 lines

We also define a set of MACRO helpers to access common fields in data structure of "meminfo" type, e.g. "struct meminfo" is one of them, and later new "struct shm_meminfo" is also one of them.
This kind of structures must have the following characteristics:
- an array of "struct membank"
- a member called "nr_banks" indicating current array size
- a field indicating the maximum array size
When introducing a new data structure, according callbacks with function type "retrieve_fn" shall be defined for using MACRO helpers.
This commit defines callback "retrieve_meminfo" for data structure "struct meminfo".

This patch introduces quite a bit of complexity with all these helpers, so adding a rationale is crucial. AFAIU, all of this is because we don't want to reuse struct meminfo where NR_MEM_BANKS is defined as 256, which is a lot more than we need for shmem. Am I right?

I would like others to share the opinion here as well.

If we decide to go with this approach, what about static inline helpers instead of macros here for type checking and overall protection such as when there is no retriever function defined (at the moment it would most probably result in some trap).

Quoted 27 lines

---
v1 -> v2:
- define a set of MACRO helpers to access common fields in data structure of "meminfo" type. "struct meminfo" is one of them, and according callback "retrieve_meminfo" is also introduced here.
- typo of changing 1ULL to 1UL
---
v2 -> v3
- rebase and no changes
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/domain_build.c      | 119 +++++++++++++++++++++++++------
 xen/arch/arm/include/asm/setup.h |  33 +++++++++
 2 files changed, 129 insertions(+), 23 deletions(-)
xen/arch/arm/domain_build.c +0 -0
index 64ae944431..a8bc78baa5 100644

xen/arch/arm/domain_build.c boolean_param("ext_regions", opt_ext_regions); · line 55

Quoted 5 lines
xen/arch/arm/domain_build.c +2 -0
@@ -51,6 +51,28 @@ boolean_param("ext_regions", opt_ext_regions);
 static u64 __initdata dom0_mem;
 static bool __initdata dom0_mem_set;
 
+#ifdef CONFIG_DOM0LESS_BOOT
+static void __init retrieve_meminfo(void *mem, unsigned int *max_mem_banks,

const void *mem

Quoted 6 lines
xen/arch/arm/domain_build.c +6 -0
+                                    struct membank **bank,
+                                    unsigned int **nr_banks)
+{
+    struct meminfo *meminfo = (struct meminfo *)mem;
+
+    if ( max_mem_banks )

xen/arch/arm/domain_build.c boolean_param("ext_regions", opt_ext_regions); · line 71

Quoted 10 lines
xen/arch/arm/domain_build.c +10 -0
+        *max_mem_banks = NR_MEM_BANKS;
+
+    if ( nr_banks )
+        *nr_banks = &(meminfo->nr_banks);
+
+    if ( bank )
+        *bank = meminfo->bank;
+}
+
+retrieve_fn __initdata retrievers[MAX_MEMINFO_TYPE] = {

meminfo_retrievers

Quoted 24 lines
xen/arch/arm/domain_build.c +7 -7
+    [NORMAL_MEMINFO] = retrieve_meminfo,
+};
+#endif
+
 static int __init parse_dom0_mem(const char *s)
 {
     dom0_mem_set = true;
@@ -415,32 +437,20 @@ static void __init allocate_memory_11(struct domain *d,
 }
 
 #ifdef CONFIG_DOM0LESS_BOOT
-bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo,
-                                 gfn_t sgfn, paddr_t tot_size)
+static bool __init allocate_domheap_memory(struct domain *d,
+                                           paddr_t tot_size,
+                                           void *mem, enum meminfo_type type)
 {
-    int res;
     struct page_info *pg;
-    struct membank *bank;
     unsigned int max_order = ~0;
-
-    /*
-     * allocate_bank_memory can be called with a tot_size of zero for

xen/arch/arm/domain_build.c static void __init allocate_memory_11(struct domain *d, · line 446

Quoted 10 lines
xen/arch/arm/domain_build.c +1 -9
-     * the second memory bank. It is not an error and we can safely
-     * avoid creating a zero-size memory bank.
-     */
-    if ( tot_size == 0 )
-        return true;
-
-    bank = &kinfo->mem.bank[kinfo->mem.nr_banks];
-    bank->start = gfn_to_gaddr(sgfn);
-    bank->size = tot_size;
+    unsigned int *nr_banks = GET_NR_BANKS(mem, type);

what if mem is NULL? You would end up dereferencing NULL pointer

Quoted 12 lines
xen/arch/arm/domain_build.c +1 -1
     while ( tot_size > 0 )
     {
         unsigned int order = get_allocation_size(tot_size);
+        struct membank *membank;
 
         order = min(max_order, order);
-
         pg = alloc_domheap_pages(d, order, 0);
         if ( !pg )
         {

xen/arch/arm/domain_build.c bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, · line 473

Quoted 8 lines
xen/arch/arm/domain_build.c +1 -4
@@ -460,15 +470,78 @@ bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo,
             continue;
         }
 
-        res = guest_physmap_add_page(d, sgfn, page_to_mfn(pg), order);
-        if ( res )
-        {
-            dprintk(XENLOG_ERR, "Failed map pages to DOMU: %d", res);
+        if ( *nr_banks == MAX_MEM_BANKS(type) )

shouldn't you move this check at the beginning of the loop before the allocation? what if you enter this function with this condition being true.

xen/arch/arm/domain_build.c bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, · line 475

Quoted 3 lines
xen/arch/arm/domain_build.c +1 -1
             return false;
-        }
+

The name of the function does not reflect that apart from allocation you will also register the allocated memory regions.

xen/arch/arm/domain_build.c bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, · line 477

Quoted 2 lines
xen/arch/arm/domain_build.c +2 -0
+        membank = GET_MEMBANK(mem, type, *nr_banks);
+        membank->start = mfn_to_maddr(page_to_mfn(pg));

page_to_maddr

Quoted 5 lines
xen/arch/arm/domain_build.c +5 -0
+        membank->size = 1ULL << (PAGE_SHIFT + order);
+        (*nr_banks)++;
+        tot_size -= membank->size;
+    }
+

xen/arch/arm/domain_build.c bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, · line 492

Quoted 10 lines
xen/arch/arm/domain_build.c +10 -0
+    return true;
+}
+
+static int __init guest_physmap_memory(struct domain *d,
+                                       void *mem, enum meminfo_type type,
+                                       gfn_t sgfn)
+{
+    unsigned int i;
+    int res;
+    unsigned int *nr_banks = GET_NR_BANKS(mem, type);

what if mem is NULL? You would end up dereferencing NULL pointer

xen/arch/arm/domain_build.c bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, · line 505

Quoted 13 lines
xen/arch/arm/domain_build.c +13 -0
+
+    for ( i = 0; i < *nr_banks; i++ )
+    {
+        struct membank *membank = GET_MEMBANK(mem, type, i);
+        paddr_t start = membank->start;
+        paddr_t size = membank->size;
+        unsigned int order = get_order_from_bytes(size);
+
+        /* Size must be power of two */
+        BUG_ON(!size || (size & (size - 1)));
+        res = guest_physmap_add_page(d, sgfn, maddr_to_mfn(start), order);
+        if ( res )
+            return res;

Here, you return a rc, but ...

Quoted 26 lines
xen/arch/arm/domain_build.c +23 -1
         sgfn = gfn_add(sgfn, 1UL << order);
-        tot_size -= (1ULL << (PAGE_SHIFT + order));
+    }
+
+    return 0;
+}
+
+bool __init allocate_bank_memory(struct domain *d,
+                                 struct kernel_info *kinfo,
+                                 gfn_t sgfn,
+                                 paddr_t total_size)
+{
+    struct membank *bank;
+    struct meminfo host = { 0 };
+
+    /*
+     * allocate_bank_memory can be called with a total_size of zero for
+     * the second memory bank. It is not an error and we can safely
+     * avoid creating a zero-size memory bank.
+     */
+    if ( total_size == 0 )
+        return true;
+
+    bank = &kinfo->mem.bank[kinfo->mem.nr_banks];
+    bank->start = gfn_to_gaddr(sgfn);

xen/arch/arm/domain_build.c bool __init allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, · line 540

Quoted 10 lines
xen/arch/arm/domain_build.c +10 -0
+    bank->size = total_size;
+
+    if ( !allocate_domheap_memory(d, total_size, (void *)&host, NORMAL_MEMINFO) )
+    {
+        printk(XENLOG_ERR "Failed to allocate (%"PRIpaddr"MB) pages to %pd\n",
+               total_size >> 20, d);
+        return false;
+    }
+
+    if ( guest_physmap_memory(d, (void *)&host, NORMAL_MEMINFO, sgfn) )

... here you are not making use of it (to print the error code).

Quoted 12 lines
xen/arch/arm/domain_build.c +4 -0
+    {
+        printk(XENLOG_ERR "Failed to map (%"PRIpaddr"MB) pages to %pd\n",
+               total_size >> 20, d);
+        return false;
     }
 
     kinfo->mem.nr_banks++;
xen/arch/arm/include/asm/setup.h +0 -0
index 3a2b35ea46..bc5f08be97 100644

xen/arch/arm/include/asm/setup.h struct meminfo { · line 59

Quoted 3 lines
xen/arch/arm/include/asm/setup.h +0 -0
@@ -57,6 +57,39 @@ struct meminfo {
     struct membank bank[NR_MEM_BANKS];
 };
 

A comment with a meaning of each value would be handy

xen/arch/arm/include/asm/setup.h struct meminfo { · line 62

Quoted 3 lines
xen/arch/arm/include/asm/setup.h +3 -0
+enum meminfo_type {
+    NORMAL_MEMINFO,
+    MAX_MEMINFO_TYPE,

do you have a need for checking the max? If so, I would try to name the values in a similar, more informative way, e.g. MEMINFO_TYPE_NORMAL

xen/arch/arm/include/asm/setup.h struct meminfo { · line 70

Quoted 8 lines
xen/arch/arm/include/asm/setup.h +8 -0
+};
+
+/*
+ * Define a set of MACRO helpers to access meminfo_type, like "struct meminfo"
+ * as type of NORMAL_MEMINFO, etc.
+ * This kind of structure must have a array of "struct membank",
+ * a member called nr_banks indicating the current array size, and also a field
+ * indicating the maximum array size.

field? Looking at struct meminfo and then shm_meminfo you just have a macro to indicate max nr.

xen/arch/arm/include/asm/setup.h struct meminfo { · line 72

Quoted 2 lines
xen/arch/arm/include/asm/setup.h +2 -0
+ */
+typedef void (*retrieve_fn)(void *, unsigned int *, struct membank **,

I think MISRA requires that parameters should be named.
void * can be const since there is no helper modifying the passed structure (here and in macros when casting)

Quoted 24 lines
xen/arch/arm/include/asm/setup.h +20 -1
+                            unsigned int **);
+
+#define MAX_MEM_BANKS(type) ({                              \
+    unsigned int _max_mem_banks;                            \
+    retrievers[type](NULL, &_max_mem_banks, NULL, NULL);    \
+    _max_mem_banks;                                         \
+})
+
+#define GET_MEMBANK(mem, type, index) ({                    \
+    struct membank *_bank;                                  \
+    retrievers[type]((void *)(mem), NULL, &_bank, NULL);    \
+    &(_bank[index]);                                        \
+})
+
+#define GET_NR_BANKS(mem, type) ({                          \
+    unsigned int *_nr_banks;                                \
+    retrievers[type]((void *)mem, NULL, NULL, &_nr_banks);  \
+    _nr_banks;                                              \
+})
+
 /*
  * The domU flag is set for kernels and ramdisks of "xen,domain" nodes.
  * The purpose of the domU flag is to avoid getting confused in
--
Quoted 2 lines

2.25.1

~Michal

#9 lore raw

Re: [PATCH v5 04/11] xen/arm: introduce allocate_domheap_memory and guest_physmap_memory

Hi,

Quoted 23 lines · Michal Orzel

Hi Penny,

Quoted 9 lines · Penny Zheng

We split the code of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory.

One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping.

Quoted 11 lines

We also define a set of MACRO helpers to access common fields in data structure of "meminfo" type, e.g. "struct meminfo" is one of them, and later new "struct shm_meminfo" is also one of them.
This kind of structures must have the following characteristics:
- an array of "struct membank"
- a member called "nr_banks" indicating current array size
- a field indicating the maximum array size
When introducing a new data structure, according callbacks with function type "retrieve_fn" shall be defined for using MACRO helpers.
This commit defines callback "retrieve_meminfo" for data structure "struct meminfo".

Quoted 3 lines

This patch introduces quite a bit of complexity with all these helpers, so adding a rationale is crucial. AFAIU, all of this is because we don't want to reuse struct meminfo where NR_MEM_BANKS is defined as 256, which is a lot more than we need for shmem. Am I right?

+1.

Quoted 2 lines

I would like others to share the opinion here as well.

If possible, I'd like to reduce the footprint of the shared memory. But this should be balanced with the complexity of the code.

Briefly looking at the patch series, we have two structures:

struct meminfo {
    unsigned int nr_banks;
    struct membank bank[NR_MEM_BANKS];
};
struct shm_meminfo {
    unsigned int nr_banks;
    struct membank bank[NR_SHM_BANKS];
    paddr_t tot_size;
};

IIUC, the logic is mostly to be able to know the maximum size of the array and also the number of slots already used.

So we could have the following common structure:

struct membanks {
unsigned int nr_banks;
unsigned int max_banks;
struct membank *banks;
}

Then the definition for the two other structures could be:

struct meminfo {
    struct membank holders[NR_MEM_BANKS];

    struct membanks banks;
}
struct shm_meminfo {
    struct membank holders[NR_SHM_BANKS];

    struct membanks banks;

    paddr_t tot_size;
}

And then 'banks.banks' would point to the 'holders'. And 'max_banks' would be set to the maximum.

There might be other way to make the structure more nicer. Like (untested):

struct membanks {
    unsigned int nr_banks;
    unsigned int max_banks;
    struct membank[];
}
struct meminfo {
    struct membanks common;
    // We should ensure there are no padding
    struct membank[NR_MEM_BANKS];
}

We would then pass &meminfo.common to allocate_domainheap_memory().

With that there should be no need for extra helpers.

What do you think?

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

Re: [PATCH v5 04/11] xen/arm: introduce allocate_domheap_memory and guest_physmap_memory

Hi Julien,

Quoted 97 lines · Julien Grall

Hi,

Quoted 23 lines · Michal Orzel

Hi Penny,

Quoted 9 lines · Penny Zheng

We split the code of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory.

One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping.

Quoted 11 lines

We also define a set of MACRO helpers to access common fields in data structure of "meminfo" type, e.g. "struct meminfo" is one of them, and later new "struct shm_meminfo" is also one of them.
This kind of structures must have the following characteristics:
- an array of "struct membank"
- a member called "nr_banks" indicating current array size
- a field indicating the maximum array size
When introducing a new data structure, according callbacks with function type "retrieve_fn" shall be defined for using MACRO helpers.
This commit defines callback "retrieve_meminfo" for data structure "struct meminfo".

Quoted 3 lines

This patch introduces quite a bit of complexity with all these helpers, so adding a rationale is crucial. AFAIU, all of this is because we don't want to reuse struct meminfo where NR_MEM_BANKS is defined as 256, which is a lot more than we need for shmem. Am I right?

+1.

Quoted 2 lines

I would like others to share the opinion here as well.

If possible, I'd like to reduce the footprint of the shared memory. But this should be balanced with the complexity of the code.

Briefly looking at the patch series, we have two structures:

struct meminfo {
     unsigned int nr_banks;
     struct membank bank[NR_MEM_BANKS];
};
struct shm_meminfo {
     unsigned int nr_banks;
     struct membank bank[NR_SHM_BANKS];
     paddr_t tot_size;
};

IIUC, the logic is mostly to be able to know the maximum size of the array and also the number of slots already used.

So we could have the following common structure:

struct membanks {
    unsigned int nr_banks;
    unsigned int max_banks;
    struct membank *banks;
}

Then the definition for the two other structures could be:

struct meminfo {
     struct membank holders[NR_MEM_BANKS];

     struct membanks banks;
}
struct shm_meminfo {
     struct membank holders[NR_SHM_BANKS];

     struct membanks banks;

     paddr_t tot_size;
}

And then 'banks.banks' would point to the 'holders'. And 'max_banks' would be set to the maximum.

There might be other way to make the structure more nicer. Like (untested):

struct membanks {
     unsigned int nr_banks;
     unsigned int max_banks;
     struct membank[];
}
struct meminfo {
     struct membanks common;
     // We should ensure there are no padding
     struct membank[NR_MEM_BANKS];
}
Quoted 5 lines

We would then pass &meminfo.common to allocate_domainheap_memory().

With that there should be no need for extra helpers.

What do you think?

I would go for flexible array member solution which looks much nicer and as far as I can tell would solve the issue with extra helpers.

The only problem is that there are quite a lot of places where we reference nr_banks of meminfo e.g. mem.nr_banks which we would need to modify to mem.common.nr_banks. Maybe we could have *nr_banks in membanks that would point to nr_banks in meminfo/shm_meminfo? At some point we still need to set common.max_banks to e.g. NR_MEM_BANKS.

~Michal

#11 lore raw

Re: [PATCH v5 04/11] xen/arm: introduce allocate_domheap_memory and guest_physmap_memory

Hi Michal,

Quoted 100 lines · Michal Orzel
Quoted 99 lines · Julien Grall

Hi,

Quoted 23 lines · Michal Orzel

Hi Penny,

Quoted 9 lines · Penny Zheng

We split the code of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory.

One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping.

Quoted 11 lines

We also define a set of MACRO helpers to access common fields in data structure of "meminfo" type, e.g. "struct meminfo" is one of them, and later new "struct shm_meminfo" is also one of them.
This kind of structures must have the following characteristics:
- an array of "struct membank"
- a member called "nr_banks" indicating current array size
- a field indicating the maximum array size
When introducing a new data structure, according callbacks with function type "retrieve_fn" shall be defined for using MACRO helpers.
This commit defines callback "retrieve_meminfo" for data structure "struct meminfo".

Quoted 3 lines

This patch introduces quite a bit of complexity with all these helpers, so adding a rationale is crucial. AFAIU, all of this is because we don't want to reuse struct meminfo where NR_MEM_BANKS is defined as 256, which is a lot more than we need for shmem. Am I right?

+1.

Quoted 2 lines

I would like others to share the opinion here as well.

If possible, I'd like to reduce the footprint of the shared memory. But this should be balanced with the complexity of the code.

Briefly looking at the patch series, we have two structures:

struct meminfo {
      unsigned int nr_banks;
      struct membank bank[NR_MEM_BANKS];
};
struct shm_meminfo {
      unsigned int nr_banks;
      struct membank bank[NR_SHM_BANKS];
      paddr_t tot_size;
};

IIUC, the logic is mostly to be able to know the maximum size of the array and also the number of slots already used.

So we could have the following common structure:

struct membanks {
     unsigned int nr_banks;
     unsigned int max_banks;
     struct membank *banks;
}

Then the definition for the two other structures could be:

struct meminfo {
      struct membank holders[NR_MEM_BANKS];

      struct membanks banks;
}
struct shm_meminfo {
      struct membank holders[NR_SHM_BANKS];

      struct membanks banks;

      paddr_t tot_size;
}

And then 'banks.banks' would point to the 'holders'. And 'max_banks' would be set to the maximum.

There might be other way to make the structure more nicer. Like (untested):

struct membanks {
      unsigned int nr_banks;
      unsigned int max_banks;
      struct membank[];
}
struct meminfo {
      struct membanks common;
      // We should ensure there are no padding
      struct membank[NR_MEM_BANKS];
}

We would then pass &meminfo.common to allocate_domainheap_memory().

Quoted 8 lines
Quoted 3 lines

With that there should be no need for extra helpers.

What do you think?

I would go for flexible array member solution which looks much nicer and as far as I can tell would solve the issue with extra helpers.

The only problem is that there are quite a lot of places where we reference nr_banks of meminfo e.g. mem.nr_banks which we would need to modify to mem.common.nr_banks.

Possibly yes. But it is not clear what's the problem here. Are you concerned about the churn? Or is it just a long name?

At least in the case of meminfo. We could possibly replace all the use with a pointer to "struct membank common". This would reduce the amount of churn and the expression length.

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH v5 04/11] xen/arm: introduce allocate_domheap_memory and guest_physmap_memory

Quoted 108 lines · Julien Grall

Hi Michal,

Quoted 103 lines · Michal Orzel
Quoted 102 lines · Julien Grall

Hi,

Quoted 23 lines · Michal Orzel

Hi Penny,

Quoted 9 lines · Penny Zheng

We split the code of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory.

One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping.

Quoted 11 lines

We also define a set of MACRO helpers to access common fields in data structure of "meminfo" type, e.g. "struct meminfo" is one of them, and later new "struct shm_meminfo" is also one of them.
This kind of structures must have the following characteristics:
- an array of "struct membank"
- a member called "nr_banks" indicating current array size
- a field indicating the maximum array size
When introducing a new data structure, according callbacks with function type "retrieve_fn" shall be defined for using MACRO helpers.
This commit defines callback "retrieve_meminfo" for data structure "struct meminfo".

Quoted 3 lines

This patch introduces quite a bit of complexity with all these helpers, so adding a rationale is crucial. AFAIU, all of this is because we don't want to reuse struct meminfo where NR_MEM_BANKS is defined as 256, which is a lot more than we need for shmem. Am I right?

+1.

Quoted 2 lines

I would like others to share the opinion here as well.

If possible, I'd like to reduce the footprint of the shared memory. But this should be balanced with the complexity of the code.

Briefly looking at the patch series, we have two structures:

struct meminfo {
      unsigned int nr_banks;
      struct membank bank[NR_MEM_BANKS];
};
struct shm_meminfo {
      unsigned int nr_banks;
      struct membank bank[NR_SHM_BANKS];
      paddr_t tot_size;
};

IIUC, the logic is mostly to be able to know the maximum size of the array and also the number of slots already used.

So we could have the following common structure:

struct membanks {
     unsigned int nr_banks;
     unsigned int max_banks;
     struct membank *banks;
}

Then the definition for the two other structures could be:

struct meminfo {
      struct membank holders[NR_MEM_BANKS];

      struct membanks banks;
}
struct shm_meminfo {
      struct membank holders[NR_SHM_BANKS];

      struct membanks banks;

      paddr_t tot_size;
}

And then 'banks.banks' would point to the 'holders'. And 'max_banks' would be set to the maximum.

There might be other way to make the structure more nicer. Like (untested):

struct membanks {
      unsigned int nr_banks;
      unsigned int max_banks;
      struct membank[];
}
struct meminfo {
      struct membanks common;
      // We should ensure there are no padding
      struct membank[NR_MEM_BANKS];
}

We would then pass &meminfo.common to allocate_domainheap_memory().

With that there should be no need for extra helpers.

What do you think?

Quoted 8 lines
Quoted 5 lines

I would go for flexible array member solution which looks much nicer and as far as I can tell would solve the issue with extra helpers.

The only problem is that there are quite a lot of places where we reference nr_banks of meminfo e.g. mem.nr_banks which we would need to modify to mem.common.nr_banks.

Possibly yes. But it is not clear what's the problem here. Are you concerned about the churn? Or is it just a long name?

I am concerned about the churn. I did a grep and we have almost 100 instances of e.g. mem{.,->}nr_banks, which in our solution would need to be replaced with mem{.,->}common.nr_banks. That said ...

Quoted 4 lines

At least in the case of meminfo. We could possibly replace all the use with a pointer to "struct membank common". This would reduce the amount of churn and the expression length.

this could help to limit the overall churn.

~Michal

#13 lore raw

PATCH v5 5/11 xen/arm: use paddr_assigned to indicate whether host address is provided

In reply to Penny Zheng

We use paddr_assigned to indicate whether host address is provided, by checking the length of "xen,shared-mem" property.

The shm matching criteria shall also be adapt to cover the new scenario, by adding when host address is not provided, if SHMID matches, the region size must exactly match too.

During domain creation, right now, a static shared memory node could be banked with a statically configured host memory bank, or arbitrary host memory of dedicated size, which will later be allocated from heap by Xen. To cover both scenarios, we create a new structure shm_meminfo. It is very alike meminfo, but with the maximum array size being a smaller number NR_SHM_BANKS(16).
As "shm_meminfo" is also a new member of "enum meminfo_type", we shall implement its own callback "retrieve_shm_meminfo" to have access to all MACRO helpers, e.g. GET_MEMBANK(...).

Also, to make codes tidy and clear, we extract codes about parsing "xen,shared-mem" property from function "process_shm" and move them into a new helper "parse_shm_property".

---
v1 -> v2
- In order to get allocated banked host memory info during domain creat ion,
we create a new structure shm_meminfo. It is very alike meminfo, with the maximum array size being NR_SHM_BANKS. As shm_meminfo is a new member of type meminfo_type, we shall implement its own callback retrieve_shm_meminfo to have access to all MACRO helpers, e.g. GET_MEMBANK(...)
- rename "acquire_shm_memnode" to "find_shm_memnode"
---
v2 -> v3
- rebase and no changes
---
v3 -> v4:
- rebase and no change
---
v4 -> v5:
- fix bugs of that tot_size and membank shall not be member of union, but struct, to differentiate two types of static shared memory node.

---
 xen/arch/arm/domain_build.c             |   3 +
 xen/arch/arm/include/asm/setup.h        |  14 +-
 xen/arch/arm/include/asm/static-shmem.h |   3 +
 xen/arch/arm/static-shmem.c             | 360 ++++++++++++++++++------
 4 files changed, 293 insertions(+), 87 deletions(-)
xen/arch/arm/domain_build.c +3 -0
index a8bc78baa5..c69d481d34 100644
@@ -70,6 +70,9 @@ static void __init retrieve_meminfo(void *mem, unsigned int *max_mem_banks,
 
 retrieve_fn __initdata retrievers[MAX_MEMINFO_TYPE] = {
     [NORMAL_MEMINFO] = retrieve_meminfo,
+#ifdef CONFIG_STATIC_SHM
+    [SHM_MEMINFO] = retrieve_shm_meminfo,
+#endif
 };
 #endif
 
xen/arch/arm/include/asm/setup.h +13 -1
index bc5f08be97..043588cd2d 100644
@@ -59,6 +59,9 @@ struct meminfo {
 
 enum meminfo_type {
     NORMAL_MEMINFO,
+#ifdef CONFIG_STATIC_SHM
+    SHM_MEMINFO,
+#endif
     MAX_MEMINFO_TYPE,
 };
 
@@ -150,7 +153,16 @@ struct bootinfo {
         unsigned int nr_nodes;
         struct {
             struct shm_node node;
-            const struct membank *membank;
+            /*
+             * For a static shared memory node, it is either banked with
+             * a statically configured host memory bank, or arbitrary host
+             * memory which will be allocated by Xen with a specified total
+             * size(tot_size).
+             */
+            struct {
+                const struct membank *membank;
+                paddr_t tot_size;
+            };
         } shm_nodes[NR_MEM_BANKS];
     } shminfo;
 #endif
xen/arch/arm/include/asm/static-shmem.h +3 -0
index 66a3f4c146..a67445cec8 100644
@@ -24,6 +24,9 @@ static inline int process_shm_chosen(struct domain *d,
 int process_shm_node(const void *fdt, int node, uint32_t address_cells,
                      uint32_t size_cells);
 
+void retrieve_shm_meminfo(void *mem, unsigned int *max_mem_banks,
+                          struct membank **bank,
+                          unsigned int **nr_banks);
 #else /* !CONFIG_STATIC_SHM */
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
xen/arch/arm/static-shmem.c +274 -86

455 lines not on this page · raw message

Signature · 1 line
2.25.1
#14 lore raw

Re: [PATCH v5 05/11] xen/arm: use paddr_assigned to indicate whether host address is provided

Hi Penny,

Quoted 9 lines · Penny Zheng

We use paddr_assigned to indicate whether host address is provided, by checking the length of "xen,shared-mem" property.

The shm matching criteria shall also be adapt to cover the new scenario, by adding when host address is not provided, if SHMID matches, the region size must exactly match too.

Quoted 9 lines

During domain creation, right now, a static shared memory node could be banked with a statically configured host memory bank, or arbitrary host memory of dedicated size, which will later be allocated from heap by Xen. To cover both scenarios, we create a new structure shm_meminfo. It is very alike meminfo, but with the maximum array size being a smaller number NR_SHM_BANKS(16).
As "shm_meminfo" is also a new member of "enum meminfo_type", we shall implement its own callback "retrieve_shm_meminfo" to have access to all MACRO helpers, e.g. GET_MEMBANK(...).

Previous comments apply here as well and this patch depends on the decision of others \wrt previous patch. I'll just add generic remarks.

Quoted 109 lines

Also, to make codes tidy and clear, we extract codes about parsing "xen,shared-mem" property from function "process_shm" and move them into a new helper "parse_shm_property".

---
v1 -> v2
- In order to get allocated banked host memory info during domain creat ion,
we create a new structure shm_meminfo. It is very alike meminfo, with the maximum array size being NR_SHM_BANKS. As shm_meminfo is a new member of type meminfo_type, we shall implement its own callback retrieve_shm_meminfo to have access to all MACRO helpers, e.g. GET_MEMBANK(...)
- rename "acquire_shm_memnode" to "find_shm_memnode"
---
v2 -> v3
- rebase and no changes
---
v3 -> v4:
- rebase and no change
---
v4 -> v5:
- fix bugs of that tot_size and membank shall not be member of union, but struct, to differentiate two types of static shared memory node.

---
 xen/arch/arm/domain_build.c             |   3 +
 xen/arch/arm/include/asm/setup.h        |  14 +-
 xen/arch/arm/include/asm/static-shmem.h |   3 +
 xen/arch/arm/static-shmem.c             | 360 ++++++++++++++++++------
 4 files changed, 293 insertions(+), 87 deletions(-)
xen/arch/arm/domain_build.c +3 -0
index a8bc78baa5..c69d481d34 100644
@@ -70,6 +70,9 @@ static void __init retrieve_meminfo(void *mem, unsigned int *max_mem_banks,
 
 retrieve_fn __initdata retrievers[MAX_MEMINFO_TYPE] = {
     [NORMAL_MEMINFO] = retrieve_meminfo,
+#ifdef CONFIG_STATIC_SHM
+    [SHM_MEMINFO] = retrieve_shm_meminfo,
+#endif
 };
 #endif
 
xen/arch/arm/include/asm/setup.h +13 -1
index bc5f08be97..043588cd2d 100644
@@ -59,6 +59,9 @@ struct meminfo {
 
 enum meminfo_type {
     NORMAL_MEMINFO,
+#ifdef CONFIG_STATIC_SHM
+    SHM_MEMINFO,
+#endif
     MAX_MEMINFO_TYPE,
 };
 
@@ -150,7 +153,16 @@ struct bootinfo {
         unsigned int nr_nodes;
         struct {
             struct shm_node node;
-            const struct membank *membank;
+            /*
+             * For a static shared memory node, it is either banked with
+             * a statically configured host memory bank, or arbitrary host
+             * memory which will be allocated by Xen with a specified total
+             * size(tot_size).
+             */
+            struct {
+                const struct membank *membank;
+                paddr_t tot_size;
+            };
         } shm_nodes[NR_MEM_BANKS];
     } shminfo;
 #endif
xen/arch/arm/include/asm/static-shmem.h +3 -0
index 66a3f4c146..a67445cec8 100644
@@ -24,6 +24,9 @@ static inline int process_shm_chosen(struct domain *d,
 int process_shm_node(const void *fdt, int node, uint32_t address_cells,
                      uint32_t size_cells);
 
+void retrieve_shm_meminfo(void *mem, unsigned int *max_mem_banks,
+                          struct membank **bank,
+                          unsigned int **nr_banks);
 #else /* !CONFIG_STATIC_SHM */
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
xen/arch/arm/static-shmem.c +9 -0
index 6a3d8a54bd..a9eb26d543 100644
@@ -6,6 +6,50 @@
 #include <asm/domain_build.h>
 #include <asm/static-shmem.h>
 
+#define NR_SHM_BANKS 16
+
+/*
+ * There are two types of static shared memory node:
+ * A static shared memory node could be banked with a statically
+ * configured host memory bank, or a set of arbitrary host memory
+ * banks allocated from heap by Xen on runtime.
+ */
+struct shm_meminfo {

xen/arch/arm/static-shmem.c line 27

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+    unsigned int nr_banks;
+    struct membank bank[NR_SHM_BANKS];
+    paddr_t tot_size;
+};
+
+/*
+ * struct shm_memnode holds banked host memory info for a static
+ * shared memory node
+ */
+struct shm_memnode {

With the number of structures introduced in this series, the chosen naming does not help

Quoted 38 lines
xen/arch/arm/static-shmem.c +31 -0
+    char shm_id[MAX_SHM_ID_LENGTH];
+    struct shm_meminfo meminfo;
+};
+
+static __initdata struct {
+    unsigned int nr_nodes;
+    struct shm_memnode node[NR_MEM_BANKS];
+} shm_memdata;
+
+void __init retrieve_shm_meminfo(void *mem, unsigned int *max_mem_banks,
+                                 struct membank **bank,
+                                 unsigned int **nr_banks)
+{
+    struct shm_meminfo *meminfo = (struct shm_meminfo *)mem;
+
+    if ( max_mem_banks )
+        *max_mem_banks = NR_SHM_BANKS;
+
+    if ( nr_banks )
+        *nr_banks = &(meminfo->nr_banks);
+
+    if ( bank )
+        *bank = meminfo->bank;
+}
+
 static int __init acquire_nr_borrower_domain(const char *shm_id,
                                              unsigned long *nr_borrowers)
 {
@@ -172,6 +216,129 @@ static int __init append_shm_bank_to_domain(struct kernel_info *kinfo,
     return 0;
 }
 
+static struct shm_memnode * __init find_shm_memnode(const char *shm_id)
+{
+    unsigned int i;
+    struct shm_memnode *shm_memnode;
+
+    for ( i = 0 ; i < shm_memdata.nr_nodes; i++ )

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 234

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+    {
+        shm_memnode = &shm_memdata.node[i];
+
+        if ( strcmp(shm_id, shm_memnode->shm_id) == 0 )
+            return shm_memnode;
+    }
+
+    if ( i == NR_MEM_BANKS )
+        return NULL;
+

We can only be here if i == 0 i.e. no shm nodes?

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 237

Quoted 3 lines
xen/arch/arm/static-shmem.c +3 -0
+    shm_memnode = &shm_memdata.node[i];
+    safe_strcpy(shm_memnode->shm_id, shm_id);
+    shm_memdata.nr_nodes++;

leave one empty line here, please

Quoted 19 lines
xen/arch/arm/static-shmem.c +19 -0
+    return shm_memnode;
+}
+
+/* Parse "xen,shared-mem" property in static shared memory node */
+static struct shm_memnode * __init parse_shm_property(struct domain *d,
+                                    const struct dt_device_node *dt_node,
+                                    bool *paddr_assigned, paddr_t *gbase,
+                                    const char *shm_id)
+{
+    uint32_t addr_cells, size_cells;
+    const struct dt_property *prop;
+    const __be32 *cells;
+    uint32_t len;
+    unsigned int i;
+    paddr_t pbase, psize;
+    struct shm_memnode *shm_memnode;
+
+    /* xen,shared-mem = <pbase, gbase, size>; And pbase could be optional. */
+    prop = dt_find_property(dt_node, "xen,shared-mem", &len);

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 266

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+    BUG_ON(!prop);
+    cells = (const __be32 *)prop->value;
+
+    addr_cells = dt_n_addr_cells(dt_node);
+    size_cells = dt_n_size_cells(dt_node);
+    if ( len != dt_cells_to_size(addr_cells + size_cells + addr_cells) )
+    {
+        /* pbase is not provided in "xen,shared-mem" */
+        if ( len == dt_cells_to_size(size_cells + addr_cells) )
+            *paddr_assigned = false;

what if paddr_assigned is NULL? Also, wouldn't pbase_provided be a better name?

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 269

Quoted 3 lines
xen/arch/arm/static-shmem.c +3 -0
+        else
+        {
+            printk("fdt: invalid `xen,shared-mem` property.\n");

If you are modifying the code anyway, please drop '.' at the end of line. No need for that

Quoted 7 lines
xen/arch/arm/static-shmem.c +7 -0
+            return NULL;
+        }
+    }
+
+    /*
+     * If we firstly process the shared memory node with unique "xen,shm-id",
+     * we allocate a new member "shm_memnode" for it in shm_memdata.

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 286

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+     */
+    shm_memnode = find_shm_memnode(shm_id);
+    BUG_ON(!shm_memnode);
+    if ( !(*paddr_assigned) )
+    {
+        device_tree_get_reg(&cells, addr_cells, size_cells, gbase, &psize);
+        /* Whether it is a new shm node? */
+        if ( shm_memnode->meminfo.tot_size == 0 )
+            goto out_early1;
+        else

no need for this else

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 289

Quoted 3 lines
xen/arch/arm/static-shmem.c +3 -0
+            goto out_early2;
+    }
+    else

no need for this else. You can be here only if pbase was specified

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 295

Quoted 6 lines
xen/arch/arm/static-shmem.c +6 -0
+    {
+        device_tree_get_reg(&cells, addr_cells, addr_cells, &pbase, gbase);
+        psize = dt_read_number(cells, size_cells);
+
+        /* Whether it is a new shm node? */
+        if ( shm_memnode->meminfo.nr_banks != 0 )

In previous check with the same comment, you use tot_size to determine if it is a new shm mode

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 305

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+            goto out_early2;
+    }
+
+    /*
+     * The static shared memory node #dt_node is banked with a
+     * statically configured host memory bank.
+     */
+    shm_memnode->meminfo.bank[0].start = pbase;
+    shm_memnode->meminfo.bank[0].size = psize;
+    shm_memnode->meminfo.nr_banks = 1;

You should be first checking for below error conditions before doing assignment?

Quoted 7 lines
xen/arch/arm/static-shmem.c +7 -0
+
+    if ( !IS_ALIGNED(pbase, PAGE_SIZE) )
+    {
+        printk("%pd: physical address 0x%"PRIpaddr" is not suitably aligned.\n",
+               d, pbase);
+        return NULL;
+    }

xen/arch/arm/static-shmem.c static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, · line 322

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+
+    for ( i = 0; i < PFN_DOWN(psize); i++ )
+        if ( !mfn_valid(mfn_add(maddr_to_mfn(pbase), i)) )
+        {
+            printk("%pd: invalid physical MFN 0x%"PRI_mfn"\n for static shared memory node\n",
+                   d, mfn_x(mfn_add(maddr_to_mfn(pbase), i)));
+            return NULL;
+        }
+
+ out_early1:

not really informative. How about new_shm_node:?

Quoted 26 lines
xen/arch/arm/static-shmem.c +19 -0
+    if ( !IS_ALIGNED(psize, PAGE_SIZE) )
+    {
+        printk("%pd: size 0x%"PRIpaddr" is not suitably aligned\n",
+               d, psize);
+        return NULL;
+    }
+    shm_memnode->meminfo.tot_size = psize;
+
+ out_early2:
+    if ( !IS_ALIGNED(*gbase, PAGE_SIZE) )
+    {
+        printk("%pd: guest address 0x%"PRIpaddr" is not suitably aligned.\n",
+               d, *gbase);
+        return NULL;
+    }
+
+    return shm_memnode;
+}
+
 int __init process_shm(struct domain *d, struct kernel_info *kinfo,
                        const struct dt_device_node *node)
 {
@@ -179,51 +346,17 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
 
     dt_for_each_child_node(node, shm_node)
     {

xen/arch/arm/static-shmem.c int __init process_shm(struct domain *d, struct kernel_info *kinfo, · line 354

Quoted 10 lines
xen/arch/arm/static-shmem.c +1 -4
-        const struct dt_property *prop;
-        const __be32 *cells;
-        uint32_t addr_cells, size_cells;
         paddr_t gbase, pbase, psize;
         int ret = 0;
-        unsigned int i;
         const char *role_str;
         const char *shm_id;
         bool owner_dom_io = true;
+        bool paddr_assigned = true;

wouldn't it be better if parse_shm_property was responsible for setting this to either false or true. At the moment it only sets it to false.

Quoted 41 lines
xen/arch/arm/static-shmem.c +1 -32
+        struct shm_memnode *shm_memnode;
 
         if ( !dt_device_is_compatible(shm_node, "xen,domain-shared-memory-v1") )
             continue;
 
-        /*
-         * xen,shared-mem = <pbase, gbase, size>;
-         * TODO: pbase is optional.
-         */
-        addr_cells = dt_n_addr_cells(shm_node);
-        size_cells = dt_n_size_cells(shm_node);
-        prop = dt_find_property(shm_node, "xen,shared-mem", NULL);
-        BUG_ON(!prop);
-        cells = (const __be32 *)prop->value;
-        device_tree_get_reg(&cells, addr_cells, addr_cells, &pbase, &gbase);
-        psize = dt_read_paddr(cells, size_cells);
-        if ( !IS_ALIGNED(pbase, PAGE_SIZE) || !IS_ALIGNED(gbase, PAGE_SIZE) )
-        {
-            printk("%pd: physical address 0x%"PRIpaddr", or guest address 0x%"PRIpaddr" is not suitably aligned.\n",
-                   d, pbase, gbase);
-            return -EINVAL;
-        }
-        if ( !IS_ALIGNED(psize, PAGE_SIZE) )
-        {
-            printk("%pd: size 0x%"PRIpaddr" is not suitably aligned\n",
-                   d, psize);
-            return -EINVAL;
-        }
-
-        for ( i = 0; i < PFN_DOWN(psize); i++ )
-            if ( !mfn_valid(mfn_add(maddr_to_mfn(pbase), i)) )
-            {
-                printk("%pd: invalid physical address 0x%"PRI_mfn"\n",
-                       d, mfn_x(mfn_add(maddr_to_mfn(pbase), i)));
-                return -EINVAL;
-            }
-
         /*
          * "role" property is optional and if it is defined explicitly,
          * then the owner domain is not the default "dom_io" domain.

xen/arch/arm/static-shmem.c int __init process_shm(struct domain *d, struct kernel_info *kinfo, · line 377

Quoted 7 lines
xen/arch/arm/static-shmem.c +4 -0
@@ -238,6 +371,13 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
         }
         BUG_ON((strlen(shm_id) <= 0) || (strlen(shm_id) >= MAX_SHM_ID_LENGTH));
 
+        shm_memnode = parse_shm_property(d, shm_node, &paddr_assigned, &gbase,
+                                         shm_id);
+        if ( !shm_memnode )
+            return -EINVAL;

empty line here please

Quoted 93 lines
xen/arch/arm/static-shmem.c +40 -20
+        pbase = shm_memnode->meminfo.bank[0].start;
+        psize = shm_memnode->meminfo.bank[0].size;
+
         /*
          * DOMID_IO is a fake domain and is not described in the Device-Tree.
          * Therefore when the owner of the shared region is DOMID_IO, we will
@@ -349,10 +489,10 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
 {
     const struct fdt_property *prop, *prop_id, *prop_role;
     const __be32 *cell;
-    paddr_t paddr, gaddr, size, end;
+    paddr_t paddr, gaddr, size;
     unsigned int i;
     int len;
-    bool owner = false;
+    bool owner = false, paddr_assigned = true;
     const char *shm_id;
 
     if ( address_cells < 1 || size_cells < 1 )
@@ -404,96 +544,140 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
 
     if ( len != dt_cells_to_size(address_cells + size_cells + address_cells) )
     {
+        /* paddr is not provided in "xen,shared-mem" */
         if ( len == dt_cells_to_size(size_cells + address_cells) )
-            printk("fdt: host physical address must be chosen by users at the moment.\n");
-
-        printk("fdt: invalid `xen,shared-mem` property.\n");
-        return -EINVAL;
+            paddr_assigned = false;
+        else
+        {
+            printk("fdt: invalid `xen,shared-mem` property.\n");
+            return -EINVAL;
+        }
     }
 
     cell = (const __be32 *)prop->data;
-    device_tree_get_reg(&cell, address_cells, address_cells, &paddr, &gaddr);
-    size = dt_next_cell(size_cells, &cell);
-
-    if ( !size )
+    if ( !paddr_assigned )
+        device_tree_get_reg(&cell, address_cells, size_cells, &gaddr, &size);
+    else
     {
-        printk("fdt: the size for static shared memory region can not be zero\n");
-        return -EINVAL;
-    }
+        paddr_t end;
+
+        device_tree_get_reg(&cell, address_cells, address_cells, &paddr, &gaddr);
+        size = dt_next_cell(size_cells, &cell);
+
+        if ( !size )
+        {
+            printk("fdt: the size for static shared memory region can not be zero\n");
+            return -EINVAL;
+        }
+
+        end = paddr + size;
+        if ( end <= paddr )
+        {
+            printk("fdt: static shared memory region %s overflow\n", shm_id);
+            return -EINVAL;
+        }
 
-    end = paddr + size;
-    if ( end <= paddr )
-    {
-        printk("fdt: static shared memory region %s overflow\n", shm_id);
-        return -EINVAL;
     }
 
     for ( i = 0; i < bootinfo.shminfo.nr_nodes; i++ )
     {
-        paddr_t bank_start = bootinfo.shminfo.shm_nodes[i].membank->start;
-        paddr_t bank_size = bootinfo.shminfo.shm_nodes[i].membank->size;
         const char *bank_id = bootinfo.shminfo.shm_nodes[i].node.shm_id;
+        bool is_shmid_equal = strncmp(shm_id, bank_id, MAX_SHM_ID_LENGTH) == 0 ?
+                              true : false;
 
         /*
          * Meet the following check:
+         * when host address is provided:
          * 1) The shm ID matches and the region exactly match
          * 2) The shm ID doesn't match and the region doesn't overlap
          * with an existing one
+         * when host address is not provided:
+         * 1) The shm ID matches and the region size exactly match
+         */
+        /*
+         * For a static shared memory node, it is either banked with

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 606

Quoted 10 lines
xen/arch/arm/static-shmem.c +6 -2
+         * a statically configured host memory bank(membank != NULL), or
+         * arbitrary host memory which will later be allocated by Xen(
+         * tot_size != 0).
          */
-        if ( paddr == bank_start && size == bank_size )
+        if ( bootinfo.shminfo.shm_nodes[i].membank != NULL )
         {
-            if ( strncmp(shm_id, bank_id, MAX_SHM_ID_LENGTH) == 0 )
+            paddr_t bank_start = bootinfo.shminfo.shm_nodes[i].membank->start;
+            paddr_t bank_size = bootinfo.shminfo.shm_nodes[i].membank->size;

both lines > 80 chars

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 617

Quoted 12 lines
xen/arch/arm/static-shmem.c +5 -1
+            bool is_same_region = (paddr == bank_start) && (size == bank_size) ?
+                                  true : false;
+
+            if ( is_same_region && is_shmid_equal )
                 break;
-            else
+            else if ( is_same_region || is_shmid_equal )
             {
                 printk("fdt: xen,shm-id %s does not match for all the nodes using the same region.\n",
                        shm_id);
                 return -EINVAL;
             }

continue here and ...

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 619

Quoted 4 lines
xen/arch/arm/static-shmem.c +0 -2
         }
-        else if ( strcmp(shm_id, mem->bank[i].shm_id) != 0 )
-            continue;
         else

no need for this else

Quoted 21 lines
xen/arch/arm/static-shmem.c +17 -3
         {
-            printk("fdt: different shared memory region could not share the same shm ID %s\n",
-                   shm_id);
-            return -EINVAL;
+            paddr_t tot_size = bootinfo.shminfo.shm_nodes[i].tot_size;
+            bool is_same_region = tot_size == size ? true : false;
+
+            if ( !paddr_assigned && is_same_region && is_shmid_equal )
+                break;
+            else if ( is_shmid_equal )
+            {
+                if ( paddr_assigned )
+                {
+                    printk("fdt: two different types of static shared memory region could not share the same shm-id %s\n",
+                           shm_id);
+                    return -EINVAL;
+                }
+
+                printk("fdt: when host address is not provided, if xen,shm-id matches, size must stay the same too(%"PRIpaddr" -> %"PRIpaddr")\n",
+                       size, tot_size);
+                return -EINVAL;

xen/arch/arm/static-shmem.c int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, · line 644

Quoted 10 lines
xen/arch/arm/static-shmem.c +2 -3
+            }
         }
     }
 
     if ( i == bootinfo.shminfo.nr_nodes )
     {
-        struct meminfo *mem = &bootinfo.reserved_mem;
-
-        if ( (i < NR_MEM_BANKS) && (mem->nr_banks < NR_MEM_BANKS) )
+        if ( i < NR_MEM_BANKS )

if ( i == NR_MEM_BANKS )
goto fail;
this would reduce the number of if/else blocks

Quoted 70 lines
xen/arch/arm/static-shmem.c +31 -20
         {
-            struct membank *membank = &mem->bank[mem->nr_banks];
             struct shm_node *shm_node = &bootinfo.shminfo.shm_nodes[i].node;
-
-            if ( check_reserved_regions_overlap(paddr, size) )
-                return -EINVAL;
-
-            /* Static shared memory shall be reserved from any other use. */
-            membank->start = paddr;
-            membank->size = size;
-            membank->type = MEMBANK_STATIC_DOMAIN;
-            mem->nr_banks++;
+            struct meminfo *mem = &bootinfo.reserved_mem;
 
             /* Record static shared memory node info in bootinfo.shminfo */
             safe_strcpy(shm_node->shm_id, shm_id);
-            /*
-             * Reserved memory bank is recorded together to assist
-             * doing shm node verification.
-             */
-            bootinfo.shminfo.shm_nodes[i].membank = membank;
             bootinfo.shminfo.nr_nodes++;
+
+            if ( !paddr_assigned )
+                bootinfo.shminfo.shm_nodes[i].tot_size = size;
+            else if ( mem->nr_banks < NR_MEM_BANKS )
+            {
+                struct membank *membank = &mem->bank[mem->nr_banks];
+
+                if ( check_reserved_regions_overlap(paddr, size) )
+                    return -EINVAL;
+
+                /* Static shared memory shall be reserved from any other use. */
+                membank->start = paddr;
+                membank->size = size;
+                membank->type = MEMBANK_STATIC_DOMAIN;
+                mem->nr_banks++;
+
+                /*
+                 * Reserved memory bank is recorded together to assist
+                 * doing shm node verification.
+                 */
+                bootinfo.shminfo.shm_nodes[i].membank = membank;
+            }
+            else
+                goto fail;
         }
         else
-        {
-            printk("Warning: Max number of supported memory regions reached.\n");
-            return -ENOSPC;
-        }
+            goto fail;
     }
+
     /*
      * keep a count of the number of borrowers, which later may be used
      * to calculate the reference count.
@@ -502,6 +686,10 @@ int __init process_shm_node(const void *fdt, int node, uint32_t address_cells,
         bootinfo.shminfo.shm_nodes[i].node.nr_shm_borrowers++;
 
     return 0;
+
+ fail:
+    printk("Warning: Max number of supported memory regions reached.\n");
+    return -ENOSPC;
 }
 
 int __init make_resv_memory_node(const struct domain *d, void *fdt,
--
Quoted 2 lines

2.25.1

~Michal

#15 lore raw

PATCH v5 6/11 xen/arm: support static shared memory when host address not provided

In reply to Penny Zheng

In order to support static shared memory when host address not provided, we shall do the following modification:
- we shall let Xen allocate memory from heap for static shared memory at first domain, no matter it is owner or borrower.
- In acquire_shared_memory_bank, as static shared memory has already been allocated from heap, we shall assign them to the owner domain using function "assign_pages".
- Function get_shm_pages_reference is created to add as many additional reference as the number of borrowers.
- We implement a new helper "add_foreign_mapping_for_borrower" to set up foreign memory mapping for borrower.

Instead of using multiple function parameters to deliver various shm-related info, like host physical address, SHMID, etc, and with the introduction of new struct "shm_memnode" to include banked host memory info, we switch to use "shm_memnode" as function parameter to replace them all, to make codes more clear and tidy.

---
v1 -> v2:
- combine commits 4 - 6 in Serie 1
- Adapt to changes of introducing "struct shm_memnode"
---
v2 -> v3
- fix infinite loop bug and bad indentation
- rebase
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/domain_build.c             |   6 +-
 xen/arch/arm/include/asm/domain_build.h |   5 +
 xen/arch/arm/static-shmem.c             | 223 ++++++++++++++++--------
 3 files changed, 163 insertions(+), 71 deletions(-)
xen/arch/arm/domain_build.c +3 -3
index c69d481d34..c58996e3e9 100644
@@ -440,9 +440,9 @@ static void __init allocate_memory_11(struct domain *d,
 }
 
 #ifdef CONFIG_DOM0LESS_BOOT
-static bool __init allocate_domheap_memory(struct domain *d,
-                                           paddr_t tot_size,
-                                           void *mem, enum meminfo_type type)
+bool __init allocate_domheap_memory(struct domain *d,
+                                    paddr_t tot_size,
+                                    void *mem, enum meminfo_type type)
 {
     struct page_info *pg;
     unsigned int max_order = ~0;
xen/arch/arm/include/asm/domain_build.h +5 -0
index da9e6025f3..1b75a4c6a8 100644
@@ -51,6 +51,11 @@ static inline int prepare_acpi(struct domain *d, struct kernel_info *kinfo)
 int prepare_acpi(struct domain *d, struct kernel_info *kinfo);
 #endif
 
+#ifdef CONFIG_DOM0LESS_BOOT
+bool allocate_domheap_memory(struct domain *d, paddr_t tot_size,
+                             void *mem, enum meminfo_type type);
+#endif
+
 #endif
 
 /*
xen/arch/arm/static-shmem.c +155 -68

316 lines not on this page · raw message

Signature · 1 line
2.25.1
#16 lore raw

PATCH v5 7/11 xen/arm: remove shm holes for extended regions

In reply to Penny Zheng

Static shared memory acts as reserved memory in guest, so it shall be excluded from extended regions.

Extended regions are taken care of under three different scenarios:
normal DomU, direct-map domain with iommu on, and direct-map domain with iommu off.

For normal DomU, we create a new function "remove_shm_holes_for_domU", to firstly transfer original outputs into the format of "struct rangeset", then use "remove_shm_from_rangeset" to remove static shm from them.

For direct-map domain with iommu on, after we get guest shm info from "kinfo", we use "remove_shm_from_rangeset" to remove static shm.

For direct-map domain with iommu off, as static shm has already been taken care of through reserved memory banks, we do nothing.

---
v1 -> v2:
- new commit
---
v2 -> v3:
- error out non-zero res before remove_shm_holes_for_domU
- rebase
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/domain_build.c             | 19 +++++-
 xen/arch/arm/include/asm/domain_build.h |  2 +
 xen/arch/arm/include/asm/static-shmem.h | 17 +++++
 xen/arch/arm/static-shmem.c             | 83 +++++++++++++++++++++++++
 4 files changed, 118 insertions(+), 3 deletions(-)
xen/arch/arm/domain_build.c +16 -3
index c58996e3e9..e040f8a6d9 100644
@@ -887,8 +887,8 @@ int __init make_memory_node(const struct domain *d,
     return res;
 }
 
-static int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn,
-                                  void *data)
+int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn,
+                           void *data)
 {
     struct meminfo *ext_regions = data;
     paddr_t start, size;
@@ -1062,6 +1062,8 @@ static int __init handle_pci_range(const struct dt_device_node *dev,
  * - MMIO
  * - Host RAM
  * - PCI aperture
+ * - Static shared memory regions, which are described by special property
+ *   "xen,static-shm"
  */
 static int __init find_memory_holes(const struct kernel_info *kinfo,
                                     struct meminfo *ext_regions)
@@ -1078,6 +1080,14 @@ static int __init find_memory_holes(const struct kernel_info *kinfo,
     if ( !mem_holes )
         return -ENOMEM;
 
+    /* Remove static shared memory regions */
+    if ( kinfo->shminfo.nr_banks != 0 )
+    {
+        res = remove_shm_from_rangeset(kinfo, mem_holes);
+        if ( res )
+            goto out;
+    }
+
     /* Start with maximum possible addressable physical memory range */
     start = 0;
     end = (1ULL << p2m_ipa_bits) - 1;
@@ -1180,7 +1190,10 @@ static int __init find_domU_holes(const struct kernel_info *kinfo,
         res = 0;
     }
 
-    return res;
+    if ( res )
+        return res;
+
+    return remove_shm_holes_for_domU(kinfo, ext_regions);
 }
 
 int __init make_hypervisor_node(struct domain *d,
xen/arch/arm/include/asm/domain_build.h +2 -0
index 1b75a4c6a8..0433e76e68 100644
@@ -56,6 +56,8 @@ bool allocate_domheap_memory(struct domain *d, paddr_t tot_size,
                              void *mem, enum meminfo_type type);
 #endif
 
+int add_ext_regions(unsigned long s_gfn, unsigned long e_gfn, void *data);
+
 #endif
 
 /*
xen/arch/arm/include/asm/static-shmem.h +17 -0
index a67445cec8..d149985291 100644
@@ -27,6 +27,12 @@ int process_shm_node(const void *fdt, int node, uint32_t address_cells,
 void retrieve_shm_meminfo(void *mem, unsigned int *max_mem_banks,
                           struct membank **bank,
                           unsigned int **nr_banks);
+
+int remove_shm_from_rangeset(const struct kernel_info *kinfo,
+                             struct rangeset *rangeset);
+
+int remove_shm_holes_for_domU(const struct kernel_info *kinfo,
+                              struct meminfo *orig_ext);
 #else /* !CONFIG_STATIC_SHM */
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
@@ -55,6 +61,17 @@ static inline int process_shm_node(const void *fdt, int node,
     return -EINVAL;
 }
 
+static inline int remove_shm_from_rangeset(const struct kernel_info *kinfo,
+                                           struct rangeset *rangeset)
+{
+    return 0;
+}
+
+static inline int remove_shm_holes_for_domU(const struct kernel_info *kinfo,
+                                            struct meminfo *orig_ext)
+{
+    return 0;
+}
 #endif /* CONFIG_STATIC_SHM */
 
 #endif /* __ASM_STATIC_SHMEM_H_ */
xen/arch/arm/static-shmem.c +83 -0

95 lines not on this page · raw message

Signature · 1 line
2.25.1
#17 lore raw

PATCH v5 8/11 xen/p2m: put reference for superpage

In reply to Penny Zheng

We are doing foreign memory mapping for static shared memory, and there is a great possibility that it could be super mapped. But today, p2m_put_l3_page could not handle superpages.

This commits implements a new function p2m_put_superpage to handle superpages, specifically for helping put extra references for foreign superpages.

---
v1 -> v2:
- new commit
---
v2 -> v3:
- rebase and no change
---
v3 -> v4:
rebase and no change
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/mmu/p2m.c | 58 +++++++++++++++++++++++++++++++-----------
 1 file changed, 43 insertions(+), 15 deletions(-)
xen/arch/arm/mmu/p2m.c +43 -15
index 6a5a080307..810c89397c 100644
@@ -752,17 +752,9 @@ static int p2m_mem_access_radix_set(struct p2m_domain *p2m, gfn_t gfn,
     return rc;
 }
 
-/*
- * Put any references on the single 4K page referenced by pte.
- * TODO: Handle superpages, for now we only take special references for leaf
- * pages (specifically foreign ones, which can't be super mapped today).
- */
-static void p2m_put_l3_page(const lpae_t pte)
+/* Put any references on the single 4K page referenced by mfn. */
+static void p2m_put_l3_page(mfn_t mfn, unsigned type)
 {
-    mfn_t mfn = lpae_get_mfn(pte);
-
-    ASSERT(p2m_is_valid(pte));
-
     /*
      * TODO: Handle other p2m types
      *
@@ -770,16 +762,53 @@ static void p2m_put_l3_page(const lpae_t pte)
      * flush the TLBs if the page is reallocated before the end of
      * this loop.
      */
-    if ( p2m_is_foreign(pte.p2m.type) )
+    if ( p2m_is_foreign(type) )
     {
         ASSERT(mfn_valid(mfn));
         put_page(mfn_to_page(mfn));
     }
     /* Detect the xenheap page and mark the stored GFN as invalid. */
-    else if ( p2m_is_ram(pte.p2m.type) && is_xen_heap_mfn(mfn) )
+    else if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) )
         page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN);
 }
 
+/* Put any references on the superpage referenced by mfn. */
+static void p2m_put_superpage(mfn_t mfn, unsigned int next_level, unsigned type)
+{
+    unsigned int i;
+    unsigned int level_order = XEN_PT_LEVEL_ORDER(next_level);
+
+    for ( i = 0; i < XEN_PT_LPAE_ENTRIES; i++ )
+    {
+        if ( next_level == 3 )
+            p2m_put_l3_page(mfn, type);
+        else
+            p2m_put_superpage(mfn, next_level + 1, type);
+
+        mfn = mfn_add(mfn, 1 << level_order);
+    }
+}
+
+/* Put any references on the page referenced by pte. */
+static void p2m_put_page(const lpae_t pte, unsigned int level)
+{
+    mfn_t mfn = lpae_get_mfn(pte);
+
+    ASSERT(p2m_is_valid(pte));
+
+    /*
+     * We are either having a first level 1G superpage or a
+     * second level 2M superpage.
+     */
+    if ( p2m_is_superpage(pte, level) )
+        return p2m_put_superpage(mfn, level + 1, pte.p2m.type);
+    else
+    {
+        ASSERT(level == 3);
+        return p2m_put_l3_page(mfn, pte.p2m.type);
+    }
+}
+
 /* Free lpae sub-tree behind an entry */
 static void p2m_free_entry(struct p2m_domain *p2m,
                            lpae_t entry, unsigned int level)
@@ -808,9 +837,8 @@ static void p2m_free_entry(struct p2m_domain *p2m,
 #endif
 
         p2m->stats.mappings[level]--;
-        /* Nothing to do if the entry is a super-page. */
-        if ( level == 3 )
-            p2m_put_l3_page(entry);
+        p2m_put_page(entry, level);
+
         return;
     }
 
Signature · 1 line
2.25.1
#18 lore raw

PATCH v5 9/11 xen/docs: refine docs about static shared memory

In reply to Penny Zheng

This commit amends docs(docs/misc/arm/device-tree/booting.txt) to include the new scenario where host address is not provided in "xen,shared-mem" property, and we also add a new example to explain in detail.

We also fix some buggy info in the docs, like SHMID is "my-shared-mem-1", not "0x1".

---
 docs/misc/arm/device-tree/booting.txt | 52 ++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 13 deletions(-)
docs/misc/arm/device-tree/booting.txt +39 -13
index bbd955e9c2..ac4bad6fe5 100644
@@ -590,7 +590,7 @@ communication.
     An array takes a physical address, which is the base address of the
     shared memory region in host physical address space, a size, and a guest
     physical address, as the target address of the mapping.
-    e.g. xen,shared-mem = < [host physical address] [guest address] [size] >
+    e.g. xen,shared-mem = < [host physical address] [guest address] [size] >;
 
     It shall also meet the following criteria:
     1) If the SHM ID matches with an existing region, the address range of the
@@ -601,8 +601,8 @@ communication.
     The number of cells for the host address (and size) is the same as the
     guest pseudo-physical address and they are inherited from the parent node.
 
-    Host physical address is optional, when missing Xen decides the location
-    (currently unimplemented).
+    Host physical address is optional, when missing Xen decides the location.
+    e.g. xen,shared-mem = < [guest address] [size] >;
 
 - role (Optional)
 
@@ -629,7 +629,7 @@ chosen {
         role = "owner";
         xen,shm-id = "my-shared-mem-0";
         xen,shared-mem = <0x10000000 0x10000000 0x10000000>;
-    }
+    };
 
     domU1 {
         compatible = "xen,domain";
@@ -640,25 +640,36 @@ chosen {
         vpl011;
 
         /*
-         * shared memory region identified as 0x0(xen,shm-id = <0x0>)
-         * is shared between Dom0 and DomU1.
+         * shared memory region "my-shared-mem-0" is shared
+         * between Dom0 and DomU1.
          */
         domU1-shared-mem@10000000 {
             compatible = "xen,domain-shared-memory-v1";
             role = "borrower";
             xen,shm-id = "my-shared-mem-0";
             xen,shared-mem = <0x10000000 0x50000000 0x10000000>;
-        }
+        };
 
         /*
-         * shared memory region identified as 0x1(xen,shm-id = <0x1>)
-         * is shared between DomU1 and DomU2.
+         * shared memory region "my-shared-mem-1" is shared between
+         * DomU1 and DomU2.
          */
         domU1-shared-mem@50000000 {
             compatible = "xen,domain-shared-memory-v1";
             xen,shm-id = "my-shared-mem-1";
             xen,shared-mem = <0x50000000 0x60000000 0x20000000>;
-        }
+        };
+
+        /*
+         * shared memory region "my-shared-mem-2" is shared between
+         * DomU1 and DomU2.
+         */
+        domU1-shared-mem-2 {
+            compatible = "xen,domain-shared-memory-v1";
+            xen,shm-id = "my-shared-mem-2";
+            role = "owner";
+            xen,shared-mem = <0x80000000 0x20000000>;
+        };
 
         ......
 
@@ -672,14 +683,21 @@ chosen {
         cpus = <1>;
 
         /*
-         * shared memory region identified as 0x1(xen,shm-id = <0x1>)
-         * is shared between domU1 and domU2.
+         * shared memory region "my-shared-mem-1" is shared between
+         * domU1 and domU2.
          */
         domU2-shared-mem@50000000 {
             compatible = "xen,domain-shared-memory-v1";
             xen,shm-id = "my-shared-mem-1";
             xen,shared-mem = <0x50000000 0x70000000 0x20000000>;
-        }
+        };
+
+        domU2-shared-mem-2 {
+            compatible = "xen,domain-shared-memory-v1";
+            xen,shm-id = "my-shared-mem-2";
+            role = "borrower";
+            xen,shared-mem = <0x90000000 0x20000000>;
+        };
 
         ......
     };
@@ -699,3 +717,11 @@ shared between DomU1 and DomU2. It will get mapped at 0x60000000 in DomU1 guest
 physical address space, and at 0x70000000 in DomU2 guest physical address space.
 DomU1 and DomU2 are both the borrower domain, the owner domain is the default
 owner domain DOMID_IO.
+
+For the static shared memory region "my-shared-mem-2", since host physical
+address is not provided by user, Xen will automatically allocate 512MB
+from heap as static shared memory to be shared between DomU1 and DomU2.
+The automatically allocated static shared memory will get mapped at
+0x80000000 in DomU1 guest physical address space, and at 0x90000000 in DomU2
+guest physical address space. DomU1 is explicitly defined as the owner domain,
+and DomU2 is the borrower domain.
Signature · 1 line
2.25.1
#19 lore raw

PATCH v5 10/11 xen/arm: fix duplicate /reserved-memory node in Dom0

In reply to Penny Zheng

In case there is a /reserved-memory node already present in the host dtb, current Xen codes would create yet another /reserved-memory node specially for the static shm in Dom0 Device Tree.

Xen will use write_properties() to copy the reserved memory nodes from host dtb to Dom0 FDT, so we want to insert the shm node along with the copying. And avoiding duplication, we add a checking before make_resv_memory_node().

---
v3 -> v4:
new commit
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/domain_build.c             | 27 ++++++++++++++++++++++---
 xen/arch/arm/include/asm/kernel.h       |  2 ++
 xen/arch/arm/include/asm/static-shmem.h | 14 +++++++++++++
 xen/arch/arm/static-shmem.c             |  6 +++---
 4 files changed, 43 insertions(+), 6 deletions(-)
xen/arch/arm/domain_build.c +24 -3
index e040f8a6d9..f098678ea3 100644
@@ -752,6 +752,23 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,
         }
     }
 
+    if ( dt_node_path_is_equal(node, "/reserved-memory") )
+    {
+        kinfo->resv_mem = true;
+
+        /* shared memory provided. */
+        if ( kinfo->shminfo.nr_banks != 0 )
+        {
+            uint32_t addrcells = dt_n_addr_cells(node),
+                     sizecells = dt_n_size_cells(node);
+
+            res = make_shm_memory_node(d, kinfo->fdt,
+                                       addrcells, sizecells, kinfo);
+            if ( res )
+                return res;
+        }
+    }
+
     return 0;
 }
 
@@ -1856,9 +1873,13 @@ static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
                 return res;
         }
 
-        res = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
-        if ( res )
-            return res;
+        /* Avoid duplicate /reserved-memory nodes in Device Tree */
+        if ( !kinfo->resv_mem )
+        {
+            res = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
+            if ( res )
+                return res;
+        }
     }
 
     res = fdt_end_node(kinfo->fdt);
xen/arch/arm/include/asm/kernel.h +2 -0
index db3d8232fa..8fe2105a91 100644
@@ -39,6 +39,8 @@ struct kernel_info {
     void *fdt; /* flat device tree */
     paddr_t unassigned_mem; /* RAM not (yet) assigned to a bank */
     struct meminfo mem;
+    /* Whether we have /reserved-memory node in host Device Tree */
+    bool resv_mem;
     /* Static shared memory banks */
     struct {
         unsigned int nr_banks;
xen/arch/arm/include/asm/static-shmem.h +14 -0
index d149985291..6cb4ef9646 100644
@@ -33,6 +33,11 @@ int remove_shm_from_rangeset(const struct kernel_info *kinfo,
 
 int remove_shm_holes_for_domU(const struct kernel_info *kinfo,
                               struct meminfo *orig_ext);
+
+int make_shm_memory_node(const struct domain *d,
+                         void *fdt,
+                         int addrcells, int sizecells,
+                         const struct kernel_info *kinfo);
 #else /* !CONFIG_STATIC_SHM */
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
@@ -72,6 +77,15 @@ static inline int remove_shm_holes_for_domU(const struct kernel_info *kinfo,
 {
     return 0;
 }
+
+static inline int make_shm_memory_node(const struct domain *d,
+                                       void *fdt,
+                                       int addrcells, int sizecells,
+                                       const struct kernel_info *kinfo)
+{
+    return 0;
+}
+
 #endif /* CONFIG_STATIC_SHM */
 
 #endif /* __ASM_STATIC_SHMEM_H_ */
xen/arch/arm/static-shmem.c +3 -3
index a06949abaf..bfce5bbad0 100644
@@ -505,9 +505,9 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
     return 0;
 }
 
-static int __init make_shm_memory_node(const struct domain *d, void *fdt,
-                                       int addrcells, int sizecells,
-                                       const struct kernel_info *kinfo)
+int __init make_shm_memory_node(const struct domain *d, void *fdt,
+                                int addrcells, int sizecells,
+                                const struct kernel_info *kinfo)
 {
     unsigned int i = 0;
     int res = 0;
Signature · 1 line
2.25.1
#20 lore raw

Re: [PATCH v5 10/11] xen/arm: fix duplicate /reserved-memory node in Dom0

Hi Penny,

Quoted 5 lines · Penny Zheng

In case there is a /reserved-memory node already present in the host dtb, current Xen codes would create yet another /reserved-memory node specially for the static shm in Dom0 Device Tree.

Rational missing:
This would result in an incorrect device tree generation and guest would not be able to detect the static shared memory region.

Quoted 25 lines

Xen will use write_properties() to copy the reserved memory nodes from host dtb to Dom0 FDT, so we want to insert the shm node along with the copying. And avoiding duplication, we add a checking before make_resv_memory_node().

---
v3 -> v4:
new commit
---
v4 -> v5:
rebase and no change

---
 xen/arch/arm/domain_build.c             | 27 ++++++++++++++++++++++---
 xen/arch/arm/include/asm/kernel.h       |  2 ++
 xen/arch/arm/include/asm/static-shmem.h | 14 +++++++++++++
 xen/arch/arm/static-shmem.c             |  6 +++---
 4 files changed, 43 insertions(+), 6 deletions(-)
xen/arch/arm/domain_build.c +0 -0
index e040f8a6d9..f098678ea3 100644

xen/arch/arm/domain_build.c static int __init write_properties(struct domain *d, struct kernel_info *kinfo, · line 757

Quoted 6 lines
xen/arch/arm/domain_build.c +3 -0
@@ -752,6 +752,23 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,
         }
     }
 
+    if ( dt_node_path_is_equal(node, "/reserved-memory") )
+    {
+        kinfo->resv_mem = true;

I think I raised this concern last time. kinfo is used to store per-domain configuration. Information whether the reserved memory is present in host dtb or not does not fit there. Therefore, I would move this flag to bootinfo.

xen/arch/arm/domain_build.c static int __init write_properties(struct domain *d, struct kernel_info *kinfo, · line 763

Quoted 6 lines
xen/arch/arm/domain_build.c +6 -0
+
+        /* shared memory provided. */
+        if ( kinfo->shminfo.nr_banks != 0 )
+        {
+            uint32_t addrcells = dt_n_addr_cells(node),
+                     sizecells = dt_n_size_cells(node);

I find this always a bit odd. Just uint32_t sizecells on the next line, just like you did in all the other patches.

~Michal

#21 lore raw

PATCH v5 11/11 xen/arm: create another /memory node for static shm

In reply to Penny Zheng

Static shared memory region shall be described both under /memory and /reserved-memory.

We introduce export_shm_memory_node() to create another /memory node to contain the static shared memory ranges.

---
v3 -> v4:
new commit
---
v4 -> v5:
rebase and no changes

---
 xen/arch/arm/dom0less-build.c           |  8 ++++++++
 xen/arch/arm/domain_build.c             |  8 ++++++++
 xen/arch/arm/include/asm/static-shmem.h | 10 ++++++++++
 xen/arch/arm/static-shmem.c             | 19 +++++++++++++++++++
 4 files changed, 45 insertions(+)
xen/arch/arm/dom0less-build.c +8 -0
index ac096fa3fa..870b8a553f 100644
@@ -645,6 +645,14 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
     if ( ret )
         goto err;
 
+    /* Create a memory node to store the static shared memory regions */
+    if ( kinfo->shminfo.nr_banks != 0 )
+    {
+        ret = export_shm_memory_node(d, kinfo, addrcells, sizecells);
+        if ( ret )
+            goto err;
+    }
+
     ret = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
     if ( ret )
         goto err;
xen/arch/arm/domain_build.c +8 -0
index f098678ea3..4e450cb4c7 100644
@@ -1873,6 +1873,14 @@ static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
                 return res;
         }
 
+        /* Create a memory node to store the static shared memory regions */
+        if ( kinfo->shminfo.nr_banks != 0 )
+        {
+            res = export_shm_memory_node(d, kinfo, addrcells, sizecells);
+            if ( res )
+                return res;
+        }
+
         /* Avoid duplicate /reserved-memory nodes in Device Tree */
         if ( !kinfo->resv_mem )
         {
xen/arch/arm/include/asm/static-shmem.h +10 -0
index 6cb4ef9646..385fd24c17 100644
@@ -38,6 +38,10 @@ int make_shm_memory_node(const struct domain *d,
                          void *fdt,
                          int addrcells, int sizecells,
                          const struct kernel_info *kinfo);
+
+int export_shm_memory_node(const struct domain *d,
+                           const struct kernel_info *kinfo,
+                           int addrcells, int sizecells);
 #else /* !CONFIG_STATIC_SHM */
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
@@ -86,6 +90,12 @@ static inline int make_shm_memory_node(const struct domain *d,
     return 0;
 }
 
+static inline int export_shm_memory_node(const struct domain *d,
+                                         const struct kernel_info *kinfo,
+                                         int addrcells, int sizecells)
+{
+    return 0;
+}
 #endif /* CONFIG_STATIC_SHM */
 
 #endif /* __ASM_STATIC_SHMEM_H_ */
xen/arch/arm/static-shmem.c +19 -0
index bfce5bbad0..e583aae685 100644
@@ -505,6 +505,25 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
     return 0;
 }
 
+int __init export_shm_memory_node(const struct domain *d,
+                                  const struct kernel_info *kinfo,
+                                  int addrcells, int sizecells)
+{
+    unsigned int i = 0;
+    struct meminfo shm_meminfo;
+
+    /* Extract meminfo from kinfo.shminfo */
+    for ( ; i < kinfo->shminfo.nr_banks; i++ )
+    {
+        shm_meminfo.bank[i].start = kinfo->shminfo.bank[i].membank.start;
+        shm_meminfo.bank[i].size = kinfo->shminfo.bank[i].membank.size;
+        shm_meminfo.bank[i].type = MEMBANK_DEFAULT;
+    }
+    shm_meminfo.nr_banks = kinfo->shminfo.nr_banks;
+
+    return make_memory_node(d, kinfo->fdt, addrcells, sizecells, &shm_meminfo);
+}
+
 int __init make_shm_memory_node(const struct domain *d, void *fdt,
                                 int addrcells, int sizecells,
                                 const struct kernel_info *kinfo)
Signature · 1 line
2.25.1
#22 lore raw

Re: [PATCH v5 11/11] xen/arm: create another /memory node for static shm

Hi Penny,

Quoted 28 lines · Penny Zheng

Static shared memory region shall be described both under /memory and /reserved-memory.

We introduce export_shm_memory_node() to create another /memory node to contain the static shared memory ranges.

---
v3 -> v4:
new commit
---
v4 -> v5:
rebase and no changes

---
 xen/arch/arm/dom0less-build.c           |  8 ++++++++
 xen/arch/arm/domain_build.c             |  8 ++++++++
 xen/arch/arm/include/asm/static-shmem.h | 10 ++++++++++
 xen/arch/arm/static-shmem.c             | 19 +++++++++++++++++++
 4 files changed, 45 insertions(+)
xen/arch/arm/dom0less-build.c +0 -0
index ac096fa3fa..870b8a553f 100644

xen/arch/arm/dom0less-build.c static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo) · line 649

Quoted 5 lines
xen/arch/arm/dom0less-build.c +2 -0
@@ -645,6 +645,14 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
     if ( ret )
         goto err;
 
+    /* Create a memory node to store the static shared memory regions */
+    if ( kinfo->shminfo.nr_banks != 0 )

There is no need for this check to be repeated every time export_shm_memory_node is used. When the feature is disabled, export_shm_memory_node will return 0 anyway.
Furthermore, there is no need for kinfo->shminfo exposure. Please move the check to the function itself.

Also, I think both this and previous patch could be moved towards the beginning of the series. They are not related to other things you do in the series.

Quoted 67 lines
xen/arch/arm/dom0less-build.c +6 -0
+    {
+        ret = export_shm_memory_node(d, kinfo, addrcells, sizecells);
+        if ( ret )
+            goto err;
+    }
+
     ret = make_resv_memory_node(d, kinfo->fdt, addrcells, sizecells, kinfo);
     if ( ret )
         goto err;
xen/arch/arm/domain_build.c +8 -0
index f098678ea3..4e450cb4c7 100644
@@ -1873,6 +1873,14 @@ static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
                 return res;
         }
 
+        /* Create a memory node to store the static shared memory regions */
+        if ( kinfo->shminfo.nr_banks != 0 )
+        {
+            res = export_shm_memory_node(d, kinfo, addrcells, sizecells);
+            if ( res )
+                return res;
+        }
+
         /* Avoid duplicate /reserved-memory nodes in Device Tree */
         if ( !kinfo->resv_mem )
         {
xen/arch/arm/include/asm/static-shmem.h +10 -0
index 6cb4ef9646..385fd24c17 100644
@@ -38,6 +38,10 @@ int make_shm_memory_node(const struct domain *d,
                          void *fdt,
                          int addrcells, int sizecells,
                          const struct kernel_info *kinfo);
+
+int export_shm_memory_node(const struct domain *d,
+                           const struct kernel_info *kinfo,
+                           int addrcells, int sizecells);
 #else /* !CONFIG_STATIC_SHM */
 
 static inline int make_resv_memory_node(const struct domain *d, void *fdt,
@@ -86,6 +90,12 @@ static inline int make_shm_memory_node(const struct domain *d,
     return 0;
 }
 
+static inline int export_shm_memory_node(const struct domain *d,
+                                         const struct kernel_info *kinfo,
+                                         int addrcells, int sizecells)
+{
+    return 0;
+}
 #endif /* CONFIG_STATIC_SHM */
 
 #endif /* __ASM_STATIC_SHMEM_H_ */
xen/arch/arm/static-shmem.c +3 -0
index bfce5bbad0..e583aae685 100644
@@ -505,6 +505,25 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
     return 0;
 }
 
+int __init export_shm_memory_node(const struct domain *d,
+                                  const struct kernel_info *kinfo,
+                                  int addrcells, int sizecells)

xen/arch/arm/static-shmem.c int __init process_shm(struct domain *d, struct kernel_info *kinfo, · line 520

Quoted 10 lines
xen/arch/arm/static-shmem.c +10 -0
+{
+    unsigned int i = 0;
+    struct meminfo shm_meminfo;
+
+    /* Extract meminfo from kinfo.shminfo */
+    for ( ; i < kinfo->shminfo.nr_banks; i++ )
+    {
+        shm_meminfo.bank[i].start = kinfo->shminfo.bank[i].membank.start;
+        shm_meminfo.bank[i].size = kinfo->shminfo.bank[i].membank.size;
+        shm_meminfo.bank[i].type = MEMBANK_DEFAULT;

Is all of this really needed? This series introduces so many structures to avoid using meminfo but at the end we still need to use it. The amount of meminfo like structures copying done in this series worries me a bit.

~Michal