Skip to contentSkip to conversation

lib: sbi_ipi: Return error for invalid hartids

PATCH Applied Andrew Jones 4 participants 14 messages Mar 14, 2025 – Apr 14, 2025

lore
Share & tools
lore
#0 lore raw

cover lib: sbi_ipi: Return error for invalid hartids

Add sbi_hartmask_weight() and use it to check for invalid hartids in SBI IPI. Fixes a bug found with the kvm-unit-tests SBI tests.

Andrew Jones (2):
sbi: Introduce sbi_hartmask_weight
lib: sbi_ipi: Return error for invalid hartids

 include/sbi/sbi_bitmap.h   | 13 +++++++++++++
 include/sbi/sbi_bitops.h   | 22 +++++++++++++++-------
 include/sbi/sbi_hartmask.h | 11 +++++++++++
 lib/sbi/sbi_ipi.c          |  9 +++++++++
 4 files changed, 48 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/2 sbi: Introduce sbi_hartmask_weight

Provide a function to count the number of set bits in a hartmask, which builds on a new function for the same that operates on a bitmask. While at it, improve the performance of sbi_popcount() which is used in the implementation.

---
 include/sbi/sbi_bitmap.h   | 13 +++++++++++++
 include/sbi/sbi_bitops.h   | 22 +++++++++++++++-------
 include/sbi/sbi_hartmask.h | 11 +++++++++++
 3 files changed, 39 insertions(+), 7 deletions(-)
include/sbi/sbi_bitmap.h +13 -0
index 354476c9dd81..596bcc7daa87 100644
@@ -130,4 +130,17 @@ static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
 		__bitmap_xor(dst, src1, src2, nbits);
 }
 
+static inline int bitmap_weight(const unsigned long *src, int nbits)
+{
+	int i, res = 0;
+
+	for (i = 0; i < nbits / BITS_PER_LONG; i++)
+		res += sbi_popcount(src[i]);
+
+	if (nbits % BITS_PER_LONG)
+		res += sbi_popcount(src[i] & BITMAP_LAST_WORD_MASK(nbits));
+
+	return res;
+}
+
 #endif
include/sbi/sbi_bitops.h +15 -7
index 3ddac777d887..d7825e81ea05 100644
@@ -125,14 +125,22 @@ static inline unsigned long sbi_fls(unsigned long word)
  */
 static inline unsigned long sbi_popcount(unsigned long word)
 {
-	unsigned long count = 0;
+	unsigned long count;
 
-	while (word) {
-		word &= word - 1;
-		count++;
-	}
-
-	return count;
+#if BITS_PER_LONG == 64
+	count = word - ((word >> 1) & 0x5555555555555555ul);
+	count = (count & 0x3333333333333333ul) + ((count >> 2) & 0x3333333333333333ul);
+	count = (count + (count >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
+	count = count + (count >> 8);
+	count = count + (count >> 16);
+	return (count + (count >> 32)) & 0x00000000000000FFul;
+#else
+	count = word - ((word >> 1) & 0x55555555);
+	count = (count & 0x33333333) + ((count >> 2) & 0x33333333);
+	count = (count + (count >> 4)) & 0x0F0F0F0F;
+	count = count + (count >> 8);
+	return (count + (count >> 16)) & 0x000000FF;
+#endif
 }
 
 #define for_each_set_bit(bit, addr, size) \
include/sbi/sbi_hartmask.h +11 -0
index 07a8c076bd0a..200ab6e53e4c 100644
@@ -181,6 +181,17 @@ static inline void sbi_hartmask_xor(struct sbi_hartmask *dstp,
 		   sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS);
 }
 
+/**
+ * Count of bits in *srcp
+ * @param srcp the hartmask to count bits in
+ *
+ * Return: count of bits set in *srcp
+ */
+static inline int sbi_hartmask_weight(const struct sbi_hartmask *srcp)
+{
+	return bitmap_weight(sbi_hartmask_bits(srcp), SBI_HARTMASK_MAX_BITS);
+}
+
 /**
  * Iterate over each HART index in hartmask
  * __i hart index
Signature · 1 line
2.48.1
#2 lore raw

PATCH 1/2 sbi: Introduce sbi_hartmask_weight

Quoted 7 lines · On Fri, Mar 14, 2025 at 10:00?PM Andrew Jones <[email protected]> wrote:

Provide a function to count the number of set bits in a hartmask, which builds on a new function for the same that operates on a bitmask. While at it, improve the performance of sbi_popcount() which is used in the implementation.

LGTM.

Regards,
Anup

Quoted 92 lines
---
 include/sbi/sbi_bitmap.h   | 13 +++++++++++++
 include/sbi/sbi_bitops.h   | 22 +++++++++++++++-------
 include/sbi/sbi_hartmask.h | 11 +++++++++++
 3 files changed, 39 insertions(+), 7 deletions(-)
include/sbi/sbi_bitmap.h +13 -0
index 354476c9dd81..596bcc7daa87 100644
@@ -130,4 +130,17 @@ static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
                __bitmap_xor(dst, src1, src2, nbits);
 }
 
+static inline int bitmap_weight(const unsigned long *src, int nbits)
+{
+       int i, res = 0;
+
+       for (i = 0; i < nbits / BITS_PER_LONG; i++)
+               res += sbi_popcount(src[i]);
+
+       if (nbits % BITS_PER_LONG)
+               res += sbi_popcount(src[i] & BITMAP_LAST_WORD_MASK(nbits));
+
+       return res;
+}
+
 #endif
include/sbi/sbi_bitops.h +15 -7
index 3ddac777d887..d7825e81ea05 100644
@@ -125,14 +125,22 @@ static inline unsigned long sbi_fls(unsigned long word)
  */
 static inline unsigned long sbi_popcount(unsigned long word)
 {
-       unsigned long count = 0;
+       unsigned long count;
 
-       while (word) {
-               word &= word - 1;
-               count++;
-       }
-
-       return count;
+#if BITS_PER_LONG == 64
+       count = word - ((word >> 1) & 0x5555555555555555ul);
+       count = (count & 0x3333333333333333ul) + ((count >> 2) & 0x3333333333333333ul);
+       count = (count + (count >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
+       count = count + (count >> 8);
+       count = count + (count >> 16);
+       return (count + (count >> 32)) & 0x00000000000000FFul;
+#else
+       count = word - ((word >> 1) & 0x55555555);
+       count = (count & 0x33333333) + ((count >> 2) & 0x33333333);
+       count = (count + (count >> 4)) & 0x0F0F0F0F;
+       count = count + (count >> 8);
+       return (count + (count >> 16)) & 0x000000FF;
+#endif
 }
 
 #define for_each_set_bit(bit, addr, size) \
include/sbi/sbi_hartmask.h +11 -1
index 07a8c076bd0a..200ab6e53e4c 100644
@@ -181,6 +181,17 @@ static inline void sbi_hartmask_xor(struct sbi_hartmask *dstp,
                   sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS);
 }
 
+/**
+ * Count of bits in *srcp
+ * @param srcp the hartmask to count bits in
+ *
+ * Return: count of bits set in *srcp
+ */
+static inline int sbi_hartmask_weight(const struct sbi_hartmask *srcp)
+{
+       return bitmap_weight(sbi_hartmask_bits(srcp), SBI_HARTMASK_MAX_BITS);
+}
+
 /**
  * Iterate over each HART index in hartmask
  * __i hart index
--

2.48.1

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

#3 lore raw

PATCH 1/2 sbi: Introduce sbi_hartmask_weight

In reply to Andrew Jones

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 55 lines

Provide a function to count the number of set bits in a hartmask, which builds on a new function for the same that operates on a bitmask. While at it, improve the performance of sbi_popcount() which is used in the implementation.

---

?include/sbi/sbi_bitmap.h?? | 13 +++++++++++++
?include/sbi/sbi_bitops.h?? | 22 +++++++++++++++-------
?include/sbi/sbi_hartmask.h | 11 +++++++++++

?3 files changed, 39 insertions(+), 7 deletions(-)

include/sbi/sbi_bitmap.h +0 -0
index 354476c9dd81..596bcc7daa87 100644
@@ -130,4 +130,17 @@ static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
 ?		__bitmap_xor(dst, src1, src2, nbits);
 ?}

?

include/sbi/sbi_bitmap.h +13 -0
+static inline int bitmap_weight(const unsigned long *src, int nbits)
+{
+	int i, res = 0;
+
+	for (i = 0; i < nbits / BITS_PER_LONG; i++)
+		res += sbi_popcount(src[i]);
+
+	if (nbits % BITS_PER_LONG)
+		res += sbi_popcount(src[i] & BITMAP_LAST_WORD_MASK(nbits));
+
+	return res;
+}
+

?#endif

include/sbi/sbi_bitops.h +0 -0
index 3ddac777d887..d7825e81ea05 100644
@@ -125,14 +125,22 @@ static inline unsigned long sbi_fls(unsigned long word)

? */
?static inline unsigned long sbi_popcount(unsigned long word) ?{

include/sbi/sbi_bitops.h +1 -1
-	unsigned long count = 0;
+	unsigned long count;

?

include/sbi/sbi_bitops.h +4 -6
-	while (word) {
-		word &= word - 1;
-		count++;
-	}
-
-	return count;
+#if BITS_PER_LONG == 64
+	count = word - ((word >> 1) & 0x5555555555555555ul);
+	count = (count & 0x3333333333333333ul) + ((count >> 2) & 0x3333333333333333ul);
+	count = (count + (count >> 4)) & 0x0F0F0F0F0F0F0F0Ful;

include/sbi/sbi_bitops.h static inline unsigned long sbi_fls(unsigned long word) · line 143

Quoted 10 lines
include/sbi/sbi_bitops.h +10 -0
+	count = count + (count >> 8);
+	count = count + (count >> 16);
+	return (count + (count >> 32)) & 0x00000000000000FFul;
+#else
+	count = word - ((word >> 1) & 0x55555555);
+	count = (count & 0x33333333) + ((count >> 2) & 0x33333333);
+	count = (count + (count >> 4)) & 0x0F0F0F0F;
+	count = count + (count >> 8);
+	return (count + (count >> 16)) & 0x000000FF;
+#endif

This is an optimisation that can be put into a separate patch

Regards,
Xiang W

Quoted 28 lines

?}
?
?#define for_each_set_bit(bit, addr, size) \

include/sbi/sbi_hartmask.h +0 -0
index 07a8c076bd0a..200ab6e53e4c 100644
@@ -181,6 +181,17 @@ static inline void sbi_hartmask_xor(struct sbi_hartmask *dstp,
 ?		?? sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS);
 ?}

?

include/sbi/sbi_hartmask.h +11 -0
+/**
+ * Count of bits in *srcp
+ * @param srcp the hartmask to count bits in
+ *
+ * Return: count of bits set in *srcp
+ */
+static inline int sbi_hartmask_weight(const struct sbi_hartmask *srcp)
+{
+	return bitmap_weight(sbi_hartmask_bits(srcp), SBI_HARTMASK_MAX_BITS);
+}
+

?/**
? * Iterate over each HART index in hartmask
? * __i hart index

Signature · 1 line
2.48.1
#4 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

In reply to Andrew Jones

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

---
 lib/sbi/sbi_ipi.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/sbi/sbi_ipi.c +9 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+
 	/* Find the target harts */
 	rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
 	if (rc)
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 
 	if (hbase != -1UL) {
 		struct sbi_hartmask tmp_mask = { 0 };
+		int count = sbi_popcount(hmask);
 
 		for (i = hbase; hmask; i++, hmask >>= 1) {
 			if (hmask & 1UL)
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 		}
 
 		sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;
 	}
 
 	/* Send IPIs */
Signature · 1 line
2.48.1
#5 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 16 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

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

lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

?

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

Quoted 5 lines
lib/sbi/sbi_ipi.c +5 -0
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+

A modification here would change the original intent of the function. lib/sbi/sbi_system.c#L72 will eventually call sbi_ipi_send_many(0, -1, ...) for sending ipi interrupts to all harts, this modification breaks sbi_system_reset

Regards,
Xiang W

Quoted 24 lines

? /* Find the target harts */
? rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
? if (rc)

lib/sbi/sbi_ipi.c +0 -0
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)

?
? if (hbase != -1UL) {
? struct sbi_hartmask tmp_mask = { 0 };

lib/sbi/sbi_ipi.c +1 -0
+		int count = sbi_popcount(hmask);

?
? for (i = hbase; hmask; i++, hmask >>= 1) {
? if (hmask & 1UL)

lib/sbi/sbi_ipi.c +0 -0
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?		}

?
? sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);

lib/sbi/sbi_ipi.c +3 -0
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;

? }
?
? /* Send IPIs */

Signature · 1 line
2.48.1
#6 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

Quoted 9 lines · Xiang W

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 8 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

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

Quoted 15 lines

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

Quoted 13 lines
lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

?

lib/sbi/sbi_ipi.c +5 -0
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+

A modification here would change the original intent of the function. lib/sbi/sbi_system.c#L72 will eventually call sbi_ipi_send_many(0, -1, ...)

How could the above condition affect that call where hbase == -1? This condition explicitly checks that hbase != -1.

Quoted 2 lines

for sending ipi interrupts to all harts, this modification breaks sbi_system_reset

Is that an assertion based on a failed test? Because it looks like it should still work to me.

(kvm-unit-tests SBI testing also tests IPI broadcast, and it's still passing.)

drew

Quoted 28 lines

Regards,
Xiang W

Quoted 24 lines

? /* Find the target harts */
? rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
? if (rc)

lib/sbi/sbi_ipi.c +0 -0
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)

