Skip to contentSkip to conversation

Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS.

PATCH New Chao-ying Fu 8 participants 24 messages Jan 9, 2025 – Mar 14, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS.

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the check.

---
 lib/utils/fdt/fdt_domain.c  | 3 ---
 lib/utils/fdt/fdt_helper.c  | 9 ---------
 platform/generic/platform.c | 3 ---
 3 files changed, 15 deletions(-)
lib/utils/fdt/fdt_domain.c +0 -3
index 4bc7ed8..6bc63c2 100644
@@ -471,9 +471,6 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 		if (err)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= val32)
-			continue;
-
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
 			continue;
 
lib/utils/fdt/fdt_helper.c +0 -9
index cb350e5..aec08fa 100644
@@ -1032,9 +1032,6 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
-
 		if (match_hwirq == hwirq) {
 			if (hartid < first_hartid)
 				first_hartid = hartid;
@@ -1097,9 +1094,6 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
-
 		if (hwirq == IRQ_M_TIMER)
 			hcount++;
 	}
@@ -1153,9 +1147,6 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
-
 		if (hwirq == IRQ_M_SOFT)
 			hcount++;
 	}
platform/generic/platform.c +0 -3
index c03ed88..331ff8e 100644
@@ -200,9 +200,6 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
-
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
 			continue;
 
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS.

Quoted 8 lines · Chao-ying Fu

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the check.

---
 lib/utils/fdt/fdt_domain.c  | 3 ---
 lib/utils/fdt/fdt_helper.c  | 9 ---------
 platform/generic/platform.c | 3 ---
 3 files changed, 15 deletions(-)

Is there a specific platform to test this patch? This also applies to your other patches.

Regards,
Inochi

#2 lore raw

PATCH 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS.

Quoted 11 lines · On Thu, Jan 9, 2025 at 4:43?PM Inochi Amaoto <[email protected]> wrote:
Quoted 8 lines · Chao-ying Fu

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the check.

---
 lib/utils/fdt/fdt_domain.c  | 3 ---
 lib/utils/fdt/fdt_helper.c  | 9 ---------
 platform/generic/platform.c | 3 ---
 3 files changed, 15 deletions(-)
Quoted 5 lines

Is there a specific platform to test this patch? This also applies to your other patches.

Regards,
Inochi

We run OpenSBI and Linux on FPGA boards, RTL simulations, or QEMU. One example of 24 hart ids is as follows.
0x0, 0x1, 0x10, 0x11, 0x20, 0x21, 0x30, 0x31,
0x10000, 0x10001, 0x10010, 0x10011, 0x10020, 0x10021, 0x10030, 0x10031, 0x20000, 0x20001, 0x20010, 0x20011, 0x20020, 0x20021, 0x20030, 0x20031. After removing the hart id check, OpenSBI is fine.
Note that last year OpenSBI changed to use hart index to access the array. Thanks!

Regards,
Chao-ying

#3 lore raw

PATCH 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS.

In reply to Chao-ying Fu
Quoted 3 lines · Chao-ying Fu

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the check.

The checks aren?t there for fun, they?re required. From sbi_hartmask.h:

Quoted 7 lines

/**
* Maximum number of bits in a hartmask
*
* The hartmask is indexed using physical HART id so this define
* also represents the maximum number of HART ids generic OpenSBI
* can handle.
*/

OpenSBI does not support hart IDs outside this range today, so there is real work to do if that?s needed, not just deleting checks. Otherwise you?re going to end up with out-of-bounds memory accesses.

I?m concerned that none of the patches you?ve sent just now seem to have been thought through properly. Whilst I?m sure you have the best intentions, I also note that this kind of patch is precisely the kind of patch one might make were one to want to insert vulnerabilities into core system components.

Jess

Quoted 76 lines

---

lib/utils/fdt/fdt_domain.c  | 3 ---
lib/utils/fdt/fdt_helper.c  | 9 ---------
platform/generic/platform.c | 3 ---
3 files changed, 15 deletions(-)
lib/utils/fdt/fdt_domain.c +0 -3
index 4bc7ed8..6bc63c2 100644
@@ -471,9 +471,6 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 if (err)
 continue;
 
- if (SBI_HARTMASK_MAX_BITS <= val32)
- continue;
-
 if (!fdt_node_is_enabled(fdt, cpu_offset))
 continue;
 
lib/utils/fdt/fdt_helper.c +0 -9
index cb350e5..aec08fa 100644
@@ -1032,9 +1032,6 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 if (rc)
 continue;
 
- if (SBI_HARTMASK_MAX_BITS <= hartid)
- continue;
-
 if (match_hwirq == hwirq) {
 if (hartid < first_hartid)
 first_hartid = hartid;
@@ -1097,9 +1094,6 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 if (rc)
 continue;
 
- if (SBI_HARTMASK_MAX_BITS <= hartid)
- continue;
-
 if (hwirq == IRQ_M_TIMER)
 hcount++;
 }
@@ -1153,9 +1147,6 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 if (rc)
 continue;
 
- if (SBI_HARTMASK_MAX_BITS <= hartid)
- continue;
-
 if (hwirq == IRQ_M_SOFT)
 hcount++;
 }
platform/generic/platform.c +0 -3
index c03ed88..331ff8e 100644
@@ -200,9 +200,6 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 if (rc)
 continue;
 
- if (SBI_HARTMASK_MAX_BITS <= hartid)
- continue;
-
 if (!fdt_node_is_enabled(fdt, cpu_offset))
 continue;
 
Signature · 7 lines
2.47.1


-- 
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

PATCH 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS.

Quoted 15 lines · On Thu, Jan 9, 2025 at 4:57?PM Jessica Clarke <[email protected]> wrote:
Quoted 3 lines · Chao-ying Fu

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the check.

The checks aren?t there for fun, they?re required. From sbi_hartmask.h:

Quoted 7 lines

