[PATCH v2 2/2] lib/sbi: get rid of static variable to hold root memregions count

Anup Patel anup at brainfault.org
Mon Dec 1 21:27:24 PST 2025


On Tue, Nov 11, 2025 at 4:13 PM Vladimir Kondratiev
<vladimir.kondratiev at mobileye.com> wrote:
>
> Calculate number of used memory regions if needed.

Patch subject can be much simpler:
"lib: sbi: Remove static variable root_memregs_count"

>
> Signed-off-by: Vladimir Kondratiev <vladimir.kondratiev at mobileye.com>
> ---
>  lib/sbi/sbi_domain.c | 38 ++++++++++++++++++++++++--------------
>  1 file changed, 24 insertions(+), 14 deletions(-)
>
> diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
> index 039da39ecd6c..9fabfc105710 100644
> --- a/lib/sbi/sbi_domain.c
> +++ b/lib/sbi/sbi_domain.c
> @@ -25,7 +25,6 @@ static u32 domain_count = 0;
>  static bool domain_finalized = false;
>
>  #define ROOT_REGION_MAX        32
> -static u32 root_memregs_count = 0;
>
>  struct sbi_domain root = {
>         .name = "root",
> @@ -281,6 +280,17 @@ static void clear_region(struct sbi_domain_memregion* reg)
>         sbi_memset(reg, 0x0, sizeof(*reg));
>  }
>
> +static int sbi_domain_used_memregions(const struct sbi_domain *dom)
> +{
> +       int count = 0;
> +       struct sbi_domain_memregion *reg;
> +
> +       sbi_domain_for_each_memregion(dom, reg)
> +               count++;
> +
> +       return count;
> +}
> +
>  static int sanitize_domain(struct sbi_domain *dom)
>  {
>         u32 i, j, count;
> @@ -319,9 +329,7 @@ static int sanitize_domain(struct sbi_domain *dom)
>         }
>
>         /* Count memory regions */
> -       count = 0;
> -       sbi_domain_for_each_memregion(dom, reg)
> -               count++;
> +       count = sbi_domain_used_memregions(dom);
>
>         /* Check presence of firmware regions */
>         if (!dom->fw_region_inited) {
> @@ -364,7 +372,6 @@ static int sanitize_domain(struct sbi_domain *dom)
>                                             &dom->regions[j + 1]);
>                         clear_region(&dom->regions[count - 1]);
>                         count--;
> -                       root_memregs_count--;
>                 } else
>                         i++;
>         }
> @@ -604,10 +611,12 @@ static int root_add_memregion(const struct sbi_domain_memregion *reg)
>         int rc;
>         bool reg_merged;
>         struct sbi_domain_memregion *nreg, *nreg1, *nreg2;
> +       struct sbi_domain *dom = &root;
> +       int count = sbi_domain_used_memregions(dom);

If this local variable is named root_memregs_count then some
of the below changes will not be needed.

>
>         /* Sanity checks */
>         if (!reg || domain_finalized || !root.regions ||
> -           (ROOT_REGION_MAX <= root_memregs_count))
> +           (ROOT_REGION_MAX <= count))
>                 return SBI_EINVAL;
>
>         /* Check whether compatible region exists for the new one */
> @@ -617,10 +626,10 @@ static int root_add_memregion(const struct sbi_domain_memregion *reg)
>         }
>
>         /* Append the memregion to root memregions */
> -       nreg = &root.regions[root_memregs_count];
> +       nreg = &root.regions[count];
>         sbi_memcpy(nreg, reg, sizeof(*reg));
> -       root_memregs_count++;
> -       root.regions[root_memregs_count].order = 0;
> +       count++;
> +       root.regions[count].order = 0;
>
>         /* Sort and optimize root regions */
>         do {
> @@ -651,7 +660,7 @@ static int root_add_memregion(const struct sbi_domain_memregion *reg)
>                                         nreg1++;
>                                 }
>                                 reg_merged = true;
> -                               root_memregs_count--;
> +                               count--;
>                         }
>                 }
>         } while (reg_merged);
> @@ -777,6 +786,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
>         int rc;
>         struct sbi_hartmask *root_hmask;
>         struct sbi_domain_memregion *root_memregs;
> +       int count = 0;

Same comment as above.

>
>         SBI_INIT_LIST_HEAD(&domain_list);
>
> @@ -822,13 +832,13 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
>         sbi_domain_memregion_init(scratch->fw_start, scratch->fw_rw_offset,
>                                   (SBI_DOMAIN_MEMREGION_M_READABLE |
>                                    SBI_DOMAIN_MEMREGION_M_EXECUTABLE),
> -                                 &root_memregs[root_memregs_count++]);
> +                                 &root_memregs[count++]);
>
>         sbi_domain_memregion_init((scratch->fw_start + scratch->fw_rw_offset),
>                                   (scratch->fw_size - scratch->fw_rw_offset),
>                                   (SBI_DOMAIN_MEMREGION_M_READABLE |
>                                    SBI_DOMAIN_MEMREGION_M_WRITABLE),
> -                                 &root_memregs[root_memregs_count++]);
> +                                 &root_memregs[count++]);
>
>         root.fw_region_inited = true;
>
> @@ -843,10 +853,10 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
>                                   (SBI_DOMAIN_MEMREGION_SU_READABLE |
>                                    SBI_DOMAIN_MEMREGION_SU_WRITABLE |
>                                    SBI_DOMAIN_MEMREGION_SU_EXECUTABLE),
> -                                 &root_memregs[root_memregs_count++]);
> +                                 &root_memregs[count++]);
>
>         /* Root domain memory region end */
> -       root_memregs[root_memregs_count].order = 0;
> +       root_memregs[count].order = 0;
>
>         /* Root domain boot HART id is same as coldboot HART id */
>         root.boot_hartid = cold_hartid;
> --
> 2.43.0
>

I have taken care of the above comments at the time of merging this patch.

Reviewed-by: Anup Patel <anup at brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup



More information about the opensbi mailing list