lib: sbi: Prevent system hang when bringing up secondary harts
cover lib: sbi: Prevent system hang when bringing up secondary harts
The boot hart can race with the secondary harts when it tries to bring them up at startup time. This results in occasional system hangs at boot, which can be reproduced in QEMU with a Linux guest, even with '-machine virt'.
When the Linux kernel needs to bring up the non-boot harts, the boot hart enters opensbi via a ecall and eventually gets to sbi_hsm_hart_start(). Other (secondary) harts are spinning in the while loop in sbi_hsm_hart_wait() at that moment:
/* Wait for state transition requested by sbi_hsm_hart_start() */
while (atomic_read(&hdata->state) != SBI_HSM_STATE_START_PENDING) {
wfi();
};If I understand it correctly, the idea is that sbi_hsm_hart_start() should first set the secondary hart's state to SBI_HSM_STATE_START_PENDING atomically and check the previous value of the state. This makes sure no other hart would try to modify the secondary hart's scratch area in the meantime.
The boot hart then reads the init count of the secondary hart and sets the address it should eventually jump to, as well as other data:
init_count = sbi_init_count(hartid); rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode;
Then, the boot hart sends IPI to the secondary hart, so that it would stop spinning in the loop above and would continue.
However, as we observed during the test runs for our systems, the secondary harts could leave the loop in sbi_hsm_hart_wait() as soon as their state is set to SBI_HSM_STATE_START_PENDING. As a result, the following situation is possible:
Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits in sbi_hsm_hart_wait()
sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START,
...)enters sbi_hsm_hart_start()
sets state to START_PENDING
leaves sbi_hsm_hart_wait() runs to the end of init_warmboot() returns to scratch->next_addr (next_addr can be garbage here)
sets next_addr, etc. for hart #1
(no good: hart #1 has already left)
sends IPI to hart #1
(no good either)
(system might hang or crash)
This can be reproduced easier, if one inserts a delay into sbi_hsm_hart_start() right before "init_count = sbi_init_count(hartid);". In our case, "sbi_timer_mdelay(10)" was enough for the issue to manifest each time.
The patch series fixes 2 issues that could make the secondary harts exit the while loop in sbi_hsm_hart_wait() prematurely, i.e. before the IPI:
1. "wfi" instruction is just a hint for the CPU, it might or might not actually wait for an interrupt to happen in the hart. RISC-V Privileged Architectures spec. v20211203 even states that "a legal implementation is to simply implement WFI as a NOP".
2. Since commit 50d4fde1c5a4 ("lib: Remove redundant sbi_platform_ipi_clear() calls"), the IPI sent from the boot hart in wake_coldboot_harts() is never cleared in the secondary harts. So, they enter sbi_hsm_hart_wait() with already pending IPIs and do not wait for new ones.
In both cases, the secondary hart might leave the loop as soon as its state is set to SBI_HSM_STATE_START_PENDING, leading to the race described above.
Regards,
Evgenii
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in sbi_hsm_hart_wait() might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:
Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits in sbi_hsm_hart_wait()
sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START,
...) enters sbi_hsm_hart_start()
sets state of hart #1 to START_PENDING
leaves sbi_hsm_hart_wait() runs to the end of init_warmboot() returns to scratch->next_addr (next_addr can be garbage here)
sets next_addr, etc. for hart #1
(no good: hart #1 has already left)
sends IPI to hart #1
(no good either)
If this happens, the secondary hart jumps to a wrong next_addr at the end of init_warmboot(), which leads to a system hang or crash.
To reproduce the issue more reliably, one could add a delay in sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:
hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOPPED,
SBI_HSM_STATE_START_PENDING);
...
+ sbi_timer_mdelay(10);
init_count = sbi_init_count(hartid);
rscratch->next_arg1 = arg1;
rscratch->next_addr = saddr;The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Let us actually check in sbi_hsm_hart_wait(), if the waiting hart has received an IPI.
--- lib/sbi/sbi_hsm.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/sbi/sbi_hsm.c +6 -2
@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */
@@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie);
Signature · 1 line
2.34.1
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
Quoted 45 lines · Evgenii Shatokhin
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in
sbi_hsm_hart_wait()might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits insbi_hsm_hart_wait()sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START, ...)enters
sbi_hsm_hart_start()
sets state of hart #1 toSTART_PENDING
leavessbi_hsm_hart_wait()runs to the end ofinit_warmboot()returns toscratch->next_addr(next_addrcan be garbage here)sets
next_addr, etc. for hart #1
(no good: hart #1 has already left)sends IPI to hart #1
(no good either)If this happens, the secondary hart jumps to a wrong
next_addrat the end ofinit_warmboot(), which leads to a system hang or crash.To reproduce the issue more reliably, one could add a delay in
sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOPPED, SBI_HSM_STATE_START_PENDING); ... + sbi_timer_mdelay(10); init_count = sbi_init_count(hartid); rscratch->next_arg1 = arg1; rscratch->next_addr = saddr;
Quoted 5 lines
The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Let us actually check in
sbi_hsm_hart_wait(), if the waiting hart has received an IPI.
Using the IPI itself as the synchronisation barrier seems awfully brittle. Better to split SBI_HSM_STATE_START_PENDING into two states so there?s an additional atomic_write that the atomic_read here can synchronise with.
Jess
Quoted 41 lines
Signed-off-by Evgenii Shatokhin <[email protected]>---
lib/sbi/sbi_hsm.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +6
-2@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie);Signature · 8 lines
2.34.1 -- opensbi mailing list opensbi at lists.infradead.org http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
Quoted 50 lines · Jessica Clarke
Quoted 48 lines · Evgenii Shatokhin
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in
sbi_hsm_hart_wait()might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits insbi_hsm_hart_wait()sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START, ...)enters
sbi_hsm_hart_start()
sets state of hart #1 toSTART_PENDING
leavessbi_hsm_hart_wait()runs to the end ofinit_warmboot()returns toscratch->next_addr(next_addrcan be garbage here)sets
next_addr, etc. for hart #1
(no good: hart #1 has already left)sends IPI to hart #1
(no good either)If this happens, the secondary hart jumps to a wrong
next_addrat the end ofinit_warmboot(), which leads to a system hang or crash.To reproduce the issue more reliably, one could add a delay in
sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:hstate = atomic_cmpxchg(&hdata->state,
SBI_HSM_STATE_STOPPED,SBI_HSM_STATE_START_PENDING);...
+sbi_timer_mdelay(10);
init_count=sbi_init_count(hartid);
rscratch->next_arg1= arg1;
rscratch->next_addr= saddr;The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Quoted 7 lines
Quoted 2 lines
Let us actually check in
sbi_hsm_hart_wait(), if the waiting hart has received an IPI.Using the IPI itself as the synchronisation barrier seems awfully brittle. Better to split
SBI_HSM_STATE_START_PENDINGinto two states so there?s an additionalatomic_writethat theatomic_readhere can synchronise with.
Of course, care is needed to not expose this outside OpenSBI as these states are part of the SBI. That is, from the outside world, both states should appear as START_PENDING.
Jess
Quoted 41 lines
Quoted 41 lines
Signed-off-by Evgenii Shatokhin <[email protected]>---
lib/sbi/sbi_hsm.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +6
-2@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie);Signature · 8 lines
2.34.1 -- opensbi mailing list opensbi at lists.infradead.org http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
In reply to Jessica ClarkeHi,
Quoted 49 lines · Jessica Clarke
Quoted 48 lines · Evgenii Shatokhin
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in
sbi_hsm_hart_wait()might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits insbi_hsm_hart_wait()sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START, ...)enters
sbi_hsm_hart_start()
sets state of hart #1 toSTART_PENDING
leavessbi_hsm_hart_wait()runs to the end ofinit_warmboot()returns toscratch->next_addr(next_addrcan be garbage here)sets
next_addr, etc. for hart #1
(no good: hart #1 has already left)sends IPI to hart #1
(no good either)If this happens, the secondary hart jumps to a wrong
next_addrat the end ofinit_warmboot(), which leads to a system hang or crash.To reproduce the issue more reliably, one could add a delay in
sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOPPED, SBI_HSM_STATE_START_PENDING); ... + sbi_timer_mdelay(10); init_count = sbi_init_count(hartid); rscratch->next_arg1 = arg1; rscratch->next_addr = saddr;The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Quoted 7 lines
Quoted 2 lines
Let us actually check in
sbi_hsm_hart_wait(), if the waiting hart has received an IPI.Using the IPI itself as the synchronisation barrier seems awfully brittle. Better to split
SBI_HSM_STATE_START_PENDINGinto two states so there?s an additionalatomic_writethat theatomic_readhere can synchronise with.
Yes, using an additional state was the first idea I tried to implement to fix the issue. It turned out to be too error-prone, however:SBI_HSM_STATE_START_PENDING is used in a number of places in opensbi, and it is far from obvious how to use the new state correctly there, esp. without violation of the spec.
On the other hand, wait_for_coldboot() and wake_coldboot_harts() already use IPI and a condition variable 'coldboot_done' for synchronization, and it seems to work OK.
So, I suppose, relying on IPI in sbi_hsm_hart_wait() and sbi_hsm_hart_start() too might be a way to go. Looks like, it was the intended way:
* Set the state to START_PENDING and get exclusive access to the target hart's scratch area.
* Write next_addr, next_arg1, etc., for the target hart.
* Send IPI to the target hart to confirm that it might continue.
WFI might or might not wait for an interrupt in the target hart, so the boot hart must send an IPI to the target anyway to wake it reliably.
Or, perhaps, I am missing some particular issue with this approach?
Quoted 33 lines
Jess
Quoted 30 lines
Signed-off-by Evgenii Shatokhin <[email protected]>---
lib/sbi/sbi_hsm.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +6
-2@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);
Quoted 4 lines
Quoted 4 lines
lib/sbi/sbi_hsm.c +0
-1/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); --2.34.1
Regards,
Evgenii
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
Quoted 53 lines · Evgenii Shatokhin
Hi,
Quoted 49 lines · Jessica Clarke
Quoted 48 lines · Evgenii Shatokhin
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in
sbi_hsm_hart_wait()might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits insbi_hsm_hart_wait()sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START, ...)enters
sbi_hsm_hart_start()
sets state of hart #1 toSTART_PENDING
leavessbi_hsm_hart_wait()runs to the end ofinit_warmboot()returns toscratch->next_addr(next_addrcan be garbage here)sets
next_addr, etc. for hart #1
(no good: hart #1 has already left)sends IPI to hart #1
(no good either)If this happens, the secondary hart jumps to a wrong
next_addrat the end ofinit_warmboot(), which leads to a system hang or crash.To reproduce the issue more reliably, one could add a delay in
sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOPPED, SBI_HSM_STATE_START_PENDING); ... + sbi_timer_mdelay(10); init_count = sbi_init_count(hartid); rscratch->next_arg1 = arg1; rscratch->next_addr = saddr;The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Let us actually check in
sbi_hsm_hart_wait(), if the waiting hart has received an IPI.Using the IPI itself as the synchronisation barrier seems awfully brittle. Better to split
SBI_HSM_STATE_START_PENDINGinto two states so there?s an additionalatomic_writethat theatomic_readhere can synchronise with.Yes, using an additional state was the first idea I tried to implement to fix the issue. It turned out to be too error-prone, however:
SBI_HSM_STATE_START_PENDINGis used in a number of places in opensbi, and it is far from obvious how to use the new state correctly there, esp. without violation of the spec.
I only see 5, and they don?t look particularly hard...
Quoted 1 line
On the other hand,
wait_for_coldboot()andwake_coldboot_harts()already use IPI and a condition variable 'coldboot_done' for synchronization, and it seems to work OK.
But coldboot_done is what those synchronise on; the IPI is merely the mechanism by which the waiter gets forcefully woken and made to check coldboot_done.
Quoted 7 lines
So, I suppose, relying on IPI in
sbi_hsm_hart_wait()andsbi_hsm_hart_start()too might be a way to go. Looks like, it was the intended way:* Set the state to
START_PENDINGand get exclusive access to the target hart's scratch area.
* Writenext_addr,next_arg1, etc., for the target hart.
* Send IPI to the target hart to confirm that it might continue.WFI might or might not wait for an interrupt in the target hart, so the boot hart must send an IPI to the target anyway to wake it reliably.
But you can never truly know that the IPI you got is for the reason you think you got it. You *must* have additional state to indicate that the scratch area has been initialised; the fact you received an IPI somehow is not sufficient.
Jess
Quoted 40 lines
Or, perhaps, I am missing some particular issue with this approach?
Quoted 31 lines
Jess
Quoted 30 lines
Signed-off-by Evgenii Shatokhin <[email protected]>---
lib/sbi/sbi_hsm.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +6
-2@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);Quoted 4 lines
Quoted 4 lines
lib/sbi/sbi_hsm.c +0
-1/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); --2.34.1
Regards,
Evgenii
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
Quoted 75 lines · Jessica Clarke
?????????! ?????? ?????? ?? ???????? ????????!?
Quoted 53 lines · Evgenii Shatokhin
Hi,
Quoted 49 lines · Jessica Clarke
Quoted 48 lines · Evgenii Shatokhin
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in
sbi_hsm_hart_wait()might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits insbi_hsm_hart_wait()sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START, ...)enters
sbi_hsm_hart_start()
sets state of hart #1 toSTART_PENDING
leavessbi_hsm_hart_wait()runs to the end ofinit_warmboot()returns toscratch->next_addr(next_addrcan be garbage here)sets
next_addr, etc. for hart #1
(no good: hart #1 has already left)sends IPI to hart #1
(no good either)If this happens, the secondary hart jumps to a wrong
next_addrat the end ofinit_warmboot(), which leads to a system hang or crash.To reproduce the issue more reliably, one could add a delay in
sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOPPED, SBI_HSM_STATE_START_PENDING); ... + sbi_timer_mdelay(10); init_count = sbi_init_count(hartid); rscratch->next_arg1 = arg1; rscratch->next_addr = saddr;The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Let us actually check in
sbi_hsm_hart_wait(), if the waiting hart has received an IPI.Using the IPI itself as the synchronisation barrier seems awfully brittle. Better to split
SBI_HSM_STATE_START_PENDINGinto two states so there?s an additionalatomic_writethat theatomic_readhere can synchronise with.Yes, using an additional state was the first idea I tried to implement to fix the issue. It turned out to be too error-prone, however:
SBI_HSM_STATE_START_PENDINGis used in a number of places in opensbi, and it is far from obvious how to use the new state correctly there, esp. without violation of the spec.I only see 5, and they don?t look particularly hard...
Quoted 1 line
On the other hand,
wait_for_coldboot()andwake_coldboot_harts()already use IPI and a condition variable 'coldboot_done' for synchronization, and it seems to work OK.But
coldboot_doneis what those synchronise on; the IPI is merely the mechanism by which the waiter gets forcefully woken and made to checkcoldboot_done.Quoted 2 lines
So, I suppose, relying on IPI in
sbi_hsm_hart_wait()andsbi_hsm_hart_start()too might be a way to go. Looks like, it was the intended way:
Quoted 8 lines
Quoted 5 lines
* Set the state to
START_PENDINGand get exclusive access to the target hart's scratch area.
* Writenext_addr,next_arg1, etc., for the target hart.
* Send IPI to the target hart to confirm that it might continue.WFI might or might not wait for an interrupt in the target hart, so the boot hart must send an IPI to the target anyway to wake it reliably.
But you can never truly know that the IPI you got is for the reason you think you got it.
You are right, and my second patch is just about such problem.
Quoted 3 lines
You *must* have additional state to indicate that the scratch area has been initialised; the fact you received an IPI somehow is not sufficient.
How about using a condition variable here too and keeping it in struct sbi_hsm_data? I would rather not touch the hdata->state itself as I explained earlier.
IPI will then be used only to wake the target hart, not to confirm that next_addr and other data are ready.
Quoted 45 lines
Jess
Quoted 40 lines
Or, perhaps, I am missing some particular issue with this approach?
Quoted 31 lines
Jess
Quoted 30 lines
Signed-off-by Evgenii Shatokhin <[email protected]>---
lib/sbi/sbi_hsm.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +6
-2@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);Quoted 4 lines
Quoted 4 lines
lib/sbi/sbi_hsm.c +0
-1/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); --2.34.1
Regards,
Evgenii
PATCH 1/2 lib: sbi: Actually wait for an IPI in sbi_hsm_hart_wait()
Quoted 82 lines · Evgenii Shatokhin
Quoted 70 lines · Jessica Clarke
?????????! ?????? ?????? ?? ???????? ????????!?
Quoted 53 lines · Evgenii Shatokhin
Hi,
Quoted 49 lines · Jessica Clarke
Quoted 48 lines · Evgenii Shatokhin
"wfi" instruction is not guaranteed to wait for an interrupt to be received by the hart, it is just a hint for the CPU. According to RISC-V Privileged Architectures spec. v20211203, even an implementation of "wfi" as "nop" is legal.
As a result, a secondary (non-boot) hart waiting in the while loop in
sbi_hsm_hart_wait()might leave the loop and continue even if it got no IPI. This could lead to the following race condition when booting Linux, for example:Boot hart (#0) Secondary hart (#1)
runs Linux startup code waits insbi_hsm_hart_wait()sbi_ecall(SBI_EXT_HSM,
SBI_EXT_HSM_HART_START, ...)enters
sbi_hsm_hart_start()
sets state of hart #1 toSTART_PENDING
leavessbi_hsm_hart_wait()runs to the end ofinit_warmboot()returns toscratch->next_addr(next_addrcan be garbage here)sets
next_addr, etc. for hart #1
(no good: hart #1 has already left)sends IPI to hart #1
(no good either)If this happens, the secondary hart jumps to a wrong
next_addrat the end ofinit_warmboot(), which leads to a system hang or crash.To reproduce the issue more reliably, one could add a delay in
sbi_hsm_hart_start(): after setting the hart's state but before sending IPI to that hart:hstate = atomic_cmpxchg(&hdata->state, SBI_HSM_STATE_STOPPED, SBI_HSM_STATE_START_PENDING); ... + sbi_timer_mdelay(10); init_count = sbi_init_count(hartid); rscratch->next_arg1 = arg1; rscratch->next_addr = saddr;The issue can be reproduced, for example, in a QEMU VM with '-machine virt' and 2 or more CPUs, with Linux as the guest OS.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Let us actually check in
sbi_hsm_hart_wait(), if the waiting hart has received an IPI.Using the IPI itself as the synchronisation barrier seems awfully brittle. Better to split
SBI_HSM_STATE_START_PENDINGinto two states so there?s an additionalatomic_writethat theatomic_readhere can synchronise with.Yes, using an additional state was the first idea I tried to implement to fix the issue. It turned out to be too error-prone, however:
SBI_HSM_STATE_START_PENDINGis used in a number of places in opensbi, and it is far from obvious how to use the new state correctly there, esp. without violation of the spec.I only see 5, and they don?t look particularly hard...
Quoted 1 line
On the other hand,
wait_for_coldboot()andwake_coldboot_harts()already use IPI and a condition variable 'coldboot_done' for synchronization, and it seems to work OK.But
coldboot_doneis what those synchronise on; the IPI is merely the mechanism by which the waiter gets forcefully woken and made to checkcoldboot_done.Quoted 2 lines
So, I suppose, relying on IPI in
sbi_hsm_hart_wait()andsbi_hsm_hart_start()too might be a way to go. Looks like, it was the intended way:Quoted 7 lines
Quoted 5 lines
* Set the state to
START_PENDINGand get exclusive access to the target hart's scratch area.
* Writenext_addr,next_arg1, etc., for the target hart.
* Send IPI to the target hart to confirm that it might continue.WFI might or might not wait for an interrupt in the target hart, so the boot hart must send an IPI to the target anyway to wake it reliably.
But you can never truly know that the IPI you got is for the reason you think you got it.
You are right, and my second patch is just about such problem.
Quoted 7 lines
Quoted 3 lines
You *must* have additional state to indicate that the scratch area has been initialised; the fact you received an IPI somehow is not sufficient.
How about using a condition variable here too and keeping it in struct
sbi_hsm_data? I would rather not touch thehdata->stateitself as I explained earlier.IPI will then be used only to wake the target hart, not to confirm that
next_addrand other data are ready.
That works, but you need to be very careful about how and when you initialise it, since it?s not as simple as putting it in the existing state word.
Jess
Quoted 41 lines
Quoted 41 lines
Jess
Quoted 40 lines
Or, perhaps, I am missing some particular issue with this approach?
Quoted 31 lines
Jess
Quoted 30 lines
Signed-off-by Evgenii Shatokhin <[email protected]>---
lib/sbi/sbi_hsm.c| 8 ++++++--1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +6
-2@@ -108,6 +108,8 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid) {unsigned long saved_mie; +unsigned long ipi_received; +unsigned int hstate;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,9 +119,11 @@ static void sbi_hsm_hart_wait(struct sbi_scratch *scratch, u32 hartid)csr_set (CSR_MIE ,MIP_MSIP |MIP_MEIP );/* Wait for state transition requested by sbi_hsm_hart_start() */ -while (atomic_read (&hdata->state) !=SBI_HSM_STATE_START_PENDING ) { +do {wfi (); - }; + ipi_received =csr_read (CSR_MIP ) & (MIP_MSIP |MIP_MEIP ); + hstate =atomic_read (&hdata->state); + }while (hstate !=SBI_HSM_STATE_START_PENDING || !ipi_received);Quoted 4 lines
Quoted 4 lines
lib/sbi/sbi_hsm.c +0
-1/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); --2.34.1
Regards,
Evgenii
PATCH 2/2 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
In reply to Evgenii ShatokhinSince commit 50d4fde1c5a4 ("lib: Remove redundant sbi_platform_ipi_clear() calls"), the IPI sent from the boot hart in wake_coldboot_harts() is not cleared in the secondary harts until they reach sbi_ipi_init(). However, sbi_hsm_init() and sbi_hsm_hart_wait() are called earlier, so a secondary hart might enter sbi_hsm_hart_wait() with an already pending IPI.
Because of this, the secondary hart might exit the "wfi" loop in sbi_hsm_hart_wait() right after the boot hart changes state of that hart to SBI_HSM_STATE_START_PENDING. Then, depending on the hardware, it might be able to reach sbi_hart_switch_mode() at the end of init_warmboot() before the boot hart has set next_addr, etc., for it in sbi_hsm_hart_start(). In this case, the secondary hart will jump to a wrong address.
This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays after setting the hart state to SBI_HSM_STATE_START_PENDING in sbi_hsm_hart_start() allows reproducing the issue more reliably.
The comment in wait_for_coldboot() suggests that the initial IPI is needed in the warm resume path, so let us clear it before init_warm_startup() only.
To do this, sbi_ipi_raw_clear() was created similar to sbi_ipi_raw_send().
--- include/sbi/sbi_ipi.h | 2 ++ lib/sbi/sbi_init.c | 6 ++++-- lib/sbi/sbi_ipi.c | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-)
include/sbi/sbi_ipi.h +2 -0
@@ -77,6 +77,8 @@ void sbi_ipi_process(void);int sbi_ipi_raw_send (u32 target_hart); +void sbi_ipi_raw_clear (u32 target_hart); +const struct sbi_ipi_device *sbi_ipi_get_device (void );void sbi_ipi_set_device (const struct sbi_ipi_device *dev);
lib/sbi/sbi_init.c +4 -2
@@ -438,10 +438,12 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)if (hstate <0 )sbi_hart_hang (); -if (hstate ==SBI_HSM_STATE_SUSPENDED ) +if (hstate ==SBI_HSM_STATE_SUSPENDED ) {init_warm_resume (scratch); -else + }else { +sbi_ipi_raw_clear (hartid);init_warm_startup (scratch, hartid); + } sbi_hart_switch_mode(hartid, scratch->next_arg1, scratch->next_addr,
lib/sbi/sbi_ipi.c +6 -0
@@ -217,6 +217,12 @@ int sbi_ipi_raw_send(u32 target_hart)return 0 ; } +void sbi_ipi_raw_clear (u32 target_hart) +{ +if (ipi_dev && ipi_dev->ipi_clear) + ipi_dev->ipi_clear (target_hart); +} +const struct sbi_ipi_device *sbi_ipi_get_device (void ) {return ipi_dev;
Signature · 1 line
2.34.1