/**
* Maximum number of bits in a hartmask
*
* The hartmask is indexed using physical HART id so this define
* also represents the maximum number of HART ids generic OpenSBI
* can handle.
*/

This comment may not be accurate, as OpenSBI changed to use hart index to access the array last year.

Quoted 5 lines

OpenSBI does not support hart IDs outside this range today, so there is real work to do if that?s needed, not just deleting checks. Otherwise you?re going to end up with out-of-bounds memory accesses.

Quoted 7 lines

I?m concerned that none of the patches you?ve sent just now seem to have been thought through properly. Whilst I?m sure you have the best intentions, I also note that this kind of patch is precisely the kind of patch one might make were one to want to insert vulnerabilities into core system components.

Jess

We did run OpenSBI 1.6 and Linux 6.12 with hart ids (some of them >= SBI_HARTMASK_MAX_BITS) with this patch on FPGA boards, RTL simulations, and QEMU.
Thanks!

Regards,
Chao-ying

#5 lore raw

PATCH v2 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS

In reply to Chao-ying Fu

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the original check. Instead we check the hart index against SBI_HARTMASK_MAX_BITS.

---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 		if (err)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
 			continue;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +1 -1
index c03ed88..6ec0d73 100644
@@ -200,7 +200,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
 			continue;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS

? 2025-01-15?? 15:46 -0800?Raj Vishwanathan???

Quoted 63 lines

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the original check. Instead we check the hart index against SBI_HARTMASK_MAX_BITS.
---

?lib/utils/fdt/fdt_domain.c? | 2 +-
?lib/utils/fdt/fdt_helper.c? | 6 +++---
?platform/generic/platform.c | 2 +-

?3 files changed, 5 insertions(+), 5 deletions(-)

lib/utils/fdt/fdt_domain.c +0 -0
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 ?		if (err)
 ?			continue;

?

lib/utils/fdt/fdt_domain.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))

? continue;
?
? if (!fdt_node_is_enabled(fdt, cpu_offset))

lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (match_hwirq == hwirq) {

lib/utils/fdt/fdt_helper.c +0 -0
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (hwirq == IRQ_M_TIMER)

lib/utils/fdt/fdt_helper.c +0 -0
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (hwirq == IRQ_M_SOFT)

platform/generic/platform.c +0 -0
index c03ed88..6ec0d73 100644
@@ -200,7 +200,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?		if (rc)
 ?			continue;

?

platform/generic/platform.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
Quoted 1 line

? continue;

s/continue/break

Regards,
Xiang W

Quoted 6 lines

?
? if (!fdt_node_is_enabled(fdt, cpu_offset))

Signature · 1 line
2.43.0
#7 lore raw

PATCH v3 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS

In reply to Raj Vishwanathan

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the original check. Instead we check the hart index against SBI_HARTMASK_MAX_BITS.

Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure. The reviewer suggested continue, immediate failure quickly will localize the issue.

---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 		if (err)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
 			continue;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +2 -2
index c03ed88..c8a7a59 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
+			goto fail;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
 			continue;
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS

? 2025-01-20?? 17:12 -0800?Raj Vishwanathan???

Quoted 4 lines

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the original check. Instead we check the hart index against SBI_HARTMASK_MAX_BITS.

missing your Signed-off-by

Quoted 2 lines

Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure. The reviewer suggested continue, immediate failure quickly will localize the issue.

The previous line need move after '---'

Quoted 58 lines

---

?lib/utils/fdt/fdt_domain.c? | 2 +-
?lib/utils/fdt/fdt_helper.c? | 6 +++---
?platform/generic/platform.c | 4 ++--

?3 files changed, 6 insertions(+), 6 deletions(-)

lib/utils/fdt/fdt_domain.c +0 -0
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 ?		if (err)
 ?			continue;

?

lib/utils/fdt/fdt_domain.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))

? continue;
?
? if (!fdt_node_is_enabled(fdt, cpu_offset))

lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (match_hwirq == hwirq) {

lib/utils/fdt/fdt_helper.c +0 -0
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (hwirq == IRQ_M_TIMER)

lib/utils/fdt/fdt_helper.c +0 -0
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (hwirq == IRQ_M_SOFT)

platform/generic/platform.c +0 -0
index c03ed88..c8a7a59 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?		if (rc)
 ?			continue;

?

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 204

Quoted 4 lines
platform/generic/platform.c +2 -2
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
+			goto fail;

It can be ignored.

Regards,
Xiang W

Quoted 7 lines

?
? if (!fdt_node_is_enabled(fdt, cpu_offset))
? continue;

Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS

In reply to Raj Vishwanathan

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the original check. Instead we check the hart index against SBI_HARTMASK_MAX_BITS.

Changes in V2:
Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure.

Changes in V3:
Ignore if hart_count is more than SBI_HARTMASK_MAX_BITS.

---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 		if (err)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
 			continue;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +2 -2
index c03ed88..027ec89 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
+			break;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
 			continue;
Signature · 1 line
2.43.0
#10 lore raw

PATCH v3 1/1 Work with hartid equal to or greater than SBI_HARTMASK_MAX_BITS

Simplify patch subject and use "lib: utils:" as subject prefix.

Quoted 10 lines · On Wed, Jan 22, 2025 at 6:13?AM Raj Vishwanathan <[email protected]>…

Some platforms use hartid that is equal to greater than SBI_HARTMASK_MAX_BITS, so we should remove the original check. Instead we check the hart index against SBI_HARTMASK_MAX_BITS.

Changes in V2:
Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure.

Changes in V3:
Ignore if hart_count is more than SBI_HARTMASK_MAX_BITS.

The change log should be below after "---" so that it is not included as part of the commit description.

Regards,
Anup

Quoted 75 lines
---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
                if (err)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= val32)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
                        continue;
 
                if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
                        continue;
 
                if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
                        continue;
 
                if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
                        continue;
 
                if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +2 -3
