Skip to contentSkip to conversation

lib: sbi: Prevent redundant sbi_ipi_process

PATCH New Xiang W 2 participants 3 messages Nov 22, 2023 – Dec 8, 2023

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH 1/1 lib: sbi: Prevent redundant sbi_ipi_process

sbi_ipi_process reads ipi_type through xchg. This may occur between set ipi_type and send_ipi, which will result in redundant interrupt processing.

The specific case is as follows:

hart A hart B

send ipi--------->|                  |
                  | clear ipi        |
                  |                  | set ipi_type
                  |                  |
                  | xchg ipi_type    |
                  |                  |
                  |<-----------------| send ipi
                  |                  |
                  |                  |

This patch fixes this issue.

---
 lib/sbi/sbi_ipi.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
lib/sbi/sbi_ipi.c +3 -4
index 0bf446a..6f25a7e 100644
@@ -39,7 +39,7 @@ static const struct sbi_ipi_event_ops *ipi_ops_array[SBI_IPI_EVENT_MAX];
 static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
 			u32 event, void *data)
 {
-	int ret;
+	int ret = 0;
 	struct sbi_scratch *remote_scratch = NULL;
 	struct sbi_ipi_data *ipi_data;
 	const struct sbi_ipi_event_ops *ipi_ops;
@@ -66,9 +66,8 @@ 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
 	 */
-	atomic_raw_set_bit(event, &ipi_data->ipi_type);
-
-	ret = sbi_ipi_raw_send(remote_hartindex);
+	if (__atomic_fetch_or(&ipi_data->ipi_type, BIT(event), __ATOMIC_ACQ_REL) == 0)
+		ret = sbi_ipi_raw_send(remote_hartindex);
 
 	sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);
 
Signature · 1 line
2.42.0
author
#1 lore raw

PATCH 1/1 lib: sbi: Prevent redundant sbi_ipi_process

? 2023-11-22???? 20:54 +0800?Xiang W???

Quoted 17 lines

sbi_ipi_process reads ipi_type through xchg. This may occur between set ipi_type and send_ipi, which will result in redundant interrupt processing.

The specific case is as follows:

??????????????? hart A???????????? hart B

send ipi--------->|????????????????? |
????????????????? | clear ipi??????? |
????????????????? |????????????????? | set ipi_type
????????????????? |????????????????? |
????????????????? | xchg ipi_type??? |
????????????????? |????????????????? |
????????????????? |<-----------------| send ipi
????????????????? |????????????????? |
????????????????? |????????????????? |
Quoted 1 line

This patch fixes this issue.

This patch base on
https://github.com/avpatel/opensbi/tree/sbi_ipi_barrier_imp_v1

Regards,
Xiang W

Quoted 31 lines

---
?lib/sbi/sbi_ipi.c | 7 +++----
?1 file changed, 3 insertions(+), 4 deletions(-)

lib/sbi/sbi_ipi.c +1 -1
index 0bf446a..6f25a7e 100644
@@ -39,7 +39,7 @@ static const struct sbi_ipi_event_ops *ipi_ops_array[SBI_IPI_EVENT_MAX];
 ?static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
 ?			u32 event, void *data)
 ?{
-	int ret;
+	int ret = 0;
 ?	struct sbi_scratch *remote_scratch = NULL;
 ?	struct sbi_ipi_data *ipi_data;
 ?	const struct sbi_ipi_event_ops *ipi_ops;
@@ -66,9 +66,8 @@ 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 +2 -3
-	atomic_raw_set_bit(event, &ipi_data->ipi_type);
-
-	ret = sbi_ipi_raw_send(remote_hartindex);
+	if (__atomic_fetch_or(&ipi_data->ipi_type, BIT(event), __ATOMIC_ACQ_REL) == 0)
+		ret = sbi_ipi_raw_send(remote_hartindex);

?
? sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);
?

Applied
#2 lore raw

PATCH 1/1 lib: sbi: Prevent redundant sbi_ipi_process

In reply to Xiang W
Quoted 18 lines · On Wed, Nov 22, 2023 at 6:25?PM Xiang W <[email protected]> wrote:

sbi_ipi_process reads ipi_type through xchg. This may occur between set ipi_type and send_ipi, which will result in redundant interrupt processing.

The specific case is as follows:

hart A hart B

send ipi--------->|                  |
                  | clear ipi        |
                  |                  | set ipi_type
                  |                  |
                  | xchg ipi_type    |
                  |                  |
                  |<-----------------| send ipi
                  |                  |
                  |                  |
Quoted 1 line

This patch fixes this issue.

The optimization is in the right direction but the commit description is not appropriate.

This optimization is not about addressing a very specific case mentioned above rather it's above avoid unnecessary send_ipi() call when multiple HARTs try to send IPI to the same HART.

Quoted 20 lines
---
 lib/sbi/sbi_ipi.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
lib/sbi/sbi_ipi.c +1 -1
index 0bf446a..6f25a7e 100644
@@ -39,7 +39,7 @@ static const struct sbi_ipi_event_ops *ipi_ops_array[SBI_IPI_EVENT_MAX];
 static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
                        u32 event, void *data)
 {
-       int ret;
+       int ret = 0;
        struct sbi_scratch *remote_scratch = NULL;
        struct sbi_ipi_data *ipi_data;
        const struct sbi_ipi_event_ops *ipi_ops;

lib/sbi/sbi_ipi.c static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex, · line 69

Quoted 7 lines
lib/sbi/sbi_ipi.c +1 -3
@@ -66,9 +66,8 @@ 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
         */
-       atomic_raw_set_bit(event, &ipi_data->ipi_type);
-
-       ret = sbi_ipi_raw_send(remote_hartindex);
+       if (__atomic_fetch_or(&ipi_data->ipi_type, BIT(event), __ATOMIC_ACQ_REL) == 0)

We should use __ATOMIC_RELAXED here because we already have barriers in sbi_ipi_raw_send().

Quoted 8 lines
lib/sbi/sbi_ipi.c +1 -1
+               ret = sbi_ipi_raw_send(remote_hartindex);
 
        sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);
 
--

2.42.0

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

I have taken care of the above at the time of merging this patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup