[PATCH v2 2/2] sbi: sbi_domain_context: Add spinlock for updating domain assigned_harts

Anup Patel anup at brainfault.org
Sat Apr 6 23:23:16 PDT 2024


On Thu, Mar 28, 2024 at 9:03 AM Yu Chien Peter Lin
<peterlin at andestech.com> wrote:
>
> Add spinlock protection to avoid race condition on assigned_harts
> during domain context switching.
>
> Signed-off-by: Yu Chien Peter Lin <peterlin at andestech.com>
> Signed-off-by: Alvin Chang <alvinga at andestech.com>
> ---
> Changes v2:
>   - New patch
> ---
>  lib/sbi/sbi_domain_context.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c
> index a41dc8c7..e891c22a 100755
> --- a/lib/sbi/sbi_domain_context.c
> +++ b/lib/sbi/sbi_domain_context.c
> @@ -15,6 +15,8 @@
>  #include <sbi/sbi_string.h>
>  #include <sbi/sbi_domain_context.h>
>
> +static spinlock_t assigned_harts_lock = SPIN_LOCK_INITIALIZER;
> +

I would rather have assigned_harts_lock in "struct sbi_domain" so that
it is not a global lock like this one.

>  /**
>   * Switches the HART context from the current domain to the target domain.
>   * This includes changing domain assignments and reconfiguring PMP, as well
> @@ -34,10 +36,12 @@ static void switch_to_next_domain_context(struct sbi_context *ctx,
>
>         /* Assign current hart to target domain */
>         hartindex = sbi_hartid_to_hartindex(current_hartid());
> +       spin_lock(&assigned_harts_lock);
>         sbi_hartmask_clear_hartindex(
>                 hartindex, &sbi_domain_thishart_ptr()->assigned_harts);
>         sbi_update_hartindex_to_domain(hartindex, dom);
>         sbi_hartmask_set_hartindex(hartindex, &dom->assigned_harts);
> +       spin_unlock(&assigned_harts_lock);
>
>         /* Reconfigure PMP settings for the new domain */
>         for (int i = 0; i < pmp_count; i++) {
> --
> 2.34.1
>

Regards,
Anup



More information about the opensbi mailing list