index c03ed88..027ec89 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
-                       continue;
+               if (SBI_HARTMASK_MAX_BITS <= hart_count)
+                       break;
 
                if (!fdt_node_is_enabled(fdt, cpu_offset))
                        continue;
--

2.43.0

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

#11 lore raw

PATCH v3 1/1 New configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT

In reply to Raj Vishwanathan

We add a new configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT as an int If it is set to 0 or not defined, we will continue with the previous defintion of allocating pointer size chunks. Otherwise we will use the user specified size.
We use the scratch allocation alignment to 64 bytes or cacheline size, to avoid two atomic variables from the same cache line causing livelock on some platforms.

---

Changes in V2:
Remove platform specific references to 64 bytes in the configuration

Changes in V3:
Remove platform specific references in 64 bytes in the comments

---
 lib/sbi/Kconfig       |  7 +++++++
 lib/sbi/sbi_scratch.c | 14 ++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
lib/sbi/Kconfig +7 -0
index c6cc04b..5f7eb70 100644
@@ -69,4 +69,11 @@ config SBI_ECALL_SSE
 config SBI_ECALL_MPXY
 	bool "MPXY extension"
 	default y
+config SBI_SCRATCH_ALLOC_ALIGNMENT
+	int  "Scratch allocation alignment"
+	default 0
+	help
+	  We provide the option to customize the alignment to allocate from
+	  the extra space in sbi_scratch. Leave it 0 for default behaviour.
+
 endmenu
lib/sbi/sbi_scratch.c +12 -2
index ccbbc68..0f1be58 100644
@@ -14,6 +14,16 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+/*
+ * We do the scratch allocation on the basis of a user configuration.
+ */
+#if !defined(CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT) || (CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT==0)
+#define SCRATCH_ALLOC_ALIGNMENT __SIZEOF_POINTER__
+#else
+#define SCRATCH_ALLOC_ALIGNMENT CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT
+#endif
+
+
 u32 last_hartindex_having_scratch = 0;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -70,8 +80,8 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	size += SCRATCH_ALLOC_ALIGNMENT- 1;
+	size &= ~((unsigned long)SCRATCH_ALLOC_ALIGNMENT - 1);
 
 	spin_lock(&extra_lock);
 
Signature · 1 line
2.43.0
#12 lore raw

PATCH v3 1/1 New configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT

? 2025-01-27?? 12:20 -0800?Raj Vishwanathan???

Quoted 9 lines

We add a new configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT as an int If it is set to 0 or not defined, we will continue with the previous defintion of allocating pointer size chunks. Otherwise we will use the user specified size.
We use the scratch allocation alignment to 64 bytes or cacheline size, to avoid two atomic variables from the same cache line causing livelock on some platforms.

LGMT

Quoted 64 lines

---

Changes in V2:
Remove platform specific references to 64 bytes in the configuration

Changes in V3:
Remove platform specific references in 64 bytes in the comments
---
?lib/sbi/Kconfig?????? |? 7 +++++++
?lib/sbi/sbi_scratch.c | 14 ++++++++++++--
?2 files changed, 19 insertions(+), 2 deletions(-)

lib/sbi/Kconfig +0 -0
index c6cc04b..5f7eb70 100644
@@ -69,4 +69,11 @@ config SBI_ECALL_SSE

?config SBI_ECALL_MPXY
? bool "MPXY extension"
? default y

lib/sbi/Kconfig +7 -0
+config SBI_SCRATCH_ALLOC_ALIGNMENT
+	int? "Scratch allocation alignment"
+	default 0
+	help
+	? We provide the option to customize the alignment to allocate from
+	? the extra space in sbi_scratch. Leave it 0 for default behaviour.
+

?endmenu

lib/sbi/sbi_scratch.c +0 -0
index ccbbc68..0f1be58 100644
@@ -14,6 +14,16 @@

?#include <sbi/sbi_scratch.h>
?#include <sbi/sbi_string.h>
?

lib/sbi/sbi_scratch.c +10 -0
+/*
+ * We do the scratch allocation on the basis of a user configuration.
+ */
+#if !defined(CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT) || (CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT==0)
+#define SCRATCH_ALLOC_ALIGNMENT __SIZEOF_POINTER__
+#else
+#define SCRATCH_ALLOC_ALIGNMENT CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT
+#endif
+
+

?u32 last_hartindex_having_scratch = 0;
?u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U }; ?struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };

lib/sbi/sbi_scratch.c +0 -0
@@ -70,8 +80,8 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	if (!size)
 ?		return 0;

?

lib/sbi/sbi_scratch.c +2 -2
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	size += SCRATCH_ALLOC_ALIGNMENT- 1;
+	size &= ~((unsigned long)SCRATCH_ALLOC_ALIGNMENT - 1);

?
? spin_lock(&extra_lock);
?

Signature · 1 line
2.43.0
#13 lore raw

PATCH v3 1/1 New configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT

In reply to Raj Vishwanathan
Quoted 27 lines · On Tue, Jan 28, 2025 at 1:50?AM Raj Vishwanathan <[email protected]>…

We add a new configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT as an int If it is set to 0 or not defined, we will continue with the previous defintion of allocating pointer size chunks. Otherwise we will use the user specified size.
We use the scratch allocation alignment to 64 bytes or cacheline size, to avoid two atomic variables from the same cache line causing livelock on some platforms.

---

Changes in V2:
Remove platform specific references to 64 bytes in the configuration

Changes in V3:
Remove platform specific references in 64 bytes in the comments

---
 lib/sbi/Kconfig       |  7 +++++++
 lib/sbi/sbi_scratch.c | 14 ++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
lib/sbi/Kconfig +0 -0
index c6cc04b..5f7eb70 100644

lib/sbi/Kconfig config SBI_ECALL_SSE · line 78

