[PATCH 2/2] lib: sbi: Make ipi send/clear and access ipi_type non-reentrant

Xiang W wxjstz at 126.com
Mon Nov 20 01:52:43 PST 2023


Sending/receiving ipi messages and setting ipi_type should be
non-reentrant. If reentrant, unexpected errors may occur.

Signed-off-by: Xiang W <wxjstz at 126.com>
Reported-by: Bo Gan <ganboing at gmail.com>
---
 lib/sbi/sbi_ipi.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/sbi/sbi_ipi.c b/lib/sbi/sbi_ipi.c
index 30cb315..8e05d28 100644
--- a/lib/sbi/sbi_ipi.c
+++ b/lib/sbi/sbi_ipi.c
@@ -11,6 +11,7 @@
 #include <sbi/riscv_asm.h>
 #include <sbi/riscv_atomic.h>
 #include <sbi/riscv_barrier.h>
+#include <sbi/riscv_locks.h>
 #include <sbi/sbi_bitops.h>
 #include <sbi/sbi_domain.h>
 #include <sbi/sbi_error.h>
@@ -25,6 +26,7 @@
 
 struct sbi_ipi_data {
 	unsigned long ipi_type;
+	spinlock_t lock;
 };
 
 _Static_assert(
@@ -66,9 +68,11 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
 	 * Set IPI type on remote hart's scratch area and
 	 * trigger the interrupt
 	 */
+	spin_lock(&ipi_data->lock);
 	atomic_raw_set_bit(event, &ipi_data->ipi_type);
 	smp_wmb();
 	sbi_ipi_raw_send(remote_hartindex);
+	spin_unlock(&ipi_data->lock);
 
 	sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);
 
@@ -220,8 +224,12 @@ void sbi_ipi_process(void)
 	u32 hartindex = sbi_hartid_to_hartindex(current_hartid());
 
 	sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_RECVD);
+
+	spin_lock(&ipi_data->lock);
 	sbi_ipi_raw_clear(hartindex);
 	ipi_type = atomic_raw_xchg_ulong(&ipi_data->ipi_type, 0);
+	spin_unlock(&ipi_data->lock);
+
 	ipi_event = 0;
 	while (ipi_type) {
 		if (ipi_type & 1UL) {
@@ -289,6 +297,7 @@ int sbi_ipi_init(struct sbi_scratch *scratch, bool cold_boot)
 
 	ipi_data = sbi_scratch_offset_ptr(scratch, ipi_data_off);
 	ipi_data->ipi_type = 0x00;
+	SPIN_LOCK_INIT(ipi_data->lock);
 
 	/*
 	 * Initialize platform IPI support. This will also clear any
-- 
2.42.0




More information about the opensbi mailing list