lib: sbi: Prevent system hang when bringing up secondary harts
v3 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();
};sbi_hsm_hart_start() sets the secondary hart's state to SBI_HSM_STATE_START_PENDING first and checks 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:
1. The data required for the secondary hart to continue (next_addr, etc.) are prepared after it state is set to SBI_HSM_STATE_START_PENDING, which can be too late.
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 "wfi" loop as soon as its state is set to SBI_HSM_STATE_START_PENDING, leading to the race described above.
Changes in v3:
* Added a patch with refactoring suggested by Anup Patel.
* Moved updates of the target hart's scratch area before setting the state. Used a more readable ticket-based synchronization to prevent other harts from messing with that scratch area.
* Added comments about memory ordering.
Changes in v2:
* Added a per-hart variable to properly synchronize the boot hart and the secondary hart. This should be safer than just waiting for any IPI in sbi_hsm_hart_wait(), because the hart might, in theory, get an IPI unrelated to sbi_hsm_hart_start().
* Updated the description of the second patch: clearing of the pending IPIs is no longer needed to prevent the race. Now, it rather allows avoiding a potentially inefficient busy-wait.
Regards,
Evgenii
PATCH v3 1/3 lib: sbi: Refactor the calls to sbi_hart_switch_mode()
Move them into sbi_hsm_hart_start_finish() and sbi_hsm_hart_resume_finish() to make them easier to manage.
This will be used by subsequent patches.
--- include/sbi/sbi_hsm.h | 6 ++++-- lib/sbi/sbi_hsm.c | 13 +++++++++++-- lib/sbi/sbi_init.c | 20 ++++++++------------ 3 files changed, 23 insertions(+), 16 deletions(-)
include/sbi/sbi_hsm.h +4 -2
@@ -66,7 +66,8 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, u32 hartid, ulong saddr, ulong smode, ulong arg1);int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow);void sbi_hsm_hart_resume_start (struct sbi_scratch *scratch); -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch); +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid);int sbi_hsm_hart_suspend (struct sbi_scratch *scratch,u32 suspend_type,ulong raddr,ulong rmode,ulong arg1); bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,
@@ -76,6 +77,7 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid);int sbi_hsm_hart_interruptible_mask (const struct sbi_domain *dom,ulong hbase,ulong *out_hmask);void __sbi_hsm_suspend_non_ret_save (struct sbi_scratch *scratch); -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid); +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid);#endif
lib/sbi/sbi_hsm.c +11 -2
@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);
@@ -118,6 +119,9 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)if (!__sbi_hsm_hart_change_state (hdata,SBI_HSM_STATE_START_PENDING ,SBI_HSM_STATE_STARTED ))sbi_hart_hang (); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, + scratch->next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)
@@ -381,7 +385,8 @@ void sbi_hsm_hart_resume_start(struct sbi_scratch *scratch)hsm_device_hart_resume (); } -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch) +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);
@@ -396,6 +401,10 @@ void sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch)* the warm-boot sequence. */ __sbi_hsm_suspend_non_ret_restore (scratch); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, + scratch->next_addr, + scratch->next_mode, false); } int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type,
lib/sbi/sbi_init.c +8 -12
@@ -355,12 +355,11 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_startup (struct sbi_scratch *scratch,u32 hartid) +static void __noreturn init_warm_startup (struct sbi_scratch *scratch, +u32 hartid) {int rc;unsigned long *init_count;
@@ -412,10 +411,11 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_resume (struct sbi_scratch *scratch) +static void __noreturn init_warm_resume (struct sbi_scratch *scratch, +u32 hartid) {int rc;
@@ -429,7 +429,7 @@ static void init_warm_resume(struct sbi_scratch *scratch)if (rc)sbi_hart_hang (); -sbi_hsm_hart_resume_finish (scratch); +sbi_hsm_hart_resume_finish (scratch, hartid); }static void __noreturn init_warmboot (struct sbi_scratch *scratch,u32 hartid)
@@ -443,13 +443,9 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang ();if (hstate ==SBI_HSM_STATE_SUSPENDED ) -init_warm_resume (scratch); +init_warm_resume (scratch, hartid);else init_warm_startup (scratch, hartid); - -sbi_hart_switch_mode (hartid, scratch->next_arg1, - scratch->next_addr, - scratch->next_mode, false); }static atomic_t coldboot_lottery =ATOMIC_INITIALIZER (0 );
Signature · 1 line
2.34.1
PATCH v3 1/3 lib: sbi: Refactor the calls to sbi_hart_switch_mode()
Quoted 8 lines · On Mon, Mar 6, 2023 at 2:53?AM Evgenii Shatokhin <[email protected]> wrote:
Move them into
sbi_hsm_hart_start_finish()andsbi_hsm_hart_resume_finish()to make them easier to manage.This will be used by subsequent patches.
Suggested-by Anup Patel <[email protected]>Signed-off-by Evgenii Shatokhin <[email protected]>
Looks good to me.
Regards,
Anup
Quoted 136 lines
--- include/sbi/sbi_hsm.h | 6 ++++-- lib/sbi/sbi_hsm.c | 13 +++++++++++-- lib/sbi/sbi_init.c | 20 ++++++++------------ 3 files changed, 23 insertions(+), 16 deletions(-)include/sbi/sbi_hsm.h +4
-2@@ -66,7 +66,8 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, u32 hartid, ulong saddr, ulong smode, ulong arg1);int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow);void sbi_hsm_hart_resume_start (struct sbi_scratch *scratch); -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch); +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid);int sbi_hsm_hart_suspend (struct sbi_scratch *scratch,u32 suspend_type,ulong raddr,ulong rmode,ulong arg1); bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,@@ -76,6 +77,7 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid);int sbi_hsm_hart_interruptible_mask (const struct sbi_domain *dom,ulong hbase,ulong *out_hmask);void __sbi_hsm_suspend_non_ret_save (struct sbi_scratch *scratch); -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid); +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid);#endif lib/sbi/sbi_hsm.c +11
-2@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -118,6 +119,9 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)if (!__sbi_hsm_hart_change_state (hdata,SBI_HSM_STATE_START_PENDING ,SBI_HSM_STATE_STARTED ))sbi_hart_hang (); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, + scratch->next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)@@ -381,7 +385,8 @@ void sbi_hsm_hart_resume_start(struct sbi_scratch *scratch)hsm_device_hart_resume (); } -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch) +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -396,6 +401,10 @@ void sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch)* the warm-boot sequence. */ __sbi_hsm_suspend_non_ret_restore (scratch); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, + scratch->next_addr, + scratch->next_mode, false); } int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type,lib/sbi/sbi_init.c +8
-13@@ -355,12 +355,11 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_startup (struct sbi_scratch *scratch,u32 hartid) +static void __noreturn init_warm_startup (struct sbi_scratch *scratch, +u32 hartid) {int rc;unsigned long *init_count;@@ -412,10 +411,11 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_resume (struct sbi_scratch *scratch) +static void __noreturn init_warm_resume (struct sbi_scratch *scratch, +u32 hartid) {int rc;@@ -429,7 +429,7 @@ static void init_warm_resume(struct sbi_scratch *scratch)if (rc)sbi_hart_hang (); -sbi_hsm_hart_resume_finish (scratch); +sbi_hsm_hart_resume_finish (scratch, hartid); }static void __noreturn init_warmboot (struct sbi_scratch *scratch,u32 hartid)@@ -443,13 +443,9 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang ();if (hstate ==SBI_HSM_STATE_SUSPENDED ) -init_warm_resume (scratch); +init_warm_resume (scratch, hartid);else init_warm_startup (scratch, hartid); - -sbi_hart_switch_mode (hartid, scratch->next_arg1, - scratch->next_addr, - scratch->next_mode, false); }static atomic_t coldboot_lottery =ATOMIC_INITIALIZER (0 ); --2.34.1
Anup Patel
AppliedPATCH v3 1/3 lib: sbi: Refactor the calls to sbi_hart_switch_mode()
In reply to Evgenii ShatokhinQuoted 8 lines · On Mon, Mar 6, 2023 at 2:53?AM Evgenii Shatokhin <[email protected]> wrote:
Move them into
sbi_hsm_hart_start_finish()andsbi_hsm_hart_resume_finish()to make them easier to manage.This will be used by subsequent patches.
Suggested-by Anup Patel <[email protected]>Signed-off-by Evgenii Shatokhin <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 136 lines
--- include/sbi/sbi_hsm.h | 6 ++++-- lib/sbi/sbi_hsm.c | 13 +++++++++++-- lib/sbi/sbi_init.c | 20 ++++++++------------ 3 files changed, 23 insertions(+), 16 deletions(-)include/sbi/sbi_hsm.h +4
-2@@ -66,7 +66,8 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, u32 hartid, ulong saddr, ulong smode, ulong arg1);int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow);void sbi_hsm_hart_resume_start (struct sbi_scratch *scratch); -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch); +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid);int sbi_hsm_hart_suspend (struct sbi_scratch *scratch,u32 suspend_type,ulong raddr,ulong rmode,ulong arg1); bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,@@ -76,6 +77,7 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid);int sbi_hsm_hart_interruptible_mask (const struct sbi_domain *dom,ulong hbase,ulong *out_hmask);void __sbi_hsm_suspend_non_ret_save (struct sbi_scratch *scratch); -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid); +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid);#endif lib/sbi/sbi_hsm.c +11
-2@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -118,6 +119,9 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)if (!__sbi_hsm_hart_change_state (hdata,SBI_HSM_STATE_START_PENDING ,SBI_HSM_STATE_STARTED ))sbi_hart_hang (); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, + scratch->next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)@@ -381,7 +385,8 @@ void sbi_hsm_hart_resume_start(struct sbi_scratch *scratch)hsm_device_hart_resume (); } -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch) +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -396,6 +401,10 @@ void sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch)* the warm-boot sequence. */ __sbi_hsm_suspend_non_ret_restore (scratch); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, + scratch->next_addr, + scratch->next_mode, false); } int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type,lib/sbi/sbi_init.c +8
-13@@ -355,12 +355,11 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_startup (struct sbi_scratch *scratch,u32 hartid) +static void __noreturn init_warm_startup (struct sbi_scratch *scratch, +u32 hartid) {int rc;unsigned long *init_count;@@ -412,10 +411,11 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_resume (struct sbi_scratch *scratch) +static void __noreturn init_warm_resume (struct sbi_scratch *scratch, +u32 hartid) {int rc;@@ -429,7 +429,7 @@ static void init_warm_resume(struct sbi_scratch *scratch)if (rc)sbi_hart_hang (); -sbi_hsm_hart_resume_finish (scratch); +sbi_hsm_hart_resume_finish (scratch, hartid); }static void __noreturn init_warmboot (struct sbi_scratch *scratch,u32 hartid)@@ -443,13 +443,9 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang ();if (hstate ==SBI_HSM_STATE_SUSPENDED ) -init_warm_resume (scratch); +init_warm_resume (scratch, hartid);else init_warm_startup (scratch, hartid); - -sbi_hart_switch_mode (hartid, scratch->next_arg1, - scratch->next_addr, - scratch->next_mode, false); }static atomic_t coldboot_lottery =ATOMIC_INITIALIZER (0 ); --2.34.1
PATCH v3 1/3 lib: sbi: Refactor the calls to sbi_hart_switch_mode()
Quoted 8 lines · Anup Patel
Quoted 6 lines · On Mon, Mar 6, 2023 at 2:53?AM Evgenii Shatokhin <[email protected]> wrote:
Move them into
sbi_hsm_hart_start_finish()andsbi_hsm_hart_resume_finish()to make them easier to manage.This will be used by subsequent patches.
Quoted 7 lines
Quoted 2 lines
Suggested-by Anup Patel <[email protected]>Signed-off-by Evgenii Shatokhin <[email protected]>Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Thank you!
Regards,
Evgenii
Quoted 138 lines
Quoted 137 lines
--- include/sbi/sbi_hsm.h | 6 ++++-- lib/sbi/sbi_hsm.c | 13 +++++++++++-- lib/sbi/sbi_init.c | 20 ++++++++------------ 3 files changed, 23 insertions(+), 16 deletions(-)include/sbi/sbi_hsm.h +4
-2@@ -66,7 +66,8 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, u32 hartid, ulong saddr, ulong smode, ulong arg1);int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow);void sbi_hsm_hart_resume_start (struct sbi_scratch *scratch); -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch); +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid);int sbi_hsm_hart_suspend (struct sbi_scratch *scratch,u32 suspend_type,ulong raddr,ulong rmode,ulong arg1); bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,@@ -76,6 +77,7 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid);int sbi_hsm_hart_interruptible_mask (const struct sbi_domain *dom,ulong hbase,ulong *out_hmask);void __sbi_hsm_suspend_non_ret_save (struct sbi_scratch *scratch); -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid); +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid);#endif lib/sbi/sbi_hsm.c +11
-2@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } -void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) +void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -118,6 +119,9 @@ void sbi_hsm_prepare_next_jump(struct sbi_scratch *scratch, u32 hartid)if (!__sbi_hsm_hart_change_state (hdata,SBI_HSM_STATE_START_PENDING ,SBI_HSM_STATE_STARTED ))sbi_hart_hang (); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, + scratch->next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)@@ -381,7 +385,8 @@ void sbi_hsm_hart_resume_start(struct sbi_scratch *scratch)hsm_device_hart_resume (); } -void sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch) +void __noreturn sbi_hsm_hart_resume_finish (struct sbi_scratch *scratch, +u32 hartid) {struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -396,6 +401,10 @@ void sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch)* the warm-boot sequence. */ __sbi_hsm_suspend_non_ret_restore (scratch); + +sbi_hart_switch_mode (hartid, scratch->next_arg1, + scratch->next_addr, + scratch->next_mode, false); } int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type,lib/sbi/sbi_init.c +8
-13@@ -355,12 +355,11 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_startup (struct sbi_scratch *scratch,u32 hartid) +static void __noreturn init_warm_startup (struct sbi_scratch *scratch, +u32 hartid) {int rc;unsigned long *init_count;@@ -412,10 +411,11 @@ static void init_warm_startup(struct sbi_scratch *scratch, u32 hartid) init_count =sbi_scratch_offset_ptr (scratch, init_count_offset); (*init_count)++; -sbi_hsm_prepare_next_jump (scratch, hartid); +sbi_hsm_hart_start_finish (scratch, hartid); } -static void init_warm_resume (struct sbi_scratch *scratch) +static void __noreturn init_warm_resume (struct sbi_scratch *scratch, +u32 hartid) {int rc;@@ -429,7 +429,7 @@ static void init_warm_resume(struct sbi_scratch *scratch)if (rc)sbi_hart_hang (); -sbi_hsm_hart_resume_finish (scratch); +sbi_hsm_hart_resume_finish (scratch, hartid); }static void __noreturn init_warmboot (struct sbi_scratch *scratch,u32 hartid)@@ -443,13 +443,9 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang ();if (hstate ==SBI_HSM_STATE_SUSPENDED ) -init_warm_resume (scratch); +init_warm_resume (scratch, hartid);else init_warm_startup (scratch, hartid); - -sbi_hart_switch_mode (hartid, scratch->next_arg1, - scratch->next_addr, - scratch->next_mode, false); }static atomic_t coldboot_lottery =ATOMIC_INITIALIZER (0 ); --2.34.1
PATCH v3 2/3 lib: sbi: Set the state of a hart to START_PENDING after the hart is ready
In reply to Evgenii ShatokhinWhen a boot hart executes sbi_hsm_hart_start() to start a secondary hart, next_arg1, next_addr and next_mode for the latter are stored in the scratch area after the state has been set to SBI_HSM_STATE_START_PENDING.
The secondary hart waits in the loop with wfi() in sbi_hsm_hart_wait() at that time. However, "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.
So, the secondary might leave the loop in sbi_hsm_hart_wait() as soon as its state has been set to SBI_HSM_STATE_START_PENDING, even if it got no IPI or it got an IPI unrelated to sbi_hsm_hart_start(). 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.
This patch moves writing of next_arg1, next_addr and next_mode for the secondary hart before setting its state to SBI_HSM_STATE_START_PENDING.
In theory, it is possible that two or more harts enter sbi_hsm_hart_start() for the same target hart simultaneously. To make sure the current hart has exclusive access to the scratch area of the target hart at that point, a per-hart 'start_ticket' is used. It is initially 0. The current hart tries to acquire the ticket first (set it to 1) at the beginning of sbi_hsm_hart_start() and only proceeds if it has successfully acquired it.
The target hart reads next_addr, etc., and then the releases the ticket (sets it to 0) before calling sbi_hart_switch_mode(). This way, even if some other hart manages to enter sbi_hsm_hart_start() after the ticket has been released but before the target hart jumps to next_addr, it will not cause problems.
atomic_cmpxchg() already has "acquire" semantics, among other things, so no additional barriers are needed in hsm_start_ticket_acquire(). No hart can perform or observe the update of *rscratch before setting of 'start_ticket' to 1.
atomic_write() only imposes ordering of writes, so an explicit barrier is needed in hsm_start_ticket_release() to ensure its "release" semantics. This guarantees that reads of scratch->next_addr, etc., in sbi_hsm_hart_start_finish() cannot happen after 'start_ticket' has been released.
--- lib/sbi/sbi_hsm.c | 83 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 16 deletions(-)
lib/sbi/sbi_hsm.c +67 -16
@@ -44,6 +44,7 @@ struct sbi_hsm_data {unsigned long suspend_type;unsigned long saved_mie;unsigned long saved_mip; +atomic_t start_ticket; }; bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,
@@ -75,6 +76,32 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid)return __sbi_hsm_hart_get_state (hartid); } +/* +* Try to acquire the ticket for the given target hart to make sure only +* one hart prepares the start of the target hart. +* Returns true if the ticket has been acquired, false otherwise. +* +* The function has "acquire" semantics: no memory operations following it +* in the current hart can be seen before it by other harts. +* atomic_cmpxchg() provides the memory barriers needed for that. +*/ +static bool hsm_start_ticket_acquire (struct sbi_hsm_data *hdata) +{ +return (atomic_cmpxchg (&hdata->start_ticket,0 ,1 ) ==0 ); +} + +/* +* Release the ticket for the given target hart. +* +* The function has "release" semantics: no memory operations preceding it +* in the current hart can be seen after it by other harts. +*/ +static void hsm_start_ticket_release (struct sbi_hsm_data *hdata) +{ +RISCV_FENCE (rw, w); +atomic_write (&hdata->start_ticket,0 ); +} +/** * Get ulong HART mask for given HART base ID * @param dom the domain to be used for output HART mask
@@ -113,6 +140,9 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch,u32 hartid) { +unsigned long next_arg1; +unsigned long next_addr; +unsigned long next_mode;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);
@@ -120,8 +150,12 @@ void __noreturn sbi_hsm_hart_start_finish(struct sbi_scratch *scratch, SBI_HSM_STATE_STARTED))sbi_hart_hang (); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); + next_arg1 = scratch->next_arg1; + next_addr = scratch->next_addr; + next_mode = scratch->next_mode; +hsm_start_ticket_release (hdata); + +sbi_hart_switch_mode (hartid, next_arg1, next_addr, next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)
@@ -226,6 +260,7 @@ int sbi_hsm_init(struct sbi_scratch *scratch, u32 hartid, bool cold_boot) (i == hartid) ?SBI_HSM_STATE_START_PENDING : SBI_HSM_STATE_STOPPED); +ATOMIC_INIT (&hdata->start_ticket,0 ); } }else {sbi_hsm_hart_wait (scratch, hartid);
@@ -270,6 +305,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,unsigned int hstate;struct sbi_scratch *rscratch;struct sbi_hsm_data *hdata; +int rc;/* For now, we only allow start mode to be S-mode or U-mode. */ if (smode !=PRV_S && smode !=PRV_U )
@@ -283,34 +319,49 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, rscratch =sbi_hartid_to_scratch (hartid);if (!rscratch)return SBI_EINVAL ; + hdata =sbi_scratch_offset_ptr (rscratch, hart_data_offset); +if (!hsm_start_ticket_acquire (hdata)) +return SBI_EINVAL ; + + init_count =sbi_init_count (hartid); + rscratch->next_arg1 = arg1; + rscratch->next_addr = saddr; + rscratch->next_mode = smode; + +/* +* atomic_cmpxchg() is an implicit barrier. It makes sure that +* other harts see reading of init_count and writing to *rscratch +* before hdata->state is set to SBI_HSM_STATE_START_PENDING. +*/ hstate =atomic_cmpxchg (&hdata->state,SBI_HSM_STATE_STOPPED ,SBI_HSM_STATE_START_PENDING ); -if (hstate ==SBI_HSM_STATE_STARTED ) -return SBI_EALREADY ; +if (hstate ==SBI_HSM_STATE_STARTED ) { + rc =SBI_EALREADY ; + goto err; + }/** * if a hart is already transition to start or stop, another start call * is considered as invalid request. */ -if (hstate !=SBI_HSM_STATE_STOPPED ) -return SBI_EINVAL ; - - init_count =sbi_init_count (hartid); - rscratch->next_arg1 = arg1; - rscratch->next_addr = saddr; - rscratch->next_mode = smode; +if (hstate !=SBI_HSM_STATE_STOPPED ) { + rc =SBI_EINVAL ; + goto err; + }if (hsm_device_has_hart_hotplug () || (hsm_device_has_hart_secondary_boot () && !init_count)) { -return hsm_device_hart_start (hartid, scratch->warmboot_addr); + rc =hsm_device_hart_start (hartid, scratch->warmboot_addr); }else { -int rc =sbi_ipi_raw_send (hartid); -if (rc) -return rc; + rc =sbi_ipi_raw_send (hartid); } -return 0 ; +if (!rc) +return 0 ; +err: +hsm_start_ticket_release (hdata); +return rc; }int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow)
Signature · 1 line
2.34.1
PATCH v3 2/3 lib: sbi: Set the state of a hart to START_PENDING after the hart is ready
Quoted 172 lines · On Mon, Mar 6, 2023 at 2:53?AM Evgenii Shatokhin <[email protected]> wrote:
When a boot hart executes
sbi_hsm_hart_start()to start a secondary hart,next_arg1,next_addrandnext_modefor the latter are stored in the scratch area after the state has been set toSBI_HSM_STATE_START_PENDING.The secondary hart waits in the loop with
wfi()insbi_hsm_hart_wait()at that time. However, "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.So, the secondary might leave the loop in
sbi_hsm_hart_wait()as soon as its state has been set toSBI_HSM_STATE_START_PENDING, even if it got no IPI or it got an IPI unrelated tosbi_hsm_hart_start(). 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.
This patch moves writing of
next_arg1,next_addrandnext_modefor the secondary hart before setting its state toSBI_HSM_STATE_START_PENDING.In theory, it is possible that two or more harts enter
sbi_hsm_hart_start()for the same target hart simultaneously. To make sure the current hart has exclusive access to the scratch area of the target hart at that point, a per-hart 'start_ticket' is used. It is initially 0. The current hart tries to acquire the ticket first (set it to 1) at the beginning ofsbi_hsm_hart_start()and only proceeds if it has successfully acquired it.The target hart reads
next_addr, etc., and then the releases the ticket (sets it to 0) before callingsbi_hart_switch_mode(). This way, even if some other hart manages to entersbi_hsm_hart_start()after the ticket has been released but before the target hart jumps tonext_addr, it will not cause problems.
atomic_cmpxchg()already has "acquire" semantics, among other things, so no additional barriers are needed inhsm_start_ticket_acquire(). No hart can perform or observe the update of *rscratch before setting of 'start_ticket' to 1.
atomic_write()only imposes ordering of writes, so an explicit barrier is needed inhsm_start_ticket_release()to ensure its "release" semantics. This guarantees that reads ofscratch->next_addr, etc., insbi_hsm_hart_start_finish()cannot happen after 'start_ticket' has been released.Signed-off-by Evgenii Shatokhin <[email protected]>--- lib/sbi/sbi_hsm.c | 83 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 16 deletions(-)lib/sbi/sbi_hsm.c +38
-2@@ -44,6 +44,7 @@ struct sbi_hsm_data {unsigned long suspend_type;unsigned long saved_mie;unsigned long saved_mip; +atomic_t start_ticket; }; bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,@@ -75,6 +76,32 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid)return __sbi_hsm_hart_get_state (hartid); } +/* +* Try to acquire the ticket for the given target hart to make sure only +* one hart prepares the start of the target hart. +* Returns true if the ticket has been acquired, false otherwise. +* +* The function has "acquire" semantics: no memory operations following it +* in the current hart can be seen before it by other harts. +* atomic_cmpxchg() provides the memory barriers needed for that. +*/ +static bool hsm_start_ticket_acquire (struct sbi_hsm_data *hdata) +{ +return (atomic_cmpxchg (&hdata->start_ticket,0 ,1 ) ==0 ); +} + +/* +* Release the ticket for the given target hart. +* +* The function has "release" semantics: no memory operations preceding it +* in the current hart can be seen after it by other harts. +*/ +static void hsm_start_ticket_release (struct sbi_hsm_data *hdata) +{ +RISCV_FENCE (rw, w); +atomic_write (&hdata->start_ticket,0 ); +} +/** * Get ulong HART mask for given HART base ID * @param dom the domain to be used for output HART mask @@ -113,6 +140,9 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch,u32 hartid) { +unsigned long next_arg1; +unsigned long next_addr; +unsigned long next_mode;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -120,8 +150,12 @@ void __noreturn sbi_hsm_hart_start_finish(struct sbi_scratch *scratch, SBI_HSM_STATE_STARTED))sbi_hart_hang (); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); + next_arg1 = scratch->next_arg1; + next_addr = scratch->next_addr; + next_mode = scratch->next_mode; +hsm_start_ticket_release (hdata); + +sbi_hart_switch_mode (hartid, next_arg1, next_addr, next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)@@ -226,6 +260,7 @@ int sbi_hsm_init(struct sbi_scratch *scratch, u32 hartid, bool cold_boot) (i == hartid) ?SBI_HSM_STATE_START_PENDING : SBI_HSM_STATE_STOPPED); +ATOMIC_INIT (&hdata->start_ticket,0 ); } }else {sbi_hsm_hart_wait (scratch, hartid);@@ -270,6 +305,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,unsigned int hstate;struct sbi_scratch *rscratch;struct sbi_hsm_data *hdata; +int rc;/* For now, we only allow start mode to be S-mode or U-mode. */ if (smode !=PRV_S && smode !=PRV_U )
lib/sbi/sbi_hsm.c int sbi_hsm_hart_start(struct sbi_scratch *scratch, · line 325
Quoted 7 lines
lib/sbi/sbi_hsm.c +3
-0@@ -283,34 +319,49 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, rscratch =sbi_hartid_to_scratch (hartid);if (!rscratch)return SBI_EINVAL ; + hdata =sbi_scratch_offset_ptr (rscratch, hart_data_offset); +if (!hsm_start_ticket_acquire (hdata)) +return SBI_EINVAL ;
I think SBI_EALREADY makes more sense here.
Quoted 57 lines
lib/sbi/sbi_hsm.c +26
-15+ + init_count =sbi_init_count (hartid); + rscratch->next_arg1 = arg1; + rscratch->next_addr = saddr; + rscratch->next_mode = smode; + +/* +* atomic_cmpxchg() is an implicit barrier. It makes sure that +* other harts see reading of init_count and writing to *rscratch +* before hdata->state is set to SBI_HSM_STATE_START_PENDING. +*/ hstate =atomic_cmpxchg (&hdata->state,SBI_HSM_STATE_STOPPED ,SBI_HSM_STATE_START_PENDING ); -if (hstate ==SBI_HSM_STATE_STARTED ) -return SBI_EALREADY ; +if (hstate ==SBI_HSM_STATE_STARTED ) { + rc =SBI_EALREADY ; + goto err; + }/** * if a hart is already transition to start or stop, another start call * is considered as invalid request. */ -if (hstate !=SBI_HSM_STATE_STOPPED ) -return SBI_EINVAL ; - - init_count =sbi_init_count (hartid); - rscratch->next_arg1 = arg1; - rscratch->next_addr = saddr; - rscratch->next_mode = smode; +if (hstate !=SBI_HSM_STATE_STOPPED ) { + rc =SBI_EINVAL ; + goto err; + }if (hsm_device_has_hart_hotplug () || (hsm_device_has_hart_secondary_boot () && !init_count)) { -return hsm_device_hart_start (hartid, scratch->warmboot_addr); + rc =hsm_device_hart_start (hartid, scratch->warmboot_addr); }else { -int rc =sbi_ipi_raw_send (hartid); -if (rc) -return rc; + rc =sbi_ipi_raw_send (hartid); } -return 0 ; +if (!rc) +return 0 ; +err: +hsm_start_ticket_release (hdata); +return rc; }int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow) --
Quoted 3 lines
2.34.1
Otherwise, it looks good to me.
Regards
Anup
Anup Patel
AppliedPATCH v3 2/3 lib: sbi: Set the state of a hart to START_PENDING after the hart is ready
In reply to Evgenii ShatokhinQuoted 74 lines · On Mon, Mar 6, 2023 at 2:53?AM Evgenii Shatokhin <[email protected]> wrote:
When a boot hart executes
sbi_hsm_hart_start()to start a secondary hart,next_arg1,next_addrandnext_modefor the latter are stored in the scratch area after the state has been set toSBI_HSM_STATE_START_PENDING.The secondary hart waits in the loop with
wfi()insbi_hsm_hart_wait()at that time. However, "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.So, the secondary might leave the loop in
sbi_hsm_hart_wait()as soon as its state has been set toSBI_HSM_STATE_START_PENDING, even if it got no IPI or it got an IPI unrelated tosbi_hsm_hart_start(). 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.
This patch moves writing of
next_arg1,next_addrandnext_modefor the secondary hart before setting its state toSBI_HSM_STATE_START_PENDING.In theory, it is possible that two or more harts enter
sbi_hsm_hart_start()for the same target hart simultaneously. To make sure the current hart has exclusive access to the scratch area of the target hart at that point, a per-hart 'start_ticket' is used. It is initially 0. The current hart tries to acquire the ticket first (set it to 1) at the beginning ofsbi_hsm_hart_start()and only proceeds if it has successfully acquired it.The target hart reads
next_addr, etc., and then the releases the ticket (sets it to 0) before callingsbi_hart_switch_mode(). This way, even if some other hart manages to entersbi_hsm_hart_start()after the ticket has been released but before the target hart jumps tonext_addr, it will not cause problems.
atomic_cmpxchg()already has "acquire" semantics, among other things, so no additional barriers are needed inhsm_start_ticket_acquire(). No hart can perform or observe the update of *rscratch before setting of 'start_ticket' to 1.
Quoted 7 lines
atomic_write()only imposes ordering of writes, so an explicit barrier is needed inhsm_start_ticket_release()to ensure its "release" semantics. This guarantees that reads ofscratch->next_addr, etc., insbi_hsm_hart_start_finish()cannot happen after 'start_ticket' has been released.Signed-off-by Evgenii Shatokhin <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 158 lines
--- lib/sbi/sbi_hsm.c | 83 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 16 deletions(-)lib/sbi/sbi_hsm.c +67
-17@@ -44,6 +44,7 @@ struct sbi_hsm_data {unsigned long suspend_type;unsigned long saved_mie;unsigned long saved_mip; +atomic_t start_ticket; }; bool sbi_hsm_hart_change_state(struct sbi_scratch *scratch, long oldstate,@@ -75,6 +76,32 @@ int sbi_hsm_hart_get_state(const struct sbi_domain *dom, u32 hartid)return __sbi_hsm_hart_get_state (hartid); } +/* +* Try to acquire the ticket for the given target hart to make sure only +* one hart prepares the start of the target hart. +* Returns true if the ticket has been acquired, false otherwise. +* +* The function has "acquire" semantics: no memory operations following it +* in the current hart can be seen before it by other harts. +* atomic_cmpxchg() provides the memory barriers needed for that. +*/ +static bool hsm_start_ticket_acquire (struct sbi_hsm_data *hdata) +{ +return (atomic_cmpxchg (&hdata->start_ticket,0 ,1 ) ==0 ); +} + +/* +* Release the ticket for the given target hart. +* +* The function has "release" semantics: no memory operations preceding it +* in the current hart can be seen after it by other harts. +*/ +static void hsm_start_ticket_release (struct sbi_hsm_data *hdata) +{ +RISCV_FENCE (rw, w); +atomic_write (&hdata->start_ticket,0 ); +} +/** * Get ulong HART mask for given HART base ID * @param dom the domain to be used for output HART mask @@ -113,6 +140,9 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,void __noreturn sbi_hsm_hart_start_finish (struct sbi_scratch *scratch,u32 hartid) { +unsigned long next_arg1; +unsigned long next_addr; +unsigned long next_mode;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);@@ -120,8 +150,12 @@ void __noreturn sbi_hsm_hart_start_finish(struct sbi_scratch *scratch, SBI_HSM_STATE_STARTED))sbi_hart_hang (); -sbi_hart_switch_mode (hartid, scratch->next_arg1, scratch->next_addr, - scratch->next_mode, false); + next_arg1 = scratch->next_arg1; + next_addr = scratch->next_addr; + next_mode = scratch->next_mode; +hsm_start_ticket_release (hdata); + +sbi_hart_switch_mode (hartid, next_arg1, next_addr, next_mode, false); }static void sbi_hsm_hart_wait (struct sbi_scratch *scratch,u32 hartid)@@ -226,6 +260,7 @@ int sbi_hsm_init(struct sbi_scratch *scratch, u32 hartid, bool cold_boot) (i == hartid) ?SBI_HSM_STATE_START_PENDING : SBI_HSM_STATE_STOPPED); +ATOMIC_INIT (&hdata->start_ticket,0 ); } }else {sbi_hsm_hart_wait (scratch, hartid);@@ -270,6 +305,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,unsigned int hstate;struct sbi_scratch *rscratch;struct sbi_hsm_data *hdata; +int rc;/* For now, we only allow start mode to be S-mode or U-mode. */ if (smode !=PRV_S && smode !=PRV_U )@@ -283,34 +319,49 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, rscratch =sbi_hartid_to_scratch (hartid);if (!rscratch)return SBI_EINVAL ; + hdata =sbi_scratch_offset_ptr (rscratch, hart_data_offset); +if (!hsm_start_ticket_acquire (hdata)) +return SBI_EINVAL ; + + init_count =sbi_init_count (hartid); + rscratch->next_arg1 = arg1; + rscratch->next_addr = saddr; + rscratch->next_mode = smode; + +/* +* atomic_cmpxchg() is an implicit barrier. It makes sure that +* other harts see reading of init_count and writing to *rscratch +* before hdata->state is set to SBI_HSM_STATE_START_PENDING. +*/ hstate =atomic_cmpxchg (&hdata->state,SBI_HSM_STATE_STOPPED ,SBI_HSM_STATE_START_PENDING ); -if (hstate ==SBI_HSM_STATE_STARTED ) -return SBI_EALREADY ; +if (hstate ==SBI_HSM_STATE_STARTED ) { + rc =SBI_EALREADY ; + goto err; + }/** * if a hart is already transition to start or stop, another start call * is considered as invalid request. */ -if (hstate !=SBI_HSM_STATE_STOPPED ) -return SBI_EINVAL ; - - init_count =sbi_init_count (hartid); - rscratch->next_arg1 = arg1; - rscratch->next_addr = saddr; - rscratch->next_mode = smode; +if (hstate !=SBI_HSM_STATE_STOPPED ) { + rc =SBI_EINVAL ; + goto err; + }if (hsm_device_has_hart_hotplug () || (hsm_device_has_hart_secondary_boot () && !init_count)) { -return hsm_device_hart_start (hartid, scratch->warmboot_addr); + rc =hsm_device_hart_start (hartid, scratch->warmboot_addr); }else { -int rc =sbi_ipi_raw_send (hartid); -if (rc) -return rc; + rc =sbi_ipi_raw_send (hartid); } -return 0 ; +if (!rc) +return 0 ; +err: +hsm_start_ticket_release (hdata); +return rc; }int sbi_hsm_hart_stop (struct sbi_scratch *scratch,bool exitnow) --2.34.1
PATCH v3 3/3 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.
sbi_hsm_hart_wait() makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.
This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays 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
@@ -442,10 +442,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, hartid); -else + }else { +sbi_ipi_raw_clear (hartid);init_warm_startup (scratch, hartid); + } }static atomic_t coldboot_lottery =ATOMIC_INITIALIZER (0 );
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
PATCH v3 3/3 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 17 lines · On Mon, Mar 6, 2023 at 2:54?AM Evgenii Shatokhin <[email protected]> wrote:
Since commit
50d4fde1c5a4("lib: Remove redundantsbi_platform_ipi_clear()calls"), the IPI sent from the boot hart inwake_coldboot_harts()is not cleared in the secondary harts until they reachsbi_ipi_init(). However,sbi_hsm_init()andsbi_hsm_hart_wait()are called earlier, so a secondary hart might entersbi_hsm_hart_wait()with an already pending IPI.
sbi_hsm_hart_wait()makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays in
sbi_hsm_hart_start()allows reproducing the issue more reliably.
Quoted 5 lines
The comment in
wait_for_coldboot()suggests that the initial IPI is needed in the warm resume path, so let us clear it beforeinit_warm_startup()only.To do this,
sbi_ipi_raw_clear()was created similar tosbi_ipi_raw_send().
As noted before, the sbi_ipi_raw_clear() will not work since secondary HARTs might call sbi_ipi_raw_clear() even before platform code has a chance to register IPI device ops.
I also recollected why we have this WFI and IPI based coldboot wait logic. The rationale of using WFI and IPI based coldboot wait logic was to reduce bus traffic from secondary HARTs while the primary HART completes the coldboot sequence (i.e. init_coldboot()). Clearly, this WFI and IPI based coldboot wait logic will do the intended thing if WFI is a NOP.
I think we can do the following:
1) Remove coldboot_lock and coldboot_wait_hmask
2) Simplify the loop in wait_for_coldboot() to use cpu_relax_loop(100) (or more) to reduce bus traffic from secondary HARTs
3) Simplify wake_coldboot_harts() to only do
"__smp_store_release(&coldboot_done, 1);"
Regards,
Anup
include/sbi/riscv_barrier.h +13 -1
@@ -40,7 +40,19 @@#define smp_wmb () RISCV_FENCE(w,w)/* CPU relax for busy loop */ -#define cpu_relax () asm volatile ("" : : : "memory") +#define cpu_relax () \ +do { \ + long dummy = 0x1; \ + asm volatile ("div %0, %0, zero" : "=r" (dummy) : : "memory");\ +} while (0) + +/* CPU relax loop */ +#define cpu_relax_loop (__loop_count) \ +do { \ + long i; \ + for (i = 0; i < (__loop_count); i++) \ + cpu_relax(); \ +} while (0)/* clang-format on */
PATCH v3 3/3 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 22 lines · Anup Patel
Quoted 21 lines · On Mon, Mar 6, 2023 at 2:54?AM Evgenii Shatokhin <[email protected]> wrote:
Since commit
50d4fde1c5a4("lib: Remove redundantsbi_platform_ipi_clear()calls"), the IPI sent from the boot hart inwake_coldboot_harts()is not cleared in the secondary harts until they reachsbi_ipi_init(). However,sbi_hsm_init()andsbi_hsm_hart_wait()are called earlier, so a secondary hart might entersbi_hsm_hart_wait()with an already pending IPI.
sbi_hsm_hart_wait()makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays 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 beforeinit_warm_startup()only.
Quoted 5 lines
Quoted 1 line
To do this,
sbi_ipi_raw_clear()was created similar tosbi_ipi_raw_send().As noted before, the
sbi_ipi_raw_clear()will not work since secondary HARTs might callsbi_ipi_raw_clear()even before platform code has a chance to register IPI device ops.
How so? If I understand it correctly, the platform code registers IPI device ops and sets the IPI device structures when the boot hart calls sbi_platform_ipi_init() with cold_boot=true from sbi_ipi_init().
So, by the time the secondary harts exit wait_for_coldboot(), IPI device and ops are already in place and can be used.
sbi_platform_ipi_init() called from those harts with cold_boot == false only clears pending IPIs and disables certain interrupts, it looks like.
Quoted 33 lines
I also recollected why we have this WFI and IPI based coldboot wait logic. The rationale of using WFI and IPI based coldboot wait logic was to reduce bus traffic from secondary HARTs while the primary HART completes the coldboot sequence (i.e.
init_coldboot()). Clearly, this WFI and IPI based coldboot wait logic will do the intended thing if WFI is a NOP.I think we can do the following:
1) Removecoldboot_lockandcoldboot_wait_hmask
2) Simplify the loop inwait_for_coldboot()to usecpu_relax_loop(100)(or more) to reduce bus traffic from secondary HARTs
3) Simplifywake_coldboot_harts()to only do"__smp_store_release(&coldboot_done, 1);"Regards,
Anupinclude/sbi/riscv_barrier.h +6
-1@@ -40,7 +40,19 @@#define smp_wmb () RISCV_FENCE(w,w)/* CPU relax for busy loop */ -#define cpu_relax () asm volatile ("" : : : "memory") +#define cpu_relax () \ +do { \ + long dummy = 0x1; \ + asm volatile ("div %0, %0, zero" : "=r" (dummy) : : "memory");\ +} while (0) +
include/sbi/riscv_barrier.h line 58
Quoted 10 lines
include/sbi/riscv_barrier.h +7
-0+/* CPU relax loop */ +#define cpu_relax_loop (__loop_count) \ +do { \ + long i; \ + for (i = 0; i < (__loop_count); i++) \ + cpu_relax(); \ +} while (0)/* clang-format on */
Regards,
Evgenii
PATCH v3 3/3 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 26 lines · On Thu, Mar 9, 2023 at 11:23?PM Evgenii Shatokhin <[email protected]> wrote:
Quoted 24 lines · Anup Patel
Quoted 22 lines · On Mon, Mar 6, 2023 at 2:54?AM Evgenii Shatokhin <[email protected]> wrote:
Since commit
50d4fde1c5a4("lib: Remove redundantsbi_platform_ipi_clear()calls"), the IPI sent from the boot hart inwake_coldboot_harts()is not cleared in the secondary harts until they reachsbi_ipi_init(). However,sbi_hsm_init()andsbi_hsm_hart_wait()are called earlier, so a secondary hart might entersbi_hsm_hart_wait()with an already pending IPI.
sbi_hsm_hart_wait()makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays 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 beforeinit_warm_startup()only.To do this,
sbi_ipi_raw_clear()was created similar tosbi_ipi_raw_send().
Quoted 7 lines
Quoted 3 lines
As noted before, the
sbi_ipi_raw_clear()will not work since secondary HARTs might callsbi_ipi_raw_clear()even before platform code has a chance to register IPI device ops.How so? If I understand it correctly, the platform code registers IPI device ops and sets the IPI device structures when the boot hart calls
sbi_platform_ipi_init()with cold_boot=true fromsbi_ipi_init().
sbi_ipi_raw_clear() added by this patch is called from secondary HART whereas platform code registers IPI device ops from sbi_ipi_init().
Quoted 6 lines
So, by the time the secondary harts exit
wait_for_coldboot(), IPI device and ops are already in place and can be used.
sbi_platform_ipi_init()called from those harts withcold_boot== false only clears pending IPIs and disables certain interrupts, it looks like.
The sbi_platform_ipi_init() is called via init_warm_startup() only after cold boot sequence is completed by primary hart but over here you are calling sbi_ipi_raw_clear() before init_warm_startup().
Regards,
Anup
Quoted 48 lines
Quoted 34 lines
I also recollected why we have this WFI and IPI based coldboot wait logic. The rationale of using WFI and IPI based coldboot wait logic was to reduce bus traffic from secondary HARTs while the primary HART completes the coldboot sequence (i.e.
init_coldboot()). Clearly, this WFI and IPI based coldboot wait logic will do the intended thing if WFI is a NOP.I think we can do the following:
1) Removecoldboot_lockandcoldboot_wait_hmask
2) Simplify the loop inwait_for_coldboot()to usecpu_relax_loop(100)(or more) to reduce bus traffic from secondary HARTs
3) Simplifywake_coldboot_harts()to only do"__smp_store_release(&coldboot_done, 1);"Regards,
Anupinclude/sbi/riscv_barrier.h +6
-1@@ -40,7 +40,19 @@#define smp_wmb () RISCV_FENCE(w,w)/* CPU relax for busy loop */ -#define cpu_relax () asm volatile ("" : : : "memory") +#define cpu_relax () \ +do { \ + long dummy = 0x1; \ + asm volatile ("div %0, %0, zero" : "=r" (dummy) : : "memory");\ +} while (0) +include/sbi/riscv_barrier.h line 58
Quoted 10 lines
include/sbi/riscv_barrier.h +7
-0+/* CPU relax loop */ +#define cpu_relax_loop (__loop_count) \ +do { \ + long i; \ + for (i = 0; i < (__loop_count); i++) \ + cpu_relax(); \ +} while (0)/* clang-format on */ Regards,
Evgenii
PATCH v3 3/3 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 32 lines · Anup Patel
Quoted 30 lines · On Thu, Mar 9, 2023 at 11:23?PM Evgenii Shatokhin <[email protected]> wrote:
Quoted 27 lines · Anup Patel
Quoted 17 lines · On Mon, Mar 6, 2023 at 2:54?AM Evgenii Shatokhin <[email protected]> wrote:
Since commit
50d4fde1c5a4("lib: Remove redundantsbi_platform_ipi_clear()calls"), the IPI sent from the boot hart inwake_coldboot_harts()is not cleared in the secondary harts until they reachsbi_ipi_init(). However,sbi_hsm_init()andsbi_hsm_hart_wait()are called earlier, so a secondary hart might entersbi_hsm_hart_wait()with an already pending IPI.
sbi_hsm_hart_wait()makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays in
sbi_hsm_hart_start()allows reproducing the issue more reliably.Quoted 5 lines
The comment in
wait_for_coldboot()suggests that the initial IPI is needed in the warm resume path, so let us clear it beforeinit_warm_startup()only.To do this,
sbi_ipi_raw_clear()was created similar tosbi_ipi_raw_send().As noted before, the
sbi_ipi_raw_clear()will not work since secondary HARTs might callsbi_ipi_raw_clear()even before platform code has a chance to register IPI device ops.
Quoted 6 lines
Quoted 3 lines
How so? If I understand it correctly, the platform code registers IPI device ops and sets the IPI device structures when the boot hart calls
sbi_platform_ipi_init()with cold_boot=true fromsbi_ipi_init().
sbi_ipi_raw_clear()added by this patch is called from secondary HART whereas platform code registers IPI device ops fromsbi_ipi_init().
Yes.
Quoted 11 lines
Quoted 7 lines
So, by the time the secondary harts exit
wait_for_coldboot(), IPI device and ops are already in place and can be used.
sbi_platform_ipi_init()called from those harts withcold_boot== false only clears pending IPIs and disables certain interrupts, it looks like.The
sbi_platform_ipi_init()is called viainit_warm_startup()only after cold boot sequence is completed by primary hart but over here you are callingsbi_ipi_raw_clear()beforeinit_warm_startup().
Yes. But the IPI device is common for all harts, as far as I can see. It has already been set up by that point by the coldboot path and is operational. Why not use it then?
When sbi_platform_ipi_init() is called later in init_warm_startup(), it only clears IPIs and, on certain platforms, disables some interrupts. I do not think that clearing IPIs with sbi_ipi_raw_clear() before init_warm_startup() would break that.
Or, perhaps, I am missing something?
I could not check it on all platforms in runtime, of course, but debugging shows that, at least, "generic" platform with fdt behaves the way I described. Clearing IPIs before init_warm_startup() works OK there.
The platforms which use aclint_mswi_cold_init() and aclint_mswi_warm_init(), that is, most of them, should be fine too:aclint_mswi_cold_init() prepares the IPI device and the related structures (mswi_hartid2data, etc.) while aclint_mswi_warm_init() just clears IPI for the current hart.
Quoted 53 lines
Regards,
AnupQuoted 48 lines
Quoted 34 lines
I also recollected why we have this WFI and IPI based coldboot wait logic. The rationale of using WFI and IPI based coldboot wait logic was to reduce bus traffic from secondary HARTs while the primary HART completes the coldboot sequence (i.e.
init_coldboot()). Clearly, this WFI and IPI based coldboot wait logic will do the intended thing if WFI is a NOP.I think we can do the following:
1) Removecoldboot_lockandcoldboot_wait_hmask
2) Simplify the loop inwait_for_coldboot()to usecpu_relax_loop(100)(or more) to reduce bus traffic from secondary HARTs
3) Simplifywake_coldboot_harts()to only do"__smp_store_release(&coldboot_done, 1);"Regards,
Anupinclude/sbi/riscv_barrier.h +6
-1@@ -40,7 +40,19 @@#define smp_wmb () RISCV_FENCE(w,w)/* CPU relax for busy loop */ -#define cpu_relax () asm volatile ("" : : : "memory") +#define cpu_relax () \ +do { \ + long dummy = 0x1; \ + asm volatile ("div %0, %0, zero" : "=r" (dummy) : : "memory");\ +} while (0) +include/sbi/riscv_barrier.h line 58
Quoted 10 lines
include/sbi/riscv_barrier.h +7
-0+/* CPU relax loop */ +#define cpu_relax_loop (__loop_count) \ +do { \ + long i; \ + for (i = 0; i < (__loop_count); i++) \ + cpu_relax(); \ +} while (0)/* clang-format on */ Regards,
Evgenii
PATCH v3 3/3 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 59 lines · On Fri, Mar 10, 2023 at 1:10?AM Evgenii Shatokhin <[email protected]> wrote:
Quoted 32 lines · Anup Patel
Quoted 30 lines · On Thu, Mar 9, 2023 at 11:23?PM Evgenii Shatokhin <[email protected]> wrote:
Quoted 27 lines · Anup Patel
Quoted 17 lines · On Mon, Mar 6, 2023 at 2:54?AM Evgenii Shatokhin <[email protected]> wrote:
Since commit
50d4fde1c5a4("lib: Remove redundantsbi_platform_ipi_clear()calls"), the IPI sent from the boot hart inwake_coldboot_harts()is not cleared in the secondary harts until they reachsbi_ipi_init(). However,sbi_hsm_init()andsbi_hsm_hart_wait()are called earlier, so a secondary hart might entersbi_hsm_hart_wait()with an already pending IPI.
sbi_hsm_hart_wait()makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays in
sbi_hsm_hart_start()allows reproducing the issue more reliably.Quoted 5 lines
The comment in
wait_for_coldboot()suggests that the initial IPI is needed in the warm resume path, so let us clear it beforeinit_warm_startup()only.To do this,
sbi_ipi_raw_clear()was created similar tosbi_ipi_raw_send().As noted before, the
sbi_ipi_raw_clear()will not work since secondary HARTs might callsbi_ipi_raw_clear()even before platform code has a chance to register IPI device ops.Quoted 6 lines
Quoted 3 lines
How so? If I understand it correctly, the platform code registers IPI device ops and sets the IPI device structures when the boot hart calls
sbi_platform_ipi_init()with cold_boot=true fromsbi_ipi_init().
sbi_ipi_raw_clear()added by this patch is called from secondary HART whereas platform code registers IPI device ops fromsbi_ipi_init().Yes.
Quoted 11 lines
Quoted 7 lines
So, by the time the secondary harts exit
wait_for_coldboot(), IPI device and ops are already in place and can be used.
sbi_platform_ipi_init()called from those harts withcold_boot== false only clears pending IPIs and disables certain interrupts, it looks like.The
sbi_platform_ipi_init()is called viainit_warm_startup()only after cold boot sequence is completed by primary hart but over here you are callingsbi_ipi_raw_clear()beforeinit_warm_startup().Yes. But the IPI device is common for all harts, as far as I can see. It has already been set up by that point by the coldboot path and is operational. Why not use it then?
Quoted 6 lines
When
sbi_platform_ipi_init()is called later ininit_warm_startup(), it only clears IPIs and, on certain platforms, disables some interrupts. I do not think that clearing IPIs withsbi_ipi_raw_clear()beforeinit_warm_startup()would break that.Or, perhaps, I am missing something?
Ahh, yes. I had the old warm boot sequence in my mind. Sorry for the confusion.
Regards,
Anup
Quoted 67 lines
I could not check it on all platforms in runtime, of course, but debugging shows that, at least, "generic" platform with fdt behaves the way I described. Clearing IPIs before
init_warm_startup()works OK there.The platforms which use
aclint_mswi_cold_init()andaclint_mswi_warm_init(), that is, most of them, should be fine too:aclint_mswi_cold_init()prepares the IPI device and the related structures (mswi_hartid2data, etc.) whileaclint_mswi_warm_init()just clears IPI for the current hart.Quoted 53 lines
Regards,
AnupQuoted 48 lines
Quoted 34 lines
I also recollected why we have this WFI and IPI based coldboot wait logic. The rationale of using WFI and IPI based coldboot wait logic was to reduce bus traffic from secondary HARTs while the primary HART completes the coldboot sequence (i.e.
init_coldboot()). Clearly, this WFI and IPI based coldboot wait logic will do the intended thing if WFI is a NOP.I think we can do the following:
1) Removecoldboot_lockandcoldboot_wait_hmask
2) Simplify the loop inwait_for_coldboot()to usecpu_relax_loop(100)(or more) to reduce bus traffic from secondary HARTs
3) Simplifywake_coldboot_harts()to only do"__smp_store_release(&coldboot_done, 1);"Regards,
Anupinclude/sbi/riscv_barrier.h +6
-1@@ -40,7 +40,19 @@#define smp_wmb () RISCV_FENCE(w,w)/* CPU relax for busy loop */ -#define cpu_relax () asm volatile ("" : : : "memory") +#define cpu_relax () \ +do { \ + long dummy = 0x1; \ + asm volatile ("div %0, %0, zero" : "=r" (dummy) : : "memory");\ +} while (0) +include/sbi/riscv_barrier.h line 58
Quoted 10 lines
include/sbi/riscv_barrier.h +7
-0+/* CPU relax loop */ +#define cpu_relax_loop (__loop_count) \ +do { \ + long i; \ + for (i = 0; i < (__loop_count); i++) \ + cpu_relax(); \ +} while (0)/* clang-format on */ Regards,
Evgenii
Anup Patel
AppliedPATCH v3 3/3 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
In reply to Evgenii ShatokhinQuoted 17 lines · On Mon, Mar 6, 2023 at 2:54?AM Evgenii Shatokhin <[email protected]> wrote:
Since commit
50d4fde1c5a4("lib: Remove redundantsbi_platform_ipi_clear()calls"), the IPI sent from the boot hart inwake_coldboot_harts()is not cleared in the secondary harts until they reachsbi_ipi_init(). However,sbi_hsm_init()andsbi_hsm_hart_wait()are called earlier, so a secondary hart might entersbi_hsm_hart_wait()with an already pending IPI.
sbi_hsm_hart_wait()makes sure the hart leaves the loop only when it is actually ready, so a pending unrelated IPI should not cause safety issues. However, it might be inefficient on certain hardware, because it prevents "wfi" from stalling the hart even if the hardware supports this, making the hart needlessly spin in a "busy-wait" loop.This behaviour can be observed, for example, in a QEMU VM (QEMU 7.2.0) with "-machine virt" running a Linux guest. Inserting delays in
sbi_hsm_hart_start()allows reproducing the issue more reliably.
Quoted 7 lines
The comment in
wait_for_coldboot()suggests that the initial IPI is needed in the warm resume path, so let us clear it beforeinit_warm_startup()only.To do this,
sbi_ipi_raw_clear()was created similar tosbi_ipi_raw_send().Signed-off-by Evgenii Shatokhin <[email protected]>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 59 lines
--- 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@@ -442,10 +442,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, hartid); -else + }else { +sbi_ipi_raw_clear (hartid);init_warm_startup (scratch, hartid); + } }static atomic_t coldboot_lottery =ATOMIC_INITIALIZER (0 );lib/sbi/sbi_ipi.c +6
-1@@ -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; --2.34.1