Quoted 10 lines
lib/sbi/Kconfig +7 -0
@@ -69,4 +69,11 @@ config SBI_ECALL_SSE
 config SBI_ECALL_MPXY
        bool "MPXY extension"
        default y
+config SBI_SCRATCH_ALLOC_ALIGNMENT
+       int  "Scratch allocation alignment"
+       default 0
+       help
+         We provide the option to customize the alignment to allocate from
+         the extra space in sbi_scratch. Leave it 0 for default behaviour.
+

Introducing a kconfig option is not going to fly since we have to re-compile separately for platforms while we are trying our best to support the same firmware binary for multiple platforms.

Instead, I suggest the following:
1) The default scratch allocation alignment can be __SIZEOF_POINTER__
2) Introduce get_cache_line_size() operation in "struct sbi_platform_operations" which platforms can use to return desired "cache line size for allocations".
3) For generic platform, the get_cache_line_size() return value can be based on the "riscv,cbom-block-size" property of CPU DT nodes OR based on platform_overrride.
4) The sbi_scratch_init() should sanity check the value returned by platform get_cache_line_size() before using it as minimum allocation size for scratch allocations.

Regards,
Anup

Quoted 41 lines
lib/sbi/Kconfig +0 -0
 endmenu
lib/sbi/sbi_scratch.c +12 -3
index ccbbc68..0f1be58 100644
@@ -14,6 +14,16 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+/*
+ * We do the scratch allocation on the basis of a user configuration.
+ */
+#if !defined(CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT) || (CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT==0)
+#define SCRATCH_ALLOC_ALIGNMENT __SIZEOF_POINTER__
+#else
+#define SCRATCH_ALLOC_ALIGNMENT CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT
+#endif
+
+
 u32 last_hartindex_having_scratch = 0;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -70,8 +80,8 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        if (!size)
                return 0;
 
-       size += __SIZEOF_POINTER__ - 1;
-       size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+       size += SCRATCH_ALLOC_ALIGNMENT- 1;
+       size &= ~((unsigned long)SCRATCH_ALLOC_ALIGNMENT - 1);
 
        spin_lock(&extra_lock);
 
--

2.43.0

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

#14 lore raw

PATCH v3 1/1 New configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT

Hi Anup

I have been reviewing your suggestions, and I am a bit confused by them.

---------------------- Snipped to retain only the relevant bits
--------------------------
The scratch allocation happens too early in the boot to get the
platform override return values (from parsing the device tree.)

The other issue is that riscv,cbom-block-size is a per-hart value and testing all kinds of combinations may become unnecessarily complicated.

If the general configuration value is not acceptable, I can move the configuration to a platform specific configuration.

Let me know your thoughts

Quoted 60 lines · On Mon, Feb 10, 2025 at 8:45?PM Anup Patel <[email protected]> wrote:

Introducing a kconfig option is not going to fly since we have to re-compile separately for platforms while we are trying our best to support the same firmware binary for multiple platforms.

Instead, I suggest the following:
1) The default scratch allocation alignment can be __SIZEOF_POINTER__
2) Introduce get_cache_line_size() operation in "struct sbi_platform_operations" which platforms can use to return desired "cache line size for allocations".
3) For generic platform, the get_cache_line_size() return value can be based on the "riscv,cbom-block-size" property of CPU DT nodes OR based on platform_overrride.
4) The sbi_scratch_init() should sanity check the value returned by platform get_cache_line_size() before using it as minimum allocation size for scratch allocations.

Regards,
Anup

Quoted 41 lines

endmenu

lib/sbi/sbi_scratch.c +12 -3
index ccbbc68..0f1be58 100644
@@ -14,6 +14,16 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+/*
+ * We do the scratch allocation on the basis of a user configuration.
+ */
+#if !defined(CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT) || (CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT==0)
+#define SCRATCH_ALLOC_ALIGNMENT __SIZEOF_POINTER__
+#else
+#define SCRATCH_ALLOC_ALIGNMENT CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT
+#endif
+
+
 u32 last_hartindex_having_scratch = 0;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -70,8 +80,8 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        if (!size)
                return 0;
 
-       size += __SIZEOF_POINTER__ - 1;
-       size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+       size += SCRATCH_ALLOC_ALIGNMENT- 1;
+       size &= ~((unsigned long)SCRATCH_ALLOC_ALIGNMENT - 1);
 
        spin_lock(&extra_lock);
 
--

2.43.0

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

#15 lore raw

PATCH v3 1/1 New configuration CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT

Hi Raj,

Quoted 8 lines · Raj Vishwanathan

Hi Anup

I have been reviewing your suggestions, and I am a bit confused by them.

---------------------- Snipped to retain only the relevant bits
--------------------------
The scratch allocation happens too early in the boot to get the
platform override return values (from parsing the device tree.)

Overrides for the generic platform happen in fw_platform_init(), which is called before scratch space is set up. This function also already has logic to parse properties of harts from the devicetree, so it is a good place to put your new override logic.

Quoted 3 lines

The other issue is that riscv,cbom-block-size is a per-hart value and testing all kinds of combinations may become unnecessarily complicated.

If the goal is to have each allocation in a separate cache line, then you always want the maximum value across all harts, right?

Quoted 2 lines

If the general configuration value is not acceptable, I can move the configuration to a platform specific configuration.

As Anup said, we want to avoid build-time configuration as much as possible, so a single OpenSBI binary can run on the widest variety of platforms. So a runtime override is the best way to go, even if it requires some refactoring.

Regards,
Samuel

Quoted 66 lines

Let me know your thoughts

Quoted 60 lines · On Mon, Feb 10, 2025 at 8:45?PM Anup Patel <[email protected]> wrote:

Introducing a kconfig option is not going to fly since we have to re-compile separately for platforms while we are trying our best to support the same firmware binary for multiple platforms.