?
? if (hbase != -1UL) {
? struct sbi_hartmask tmp_mask = { 0 };

lib/sbi/sbi_ipi.c +1 -0
+		int count = sbi_popcount(hmask);

?
? for (i = hbase; hmask; i++, hmask >>= 1) {
? if (hmask & 1UL)

lib/sbi/sbi_ipi.c +0 -0
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?		}

?
? sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);

lib/sbi/sbi_ipi.c +3 -0
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;

? }
?
? /* Send IPIs */

Signature · 1 line
2.48.1
#7 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

Quoted 30 lines · On Sat, Mar 15, 2025 at 4:20?PM Andrew Jones <[email protected]> wrote:
Quoted 17 lines · Xiang W

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 16 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

---
 lib/sbi/sbi_ipi.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
    struct sbi_domain *dom = sbi_domain_thishart_ptr();
    struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
Quoted 7 lines

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

Quoted 5 lines
lib/sbi/sbi_ipi.c +5 -0
+   if (hmask == 0 && hbase != -1UL) {
+           /* Nothing to do, but it's not an error either. */
+           return 0;
+   }
+

A modification here would change the original intent of the function. lib/sbi/sbi_system.c#L72 will eventually call sbi_ipi_send_many(0, -1, ...)

How could the above condition affect that call where hbase == -1? This condition explicitly checks that hbase != -1.

Quoted 8 lines
Quoted 2 lines

for sending ipi interrupts to all harts, this modification breaks sbi_system_reset

Is that an assertion based on a failed test? Because it looks like it should still work to me.

