Skip to contentSkip to conversation

sbi_ipi bug fixes and optimizations

PATCH New Samuel Holland 3 participants 13 messages Dec 8, 2023 – Dec 18, 2023

lore
Share & tools
lore
#0 lore raw

cover sbi_ipi bug fixes and optimizations

This series makes a few improvements to the sbi_ipi library. The first patch fixes some error handling. In my specific case, no IPI device was registered, which caused Linux to silently not receive any IPIs. The second patch improves self-IPIs. In addition to fixing a deadlock that could occur for synchronous events (sbi_tlb is not affected because it implements a similar self-IPI fast path itself), it also has the effect that single-hart platforms no longer need an IPI device, since on those platforms all IPIs are self-IPIs. Patch 3 is a minor optimization.

Samuel Holland (3):
lib: sbi_ipi: Do not ignore errors from sbi_ipi_send()
lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()
lib: sbi_ipi: Drop unnecessary ipi_process check

 include/sbi/sbi_ipi.h |  2 +-
 lib/sbi/sbi_ipi.c     | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/3 lib: sbi_ipi: Do not ignore errors from sbi_ipi_send()

Currently, failures in sbi_ipi_send() are silently ignored, which makes them difficult to debug. Instead, abort sending the IPI and pass back the error, but still synchronize any IPIs already sent.

This requires changing the value of SBI_IPI_UPDATE_BREAK to 0, since sbi_ipi_send() returning that value should not be treated as an error.

---

 include/sbi/sbi_ipi.h | 2 +-
 lib/sbi/sbi_ipi.c     | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/sbi/sbi_ipi.h +1 -1
index d3962334..e81d9352 100644
@@ -31,9 +31,9 @@ struct sbi_ipi_device {
 };
 
 enum sbi_ipi_update_type {
-	SBI_IPI_UPDATE_SUCCESS,
 	SBI_IPI_UPDATE_BREAK,
 	SBI_IPI_UPDATE_RETRY,
+	SBI_IPI_UPDATE_SUCCESS,
 };
 
 struct sbi_scratch;
lib/sbi/sbi_ipi.c +4 -2
index 5c33a78b..1fd24228 100644
@@ -101,7 +101,7 @@ static int sbi_ipi_sync(struct sbi_scratch *scratch, u32 event)
  */
 int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 {
-	int rc;
+	int rc = 0;
 	bool retry_needed;
 	ulong i, m;
 	struct sbi_hartmask target_mask = {0};
@@ -137,6 +137,8 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 			rc = sbi_ipi_send(scratch, i, event, data);
 			if (rc == SBI_IPI_UPDATE_RETRY)
 				retry_needed = true;
+			else if (rc)
+				break;
 			else
 				sbi_hartmask_clear_hartindex(i, &target_mask);
 		}
@@ -145,7 +147,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 	/* Sync IPIs */
 	sbi_ipi_sync(scratch, event);
 
-	return 0;
+	return rc;
 }
 
 int sbi_ipi_event_create(const struct sbi_ipi_event_ops *ops)
Signature · 1 line
2.42.0
#2 lore raw

PATCH 1/3 lib: sbi_ipi: Do not ignore errors from sbi_ipi_send()

Quoted 20 lines · On Sat, Dec 9, 2023 at 2:53?AM Samuel Holland <[email protected]> wrote:

Currently, failures in sbi_ipi_send() are silently ignored, which makes them difficult to debug. Instead, abort sending the IPI and pass back the error, but still synchronize any IPIs already sent.

This requires changing the value of SBI_IPI_UPDATE_BREAK to 0, since sbi_ipi_send() returning that value should not be treated as an error.

---

 include/sbi/sbi_ipi.h | 2 +-
 lib/sbi/sbi_ipi.c     | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/sbi/sbi_ipi.h +0 -0
index d3962334..e81d9352 100644