Instead, I suggest the following:
1) The default scratch allocation alignment can be __SIZEOF_POINTER__
2) Introduce get_cache_line_size() operation in "struct sbi_platform_operations" which platforms can use to return desired "cache line size for allocations".
3) For generic platform, the get_cache_line_size() return value can be based on the "riscv,cbom-block-size" property of CPU DT nodes OR based on platform_overrride.
4) The sbi_scratch_init() should sanity check the value returned by platform get_cache_line_size() before using it as minimum allocation size for scratch allocations.

Regards,
Anup

Quoted 41 lines

endmenu

lib/sbi/sbi_scratch.c +12 -3
index ccbbc68..0f1be58 100644
@@ -14,6 +14,16 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+/*
+ * We do the scratch allocation on the basis of a user configuration.
+ */
+#if !defined(CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT) || (CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT==0)
+#define SCRATCH_ALLOC_ALIGNMENT __SIZEOF_POINTER__
+#else
+#define SCRATCH_ALLOC_ALIGNMENT CONFIG_SBI_SCRATCH_ALLOC_ALIGNMENT
+#endif
+
+
 u32 last_hartindex_having_scratch = 0;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -70,8 +80,8 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        if (!size)
                return 0;
 
-       size += __SIZEOF_POINTER__ - 1;
-       size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+       size += SCRATCH_ALLOC_ALIGNMENT- 1;
+       size &= ~((unsigned long)SCRATCH_ALLOC_ALIGNMENT - 1);
 
        spin_lock(&extra_lock);
 
--

2.43.0

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

#16 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

In reply to Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 34 ++++++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 +++++++++++++++++++++
 platform/generic/platform.c        |  9 ++++++++
 5 files changed, 68 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
 	 * 2. HART id < SBI_HARTMASK_MAX_BITS
 	 */
 	const u32 *hart_index2id;
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +32 -2
index ccbbc68..8df54cc 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 last_hartindex_having_scratch = 0;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -21,6 +23,27 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+	struct sbi_scratch *rscratch;
+	const struct sbi_platform *plat;
+	u32 hartid;
+	u32 hart_index;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	hartid = current_hartid();
+	hart_index = sbi_hartid_to_hartindex(hartid);
+	rscratch = sbi_hartindex_to_scratch(hart_index);
+	if (!rscratch)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	plat = sbi_platform_ptr(rscratch);
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+									DEFAULT_SCRATCH_ALLOC_ALIGN;
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
 	u32 i;
@@ -57,6 +80,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	void *ptr;
 	unsigned long ret = 0;
 	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;
 
 	/*
 	 * We have a simple brain-dead allocator which never expects
@@ -70,8 +94,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+     * We let the allocation align to cacheline bytes to avoid livelock on
+     * certain platforms due to atomic variables from the same cache line.
+     */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
 	spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
 	return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +9 -0
index c03ed88..53abf0b 100644
@@ -174,6 +174,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 	const void *fdt = (void *)arg1;
 	u32 hartid, hart_count = 0;
 	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = __SIZEOF_POINTER__;
+	u32 tmp;
+
 
 	root_offset = fdt_path_offset(fdt, "/");
 	if (root_offset < 0)
@@ -207,11 +210,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 			continue;
 
 		generic_hart_index2id[hart_count++] = hartid;
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		tmp = tmp ? tmp : __SIZEOF_POINTER__;
+		cbom_block_size = MAX(tmp,cbom_block_size);
 	}
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+	platform.cbom_block_size = cbom_block_size;
 
 	fw_platform_coldboot_harts_init(fdt);
 
Signature · 1 line
2.43.0
#17 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

? 2025-03-09?? 07:44 -0700?Raj Vishwanathan???

Quoted 69 lines

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
??? Remove platform specific references to 64 Bytes. Changes in V2:
??? Added a new configuration to get the alignment size. Change in V1:
??? Original Patch
---

?include/sbi/sbi_platform.h???????? |? 2 ++
?include/sbi_utils/fdt/fdt_helper.h |? 1 +
?lib/sbi/sbi_scratch.c????????????? | 34 ++++++++++++++++++++++++++++--
?lib/utils/fdt/fdt_helper.c???????? | 24 +++++++++++++++++++++
?platform/generic/platform.c??????? |? 9 ++++++++

?5 files changed, 68 insertions(+), 2 deletions(-)

include/sbi/sbi_platform.h +0 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {

? * 2. HART id < SBI_HARTMASK_MAX_BITS
? */
? const u32 *hart_index2id;

include/sbi/sbi_platform.h +2 -0
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;

?};
?
?/**

include/sbi_utils/fdt/fdt_helper.h +0 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);

?
?int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
?
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size); ?int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
?
?int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);

lib/sbi/sbi_scratch.c +0 -0
index ccbbc68..8df54cc 100644
@@ -14,6 +14,8 @@

?#include <sbi/sbi_scratch.h>
?#include <sbi/sbi_string.h>
?

lib/sbi/sbi_scratch.c +2 -0
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+

?u32 last_hartindex_having_scratch = 0;
?u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U }; ?struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };

lib/sbi/sbi_scratch.c +0 -0
@@ -21,6 +23,27 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0
 ?static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 ?static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;

?

lib/sbi/sbi_scratch.c +6 -0
+static u32 sbi_get_scratch_alloc_align(void)
+{
+	struct sbi_scratch *rscratch;
+	const struct sbi_platform *plat;
+	u32 hartid;
+	u32 hart_index;

lib/sbi/sbi_scratch.c struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 · line 41

Quoted 10 lines
lib/sbi/sbi_scratch.c +10 -0
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	hartid = current_hartid();
+	hart_index = sbi_hartid_to_hartindex(hartid);
+	rscratch = sbi_hartindex_to_scratch(hart_index);
+	if (!rscratch)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	plat = sbi_platform_ptr(rscratch);

plat can be directly obtained through sbi_platform_thishart_ptr

Regards,
Xiang W

Quoted 103 lines
lib/sbi/sbi_scratch.c +5 -0
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+									DEFAULT_SCRATCH_ALLOC_ALIGN;
+}

?u32 sbi_hartid_to_hartindex(u32 hartid)
?{
? u32 i;

lib/sbi/sbi_scratch.c +1 -0
@@ -57,6 +80,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	void *ptr;
 ?	unsigned long ret = 0;
 ?	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;

?
? /*
? * We have a simple brain-dead allocator which never expects

lib/sbi/sbi_scratch.c +0 -0
@@ -70,8 +94,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	if (!size)
 ?		return 0;

?

lib/sbi/sbi_scratch.c +8 -2
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+???? * We let the allocation align to cacheline bytes to avoid livelock on
+???? * certain platforms due to atomic variables from the same cache line.
+???? */
+??? size += scratch_alloc_align - 1;
+??? size &= ~((unsigned long)scratch_alloc_align - 1);

