diff mbox series

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

Message ID 20240328033303.883674-3-peterlin@andestech.com
State Changes Requested
Headers show
Series Minor fixes for domain context switch | expand

Commit Message

Yu Chien Peter Lin March 28, 2024, 3:33 a.m. UTC
Add spinlock protection to avoid race condition on assigned_harts
during domain context switching.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Signed-off-by: Alvin Chang <alvinga@andestech.com>
---
Changes v2:
  - New patch
---
 lib/sbi/sbi_domain_context.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Anup Patel April 7, 2024, 6:23 a.m. UTC | #1
On Thu, Mar 28, 2024 at 9:03 AM Yu Chien Peter Lin
<peterlin@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@andestech.com>
> Signed-off-by: Alvin Chang <alvinga@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
diff mbox series

Patch

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;
+
 /**
  * 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++) {