include/sbi/sbi_ipi.h struct sbi_ipi_device { · line 37

Quoted 8 lines
include/sbi/sbi_ipi.h +1 -1
@@ -31,9 +31,9 @@ struct sbi_ipi_device {
 };
 
 enum sbi_ipi_update_type {
-       SBI_IPI_UPDATE_SUCCESS,
        SBI_IPI_UPDATE_BREAK,
        SBI_IPI_UPDATE_RETRY,
+       SBI_IPI_UPDATE_SUCCESS,
 };

I don't think we need to change the enum order here. Please see the comment below.

Quoted 16 lines
include/sbi/sbi_ipi.h +0 -0
 struct sbi_scratch;
lib/sbi/sbi_ipi.c +1 -1
index 5c33a78b..1fd24228 100644
@@ -101,7 +101,7 @@ static int sbi_ipi_sync(struct sbi_scratch *scratch, u32 event)
  */
 int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 {
-       int rc;
+       int rc = 0;
        bool retry_needed;
        ulong i, m;
        struct sbi_hartmask target_mask = {0};

lib/sbi/sbi_ipi.c int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data) · line 140

Quoted 4 lines
lib/sbi/sbi_ipi.c +1 -0
@@ -137,6 +137,8 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
                        rc = sbi_ipi_send(scratch, i, event, data);
                        if (rc == SBI_IPI_UPDATE_RETRY)
                                retry_needed = true;
+                       else if (rc)

This needs to be "rc < 0" otherwise SBI_IPI_UPDATE_SUCCESS will also cause a break here.

lib/sbi/sbi_ipi.c int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data) · line 143

Quoted 3 lines
lib/sbi/sbi_ipi.c +1 -0
+                               break;
                        else
                                sbi_hartmask_clear_hartindex(i, &target_mask);

We have to ensure that sbi_hartmask_clear_hartindex() is done for both SBI_IPI_UPDATE_SUCCESS and SBI_IPI_UPDATE_BREAK;

Quoted 14 lines
lib/sbi/sbi_ipi.c +1 -2
                }
@@ -145,7 +147,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
        /* Sync IPIs */
        sbi_ipi_sync(scratch, event);
 
-       return 0;
+       return rc;
 }
 
 int sbi_ipi_event_create(const struct sbi_ipi_event_ops *ops)
--

2.42.0

Quoted 4 lines

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

Regards,
Anup

Applied
#3 lore raw

PATCH 1/3 lib: sbi_ipi: Do not ignore errors from sbi_ipi_send()

In reply to Samuel Holland
Quoted 9 lines · On Sat, Dec 9, 2023 at 2:53?AM Samuel Holland <[email protected]> wrote:

Currently, failures in sbi_ipi_send() are silently ignored, which makes them difficult to debug. Instead, abort sending the IPI and pass back the error, but still synchronize any IPIs already sent.

This requires changing the value of SBI_IPI_UPDATE_BREAK to 0, since sbi_ipi_send() returning that value should not be treated as an error.

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

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 60 lines

---

 include/sbi/sbi_ipi.h | 2 +-
 lib/sbi/sbi_ipi.c     | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/sbi/sbi_ipi.h +1 -1
index d3962334..e81d9352 100644
@@ -31,9 +31,9 @@ struct sbi_ipi_device {
 };
 
 enum sbi_ipi_update_type {
-       SBI_IPI_UPDATE_SUCCESS,
        SBI_IPI_UPDATE_BREAK,
        SBI_IPI_UPDATE_RETRY,
+       SBI_IPI_UPDATE_SUCCESS,
 };
 
 struct sbi_scratch;
lib/sbi/sbi_ipi.c +4 -3
index 5c33a78b..1fd24228 100644
@@ -101,7 +101,7 @@ static int sbi_ipi_sync(struct sbi_scratch *scratch, u32 event)
  */
 int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 {
-       int rc;
+       int rc = 0;
        bool retry_needed;
        ulong i, m;
        struct sbi_hartmask target_mask = {0};
@@ -137,6 +137,8 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
                        rc = sbi_ipi_send(scratch, i, event, data);
                        if (rc == SBI_IPI_UPDATE_RETRY)
                                retry_needed = true;
+                       else if (rc)
+                               break;
                        else
                                sbi_hartmask_clear_hartindex(i, &target_mask);
                }