?
? spin_lock(&extra_lock);
?

lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)

?
? return 0;
?}

lib/utils/fdt/fdt_helper.c +24 -0
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+??? int len;
+??? const void *prop;
+??? const fdt32_t *val;
+
+??? if (!fdt || cpu_offset < 0)
+??????? return SBI_EINVAL;
+
+??? prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+??? if (!prop || !len)
+??????? return SBI_EINVAL;
+??? if (strncmp (prop, "cpu", strlen ("cpu")))
+??????? return SBI_EINVAL;
+
+??? val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+??? if (!val || len < sizeof(fdt32_t))
+??????? return SBI_EINVAL;
+
+??? if (cbom_block_size)
+??????? *cbom_block_size = fdt32_to_cpu(*val);
+??? return 0;
+
+}

?
?int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid) ?{

platform/generic/platform.c +3 -0
index c03ed88..53abf0b 100644
@@ -174,6 +174,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?	const void *fdt = (void *)arg1;
 ?	u32 hartid, hart_count = 0;
 ?	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = __SIZEOF_POINTER__;
+	u32 tmp;
+

?
? root_offset = fdt_path_offset(fdt, "/");
? if (root_offset < 0)

platform/generic/platform.c +0 -0
@@ -207,11 +210,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?			continue;

?
? generic_hart_index2id[hart_count++] = hartid;

platform/generic/platform.c +5 -0
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		tmp = tmp ? tmp : __SIZEOF_POINTER__;
+		cbom_block_size = MAX(tmp,cbom_block_size);

? }
?
? platform.hart_count = hart_count;
? platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
? platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);

platform/generic/platform.c +1 -0
+	platform.cbom_block_size = cbom_block_size;

?
? fw_platform_coldboot_harts_init(fdt);
?

Signature · 1 line
2.43.0
#18 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

Hi Xiang, Anup and Samuel

Please let me know if you think any other changes need to be done. Otherwise I will make the changes suggested by Xiang and send an updated patch.

Many thanks for all your reviews and suggestions.

Raj

Quoted 189 lines · On Sun, Mar 9, 2025 at 10:27?PM Xiang W <[email protected]> wrote:

? 2025-03-09?? 07:44 -0700?Raj Vishwanathan???

Quoted 69 lines

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 34 ++++++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 +++++++++++++++++++++
 platform/generic/platform.c        |  9 ++++++++
 5 files changed, 68 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
       * 2. HART id < SBI_HARTMASK_MAX_BITS
       */
      const u32 *hart_index2id;
+     /** Allocation alignment for Scratch */
+     u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +8 -0
index ccbbc68..8df54cc 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 last_hartindex_having_scratch = 0;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -21,6 +23,27 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+     struct sbi_scratch *rscratch;
+     const struct sbi_platform *plat;
+     u32 hartid;
+     u32 hart_index;

lib/sbi/sbi_scratch.c struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 · line 41

Quoted 10 lines
lib/sbi/sbi_scratch.c +10 -0
+     /*
+      * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+      * or riscv,cbom_block_size
+      */
+     hartid = current_hartid();
+     hart_index = sbi_hartid_to_hartindex(hartid);
+     rscratch = sbi_hartindex_to_scratch(hart_index);
+     if (!rscratch)
+             return DEFAULT_SCRATCH_ALLOC_ALIGN;
+     plat = sbi_platform_ptr(rscratch);

plat can be directly obtained through sbi_platform_thishart_ptr

Regards,
Xiang W

Quoted 103 lines
lib/sbi/sbi_scratch.c +14 -2
+     if (!plat)
+             return DEFAULT_SCRATCH_ALLOC_ALIGN;
+     return plat->cbom_block_size ? plat->cbom_block_size : \
+                                                                     DEFAULT_SCRATCH_ALLOC_ALIGN;
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
      u32 i;
@@ -57,6 +80,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      void *ptr;
      unsigned long ret = 0;
      struct sbi_scratch *rscratch;
+     u32 scratch_alloc_align = 0;
 
      /*
       * We have a simple brain-dead allocator which never expects
@@ -70,8 +94,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      if (!size)
              return 0;
 
-     size += __SIZEOF_POINTER__ - 1;
-     size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+     scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+     /*
+     * We let the allocation align to cacheline bytes to avoid livelock on
+     * certain platforms due to atomic variables from the same cache line.
+     */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
      spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
      return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +9 -1
index c03ed88..53abf0b 100644
@@ -174,6 +174,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
      const void *fdt = (void *)arg1;
      u32 hartid, hart_count = 0;
      int rc, root_offset, cpus_offset, cpu_offset, len;
+     u32 cbom_block_size = __SIZEOF_POINTER__;
+     u32 tmp;
+
 
      root_offset = fdt_path_offset(fdt, "/");
      if (root_offset < 0)
@@ -207,11 +210,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                      continue;
 
              generic_hart_index2id[hart_count++] = hartid;
