Skip to contentSkip to conversation

lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

PATCH New Anup Patel 4 participants 8 messages Aug 18, 2020 – Aug 22, 2020

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH v3 1/2 lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

We can have thundering hurd problem with coldboot_lock where the boot HART can potentially starve trying to acquire coldboot_lock because some of the non-boot HARTs are continuously acquiring and releasing coldboot_lock. This can happen if MIP.MSIP bit is already set for some of the non-boot HARTs.

To avoid thundering hurd problem for coldboot_lock, we use the __smp_load_acquire() and __smp_store_release() for coldboot_done flag and use coldboot_lock only for coldboot_wait_hmask.

---
 lib/sbi/sbi_init.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
lib/sbi/sbi_init.c +13 -7
index a7fb848..c438eaa 100644
@@ -9,6 +9,7 @@
 
 #include <sbi/riscv_asm.h>
 #include <sbi/riscv_atomic.h>
+#include <sbi/riscv_barrier.h>
 #include <sbi/riscv_locks.h>
 #include <sbi/sbi_console.h>
 #include <sbi/sbi_ecall.h>
@@ -85,9 +86,10 @@ static void sbi_boot_prints(struct sbi_scratch *scratch, u32 hartid)
 }
 
 static spinlock_t coldboot_lock = SPIN_LOCK_INITIALIZER;
-static unsigned long coldboot_done = 0;
 static struct sbi_hartmask coldboot_wait_hmask = { 0 };
 
