All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luca Fancellu <luca.fancellu@arm.com>
To: xen-devel@lists.xenproject.org
Cc: Stefano Stabellini <sstabellini@kernel.org>,
	Julien Grall <julien@xen.org>,
	Bertrand Marquis <bertrand.marquis@arm.com>,
	Michal Orzel <michal.orzel@amd.com>,
	Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>
Subject: [PATCH v3 06/12] xen/arm: Avoid code duplication in find_unallocated_memory
Date: Thu, 18 Apr 2024 08:36:46 +0100	[thread overview]
Message-ID: <20240418073652.3622828-7-luca.fancellu@arm.com> (raw)
In-Reply-To: <20240418073652.3622828-1-luca.fancellu@arm.com>

The function find_unallocated_memory is using the same code to
loop through 2 structure of the same type, in order to avoid
code duplication, rework the code to have only one loop that
goes through all the structures, this will be used to avoid
duplication when the static shared memory banks will be introduced
as a separate structure from reserved memory.

Take the occasion to add the error code to the error message in
case 'rangeset_remove_range' fails.

Signed-off-by: Luca Fancellu <luca.fancellu@arm.com>
---
v3:
 - Fixed the wrong logic, now the function correctly adds the
   available ram to the rangeset and afterwards removes the
   Dom0 memory and reserved memory from it.
 - take the occasion to print the error code in the error
   message as explained in the commit msg.
v2:
 - Add comment in the loop inside find_unallocated_memory to
   improve readability
v1:
 - new patch
---
---
 xen/arch/arm/domain_build.c | 53 ++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 30 deletions(-)

diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c
index 02e4dcafe78f..7c7038254473 100644
--- a/xen/arch/arm/domain_build.c
+++ b/xen/arch/arm/domain_build.c
@@ -868,12 +868,14 @@ static int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn,
 static int __init find_unallocated_memory(const struct kernel_info *kinfo,
                                           struct membanks *ext_regions)
 {
-    const struct membanks *kinfo_mem = kernel_info_get_mem(kinfo);
     const struct membanks *mem = bootinfo_get_mem();
-    const struct membanks *reserved_mem = bootinfo_get_reserved_mem();
+    const struct membanks *mem_banks[] = {
+        kernel_info_get_mem(kinfo),
+        bootinfo_get_reserved_mem(),
+    };
     struct rangeset *unalloc_mem;
     paddr_t start, end;
-    unsigned int i;
+    unsigned int i, j;
     int res;
 
     dt_dprintk("Find unallocated memory for extended regions\n");
@@ -897,35 +899,26 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo,
         }
     }
 
-    /* Remove RAM assigned to Dom0 */
-    for ( i = 0; i < kinfo_mem->nr_banks; i++ )
-    {
-        start = kinfo_mem->bank[i].start;
-        end = kinfo_mem->bank[i].start + kinfo_mem->bank[i].size;
-        res = rangeset_remove_range(unalloc_mem, PFN_DOWN(start),
-                                    PFN_DOWN(end - 1));
-        if ( res )
-        {
-            printk(XENLOG_ERR "Failed to remove: %#"PRIpaddr"->%#"PRIpaddr"\n",
-                   start, end);
-            goto out;
-        }
-    }
-
-    /* Remove reserved-memory regions */
-    for ( i = 0; i < reserved_mem->nr_banks; i++ )
-    {
-        start = reserved_mem->bank[i].start;
-        end = reserved_mem->bank[i].start + reserved_mem->bank[i].size;
-        res = rangeset_remove_range(unalloc_mem, PFN_DOWN(start),
-                                    PFN_DOWN(end - 1));
-        if ( res )
+    /*
+     * Exclude the following regions:
+     * 1) Remove RAM assigned to Dom0
+     * 2) Remove reserved memory
+     */
+    for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
+        for ( j = 0; j < mem_banks[i]->nr_banks; j++ )
         {
-            printk(XENLOG_ERR "Failed to remove: %#"PRIpaddr"->%#"PRIpaddr"\n",
-                   start, end);
-            goto out;
+            start = mem_banks[i]->bank[j].start;
+            end = mem_banks[i]->bank[j].start + mem_banks[i]->bank[j].size;
+            res = rangeset_remove_range(unalloc_mem, PFN_DOWN(start),
+                                        PFN_DOWN(end - 1));
+            if ( res )
+            {
+                printk(XENLOG_ERR
+                       "Failed to add: %#"PRIpaddr"->%#"PRIpaddr", error %d\n",
+                       start, end, res);
+                goto out;
+            }
         }
-    }
 
     /* Remove grant table region */
     if ( kinfo->gnttab_size )
-- 
2.34.1



  parent reply	other threads:[~2024-04-18  7:37 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-18  7:36 [PATCH v3 00/12] Static shared memory followup v2 - pt1 Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 01/12] xen/arm: remove stale addr_cells/size_cells in assign_shared_memory Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 02/12] xen/arm: avoid repetitive checking in process_shm_node Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 03/12] xen/arm: Pass struct kernel_info parameter to make_{resv,shm}_memory_node Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 04/12] xen/arm: Introduce a generic way to access memory bank structures Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 05/12] xen/arm: Conditional compilation of kernel_info.shm_mem member Luca Fancellu
2024-04-18  7:36 ` Luca Fancellu [this message]
2024-04-18  8:33   ` [PATCH v3 06/12] xen/arm: Avoid code duplication in find_unallocated_memory Michal Orzel
2024-04-18  7:36 ` [PATCH v3 07/12] xen/arm: Avoid code duplication in check_reserved_regions_overlap Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 08/12] xen/arm: Introduce helper for static memory pages Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 09/12] xen/arm: Reduce struct membank size on static shared memory Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 10/12] xen/arm: remove shm holes from extended regions Luca Fancellu
2024-04-18 12:47   ` Michal Orzel
2024-04-18  7:36 ` [PATCH v3 11/12] xen/arm: fix duplicate /reserved-memory node in Dom0 Luca Fancellu
2024-04-18  7:36 ` [PATCH v3 12/12] xen/arm: List static shared memory regions as /memory nodes Luca Fancellu
2024-04-22  7:55   ` Michal Orzel
2024-04-22  8:07     ` Luca Fancellu
2024-04-22  9:26       ` Michal Orzel
2024-04-22 10:24         ` Julien Grall
2024-04-22 10:39           ` Luca Fancellu
2024-04-22 10:45             ` Julien Grall
2024-04-24 10:19           ` Julien Grall
2024-04-24 10:25             ` Luca Fancellu
2024-04-22 11:02   ` [PATCH v3.2 " Luca Fancellu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240418073652.3622828-7-luca.fancellu@arm.com \
    --to=luca.fancellu@arm.com \
    --cc=Volodymyr_Babchuk@epam.com \
    --cc=bertrand.marquis@arm.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.