+             rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+             if (rc)
+                     continue;
+             tmp = tmp ? tmp : __SIZEOF_POINTER__;
+             cbom_block_size = MAX(tmp,cbom_block_size);
      }
 
      platform.hart_count = hart_count;
      platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
      platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+     platform.cbom_block_size = cbom_block_size;
 
      fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

#19 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

In reply to Raj Vishwanathan

? 2025-03-09?? 07:44 -0700?Raj Vishwanathan???

Quoted 73 lines

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
??? Remove platform specific references to 64 Bytes. Changes in V2:
??? Added a new configuration to get the alignment size. Change in V1:
??? Original Patch
---

?include/sbi/sbi_platform.h???????? |? 2 ++
?include/sbi_utils/fdt/fdt_helper.h |? 1 +
?lib/sbi/sbi_scratch.c????????????? | 34 ++++++++++++++++++++++++++++--
?lib/utils/fdt/fdt_helper.c???????? | 24 +++++++++++++++++++++
?platform/generic/platform.c??????? |? 9 ++++++++

?5 files changed, 68 insertions(+), 2 deletions(-)

include/sbi/sbi_platform.h +0 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {

? * 2. HART id < SBI_HARTMASK_MAX_BITS
? */
? const u32 *hart_index2id;

include/sbi/sbi_platform.h +2 -0
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;

?};
?
?/**

include/sbi_utils/fdt/fdt_helper.h +0 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);

?
?int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
?
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size); ?int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
?
?int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);

lib/sbi/sbi_scratch.c +0 -0
index ccbbc68..8df54cc 100644
@@ -14,6 +14,8 @@

?#include <sbi/sbi_scratch.h>
?#include <sbi/sbi_string.h>
?

lib/sbi/sbi_scratch.c +2 -0
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+

?u32 last_hartindex_having_scratch = 0;
?u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U }; ?struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };

lib/sbi/sbi_scratch.c +0 -0
@@ -21,6 +23,27 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0
 ?static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 ?static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;

?

lib/sbi/sbi_scratch.c +10 -0
+static u32 sbi_get_scratch_alloc_align(void)
+{
+	struct sbi_scratch *rscratch;
+	const struct sbi_platform *plat;
+	u32 hartid;
+	u32 hart_index;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */

lib/sbi/sbi_scratch.c struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 · line 45

Quoted 10 lines
lib/sbi/sbi_scratch.c +10 -0
+	hartid = current_hartid();
+	hart_index = sbi_hartid_to_hartindex(hartid);
+	rscratch = sbi_hartindex_to_scratch(hart_index);
+	if (!rscratch)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	plat = sbi_platform_ptr(rscratch);
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+									DEFAULT_SCRATCH_ALLOC_ALIGN;

The minimum value of plat->cbom_block_size resolved in fw_platform_init? is DEFAULT_SCRATCH_ALLOC_ALIGN, so plat->cbom_block_size?can be returned? directly.

Quoted 69 lines

+}
?u32 sbi_hartid_to_hartindex(u32 hartid)
?{
? u32 i;

lib/sbi/sbi_scratch.c +1 -0
@@ -57,6 +80,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	void *ptr;
 ?	unsigned long ret = 0;
 ?	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;

?
? /*
? * We have a simple brain-dead allocator which never expects

lib/sbi/sbi_scratch.c +0 -0
@@ -70,8 +94,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	if (!size)
 ?		return 0;

?

lib/sbi/sbi_scratch.c +8 -2
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+???? * We let the allocation align to cacheline bytes to avoid livelock on
+???? * certain platforms due to atomic variables from the same cache line.
+???? */
+??? size += scratch_alloc_align - 1;
+??? size &= ~((unsigned long)scratch_alloc_align - 1);

?
? spin_lock(&extra_lock);
?

lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)

?
? return 0;
?}

lib/utils/fdt/fdt_helper.c +24 -0
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+??? int len;
+??? const void *prop;
+??? const fdt32_t *val;
+
+??? if (!fdt || cpu_offset < 0)
+??????? return SBI_EINVAL;
+
+??? prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+??? if (!prop || !len)
+??????? return SBI_EINVAL;
+??? if (strncmp (prop, "cpu", strlen ("cpu")))
+??????? return SBI_EINVAL;
+
+??? val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+??? if (!val || len < sizeof(fdt32_t))
+??????? return SBI_EINVAL;
+
+??? if (cbom_block_size)
+??????? *cbom_block_size = fdt32_to_cpu(*val);
+??? return 0;
+
+}

?
?int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid) ?{

platform/generic/platform.c +0 -0
index c03ed88..53abf0b 100644

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 177

Quoted 4 lines
platform/generic/platform.c +1 -0
@@ -174,6 +174,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?	const void *fdt = (void *)arg1;
 ?	u32 hartid, hart_count = 0;
 ?	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = __SIZEOF_POINTER__;

replace __SIZEOF_POINTER__ with DEFAULT_SCRATCH_ALLOC_ALIGN

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 216

Quoted 13 lines
platform/generic/platform.c +2 -0
+	u32 tmp;
+

?
? root_offset = fdt_path_offset(fdt, "/");
? if (root_offset < 0)

platform/generic/platform.c +0 -0
@@ -207,11 +210,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?			continue;

?
? generic_hart_index2id[hart_count++] = hartid;

platform/generic/platform.c +4 -0
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		tmp = tmp ? tmp : __SIZEOF_POINTER__;

delete before line

Quoted 7 lines
platform/generic/platform.c +1 -0
+		cbom_block_size = MAX(tmp,cbom_block_size);

? }
?
? platform.hart_count = hart_count;
? platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
? platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);

platform/generic/platform.c +1 -0
+	platform.cbom_block_size = cbom_block_size;
Quoted 6 lines

?
? fw_platform_coldboot_harts_init(fdt);
?