+static unsigned long coldboot_done;
+
 static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
 {
 	unsigned long saved_mie, cmip;
@@ -105,16 +107,20 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
 	/* Mark current HART as waiting */
 	sbi_hartmask_set_hart(hartid, &coldboot_wait_hmask);
 
+	/* Release coldboot lock */
+	spin_unlock(&coldboot_lock);
+
 	/* Wait for coldboot to finish using WFI */
-	while (!coldboot_done) {
-		spin_unlock(&coldboot_lock);
+	while (!__smp_load_acquire(&coldboot_done)) {
 		do {
 			wfi();
 			cmip = csr_read(CSR_MIP);
 		 } while (!(cmip & MIP_MSIP));
-		spin_lock(&coldboot_lock);
 	};
 
+	/* Acquire coldboot lock */
+	spin_lock(&coldboot_lock);
+
 	/* Unmark current HART as waiting */
 	sbi_hartmask_clear_hart(hartid, &coldboot_wait_hmask);
 
@@ -132,12 +138,12 @@ static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
 {
 	const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 
+	/* Mark coldboot done */
+	__smp_store_release(&coldboot_done, 1);
+
 	/* Acquire coldboot lock */
 	spin_lock(&coldboot_lock);
 
-	/* Mark coldboot done */
-	coldboot_done = 1;
-
 	/* Send an IPI to all HARTs waiting for coldboot */
 	for (int i = 0; i <= sbi_scratch_last_hartid(); i++) {
 		if ((i != hartid) &&
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 2/2 lib: sbi: Handle the case where MTVAL has illegal instruction address

The Kendryte K210 follows RISC-V v1.9 spec so MTVAL has instruction address (instead of instruction encoding) on illegal instruction trap.

To handle above case, we fix sbi_illegal_insn_handler() without any impact on RISC-V v1.10 (or higher) systems. This achieved by exploiting the fact that program counter (and instruction address) is always 2-byte aligned in RISC-V world.

---
 lib/sbi/sbi_illegal_insn.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
lib/sbi/sbi_illegal_insn.c +15 -6
index 0e5523f..9af3d24 100644
@@ -118,13 +118,22 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 {
 	struct sbi_trap_info uptrap;
 
+	/*
+	 * We only deal with 32-bit (or longer) illegal instructions. If we
+	 * see instruction is zero OR instruction is 16-bit then we fetch and
+	 * check the instruction encoding using unprivilege access.
+	 *
+	 * The program counter (PC) in RISC-V world is always 2-byte aligned
+	 * so handling only 32-bit (or longer) illegal instructions also help
+	 * the case where MTVAL CSR contains instruction address for illegal
+	 * instruction trap.
+	 */
+
 	if (unlikely((insn & 3) != 3)) {
-		if (insn == 0) {
-			insn = sbi_get_insn(regs->mepc, &uptrap);
-			if (uptrap.cause) {
-				uptrap.epc = regs->mepc;
-				return sbi_trap_redirect(regs, &uptrap);
-			}
+		insn = sbi_get_insn(regs->mepc, &uptrap);
+		if (uptrap.cause) {
+			uptrap.epc = regs->mepc;
+			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
 			return truly_illegal_insn(insn, regs);
Signature · 1 line
2.25.1
author Applied
#2 lore raw

PATCH v3 2/2 lib: sbi: Handle the case where MTVAL has illegal instruction address

Quoted 63 lines

-----Original Message-----
From: Anup Patel <[email protected]>
Sent: 18 August 2020 09:37
To: Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>; Anup Patel <[email protected]>; opensbi at lists.infradead.org; Anup Patel <[email protected]>; Bin Meng <[email protected]>
Subject: [PATCH v3 2/2] lib: sbi: Handle the case where MTVAL has illegal instruction address

The Kendryte K210 follows RISC-V v1.9 spec so MTVAL has instruction address (instead of instruction encoding) on illegal instruction trap.

To handle above case, we fix sbi_illegal_insn_handler() without any impact on RISC-V v1.10 (or higher) systems. This achieved by exploiting the fact that program counter (and instruction address) is always 2-byte aligned in RISC-V world.

---
 lib/sbi/sbi_illegal_insn.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
lib/sbi/sbi_illegal_insn.c index renamed from lib/sbi/sbi_illegal_insn.c +0 -0

0e5523f..9af3d24 100644

lib/sbi/sbi_illegal_insn.c +15 -7
@@ -118,13 +118,22 @@ int sbi_illegal_insn_handler(ulong insn, struct
 sbi_trap_regs *regs)  {
 	struct sbi_trap_info uptrap;
 
+	/*
+	 * We only deal with 32-bit (or longer) illegal instructions. If we
+	 * see instruction is zero OR instruction is 16-bit then we fetch and
+	 * check the instruction encoding using unprivilege access.
+	 *
+	 * The program counter (PC) in RISC-V world is always 2-byte aligned
+	 * so handling only 32-bit (or longer) illegal instructions also help
+	 * the case where MTVAL CSR contains instruction address for illegal
+	 * instruction trap.
+	 */
+
 	if (unlikely((insn & 3) != 3)) {
-		if (insn == 0) {
-			insn = sbi_get_insn(regs->mepc, &uptrap);
-			if (uptrap.cause) {
-				uptrap.epc = regs->mepc;
-				return sbi_trap_redirect(regs, &uptrap);
-			}
+		insn = sbi_get_insn(regs->mepc, &uptrap);
+		if (uptrap.cause) {
+			uptrap.epc = regs->mepc;
+			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
 			return truly_illegal_insn(insn, regs);
--
Quoted 1 line

2.25.1

Applied this patch to the riscv/opensbi repo

Regards,
Anup

#3 lore raw

PATCH v3 1/2 lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

In reply to Anup Patel
Quoted 11 lines · Anup Patel

We can have thundering hurd problem with coldboot_lock where the boot HART can potentially starve trying to acquire coldboot_lock because some of the non-boot HARTs are continuously acquiring and releasing coldboot_lock. This can happen if MIP.MSIP bit is already set for some of the non-boot HARTs.

To avoid thundering hurd problem for coldboot_lock, we use the __smp_load_acquire() and __smp_store_release() for coldboot_done flag and use coldboot_lock only for coldboot_wait_hmask.

Quoted 7 lines
---
 lib/sbi/sbi_init.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
author Applied
#4 lore raw

PATCH v3 1/2 lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

Quoted 32 lines

-----Original Message-----
From: Bin Meng <[email protected]>
Sent: 18 August 2020 14:46
To: Anup Patel <[email protected]>
Cc: Atish Patra <[email protected]>; Alistair Francis <[email protected]>; Heinrich Schuchardt <[email protected]>; OpenSBI <[email protected]>; Anup Patel <[email protected]>
Subject: Re: [PATCH v3 1/2] lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

Quoted 11 lines · Anup Patel

We can have thundering hurd problem with coldboot_lock where the boot HART can potentially starve trying to acquire coldboot_lock because some of the non-boot HARTs are continuously acquiring and releasing coldboot_lock. This can happen if MIP.MSIP bit is already set for some of the non-boot HARTs.

To avoid thundering hurd problem for coldboot_lock, we use the __smp_load_acquire() and __smp_store_release() for coldboot_done flag and use coldboot_lock only for coldboot_wait_hmask.

Quoted 7 lines
---
 lib/sbi/sbi_init.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
Quoted 1 line

Applied this patch to the riscv/opensbi repo

Regards,
Anup

#5 lore raw

PATCH v3 1/2 lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

In reply to Anup Patel
Quoted 6 lines · Anup Patel

We can have thundering hurd problem with coldboot_lock where the boot HART can potentially starve trying to acquire coldboot_lock because some of the non-boot HARTs are continuously acquiring and releasing coldboot_lock. This can happen if MIP.MSIP bit is already set for some of the non-boot HARTs.

Quoted 7 lines

To avoid thundering hurd problem for coldboot_lock, we use the __smp_load_acquire() and __smp_store_release() for coldboot_done flag and use coldboot_lock only for coldboot_wait_hmask.

Just curious, what are the changes from v1 and v2?

--Sean

#6 lore raw

PATCH v3 1/2 lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

Quoted 11 lines · Sean Anderson
Quoted 10 lines · Anup Patel

We can have thundering hurd problem with coldboot_lock where the boot HART can potentially starve trying to acquire coldboot_lock because some of the non-boot HARTs are continuously acquiring and releasing coldboot_lock. This can happen if MIP.MSIP bit is already set for some of the non-boot HARTs.

To avoid thundering hurd problem for coldboot_lock, we use the __smp_load_acquire() and __smp_store_release() for coldboot_done flag and use coldboot_lock only for coldboot_wait_hmask.

Quoted 5 lines
Quoted 3 lines

Just curious, what are the changes from v1 and v2?

Barriers are realized in a different way in patch 1.

Except for the barriers patch 1 follows what I proposed in my original patch and the following discussion.

And for sure Jessica was right that at least compiler barriers are needed.

Best regards

Heinrich

author
#7 lore raw

PATCH v3 1/2 lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

Quoted 30 lines

-----Original Message-----
From: Heinrich Schuchardt <[email protected]>
Sent: 19 August 2020 17:39
To: Sean Anderson <[email protected]>; Anup Patel <[email protected]>; Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: opensbi at lists.infradead.org; Anup Patel <[email protected]>
Subject: Re: [PATCH v3 1/2] lib: sbi_init: Avoid thundering hurd problem with coldboot_lock

Quoted 2 lines · Sean Anderson
Quoted 1 line · Anup Patel

We can have thundering hurd problem with coldboot_lock where the

boot

Quoted 7 lines
Quoted 7 lines

HART can potentially starve trying to acquire coldboot_lock because some of the non-boot HARTs are continuously acquiring and releasing coldboot_lock. This can happen if MIP.MSIP bit is already set for some of the non-boot HARTs.

To avoid thundering hurd problem for coldboot_lock, we use the __smp_load_acquire() and __smp_store_release() for coldboot_done

flag

Quoted 6 lines
Quoted 5 lines

and use coldboot_lock only for coldboot_wait_hmask.

Quoted 8 lines
Quoted 1 line

Just curious, what are the changes from v1 and v2?

Barriers are realized in a different way in patch 1.

Except for the barriers patch 1 follows what I proposed in my original patch and the following discussion.

And for sure Jessica was right that at least compiler barriers are needed.

Yes, v1->v2 is replacing use of atomic_read/write() with__smp_load/store_xyz().

v2->v3 is only a typo-fix in PATCH1 subject.

Regards,
Anup