[PATCH v2 1/2] lib/sbi: fix covered regions handling in sanitize_domain()

Anup Patel anup at brainfault.org
Mon Dec 1 21:20:38 PST 2025


On Tue, Nov 11, 2025 at 4:13 PM Vladimir Kondratiev
<vladimir.kondratiev at mobileye.com> wrote:
>
> In the sanitize_domain, code that checks for the case when one
> memory region covered by the other, was never executed. Quote:
>
>         /* Sort the memory regions */
>         for (i = 0; i < (count - 1); i++) {
> <skip>
>         }
>
>        /* Remove covered regions */
>        while(i < (count - 1)) {
>
> Here "while" loop never executed because condition "i < (count - 1)"
> is always false after the "for" loop just above.
>
> In addition, when clearing region, "root_memregs_count"
> should be adjusted as well, otherwise code that adds memory region
> in the "root_add_memregion" will use wrong position:
>
>         /* Append the memregion to root memregions */
>         nreg = &root.regions[root_memregs_count];
>
> empty entry will be created in the middle of regions array, new
> regions will be added after this empty entry while sanitizing code
> will stop when reaching empty entry.
>
> Fixes: 3b03cdd60ce5 ("lib: sbi: Add regions merging when sanitizing domain region")
> Signed-off-by: Vladimir Kondratiev <vladimir.kondratiev at mobileye.com>

Good catch! A minor comment below.

> ---
>  lib/sbi/sbi_domain.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
> index 8cf5fcfe4926..039da39ecd6c 100644
> --- a/lib/sbi/sbi_domain.c
> +++ b/lib/sbi/sbi_domain.c
> @@ -344,7 +344,7 @@ static int sanitize_domain(struct sbi_domain *dom)
>         }
>
>         /* Remove covered regions */
> -       while(i < (count - 1)) {
> +       for (i = 0; i < (count - 1);) {
>                 is_covered = false;
>                 reg = &dom->regions[i];
>
> @@ -364,6 +364,7 @@ static int sanitize_domain(struct sbi_domain *dom)
>                                             &dom->regions[j + 1]);
>                         clear_region(&dom->regions[count - 1]);
>                         count--;
> +                       root_memregs_count--;

Your PATCH2 anyways removes this change so no point in having it here.
I will take care of it at the time of merging this patch.

>                 } else
>                         i++;
>         }
> --
> 2.43.0
>

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