Signature · 1 line
2.43.0

Regards,
Xiang W

#20 lore raw

PATCH v3 1/1 Check that hartid is within the SBI_HARTMASK_MAX_BITS

In reply to Raj Vishwanathan

It is possible that hartid may not be sequential and it should not be validated
against SBI_HARTMASK_MAX_BITS. Instead we should check the index of the hartid, hart index, against SBI_HARTMASK_MAX_BITS.

--
Changes in V2:
Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure.

Changes in V1:
Ignore if hart_count is more than SBI_HARTMASK_MAX_BITS.

---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 		if (err)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
 			continue;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +2 -2
index c03ed88..027ec89 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
+			break;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
 			continue;
Signature · 1 line
2.43.0
#21 lore raw

PATCH v3 1/1 Check that hartid is within the SBI_HARTMASK_MAX_BITS

? 2025-01-28?? 13:33 -0800?Raj Vishwanathan???

Quoted 4 lines

It is possible that hartid may not be sequential and it should not be validated
against SBI_HARTMASK_MAX_BITS. Instead we should check the index of the hartid, hart index, against SBI_HARTMASK_MAX_BITS.

LGTM

Quoted 75 lines

--
Changes in V2:
??? Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure.

Changes in V1:
??? Ignore if hart_count is more than SBI_HARTMASK_MAX_BITS.
---

?lib/utils/fdt/fdt_domain.c? | 2 +-
?lib/utils/fdt/fdt_helper.c? | 6 +++---
?platform/generic/platform.c | 4 ++--

?3 files changed, 6 insertions(+), 6 deletions(-)

lib/utils/fdt/fdt_domain.c +0 -0
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 ?		if (err)
 ?			continue;

?

lib/utils/fdt/fdt_domain.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))

? continue;
?
? if (!fdt_node_is_enabled(fdt, cpu_offset))

lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (match_hwirq == hwirq) {

lib/utils/fdt/fdt_helper.c +0 -0
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (hwirq == IRQ_M_TIMER)

lib/utils/fdt/fdt_helper.c +0 -0
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 ?		if (rc)
 ?			continue;

?

lib/utils/fdt/fdt_helper.c +1 -1
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))

? continue;
?
? if (hwirq == IRQ_M_SOFT)

platform/generic/platform.c +0 -0
index c03ed88..027ec89 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?		if (rc)
 ?			continue;

?

platform/generic/platform.c +2 -2
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
+			break;

?
? if (!fdt_node_is_enabled(fdt, cpu_offset))
? continue;

Signature · 1 line
2.43.0
#22 lore raw

PATCH v3 1/1 lib: utils:Check that hartid is valid

In reply to Raj Vishwanathan

It is possible that hartid may not be sequential and it should not be validated
against SBI_HARTMASK_MAX_BITS. Instead we should check the index of the hartid, hart index, against SBI_HARTMASK_MAX_BITS.

---
Changes in V2:
Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure.

Changes in V1:
Ignore if hart_count is more than SBI_HARTMASK_MAX_BITS.

---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
 		if (err)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= val32)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
 			continue;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
+		if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
 			continue;
 
 		if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +2 -2
index c03ed88..027ec89 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 		if (rc)
 			continue;
 
-		if (SBI_HARTMASK_MAX_BITS <= hartid)
-			continue;
+		if (SBI_HARTMASK_MAX_BITS <= hart_count)
+			break;
 
 		if (!fdt_node_is_enabled(fdt, cpu_offset))
 			continue;
Signature · 1 line
2.43.0
Applied
#23 lore raw

PATCH v3 1/1 lib: utils:Check that hartid is valid

Quoted 3 lines · On Wed, Feb 12, 2025 at 3:30?AM Raj Vishwanathan <[email protected]>…

It is possible that hartid may not be sequential and it should not be validated
against SBI_HARTMASK_MAX_BITS. Instead we should check the index of the hartid, hart index, against SBI_HARTMASK_MAX_BITS.

Minor nit: Try to wrap the line around 75 characters

Quoted 2 lines

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 79 lines

---
Changes in V2:
Update: Make hart_count > SBI_HARTMASK_MAX_BITS a catastrophic failure.

Changes in V1:
Ignore if hart_count is more than SBI_HARTMASK_MAX_BITS.

---
 lib/utils/fdt/fdt_domain.c  | 2 +-
 lib/utils/fdt/fdt_helper.c  | 6 +++---
 platform/generic/platform.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_domain.c +1 -1
index 4bc7ed8..7d10d10 100644
@@ -471,7 +471,7 @@ static int __fdt_parse_domain(const void *fdt, int domain_offset, void *opaque)
                if (err)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= val32)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(val32))
                        continue;
 
                if (!fdt_node_is_enabled(fdt, cpu_offset))
lib/utils/fdt/fdt_helper.c +3 -3
index cb350e5..4673921 100644
@@ -1032,7 +1032,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
                        continue;
 
                if (match_hwirq == hwirq) {
@@ -1097,7 +1097,7 @@ int fdt_parse_plmt_node(const void *fdt, int nodeoffset, unsigned long *plmt_bas
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
                        continue;
 
                if (hwirq == IRQ_M_TIMER)
@@ -1153,7 +1153,7 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
+               if (SBI_HARTMASK_MAX_BITS <= sbi_hartid_to_hartindex(hartid))
                        continue;
 
                if (hwirq == IRQ_M_SOFT)
platform/generic/platform.c +2 -3
index c03ed88..027ec89 100644
@@ -200,8 +200,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                if (rc)
                        continue;
 
-               if (SBI_HARTMASK_MAX_BITS <= hartid)
-                       continue;
+               if (SBI_HARTMASK_MAX_BITS <= hart_count)
+                       break;
 
                if (!fdt_node_is_enabled(fdt, cpu_offset))
                        continue;
--

2.43.0

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