@@ -145,7 +147,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
        /* Sync IPIs */
        sbi_ipi_sync(scratch, event);
 
-       return 0;
+       return rc;
 }
 
 int sbi_ipi_event_create(const struct sbi_ipi_event_ops *ops)
--

2.42.0

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

#4 lore raw

PATCH 2/3 lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()

In reply to Samuel Holland

An IPI sent to the local hart can be processed directly instead of triggering the IPI device. This is more efficient, and it avoids a deadlock when the .sync callback is defined. Since interrupts are disabled while handling an ecall, the IPI would not get delivered until the next mret, but sbi_ipi_sync() is called before then.

---

 lib/sbi/sbi_ipi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/sbi/sbi_ipi.c +8 -0
index 1fd24228..ac31cd94 100644
@@ -60,6 +60,14 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
 				      remote_hartindex, data);
 		if (ret != SBI_IPI_UPDATE_SUCCESS)
 			return ret;
+	} else if (scratch == remote_scratch) {
+		/*
+		 * IPI events with an update() callback are expected to return
+		 * SBI_IPI_UPDATE_BREAK for self-IPIs. For other events, check
+		 * for self-IPI and execute the callback directly here.
+		 */
+		ipi_ops->process(scratch);
+		return 0;
 	}
 
 	/*
Signature · 1 line
2.42.0
#5 lore raw

PATCH 2/3 lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()

Quoted 8 lines · On Sat, Dec 9, 2023 at 2:53?AM Samuel Holland <[email protected]> wrote:

An IPI sent to the local hart can be processed directly instead of triggering the IPI device. This is more efficient, and it avoids a deadlock when the .sync callback is defined. Since interrupts are disabled while handling an ecall, the IPI would not get delivered until the next mret, but sbi_ipi_sync() is called before then.

Looks good to me.

Regards,
Anup

Quoted 32 lines

---

 lib/sbi/sbi_ipi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/sbi/sbi_ipi.c +8 -1
index 1fd24228..ac31cd94 100644
@@ -60,6 +60,14 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
                                      remote_hartindex, data);
                if (ret != SBI_IPI_UPDATE_SUCCESS)
                        return ret;
+       } else if (scratch == remote_scratch) {
+               /*
+                * IPI events with an update() callback are expected to return
+                * SBI_IPI_UPDATE_BREAK for self-IPIs. For other events, check
+                * for self-IPI and execute the callback directly here.
+                */
+               ipi_ops->process(scratch);
+               return 0;
        }
 
        /*
--

2.42.0

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

#6 lore raw

PATCH 2/3 lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()

In reply to Samuel Holland

? 2023-12-08???? 13:22 -0800?Samuel Holland???

Quoted 18 lines

An IPI sent to the local hart can be processed directly instead of triggering the IPI device. This is more efficient, and it avoids a deadlock when the .sync callback is defined. Since interrupts are disabled while handling an ecall, the IPI would not get delivered until the next mret, but sbi_ipi_sync() is called before then.

---

?lib/sbi/sbi_ipi.c | 8 ++++++++
?1 file changed, 8 insertions(+)

lib/sbi/sbi_ipi.c +0 -0
index 1fd24228..ac31cd94 100644
@@ -60,6 +60,14 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
 ?				????? remote_hartindex, data);

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

Quoted 10 lines
lib/sbi/sbi_ipi.c +8 -0
 ?		if (ret != SBI_IPI_UPDATE_SUCCESS)
 ?			return ret;
+	} else if (scratch == remote_scratch) {
+		/*
+		 * IPI events with an update() callback are expected to return
+		 * SBI_IPI_UPDATE_BREAK for self-IPIs. For other events, check
+		 * for self-IPI and execute the callback directly here.
+		 */
+		ipi_ops->process(scratch);
+		return 0;