(kvm-unit-tests SBI testing also tests IPI broadcast, and it's still passing.)

sbi_ipi_send_many() is an internal function used from a variety of places. Xiang concern is correct because for system reset should HALT all CPUs before issuing the actual platform specific reset operation. If we have to change something then sbi_ipi_send_smode() is the right place because that's the function called via SBI calls.

Now hart_mask = 0 and hart_mask_base = -1UL are indeed valid parameters and represents "All HARTs" as-per SBI spec. Refer, the following text from "3.1. Hart list parameter":

"In a single SBI function call, the maximum number of harts that can be set is always XLEN. If a lower privilege mode needs to pass information about more than XLEN harts, it must invoke the SBI function multiple times. hart_mask_base can be set to -1 to indicate that hart_mask shall be ignored and all available harts must be considered."

In the "Table 7. IPI Send Errors" should be further refined to say the following for SBI_ERR_INVALID_PARAM:

"At least one hartid constructed from hart_mask_base and hart_mask as-per section 3.1 , is not valid, i.e. either the hartid is not enabled by the platform or is not available to the supervisor."

Regards,
Anup

Quoted 31 lines

drew

Quoted 28 lines

Regards,
Xiang W

Quoted 24 lines
lib/sbi/sbi_ipi.c +4 -1
    /* Find the target harts */
    rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
    if (rc)
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 
    if (hbase != -1UL) {
            struct sbi_hartmask tmp_mask = { 0 };
+           int count = sbi_popcount(hmask);
 
            for (i = hbase; hmask; i++, hmask >>= 1) {
                    if (hmask & 1UL)
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
            }
 
            sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
+
+           if (sbi_hartmask_weight(&target_mask) != count)
+                   return SBI_EINVAL;
    }
 
    /* Send IPIs */
--

2.48.1

#8 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

Quoted 58 lines · On Sat, Mar 15, 2025 at 4:50?PM Anup Patel <[email protected]> wrote:
Quoted 30 lines · On Sat, Mar 15, 2025 at 4:20?PM Andrew Jones <[email protected]> wrote:
Quoted 17 lines · Xiang W

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 16 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

---
 lib/sbi/sbi_ipi.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
    struct sbi_domain *dom = sbi_domain_thishart_ptr();
    struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
Quoted 7 lines

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

Quoted 5 lines
lib/sbi/sbi_ipi.c +5 -0
+   if (hmask == 0 && hbase != -1UL) {
+           /* Nothing to do, but it's not an error either. */
+           return 0;
+   }
+

A modification here would change the original intent of the function. lib/sbi/sbi_system.c#L72 will eventually call sbi_ipi_send_many(0, -1, ...)

How could the above condition affect that call where hbase == -1? This condition explicitly checks that hbase != -1.

Quoted 8 lines
Quoted 2 lines

for sending ipi interrupts to all harts, this modification breaks sbi_system_reset

Is that an assertion based on a failed test? Because it looks like it should still work to me.

(kvm-unit-tests SBI testing also tests IPI broadcast, and it's still passing.)

sbi_ipi_send_many() is an internal function used from a variety of places. Xiang concern is correct because for system reset should HALT all CPUs before issuing the actual platform specific reset operation. If we have to change something then sbi_ipi_send_smode() is the right place because that's the function called via SBI calls.

Now hart_mask = 0 and hart_mask_base = -1UL are indeed valid parameters and represents "All HARTs" as-per SBI spec. Refer, the following text from "3.1. Hart list parameter":

"In a single SBI function call, the maximum number of harts that can be set is always XLEN. If a lower privilege mode needs to pass information about more than XLEN harts, it must invoke the SBI function multiple times. hart_mask_base can be set to -1 to indicate that hart_mask shall be ignored and all available harts must be considered."

Quoted 6 lines

In the "Table 7. IPI Send Errors" should be further refined to say the following for SBI_ERR_INVALID_PARAM:

"At least one hartid constructed from hart_mask_base and hart_mask as-per section 3.1 , is not valid, i.e. either the hartid is not enabled by the platform or is not available to the supervisor."

Please ignore this comment. I misread the "if ()" check added by this patch. Indeed, the sbi_ipi_send_many(0, -1, ...) call from system reset won't be impacted by "if (hmask == 0 && hbase != -1UL)" in sbi_ipi_send_many().

Apologies for the noise.

Regards,
Anup

Quoted 40 lines

Regards,
Anup

Quoted 31 lines

drew

Quoted 28 lines

Regards,
Xiang W

Quoted 24 lines
lib/sbi/sbi_ipi.c +4 -1
    /* Find the target harts */
    rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
    if (rc)
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 
    if (hbase != -1UL) {
            struct sbi_hartmask tmp_mask = { 0 };
+           int count = sbi_popcount(hmask);
 
            for (i = hbase; hmask; i++, hmask >>= 1) {
                    if (hmask & 1UL)
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
            }
 
            sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);
+
+           if (sbi_hartmask_weight(&target_mask) != count)
+                   return SBI_EINVAL;
    }
 
    /* Send IPIs */
--

2.48.1

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

#9 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

In reply to Andrew Jones

? 2025-03-15?? 11:50 +0100?Andrew Jones???

Quoted 26 lines · Xiang W
Quoted 9 lines

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 8 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

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

Quoted 15 lines

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

Quoted 13 lines
lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

?

lib/sbi/sbi_ipi.c +5 -0
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+

A modification here would change the original intent of the function. lib/sbi/sbi_system.c#L72 will eventually call sbi_ipi_send_many(0, -1, ...)

Quoted 3 lines

How could the above condition affect that call where hbase == -1? This condition explicitly checks that hbase != -1.

I read it wrong. Sorry

Regards,
Xiang W

Quoted 39 lines
Quoted 2 lines

for sending ipi interrupts to all harts, this modification breaks sbi_system_reset

Is that an assertion based on a failed test? Because it looks like it should still work to me.

(kvm-unit-tests SBI testing also tests IPI broadcast, and it's still passing.)

drew

Quoted 28 lines

Regards,
Xiang W

Quoted 24 lines

? /* Find the target harts */
? rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
? if (rc)

lib/sbi/sbi_ipi.c +0 -0
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)

?
? if (hbase != -1UL) {
? struct sbi_hartmask tmp_mask = { 0 };

lib/sbi/sbi_ipi.c +1 -0
+		int count = sbi_popcount(hmask);

?
? for (i = hbase; hmask; i++, hmask >>= 1) {
? if (hmask & 1UL)

lib/sbi/sbi_ipi.c +0 -0
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?		}

?
? sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);

lib/sbi/sbi_ipi.c +3 -0
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;

? }
?
? /* Send IPIs */

