xen/arm: Remove recursion from dt_unreserved_regions
cover xen/arm: Remove recursion from dt_unreserved_regions
Hi, this is an RFC that is exploiting the new 'struct membank' interface introduced here 2209c1e35b479dff8ed3d3161001ffdefa0a704e ("xen/arm: Introduce a generic way to access memory bank structures") to start removing recursion from some function, in this serie the dt_unreserved_regions is reworked for that reason.
This is an RFC to understand if the proposed approach can be accepted.
Another function can benefit from this approach, consider_modules in arm32/mmu/mm.c, but it might require to rework also the 'struct bootmodules' to adhere to the new interface and have just a loop that can go through all the different structures.
Comments are welcome.
Luca Fancellu (2):
xen/arm: Add DT reserve map regions to bootinfo.reserved_mem xen/arm: Rework dt_unreserved_regions to avoid recursion
xen/arch/arm/arm32/mmu/mm.c | 29 +---- xen/arch/arm/bootfdt.c | 51 +++++--- xen/arch/arm/domain_build.c | 3 +- xen/arch/arm/include/asm/setup.h | 13 +- xen/arch/arm/include/asm/static-shmem.h | 7 +- xen/arch/arm/kernel.c | 2 +- xen/arch/arm/setup.c | 158 +++++++++++++----------- 7 files changed, 135 insertions(+), 128 deletions(-)
Signature · 1 line
2.34.1
RFC PATCH 1/2 xen/arm: Add DT reserve map regions to bootinfo.reserved_mem
Currently the code is listing device tree reserve map regions as reserved memory for Xen, but they are not added into bootinfo.reserved_mem and they are fetched in multiple places using the same code sequence, causing duplication. Fix this by adding them to the bootinfo.reserved_mem at early stage.
--- xen/arch/arm/arm32/mmu/mm.c | 29 +---------------- xen/arch/arm/bootfdt.c | 51 ++++++++++++++++++------------ xen/arch/arm/domain_build.c | 3 +- xen/arch/arm/include/asm/setup.h | 5 +++ xen/arch/arm/setup.c | 53 +++++++++----------------------- 5 files changed, 53 insertions(+), 88 deletions(-)
xen/arch/arm/arm32/mmu/mm.c +1 -28
@@ -73,33 +73,6 @@ static paddr_t __init consider_modules(paddr_t s, paddr_t e, } } -/* Now check any fdt reserved areas. */ - - nr =fdt_num_mem_rsv (device_tree_flattened); - -for ( ; i < mi->nr_mods + nr; i++ ) - { -paddr_t mod_s, mod_e; - -if (fdt_get_mem_rsv_paddr (device_tree_flattened, - i - mi->nr_mods, - &mod_s, &mod_e ) <0 ) -/* If we can't read it, pretend it doesn't exist... */ -continue ; - -/* fdt_get_mem_rsv_paddr returns length */ - mod_e += mod_s; - -if ( s < mod_e && mod_s < e ) - { - mod_e =consider_modules (mod_e, e, size, align, i+1 ); -if ( mod_e ) -return mod_e; - -return consider_modules (s, mod_s, size, align, i+1 ); - } - } -/* * i is the current bootmodule we are evaluating, across all * possible kinds of bootmodules.
@@ -108,7 +81,7 @@ static paddr_t __init consider_modules(paddr_t s, paddr_t e,* need to index the reserved_mem bank starting from 0, and only counting * the reserved-memory modules. Hence, we need to use i - nr. */ - nr += mi->nr_mods; + nr = mi->nr_mods;for ( ; i - nr < reserved_mem->nr_banks; i++ ) {paddr_t r_s = reserved_mem->bank[i - nr].start;
xen/arch/arm/bootfdt.c +31 -20
78 lines not on this page · raw message
xen/arch/arm/domain_build.c +2 -1
@@ -793,7 +793,8 @@ int __init make_memory_node(const struct kernel_info *kinfo, int addrcells,u64 start = mem->bank[i].start;u64 size = mem->bank[i].size; -if ( mem->bank[i].type ==MEMBANK_STATIC_DOMAIN ) +if ( (mem->bank[i].type ==MEMBANK_STATIC_DOMAIN ) || + (mem->bank[i].type ==MEMBANK_FDT_RESVMEM ) )continue ; nr_cells += reg_size;
xen/arch/arm/include/asm/setup.h +5 -0
@@ -42,6 +42,11 @@ enum membank_type {* in reserved_mem. */ MEMBANK_STATIC_HEAP , +/* +* The MEMBANK_FDT_RESVMEM type is used to indicate whether the memory +* bank is from the FDT reserve map. +*/ + MEMBANK_FDT_RESVMEM, };/* Indicates the maximum number of characters(\0 included) for shm_id */
xen/arch/arm/setup.c +14 -39
73 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH 1/2] xen/arm: Add DT reserve map regions to bootinfo.reserved_mem
Hi Luca,
Quoted 5 lines · Luca Fancellu
Currently the code is listing device tree reserve map regions as reserved memory for Xen, but they are not added into bootinfo.reserved_mem and they are fetched in multiple places using the same code sequence, causing duplication. Fix this by adding them to the bootinfo.reserved_mem at early stage.
Do we have enough space in bootinfo.reserved_mem for them?
The rest of the changes LGTM.
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/2] xen/arm: Add DT reserve map regions to bootinfo.reserved_mem
Hi Julien,
Thanks for having a look on the patch,
Quoted 12 lines · Julien Grall
Hi Luca,
Quoted 5 lines · Luca Fancellu
Currently the code is listing device tree reserve map regions as reserved memory for Xen, but they are not added into bootinfo.reserved_mem and they are fetched in multiple places using the same code sequence, causing duplication. Fix this by adding them to the bootinfo.reserved_mem at early stage.
Do we have enough space in bootinfo.reserved_mem for them?
So we have 255 banks, in my experience I would say I’ve never saw too many reserved regions in the DT, maybe a couple, but I’ve always had to deal with embedded platforms.
I’ve tested this one with ADLINK AVA board, n1sdp, Juno, raspberry pi, qemu, fvp.
In your experience, have you seen any numbers that could be concerning?
Cheers,
Luca
Julien Grall
Acked-byRe: [RFC PATCH 1/2] xen/arm: Add DT reserve map regions to bootinfo.reserved_mem
Hi,
Quoted 15 lines · Luca Fancellu
Hi Julien,
Thanks for having a look on the patch,
Quoted 11 lines · Julien Grall
Hi Luca,
Quoted 5 lines · Luca Fancellu
Currently the code is listing device tree reserve map regions as reserved memory for Xen, but they are not added into bootinfo.reserved_mem and they are fetched in multiple places using the same code sequence, causing duplication. Fix this by adding them to the bootinfo.reserved_mem at early stage.
Quoted 8 lines
Quoted 1 line
Do we have enough space in bootinfo.reserved_mem for them?
So we have 255 banks, in my experience I would say I’ve never saw too many reserved regions in the DT, maybe a couple, but I’ve always had to deal with embedded platforms.
I’ve tested this one with ADLINK AVA board, n1sdp, Juno, raspberry pi, qemu, fvp.
In your experience, have you seen any numbers that could be concerning?
I know in the past we had to bump the memory banks a few times. But as you tested on a few platforms, I think we should be ok.
It would be best if this patch goes sooner than later to allow wider testing before we release 4.19.
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/2] xen/arm: Add DT reserve map regions to bootinfo.reserved_mem
Quoted 25 lines · Julien Grall
Hi,
Quoted 13 lines · Luca Fancellu
Hi Julien,
Thanks for having a look on the patch,Quoted 11 lines · Julien Grall
Hi Luca,
Quoted 5 lines · Luca Fancellu
Currently the code is listing device tree reserve map regions as reserved memory for Xen, but they are not added into bootinfo.reserved_mem and they are fetched in multiple places using the same code sequence, causing duplication. Fix this by adding them to the bootinfo.reserved_mem at early stage.
Quoted 5 lines
Quoted 1 line
Do we have enough space in bootinfo.reserved_mem for them?
So we have 255 banks, in my experience I would say I’ve never saw too many reserved regions in the DT, maybe a couple, but I’ve always had to deal with embedded platforms. I’ve tested this one with ADLINK AVA board, n1sdp, Juno, raspberry pi, qemu, fvp. In your experience, have you seen any numbers that could be concerning?
Quoted 5 lines
I know in the past we had to bump the memory banks a few times. But as you tested on a few platforms, I think we should be ok.
It would be best if this patch goes sooner than later to allow wider testing before we release 4.19.
Acked-by Julien Grall <[email protected]>
Yes it would make sense, this patch makes sense on its own, would you/anyone commit it separately while I work on the second patch?
Re: [RFC PATCH 1/2] xen/arm: Add DT reserve map regions to bootinfo.reserved_mem
Hi Luca,
Quoted 25 lines · Luca Fancellu
Quoted 25 lines · Julien Grall
Hi,
Quoted 18 lines · Luca Fancellu
Hi Julien,
Thanks for having a look on the patch,Quoted 12 lines · Julien Grall
Hi Luca,
Quoted 5 lines · Luca Fancellu
Currently the code is listing device tree reserve map regions as reserved memory for Xen, but they are not added into bootinfo.reserved_mem and they are fetched in multiple places using the same code sequence, causing duplication. Fix this by adding them to the bootinfo.reserved_mem at early stage.
Do we have enough space in bootinfo.reserved_mem for them?
So we have 255 banks, in my experience I would say I’ve never saw too many reserved regions in the DT, maybe a couple, but I’ve always had to deal with embedded platforms. I’ve tested this one with ADLINK AVA board, n1sdp, Juno, raspberry pi, qemu, fvp. In your experience, have you seen any numbers that could be concerning?
Quoted 8 lines
Quoted 5 lines
I know in the past we had to bump the memory banks a few times. But as you tested on a few platforms, I think we should be ok.
It would be best if this patch goes sooner than later to allow wider testing before we release 4.19.
Acked-by Julien Grall <[email protected]>Yes it would make sense, this patch makes sense on its own, would you/anyone commit it separately while I work on the second patch?
Thank you for the confirmation. This is now committed.
Cheers,
Signature · 1 line
Julien Grall
RFC PATCH 2/2 xen/arm: Rework dt_unreserved_regions to avoid recursion
In reply to Luca FancelluThe function dt_unreserved_regions is currently using recursion to compute the non overlapping ranges of a passed region against the reserved memory banks, in the spirit of removing the recursion to improve safety and also improve the scalability of the function, rework its code to use an iterative algorithm with the same result.
The function was taking an additional parameter 'first', but given the rework and given that the function was always initially called with this parameter as zero, remove the parameter and update the codebase to reflect the change.
--- xen/arch/arm/include/asm/setup.h | 8 +- xen/arch/arm/include/asm/static-shmem.h | 7 +- xen/arch/arm/kernel.c | 2 +- xen/arch/arm/setup.c | 133 ++++++++++++++++-------- 4 files changed, 96 insertions(+), 54 deletions(-)
xen/arch/arm/include/asm/setup.h +6 -2
@@ -9,7 +9,12 @@#define MAX_FDT_SIZE SZ_2M#define NR_MEM_BANKS 256 + +#ifdef CONFIG_STATIC_SHM #define NR_SHMEM_BANKS 32 +#else +#define NR_SHMEM_BANKS 0 +#endif #define MAX_MODULES 32/* Current maximum useful modules */
@@ -215,8 +220,7 @@ void create_dom0(void);void discard_initial_modules (void );void fw_unreserved_regions (paddr_t s,paddr_t e, -void (*cb)(paddr_t ps,paddr_t pe), -unsigned int first); +void (*cb)(paddr_t ps,paddr_t pe));size_t boot_fdt_info (const void *fdt,paddr_t paddr);const char *boot_fdt_cmdline (const void *fdt);
xen/arch/arm/include/asm/static-shmem.h +2 -5
@@ -7,11 +7,11 @@#include <asm/kernel.h> #include <asm/setup.h> -#ifdef CONFIG_STATIC_SHM -/* Worst case /memory node reg element: (addrcells + sizecells) */ #define DT_MEM_NODE_REG_RANGE_SIZE ((NR_MEM_BANKS + NR_SHMEM_BANKS) * 4) +#ifdef CONFIG_STATIC_SHM +int make_resv_memory_node (const struct kernel_info *kinfo,int addrcells,int sizecells);
@@ -47,9 +47,6 @@ void shm_mem_node_fill_reg_range(const struct kernel_info *kinfo, __be32 *reg,#else /* !CONFIG_STATIC_SHM */ -/* Worst case /memory node reg element: (addrcells + sizecells) */ -#define DT_MEM_NODE_REG_RANGE_SIZE (NR_MEM_BANKS * 4) -static inline int make_resv_memory_node (const struct kernel_info *kinfo,int addrcells,int sizecells) {
xen/arch/arm/kernel.c +1 -1
@@ -247,7 +247,7 @@ static __init int kernel_decompress(struct bootmodule *mod, uint32_t offset)* Free the original kernel, update the pointers to the * decompressed kernel */ -fw_unreserved_regions (addr, addr + size, init_domheap_pages,0 ); +fw_unreserved_regions (addr, addr + size, init_domheap_pages);return 0 ; }
xen/arch/arm/setup.c +87 -46
167 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH 2/2] xen/arm: Rework dt_unreserved_regions to avoid recursion
Hi Luca,
Quoted 10 lines · Luca Fancellu
The function
dt_unreserved_regionsis currently using recursion to compute the non overlapping ranges of a passed region against the reserved memory banks, in the spirit of removing the recursion to improve safety and also improve the scalability of the function, rework its code to use an iterative algorithm with the same result.The function was taking an additional parameter 'first', but given the rework and given that the function was always initially called with this parameter as zero, remove the parameter and update the codebase to reflect the change.
In general, I am in favor to remove the recursion. I have some questions/remarks below though.
Quoted 55 lines
Signed-off-by Luca Fancellu <[email protected]>--- xen/arch/arm/include/asm/setup.h | 8 +- xen/arch/arm/include/asm/static-shmem.h | 7 +- xen/arch/arm/kernel.c | 2 +- xen/arch/arm/setup.c | 133 ++++++++++++++++-------- 4 files changed, 96 insertions(+), 54 deletions(-)xen/arch/arm/include/asm/setup.h +6
-2@@ -9,7 +9,12 @@#define MAX_FDT_SIZE SZ_2M#define NR_MEM_BANKS 256 + +#ifdef CONFIG_STATIC_SHM #define NR_SHMEM_BANKS 32 +#else +#define NR_SHMEM_BANKS 0 +#endif #define MAX_MODULES 32/* Current maximum useful modules */ @@ -215,8 +220,7 @@ void create_dom0(void);void discard_initial_modules (void );void fw_unreserved_regions (paddr_t s,paddr_t e, -void (*cb)(paddr_t ps,paddr_t pe), -unsigned int first); +void (*cb)(paddr_t ps,paddr_t pe));size_t boot_fdt_info (const void *fdt,paddr_t paddr);const char *boot_fdt_cmdline (const void *fdt);xen/arch/arm/include/asm/static-shmem.h +2
-2@@ -7,11 +7,11 @@#include <asm/kernel.h> #include <asm/setup.h> -#ifdef CONFIG_STATIC_SHM -/* Worst case /memory node reg element: (addrcells + sizecells) */ #define DT_MEM_NODE_REG_RANGE_SIZE ((NR_MEM_BANKS + NR_SHMEM_BANKS) * 4) +#ifdef CONFIG_STATIC_SHM +int make_resv_memory_node (const struct kernel_info *kinfo,int addrcells,int sizecells);
xen/arch/arm/include/asm/static-shmem.h void shm_mem_node_fill_reg_range(const struct kernel_info *kinfo, __be32 *reg, · line 52
Quoted 6 lines
xen/arch/arm/include/asm/static-shmem.h +0
-3@@ -47,9 +47,6 @@ void shm_mem_node_fill_reg_range(const struct kernel_info *kinfo, __be32 *reg,#else /* !CONFIG_STATIC_SHM */ -/* Worst case /memory node reg element: (addrcells + sizecells) */ -#define DT_MEM_NODE_REG_RANGE_SIZE (NR_MEM_BANKS * 4) -
I don't understand how this is related to the rest patch. Can you clarify?
Quoted 50 lines
xen/arch/arm/include/asm/static-shmem.h +0
-0static inline int make_resv_memory_node (const struct kernel_info *kinfo,int addrcells,int sizecells) {xen/arch/arm/kernel.c +1
-1@@ -247,7 +247,7 @@ static __init int kernel_decompress(struct bootmodule *mod, uint32_t offset)* Free the original kernel, update the pointers to the * decompressed kernel */ -fw_unreserved_regions (addr, addr + size, init_domheap_pages,0 ); +fw_unreserved_regions (addr, addr + size, init_domheap_pages);return 0 ; }xen/arch/arm/setup.c +3
-20@@ -204,55 +204,97 @@ static void __init processor_id(void) }static void __init dt_unreserved_regions (paddr_t s,paddr_t e, -void (*cb)(paddr_t ps,paddr_t pe), -unsigned int first) +void (*cb)(paddr_t ps,paddr_t pe)) { -const struct membanks *reserved_mem =bootinfo_get_reserved_mem (); -#ifdef CONFIG_STATIC_SHM -const struct membanks *shmem =bootinfo_get_shmem (); -unsigned int offset; -#endif -unsigned int i; -/* -* i is the current bootmodule we are evaluating across all possible -* kinds. +* The worst case scenario is to have N reserved region ovelapping the +* passed one, so having N+1 regions in the stack */ -for ( i = first; i < reserved_mem->nr_banks; i++ ) - { -paddr_t r_s = reserved_mem->bank[i].start; -paddr_t r_e = r_s + reserved_mem->bank[i].size; - -if ( s < r_e && r_s < e ) - { -dt_unreserved_regions (r_e, e, cb, i +1 ); -dt_unreserved_regions (s, r_s, cb, i +1 );
xen/arch/arm/setup.c static void __init processor_id(void) · line 218
Quoted 10 lines
xen/arch/arm/setup.c +6
-4-return ; - } - } - +struct { +paddr_t s; +paddr_t e; +unsigned int dict; +unsigned int bank; + } stack[NR_MEM_BANKS +NR_SHMEM_BANKS +1 ];
If I am not mistaken, the variable could take up to 7KB of the stack. This is quite a bit (Xen stack is 32KB on Arm).
AFAIK, the function is not expected to be called concurrently. So it would be best to move to .initdata (by adding static __inidata).
Even with that in mind, do we actually need such a large stack?
Quoted 5 lines
xen/arch/arm/setup.c +2
-2+const struct membanks *mem_banks[] = { +bootinfo_get_reserved_mem (), #ifdef CONFIG_STATIC_SHM -/* -* When retrieving the corresponding shared memory addresses
xen/arch/arm/setup.c static void __init processor_id(void) · line 226
Quoted 10 lines
xen/arch/arm/setup.c +5
-5-* below, we need to index the shmem->bank starting from 0, hence -* we need to use i - reserved_mem->nr_banks. -*/ - offset = reserved_mem->nr_banks; -for ( ; i - offset < shmem->nr_banks; i++ ) + bootinfo_get_shmem(), +#endif + }; +unsigned int count =0 ; +unsigned int i, j;
i and j only seemed to be used within the loop. So can this be only be declared within it?
xen/arch/arm/setup.c static void __init processor_id(void) · line 233
Quoted 7 lines
xen/arch/arm/setup.c +7
-0+ +/* Push the initial region into the stack */ + stack[count].s = s; + stack[count].e = e; + stack[count].dict =0 ; + stack[count].bank =0 ; + count++;
You are repeating the 5 lines quite a bit. Can we add a wrapper for it? This would make the code a bit easier to read.
xen/arch/arm/setup.c static void __init processor_id(void) · line 236
Quoted 7 lines
xen/arch/arm/setup.c +2
-4+while ( count >0 ) { -paddr_t r_s = shmem->bank[i - offset].start; -paddr_t r_e = r_s + shmem->bank[i - offset].size; - -if ( s < r_e && r_s < e ) +paddr_t b_s, b_e;
Coding style: Please add a newline.
Quoted 6 lines
xen/arch/arm/setup.c +6
-0+/* Take out last element of the stack */ + count--; + i = stack[count].dict; + j = stack[count].bank; + b_s = stack[count].s; + b_e = stack[count].e;
xen/arch/arm/setup.c static void __init processor_id(void) · line 249
Quoted 10 lines
xen/arch/arm/setup.c +6
-3+/* Start checking for overlapping banks */ +for ( ; i <ARRAY_SIZE (mem_banks); i++ ) { -dt_unreserved_regions (r_e, e, cb, i +1 ); -dt_unreserved_regions (s, r_s, cb, i +1 ); -return ; +for ( ; j < mem_banks[i]->nr_banks; j++ ) + { +paddr_t r_s = mem_banks[i]->bank[j].start; +paddr_t r_e = r_s + mem_banks[i]->bank[j].size;
As you are reworking the code, I would like us to avoid using 'start + size' because it could overflow.
I would prefer if we use either start, size or using 'start + size - 1'.
Also coding style: Newlinen missing
xen/arch/arm/setup.c static void __init processor_id(void) · line 253
Quoted 4 lines
xen/arch/arm/setup.c +4
-0+ +if ( b_s < r_e && r_s < b_e ) + { +/*
The line seems to be misindented compare to the other. Or maybe the rest is?
Quoted 16 lines
xen/arch/arm/setup.c +16
-0+* Region is overlapping, push the two non overlap ranges in +* the stack +*/ +if ( (count +2 ) <ARRAY_SIZE (stack) ) + { +/* +* Push initial non overlapping part of the region into +* the stack +*/ + stack[count].s = b_s; + stack[count].e = r_s; + stack[count].dict = i; + stack[count].bank = j +1 ; + count++; +/* +* Push last non overlapping part of the region into
xen/arch/arm/setup.c static void __init processor_id(void) · line 279
Quoted 10 lines
xen/arch/arm/setup.c +10
-0+* the stack +*/ + stack[count].s = r_e; + stack[count].e = b_e; + stack[count].dict = i; + stack[count].bank = j +1 ; + count++; + } +else +panic ("dt_unreserved_regions: stack space exausted" );
Can you document in which circumstances this can happen?
Quoted 48 lines
xen/arch/arm/setup.c +19
-7+ +/* +* Retrieve the last region pushed to check against other +* banks, so break the loop through the banks and pop +* a new region from the stack +*/ + goto regions_pushed; + } + } + j =0 ; } +/* +* This point is reached when the region doesn't overlap with any other +* region, so call the callback on it. +*/ +cb (b_s, b_e); + regions_pushed: } -#endif - -cb (s, e); }/* @@ -316,11 +358,10 @@ static bool __init bootmodules_overlap_check(struct bootmodules *bootmodules, }void __init fw_unreserved_regions (paddr_t s,paddr_t e, -void (*cb)(paddr_t ps,paddr_t pe), -unsigned int first) +void (*cb)(paddr_t ps,paddr_t pe)) {if ( acpi_disabled ) -dt_unreserved_regions (s, e, cb, first); +dt_unreserved_regions (s, e, cb);else cb (s, e); }@@ -527,7 +568,7 @@ void __init discard_initial_modules(void) !mfn_valid(maddr_to_mfn(e)) )continue ; -fw_unreserved_regions (s, e, init_domheap_pages,0 ); +fw_unreserved_regions (s, e, init_domheap_pages); } mi->nr_mods =0 ;
xen/arch/arm/setup.c void __init populate_boot_allocator(void) · line 749
Quoted 8 lines
xen/arch/arm/setup.c +1
-1@@ -702,7 +743,7 @@ void __init populate_boot_allocator(void) }#endif -fw_unreserved_regions (s, e, init_boot_pages,0 ); +fw_unreserved_regions (s, e, init_boot_pages); s = n; } }
Cheers,
Signature · 1 line
Julien Grall