This modification will affect sbi_tlb.c. tlb_update has already called tinfo->local_fn(tinfo) when returning SBI_IPI_UPDATE_BREAK. And the? synchronization variable is only updated when SBI_IPI_UPDATE_SUCCESS is? returned. If tlb_process is called, the synchronization variable will be? affected.

Regards,
Xiang W

Quoted 7 lines
lib/sbi/sbi_ipi.c +0 -0
 ?	}

?
? /*

Signature · 1 line
2.42.0
#7 lore raw

PATCH 2/3 lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()

? 2023-12-11???? 15:24 +0800?Xiang W???

Quoted 35 lines

? 2023-12-08???? 13:22 -0800?Samuel Holland???

Quoted 18 lines

An IPI sent to the local hart can be processed directly instead of triggering the IPI device. This is more efficient, and it avoids a deadlock when the .sync callback is defined. Since interrupts are disabled while handling an ecall, the IPI would not get delivered until the next mret, but sbi_ipi_sync() is called before then.

---

?lib/sbi/sbi_ipi.c | 8 ++++++++
?1 file changed, 8 insertions(+)

lib/sbi/sbi_ipi.c +0 -0
index 1fd24228..ac31cd94 100644
@@ -60,6 +60,14 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
 ?				????? remote_hartindex, data);

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

Quoted 10 lines
lib/sbi/sbi_ipi.c +8 -0
 ?		if (ret != SBI_IPI_UPDATE_SUCCESS)
 ?			return ret;
+	} else if (scratch == remote_scratch) {
+		/*
+		 * IPI events with an update() callback are expected to return
+		 * SBI_IPI_UPDATE_BREAK for self-IPIs. For other events, check
+		 * for self-IPI and execute the callback directly here.
+		 */
+		ipi_ops->process(scratch);
+		return 0;

This modification will affect sbi_tlb.c. tlb_update has already called tinfo->local_fn(tinfo) when returning SBI_IPI_UPDATE_BREAK. And the? synchronization variable is only updated when SBI_IPI_UPDATE_SUCCESS is? returned. If tlb_process is called, the synchronization variable will be? affected.

Quoted 2 lines

Regards,
Xiang W

I made a mistake! I thought ipi_ops->process(scratch) was called when? SBI_IPI_UPDATE_BREAK was returned. I ignored that there is no ipi_ops->update when it doesn't exist.

Look good to me.

Quoted 10 lines
Quoted 8 lines
lib/sbi/sbi_ipi.c +0 -0
 ?	}

