From patchwork Tue Apr 16 05:02:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-Chien Peter Lin X-Patchwork-Id: 1923989 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=ETil7BsM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VJX3j1Mhsz1yZC for ; Tue, 16 Apr 2024 15:02:57 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=gtxPhWtIZFnvoK0Hmywzl/+Lo7egnBeQIdZkuNTwbg4=; b=ETil7BsMx9SVYK 8liQfkZsiSwY26+lfIz7KooUwvgGxpCEegVIV9pQKG2FhA83p00rykQD0842T0gayWikc9w/HAHf6 xuVkPNkb0lzUaErAZs8FXedtL4kow3BCmdRwbFh9sUY9iHP3CaIFrEZhIQWCCJpq++DeJhHMEw64L GBMdvApdlfB6ofwRv/6oLUDxnIoYnZ7Qtev68F1fjOllH66K/FVpgQiujzf/ODImJ64KUk1oKyfIy tUxsxuLbgHyGVo/Dm/9IJbMqwjP2kvI9zhfnGKLwiwZgMONGqJadVHzwTEKVCQD/SCOM0kh+obtGQ q/C8Lq6MPT8bi/kzUAcw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rwaxy-0000000Aslf-2vJy; Tue, 16 Apr 2024 05:02:46 +0000 Received: from 60-248-80-70.hinet-ip.hinet.net ([60.248.80.70] helo=Atcsqr.andestech.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rwaxv-0000000AslE-2KH1 for opensbi@lists.infradead.org; Tue, 16 Apr 2024 05:02:45 +0000 Received: from mail.andestech.com (ATCPCS16.andestech.com [10.0.1.222]) by Atcsqr.andestech.com with ESMTP id 43G52QPJ080258; Tue, 16 Apr 2024 13:02:26 +0800 (+08) (envelope-from peterlin@andestech.com) Received: from swlinux02.andestech.com (10.0.15.183) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.498.0; Tue, 16 Apr 2024 13:02:22 +0800 From: Yu Chien Peter Lin To: CC: , , <2931013282@sjtu.edu.cn>, , Alvin Chang , "Yu Chien Peter Lin" Subject: [PATCH v3] sbi: sbi_domain_context: Add spinlock for updating domain assigned_harts Date: Tue, 16 Apr 2024 13:02:13 +0800 Message-ID: <20240416050213.665461-1-peterlin@andestech.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.0.15.183] X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: Atcsqr.andestech.com 43G52QPJ080258 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240415_220243_903261_F831DBBA X-CRM114-Status: GOOD ( 13.75 ) X-Spam-Score: 0.4 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Alvin Chang Add spinlock protection to avoid race condition on assigned_harts during domain context switching. Also, rename/add variables for accessing the corresponding domain of target/current context. Content analysis details: (0.4 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 TVD_RCVD_IP Message was received from an IP address 0.4 RDNS_DYNAMIC Delivered to internal network by host with dynamic-looking rDNS X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Alvin Chang Add spinlock protection to avoid race condition on assigned_harts during domain context switching. Also, rename/add variables for accessing the corresponding domain of target/current context. Signed-off-by: Alvin Chang Reviewed-by: Yu Chien Peter Lin Reviewed-by: Anup Patel --- include/sbi/sbi_domain.h | 3 +++ lib/sbi/sbi_domain.c | 3 +++ lib/sbi/sbi_domain_context.c | 27 +++++++++++++++++---------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h index 5432dacb..02765777 100644 --- a/include/sbi/sbi_domain.h +++ b/include/sbi/sbi_domain.h @@ -10,6 +10,7 @@ #ifndef __SBI_DOMAIN_H__ #define __SBI_DOMAIN_H__ +#include #include #include #include @@ -174,6 +175,8 @@ struct sbi_domain { * in the coldboot path */ struct sbi_hartmask assigned_harts; + /** Spinlock for accessing assigned_harts */ + spinlock_t assigned_harts_lock; /** Name of this domain */ char name[64]; /** Possible HARTs in this domain */ diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c index 50749f15..51770d8d 100644 --- a/lib/sbi/sbi_domain.c +++ b/lib/sbi/sbi_domain.c @@ -555,6 +555,9 @@ int sbi_domain_register(struct sbi_domain *dom, dom->index = domain_count++; domidx_to_domain_table[dom->index] = dom; + /* Initialize spinlock for dom->assigned_harts */ + SPIN_LOCK_INIT(dom->assigned_harts_lock); + /* Clear assigned HARTs of domain */ sbi_hartmask_clear_all(&dom->assigned_harts); diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c index a41dc8c7..a3d3988d 100755 --- a/lib/sbi/sbi_domain_context.c +++ b/lib/sbi/sbi_domain_context.c @@ -26,18 +26,23 @@ static void switch_to_next_domain_context(struct sbi_context *ctx, struct sbi_context *dom_ctx) { - u32 hartindex; + u32 hartindex = sbi_hartid_to_hartindex(current_hartid()); struct sbi_trap_regs *trap_regs; - struct sbi_domain *dom = dom_ctx->dom; + struct sbi_domain *current_dom = ctx->dom; + struct sbi_domain *target_dom = dom_ctx->dom; struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); unsigned int pmp_count = sbi_hart_pmp_count(scratch); /* Assign current hart to target domain */ - hartindex = sbi_hartid_to_hartindex(current_hartid()); - 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_lock(¤t_dom->assigned_harts_lock); + sbi_hartmask_clear_hartindex(hartindex, ¤t_dom->assigned_harts); + spin_unlock(¤t_dom->assigned_harts_lock); + + sbi_update_hartindex_to_domain(hartindex, target_dom); + + spin_lock(&target_dom->assigned_harts_lock); + sbi_hartmask_set_hartindex(hartindex, &target_dom->assigned_harts); + spin_unlock(&target_dom->assigned_harts_lock); /* Reconfigure PMP settings for the new domain */ for (int i = 0; i < pmp_count; i++) { @@ -71,9 +76,11 @@ static void switch_to_next_domain_context(struct sbi_context *ctx, /* If target domain context is not initialized or runnable */ if (!dom_ctx->initialized) { /* Startup boot HART of target domain */ - if (current_hartid() == dom->boot_hartid) - sbi_hart_switch_mode(dom->boot_hartid, dom->next_arg1, - dom->next_addr, dom->next_mode, + if (current_hartid() == target_dom->boot_hartid) + sbi_hart_switch_mode(target_dom->boot_hartid, + target_dom->next_arg1, + target_dom->next_addr, + target_dom->next_mode, false); else sbi_hsm_hart_stop(scratch, true);