Signature · 1 line
2.48.1
#10 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

In reply to Andrew Jones

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 36 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

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

lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

?

lib/sbi/sbi_ipi.c +5 -0
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+

? /* Find the target harts */
? rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
? if (rc)

lib/sbi/sbi_ipi.c +0 -0
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)

?
? if (hbase != -1UL) {
? struct sbi_hartmask tmp_mask = { 0 };

lib/sbi/sbi_ipi.c +1 -0
+		int count = sbi_popcount(hmask);

?
? for (i = hbase; hmask; i++, hmask >>= 1) {
? if (hmask & 1UL)

lib/sbi/sbi_ipi.c +0 -0
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?		}

?
? sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);

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

Quoted 3 lines
lib/sbi/sbi_ipi.c +3 -0
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;

If hmask is equal to 0 and hbase is equal to -1, count will be 0. This will always return SBI_EINVAL. Need to add some code to skip this case

Regards,
Xiang W

Quoted 6 lines

? }
?
? /* Send IPIs */

Signature · 1 line
2.48.1
#11 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

Quoted 35 lines · Xiang W

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 34 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

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

lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

?

lib/sbi/sbi_ipi.c +5 -0
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+

? /* Find the target harts */
? rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
? if (rc)

lib/sbi/sbi_ipi.c +0 -0
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)