?
? /*

Signature · 1 line
2.42.0
Applied
#8 lore raw

PATCH 2/3 lib: sbi_ipi: Process self-IPIs in sbi_ipi_send()

In reply to Samuel Holland
Quoted 8 lines · On Sat, Dec 9, 2023 at 2:53?AM Samuel Holland <[email protected]> wrote:

An IPI sent to the local hart can be processed directly instead of triggering the IPI device. This is more efficient, and it avoids a deadlock when the .sync callback is defined. Since interrupts are disabled while handling an ecall, the IPI would not get delivered until the next mret, but sbi_ipi_sync() is called before then.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 32 lines

---

 lib/sbi/sbi_ipi.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/sbi/sbi_ipi.c +8 -1
index 1fd24228..ac31cd94 100644
@@ -60,6 +60,14 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
                                      remote_hartindex, data);
                if (ret != SBI_IPI_UPDATE_SUCCESS)
                        return ret;
+       } else if (scratch == remote_scratch) {
+               /*
+                * IPI events with an update() callback are expected to return
+                * SBI_IPI_UPDATE_BREAK for self-IPIs. For other events, check
+                * for self-IPI and execute the callback directly here.
+                */
+               ipi_ops->process(scratch);
+               return 0;
        }
 
        /*
--

2.42.0

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

#9 lore raw

PATCH 3/3 lib: sbi_ipi: Drop unnecessary ipi_process check

In reply to Samuel Holland

sbi_ipi_event_create() disallows registering an IPI event with a NULL .process callback, so the function pointer will never be NULL here.

---

 lib/sbi/sbi_ipi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_ipi.c +1 -1
index ac31cd94..1115912b 100644
@@ -241,7 +241,7 @@ void sbi_ipi_process(void)
 	while (ipi_type) {
 		if (ipi_type & 1UL) {
 			ipi_ops = ipi_ops_array[ipi_event];
-			if (ipi_ops && ipi_ops->process)
+			if (ipi_ops)
 				ipi_ops->process(scratch);
 		}
 		ipi_type = ipi_type >> 1;
Signature · 1 line
2.42.0
#10 lore raw

PATCH 3/3 lib: sbi_ipi: Drop unnecessary ipi_process check

Quoted 5 lines · On Sat, Dec 9, 2023 at 2:53?AM Samuel Holland <[email protected]> wrote:

sbi_ipi_event_create() disallows registering an IPI event with a NULL .process callback, so the function pointer will never be NULL here.

Looks good to me.

Regards,
Anup

Quoted 26 lines

---

 lib/sbi/sbi_ipi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_ipi.c +1 -2
index ac31cd94..1115912b 100644
@@ -241,7 +241,7 @@ void sbi_ipi_process(void)
        while (ipi_type) {
                if (ipi_type & 1UL) {
                        ipi_ops = ipi_ops_array[ipi_event];
-                       if (ipi_ops && ipi_ops->process)
+                       if (ipi_ops)
                                ipi_ops->process(scratch);
                }
                ipi_type = ipi_type >> 1;
--

2.42.0

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

#11 lore raw

PATCH 3/3 lib: sbi_ipi: Drop unnecessary ipi_process check

In reply to Samuel Holland

? 2023-12-08???? 13:22 -0800?Samuel Holland???

Quoted 4 lines

sbi_ipi_event_create() disallows registering an IPI event with a NULL .process callback, so the function pointer will never be NULL here.

LGTM

Quoted 22 lines

---

?lib/sbi/sbi_ipi.c | 2 +-
?1 file changed, 1 insertion(+), 1 deletion(-)

lib/sbi/sbi_ipi.c +1 -1
index ac31cd94..1115912b 100644
@@ -241,7 +241,7 @@ void sbi_ipi_process(void)
 ?	while (ipi_type) {
 ?		if (ipi_type & 1UL) {
 ?			ipi_ops = ipi_ops_array[ipi_event];
-			if (ipi_ops && ipi_ops->process)
+			if (ipi_ops)
 ?				ipi_ops->process(scratch);
 ?		}
 ?		ipi_type = ipi_type >> 1;
Signature · 1 line
2.42.0
Applied
#12 lore raw

PATCH 3/3 lib: sbi_ipi: Drop unnecessary ipi_process check

In reply to Samuel Holland
Quoted 5 lines · On Sat, Dec 9, 2023 at 2:53?AM Samuel Holland <[email protected]> wrote:

sbi_ipi_event_create() disallows registering an IPI event with a NULL .process callback, so the function pointer will never be NULL here.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 26 lines

---

 lib/sbi/sbi_ipi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_ipi.c +1 -2
index ac31cd94..1115912b 100644
@@ -241,7 +241,7 @@ void sbi_ipi_process(void)
        while (ipi_type) {
                if (ipi_type & 1UL) {
                        ipi_ops = ipi_ops_array[ipi_event];
-                       if (ipi_ops && ipi_ops->process)
+                       if (ipi_ops)
                                ipi_ops->process(scratch);
                }
                ipi_type = ipi_type >> 1;
--

2.42.0

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