lib: sbi: simplify sbi_ipi_send/sbi_ipi_process
PATCH 1/2 lib: sbi: simplify sbi_ipi_send/sbi_ipi_process
Pass sbi_ipi_raw_send/sbi_ipi_raw_clear instead of direct access to ipi_dev.
--- lib/sbi/sbi_ipi.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
lib/sbi/sbi_ipi.c +2 -6
@@ -68,9 +68,7 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,*/ atomic_raw_set_bit (event, &ipi_data->ipi_type);smp_wmb (); - -if (ipi_dev && ipi_dev->ipi_send) - ipi_dev->ipi_send (remote_hartindex); +sbi_ipi_raw_send (remote_hartindex);sbi_pmu_ctr_incr_fw (SBI_PMU_FW_IPI_SENT );
@@ -222,9 +220,7 @@ void sbi_ipi_process(void)u32 hartindex =sbi_hartid_to_hartindex (current_hartid ());sbi_pmu_ctr_incr_fw (SBI_PMU_FW_IPI_RECVD ); -if (ipi_dev && ipi_dev->ipi_clear) - ipi_dev->ipi_clear (hartindex); - +sbi_ipi_raw_clear (hartindex); ipi_type =atomic_raw_xchg_ulong (&ipi_data->ipi_type,0 ); ipi_event =0 ;while (ipi_type) {
Signature · 1 line
2.42.0
PATCH 2/2 lib: sbi: Make ipi send/clear and access ipi_type non-reentrant
Sending/receiving ipi messages and setting ipi_type should be non-reentrant. If reentrant, unexpected errors may occur.
--- lib/sbi/sbi_ipi.c | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/sbi/sbi_ipi.c +9 -0
@@ -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
Signature · 1 line
2.42.0
PATCH 2/2 lib: sbi: Make ipi send/clear and access ipi_type non-reentrant
? 2023-11-20???? 17:52 +0800?Xiang W???
Quoted 2 lines
Sending/receiving ipi messages and setting
ipi_typeshould be non-reentrant. If reentrant, unexpected errors may occur.
If reentrant, the following case may occur:
hart A hart B
send ipi--------->| |
| clear ipi |
| | set ipi_type
| |
| xchg ipi_type |
| |
|<-----------------| send ipi
| |
| |For more discussion, please refer to
https://lists.infradead.org/pipermail/opensbi/2023-November/005817.html
Regards,
Xiang W
Quoted 60 lines
Signed-off-by Xiang W <[email protected]>Reported-by Bo Gan <[email protected]>---
?lib/sbi/sbi_ipi.c | 9 +++++++++
?1 file changed, 9 insertions(+)lib/sbi/sbi_ipi.c +0
-0@@ -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>lib/sbi/sbi_ipi.c +0
-0@@ -25,6 +26,7 @@?
?structsbi_ipi_data{
? unsigned longipi_type;lib/sbi/sbi_ipi.c +1
-0+spinlock_t lock;?};
?
?_Static_assert(lib/sbi/sbi_ipi.c +0
-0@@ -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
? */lib/sbi/sbi_ipi.c +1
-0+spin_lock (&ipi_data->lock);?
atomic_raw_set_bit(event, &ipi_data->ipi_type);
?smp_wmb();
?sbi_ipi_raw_send(remote_hartindex);lib/sbi/sbi_ipi.c +1
-0+spin_unlock (&ipi_data->lock);?
?sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);
?lib/sbi/sbi_ipi.c +0
-0@@ -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);lib/sbi/sbi_ipi.c +2
-0+ +spin_lock (&ipi_data->lock);?
sbi_ipi_raw_clear(hartindex);
?ipi_type=atomic_raw_xchg_ulong(&ipi_data->ipi_type, 0);lib/sbi/sbi_ipi.c +2
-0+spin_unlock (&ipi_data->lock); +?
ipi_event= 0;
? while (ipi_type) {
? if (ipi_type& 1UL) {lib/sbi/sbi_ipi.c +0
-0@@ -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;lib/sbi/sbi_ipi.c +1
-0+SPIN_LOCK_INIT (ipi_data->lock);?
? /*
? * Initialize platform IPI support. This will also clear any
PATCH 2/2 lib: sbi: Make ipi send/clear and access ipi_type non-reentrant
Quoted 13 lines
Review void sbi_ipi_process(void) · line 235
Quoted 13 lines
(quoted diff) +4
-0@@ -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) {
Hi Xiang,
I think we should drop this, and use Anup's new patch instead. I don't think the spinlock here is helping to ensure the ordering between
sbi_ipi_raw_clear and xchg(). Furthermore, spinlock in OpenSBI doesn't order IO, so ipi_raw_clear can leak below spin_unlock. Thus, it won't address the race condition in previous discussions.
With Anip's new patch that uses wmb() in both sender and receiver, it fixes the correctness issue, and hopefully has negligible performance
impact: See the discussion here:
http://lists.infradead.org/pipermail/opensbi/2023-November/005937.html
Bo
PATCH 2/2 lib: sbi: Make ipi send/clear and access ipi_type non-reentrant
? 2023-11-21???? 16:56 -0800?Bo Gan???
Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +0
-0@@ -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);
+Quoted 8 lines
Quoted 8 lines
(quoted diff) +1
-0+ spin_lock(&ipi_data->lock);??
sbi_ipi_raw_clear(hartindex);
??ipi_type=atomic_raw_xchg_ulong(&ipi_data->ipi_type, 0);lib/sbi/sbi_ipi.c +2
-0+spin_unlock (&ipi_data->lock); +??
ipi_event= 0;
?? while (ipi_type) {
?? if (ipi_type& 1UL) {Hi Xiang,
I think we should drop this, and use Anup's new patch instead. I don't think the spinlock here is helping to ensure the ordering between
sbi_ipi_raw_clearandxchg(). Furthermore, spinlock in OpenSBI doesn't order IO, soipi_raw_clearcan leak belowspin_unlock. Thus, it won't address the race condition in previous discussions.
Quoted 7 lines
With Anip's new patch that uses
wmb()in both sender and receiver, it fixes the correctness issue, and hopefully has negligible performance
impact: See the discussion here:http://lists.infradead.org/pipermail/opensbi/2023-November/005937.html
Bo
This bug is not related to reorder. It's one more ipi interrupt is sent.
Regards,
Xiang W