?
? if (hbase != -1UL) {
? struct sbi_hartmask tmp_mask = { 0 };

lib/sbi/sbi_ipi.c +1 -0
+		int count = sbi_popcount(hmask);

?
? for (i = hbase; hmask; i++, hmask >>= 1) {
? if (hmask & 1UL)

lib/sbi/sbi_ipi.c +0 -0
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?		}
Quoted 8 lines

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

Quoted 5 lines

?
? sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);

lib/sbi/sbi_ipi.c +3 -0
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;

If hmask is equal to 0 and hbase is equal to -1, count will be 0. This will always return SBI_EINVAL. Need to add some code to skip this case

Wrong, again, and this time for two reasons. This code is in an 'hbase != -1' block (as can be seen a few lines above). Also, if count were to equal 0 (which it can't, due to the early out added above), then the weight of target_mask would also be 0, which means we would never return SBI_EINVAL, as that would require 0 != 0 to be true.

drew

#12 lore raw

PATCH 2/2 lib: sbi_ipi: Return error for invalid hartids

? 2025-03-17?? 09:45 +0100?Andrew Jones???

Quoted 45 lines · Xiang W
Quoted 35 lines

? 2025-03-14?? 17:30 +0100?Andrew Jones???

Quoted 34 lines

sbi_send_ipi() should return SBI_ERR_INVALID_PARAM if even one hartid constructed from hart_mask_base and hart_mask, is not valid.

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

lib/sbi/sbi_ipi.c +0 -0
index 52898d302376..2de459b089ff 100644
@@ -116,6 +116,11 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

?

lib/sbi/sbi_ipi.c +5 -0
+	if (hmask == 0 && hbase != -1UL) {
+		/* Nothing to do, but it's not an error either. */
+		return 0;
+	}
+

? /* Find the target harts */
? rc = sbi_hsm_hart_interruptible_mask(dom, &target_mask);
? if (rc)

lib/sbi/sbi_ipi.c +0 -0
@@ -123,6 +128,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)

?
? if (hbase != -1UL) {
? struct sbi_hartmask tmp_mask = { 0 };

lib/sbi/sbi_ipi.c +1 -0
+		int count = sbi_popcount(hmask);

?
? for (i = hbase; hmask; i++, hmask >>= 1) {
? if (hmask & 1UL)

lib/sbi/sbi_ipi.c +0 -0
@@ -130,6 +136,9 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
 ?		}
Quoted 8 lines

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

Quoted 5 lines

?
? sbi_hartmask_and(&target_mask, &target_mask, &tmp_mask);

lib/sbi/sbi_ipi.c +3 -0
+
+		if (sbi_hartmask_weight(&target_mask) != count)
+			return SBI_EINVAL;

If hmask is equal to 0 and hbase is equal to -1, count will be 0. This will always return SBI_EINVAL. Need to add some code to skip this case

Quoted 5 lines

Wrong, again, and this time for two reasons. This code is in an 'hbase != -1' block (as can be seen a few lines above). Also, if count were to equal 0 (which it can't, due to the early out added above), then the weight of target_mask would also be 0, which means we would never return SBI_EINVAL, as that would require 0 != 0 to be true.

pardon me!

Xiang

Quoted 1 line

drew

Applied
#13 lore raw
In reply to Andrew Jones
Quoted 7 lines · Andrew Jones

Add sbi_hartmask_weight() and use it to check for invalid hartids in SBI IPI. Fixes a bug found with the kvm-unit-tests SBI tests.

Andrew Jones (2):
sbi: Introduce sbi_hartmask_weight
lib: sbi_ipi: Return error for invalid hartids

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Quoted 15 lines
 include/sbi/sbi_bitmap.h   | 13 +++++++++++++
 include/sbi/sbi_bitops.h   | 22 +++++++++++++++-------
 include/sbi/sbi_hartmask.h | 11 +++++++++++
 lib/sbi/sbi_ipi.c          |  9 +++++++++
 4 files changed, 48 insertions(+), 7 deletions(-)

--
2.48.1

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi