lib: sbi: Prevent system hang when bringing up secondary harts
v2 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.
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 v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
"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 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 adds an auxiliary per-hart state variable, aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.
Initially, aux_state is 0: sbi_scratch_alloc_offset() zeroes the allocated memory. The boot hart sets it to 1 after it has set next_addr, etc., for the secondary hart, then sends the IPI to the latter.
The secondary hart waits in sbi_hsm_hart_wait() both for aux_state to become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated to sbi_hsm_hart_start().
Memory barriers are used to make sure the writes to next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zero aux_state.
Only one bit of aux_state is actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.
--- lib/sbi/sbi_hsm.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)
lib/sbi/sbi_hsm.c +33 -2
@@ -35,6 +35,7 @@ struct sbi_hsm_data {unsigned long suspend_type;unsigned long saved_mie;unsigned long saved_mip; +unsigned long aux_state; };static inline int __sbi_hsm_hart_get_state (u32 hartid)
@@ -93,6 +94,23 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } +static inline bool sbi_hsm_is_runnable (struct sbi_hsm_data *hdata) +{ +unsigned long aux_state =__smp_load_acquire (&hdata->aux_state); + +return !!aux_state; +} + +static inline void sbi_hsm_set_runnable (struct sbi_hsm_data *hdata) +{ +__smp_store_release (&hdata->aux_state,1 ); +} + +static inline void sbi_hsm_reset_runnable (struct sbi_hsm_data *hdata) +{ +__smp_store_release (&hdata->aux_state,0 ); +} +void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) {u32 oldstate;
@@ -108,6 +126,7 @@ 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 cmip;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */
@@ -117,13 +136,22 @@ 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 ) { -wfi (); +while (!sbi_hsm_is_runnable (hdata)) { +do { +wfi (); + cmip =csr_read (CSR_MIP ); + }while (!(cmip & (MIP_MSIP |MIP_MEIP ))); };/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); +/* +* If the hart ever needs to wait for such state transition again, +* make sure the hart's data allows that. +*/ +sbi_hsm_reset_runnable (hdata); +/* * No need to clear IPI here because the sbi_ipi_init() will * clear it for current HART via sbi_platform_ipi_init().
@@ -284,6 +312,9 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, rscratch->next_addr = saddr; rscratch->next_mode = smode; +/* Inform the hart that it may continue as soon as it gets IPI. */ +sbi_hsm_set_runnable (hdata); +if (hsm_device_has_hart_hotplug () || (hsm_device_has_hart_secondary_boot () && !init_count)) {return hsm_device_hart_start (hartid, scratch->warmboot_addr);
Signature · 1 line
2.34.1
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
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 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;
Quoted 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().
Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.
Adding aux_state is an overly complicated solution in my opinion.
Instead, you can simply do the following:
rscratch->next_arg1 = arg1;
rscratch->next_addr = saddr;
rscratch->next_mode = smode;
/*
* Barrier to ensure that the above writes have happened before
* updating the HART state.
*/
smp_wmb();Before "hdata = sbi_scratch_offset_ptr(rscratch, hart_data_offset);" in sbi_hsm_hart_start().
Quoted 91 lines
Signed-off-by Evgenii Shatokhin <[email protected]>--- lib/sbi/sbi_hsm.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +33
-3@@ -35,6 +35,7 @@ struct sbi_hsm_data {unsigned long suspend_type;unsigned long saved_mie;unsigned long saved_mip; +unsigned long aux_state; };static inline int __sbi_hsm_hart_get_state (u32 hartid)@@ -93,6 +94,23 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } +static inline bool sbi_hsm_is_runnable (struct sbi_hsm_data *hdata) +{ +unsigned long aux_state =__smp_load_acquire (&hdata->aux_state); + +return !!aux_state; +} + +static inline void sbi_hsm_set_runnable (struct sbi_hsm_data *hdata) +{ +__smp_store_release (&hdata->aux_state,1 ); +} + +static inline void sbi_hsm_reset_runnable (struct sbi_hsm_data *hdata) +{ +__smp_store_release (&hdata->aux_state,0 ); +} +void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) {u32 oldstate;@@ -108,6 +126,7 @@ 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 cmip;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,13 +136,22 @@ 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 ) { -wfi (); +while (!sbi_hsm_is_runnable (hdata)) { +do { +wfi (); + cmip =csr_read (CSR_MIP ); + }while (!(cmip & (MIP_MSIP |MIP_MEIP ))); };/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); +/* +* If the hart ever needs to wait for such state transition again, +* make sure the hart's data allows that. +*/ +sbi_hsm_reset_runnable (hdata); +/* * No need to clear IPI here because the sbi_ipi_init() will * clear it for current HART via sbi_platform_ipi_init(). @@ -284,6 +312,9 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, rscratch->next_addr = saddr; rscratch->next_mode = smode; +/* Inform the hart that it may continue as soon as it gets IPI. */ +sbi_hsm_set_runnable (hdata); +if (hsm_device_has_hart_hotplug () || (hsm_device_has_hart_secondary_boot () && !init_count)) {return hsm_device_hart_start (hartid, scratch->warmboot_addr); --2.34.1
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Regards,
Anup
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
Quoted 84 lines · Anup Patel
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 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;Quoted 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.Adding
aux_stateis an overly complicated solution in my opinion.Instead, you can simply do the following:
rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode; /* * Barrier to ensure that the above writes have happened before * updating the HART state. */ smp_wmb();
Quoted 2 lines
Before "hdata =
sbi_scratch_offset_ptr(rscratch, hart_data_offset);" insbi_hsm_hart_start().
True, the proposed fix it is more complicated than I would like it to be, but there is a reason.
Should we worry about the scenario when two or more harts enter sbi_hsm_hart_start() with the same target hartid? It must not happen during normal operation of the OS, but, in theory, errors in the higher-level software could lead to this.
Setting hstate to SBI_HSM_STATE_START_PENDING atomically and checking its previous values before changing *rscratch in sbi_hsm_hart_start() guarantees that the current hart has exclusive access to *rscratch after that. If we move rscratch->next_arg1 = arg1, etc., before hdata = sbi_scratch_offset_ptr(...), it would be possible for two harts to race and write different values to *rscratch for the same target hart. It is very unlikely to happen, but still.
If it is somehow guaranteed that the current hart has exclusive access to *rscratch even at the beginning of sbi_hsm_hart_start(), then yes, we can drop aux_state, etc., and use a simpler fix.
smp_wmb() will then be paired with rmb() in atomic_read(&hdata->state) from sbi_hsm_hart_wait() and it would make sure the writes are seen in correct order.
So, should we worry about misbehaving OSes which could allow 2 or more harts to enter sbi_hsm_hart_start() for the same target hart in parallel?
What do you think?
Quoted 92 lines
Quoted 92 lines
Signed-off-by Evgenii Shatokhin <[email protected]>--- lib/sbi/sbi_hsm.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)lib/sbi/sbi_hsm.c +33
-3@@ -35,6 +35,7 @@ struct sbi_hsm_data {unsigned long suspend_type;unsigned long saved_mie;unsigned long saved_mip; +unsigned long aux_state; };static inline int __sbi_hsm_hart_get_state (u32 hartid)@@ -93,6 +94,23 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,return 0 ; } +static inline bool sbi_hsm_is_runnable (struct sbi_hsm_data *hdata) +{ +unsigned long aux_state =__smp_load_acquire (&hdata->aux_state); + +return !!aux_state; +} + +static inline void sbi_hsm_set_runnable (struct sbi_hsm_data *hdata) +{ +__smp_store_release (&hdata->aux_state,1 ); +} + +static inline void sbi_hsm_reset_runnable (struct sbi_hsm_data *hdata) +{ +__smp_store_release (&hdata->aux_state,0 ); +} +void sbi_hsm_prepare_next_jump (struct sbi_scratch *scratch,u32 hartid) {u32 oldstate;@@ -108,6 +126,7 @@ 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 cmip;struct sbi_hsm_data *hdata =sbi_scratch_offset_ptr (scratch, hart_data_offset);/* Save MIE CSR */ @@ -117,13 +136,22 @@ 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 ) { -wfi (); +while (!sbi_hsm_is_runnable (hdata)) { +do { +wfi (); + cmip =csr_read (CSR_MIP ); + }while (!(cmip & (MIP_MSIP |MIP_MEIP ))); };/* Restore MIE CSR */ csr_write (CSR_MIE , saved_mie); +/* +* If the hart ever needs to wait for such state transition again, +* make sure the hart's data allows that. +*/ +sbi_hsm_reset_runnable (hdata); +/* * No need to clear IPI here because the sbi_ipi_init() will * clear it for current HART via sbi_platform_ipi_init(). @@ -284,6 +312,9 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch, rscratch->next_addr = saddr; rscratch->next_mode = smode; +/* Inform the hart that it may continue as soon as it gets IPI. */ +sbi_hsm_set_runnable (hdata); +if (hsm_device_has_hart_hotplug () || (hsm_device_has_hart_secondary_boot () && !init_count)) {return hsm_device_hart_start (hartid, scratch->warmboot_addr); --2.34.1
Quoted 8 lines
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbiRegards,
Anup
Regards,
Evgenii
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
Quoted 109 lines · Evgenii Shatokhin
Quoted 84 lines · Anup Patel
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 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;Quoted 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.Adding
aux_stateis an overly complicated solution in my opinion.Instead, you can simply do the following:
rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode; /* * Barrier to ensure that the above writes have happened before * updating the HART state. */ smp_wmb();Quoted 2 lines
Before "hdata =
sbi_scratch_offset_ptr(rscratch, hart_data_offset);" insbi_hsm_hart_start().True, the proposed fix it is more complicated than I would like it to be, but there is a reason.
Should we worry about the scenario when two or more harts enter
sbi_hsm_hart_start()with the same target hartid? It must not happen during normal operation of the OS, but, in theory, errors in the higher-level software could lead to this.Setting hstate to
SBI_HSM_STATE_START_PENDINGatomically and checking its previous values before changing *rscratch insbi_hsm_hart_start()guarantees that the current hart has exclusive access to *rscratch after that. If we moverscratch->next_arg1= arg1, etc., before hdata =sbi_scratch_offset_ptr(...), it would be possible for two harts to race and write different values to *rscratch for the same target hart. It is very unlikely to happen, but still.If it is somehow guaranteed that the current hart has exclusive access to *rscratch even at the beginning of
sbi_hsm_hart_start(), then yes, we can dropaux_state, etc., and use a simpler fix.
Quoted 8 lines
smp_wmb()will then be paired withrmb()inatomic_read(&hdata->state)fromsbi_hsm_hart_wait()and it would make sure the writes are seen in correct order.So, should we worry about misbehaving OSes which could allow 2 or more harts to enter
sbi_hsm_hart_start()for the same target hart in parallel?What do you think?
I see your point but it is better to tackle the situation of 2 or more harts entering sbi_hsm_hart_start() rather than aux_state which less readable.
How about this ?
1) Re-base on-top of the below patch
2) Update this patch to:
a) Move "rscratch->next_*" updates before
"hdata = sbi_scratch_offset_ptr(rscratch, hart_data_offset);"
in sbi_hsm_hart_start()
b) Add "start_ticket" which needs to be acquired (0->1) before
"rscratch->next_*" updates in sbi_hsm_hart_start() and
if a HART fails to acquire it then sbi_hsm_hart_start() fails c) Read the "scratch->next_" data and release "start_ticket" (1->0) in sbi_hsm_hart_start_finish() before doing sbi_hart_switch_mode().
Regards,
Anup
include/sbi/sbi_hsm.h +2 -1
@@ -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 +0 -0
@@ -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 +0 -0
@@ -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);
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
Quoted 127 lines · Anup Patel
Quoted 109 lines · Evgenii Shatokhin
Quoted 84 lines · Anup Patel
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 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,
...)
enterssbi_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 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.Adding
aux_stateis an overly complicated solution in my opinion.Instead, you can simply do the following:
rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode; /* * Barrier to ensure that the above writes have happened before * updating the HART state. */ smp_wmb();Quoted 2 lines
Before "hdata =
sbi_scratch_offset_ptr(rscratch, hart_data_offset);" insbi_hsm_hart_start().True, the proposed fix it is more complicated than I would like it to be, but there is a reason.
Should we worry about the scenario when two or more harts enter
sbi_hsm_hart_start()with the same target hartid? It must not happen during normal operation of the OS, but, in theory, errors in the higher-level software could lead to this.Setting hstate to
SBI_HSM_STATE_START_PENDINGatomically and checking its previous values before changing *rscratch insbi_hsm_hart_start()guarantees that the current hart has exclusive access to *rscratch after that. If we moverscratch->next_arg1= arg1, etc., before hdata =sbi_scratch_offset_ptr(...), it would be possible for two harts to race and write different values to *rscratch for the same target hart. It is very unlikely to happen, but still.If it is somehow guaranteed that the current hart has exclusive access to *rscratch even at the beginning of
sbi_hsm_hart_start(), then yes, we can dropaux_state, etc., and use a simpler fix.Quoted 8 lines
smp_wmb()will then be paired withrmb()inatomic_read(&hdata->state)fromsbi_hsm_hart_wait()and it would make sure the writes are seen in correct order.So, should we worry about misbehaving OSes which could allow 2 or more harts to enter
sbi_hsm_hart_start()for the same target hart in parallel?What do you think?
I see your point but it is better to tackle the situation of 2 or more harts entering
sbi_hsm_hart_start()rather thanaux_statewhich less readable.How about this ?
1) Re-base on-top of the below patch
2) Update this patch to:
Quoted 9 lines
a) Move "rscratch->next_*" updates before
"hdata = sbi_scratch_offset_ptr(rscratch, hart_data_offset);"
in sbi_hsm_hart_start()
b) Add "start_ticket" which needs to be acquired (0->1) before
"rscratch->next_*" updates in sbi_hsm_hart_start() and
if a HART fails to acquire it then sbi_hsm_hart_start() fails
c) Read the "scratch->next_" data and release "start_ticket" (1->0)
in sbi_hsm_hart_start_finish() before doing sbi_hart_switch_mode().If 'start_ticket' is a global, rather than a per-hart variable, no hart would be allowed to run sbi_hsm_hart_start() till the secondary hart finishes init_warm_startup() and releases the ticket. This could prevent the boot hart to call sbi_hsm_hart_start() for other harts, e.g. if the boot hart executes sbi_hsm_hart_start() quickly, while the secondary hart is slow.
I think, it would be sufficient to prevent entering sbi_hsm_hart_start() only for the *same* target hart rather than for *all* target harts.
If it is OK to add fields to 'struct sbi_hsm_data', we could add 'start_ticket' there, instead of 'aux_data', and use it as you suggested.
Would that be OK?
Regards,
Evgenii
Quoted 137 lines
Regards,
Anupinclude/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);#endiflib/sbi/sbi_hsm.c +2
-1@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const structsbi_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);lib/sbi/sbi_hsm.c +3
-0@@ -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)lib/sbi/sbi_hsm.c +6
-1@@ -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(structsbi_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, u32suspend_type,lib/sbi/sbi_init.c +3
-4@@ -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;lib/sbi/sbi_init.c +5
-8@@ -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);
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
Quoted 146 lines · On Fri, Mar 3, 2023 at 8:31?PM Evgenii Shatokhin <[email protected]> wrote:
Quoted 127 lines · Anup Patel
Quoted 109 lines · Evgenii Shatokhin
Quoted 84 lines · Anup Patel
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 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,
...)
enterssbi_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 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.Adding
aux_stateis an overly complicated solution in my opinion.Instead, you can simply do the following:
rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode; /* * Barrier to ensure that the above writes have happened before * updating the HART state. */ smp_wmb();Quoted 2 lines
Before "hdata =
sbi_scratch_offset_ptr(rscratch, hart_data_offset);" insbi_hsm_hart_start().True, the proposed fix it is more complicated than I would like it to be, but there is a reason.
Should we worry about the scenario when two or more harts enter
sbi_hsm_hart_start()with the same target hartid? It must not happen during normal operation of the OS, but, in theory, errors in the higher-level software could lead to this.Setting hstate to
SBI_HSM_STATE_START_PENDINGatomically and checking its previous values before changing *rscratch insbi_hsm_hart_start()guarantees that the current hart has exclusive access to *rscratch after that. If we moverscratch->next_arg1= arg1, etc., before hdata =sbi_scratch_offset_ptr(...), it would be possible for two harts to race and write different values to *rscratch for the same target hart. It is very unlikely to happen, but still.If it is somehow guaranteed that the current hart has exclusive access to *rscratch even at the beginning of
sbi_hsm_hart_start(), then yes, we can dropaux_state, etc., and use a simpler fix.Quoted 8 lines
smp_wmb()will then be paired withrmb()inatomic_read(&hdata->state)fromsbi_hsm_hart_wait()and it would make sure the writes are seen in correct order.So, should we worry about misbehaving OSes which could allow 2 or more harts to enter
sbi_hsm_hart_start()for the same target hart in parallel?What do you think?
I see your point but it is better to tackle the situation of 2 or more harts entering
sbi_hsm_hart_start()rather thanaux_statewhich less readable.How about this ?
1) Re-base on-top of the below patch
2) Update this patch to:Quoted 9 lines
a) Move "rscratch->next_*" updates before "hdata = sbi_scratch_offset_ptr(rscratch, hart_data_offset);" in sbi_hsm_hart_start() b) Add "start_ticket" which needs to be acquired (0->1) before "rscratch->next_*" updates in sbi_hsm_hart_start() and if a HART fails to acquire it then sbi_hsm_hart_start() fails c) Read the "scratch->next_" data and release "start_ticket" (1->0) in sbi_hsm_hart_start_finish() before doing sbi_hart_switch_mode().If '
start_ticket' is a global, rather than a per-hart variable, no hart would be allowed to runsbi_hsm_hart_start()till the secondary hart finishesinit_warm_startup()and releases the ticket. This could prevent the boot hart to callsbi_hsm_hart_start()for other harts, e.g. if the boot hart executessbi_hsm_hart_start()quickly, while the secondary hart is slow.
Quoted 7 lines
I think, it would be sufficient to prevent entering
sbi_hsm_hart_start()only for the *same* target hart rather than for *all* target harts.If it is OK to add fields to 'struct
sbi_hsm_data', we could add 'start_ticket' there, instead of 'aux_data', and use it as you suggested.Would that be OK?
Sorry, I was not clear previously but I assumed "start_ticket" to be per-HART. It should be added in 'struct sbi_hsm_data'
Regards,
Anup
Quoted 148 lines
Regards,
EvgeniiQuoted 135 lines
Regards,
Anupinclude/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);#endiflib/sbi/sbi_hsm.c +2
-1@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const structsbi_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);lib/sbi/sbi_hsm.c +3
-0@@ -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)lib/sbi/sbi_hsm.c +6
-1@@ -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(structsbi_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, u32suspend_type,lib/sbi/sbi_init.c +3
-4@@ -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;lib/sbi/sbi_init.c +5
-8@@ -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); }Quoted 2 lines
static atomic_t coldboot_lottery = ATOMIC_INITIALIZER(0);--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
Quoted 153 lines · Anup Patel
Quoted 152 lines · On Fri, Mar 3, 2023 at 8:31?PM Evgenii Shatokhin <[email protected]> wrote:
Quoted 127 lines · Anup Patel
Quoted 109 lines · Evgenii Shatokhin
Quoted 84 lines · Anup Patel
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 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,
...)
enterssbi_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 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.Adding
aux_stateis an overly complicated solution in my opinion.Instead, you can simply do the following:
rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode; /* * Barrier to ensure that the above writes have happened before * updating the HART state. */ smp_wmb();Quoted 2 lines
Before "hdata =
sbi_scratch_offset_ptr(rscratch, hart_data_offset);" insbi_hsm_hart_start().True, the proposed fix it is more complicated than I would like it to be, but there is a reason.
Should we worry about the scenario when two or more harts enter
sbi_hsm_hart_start()with the same target hartid? It must not happen during normal operation of the OS, but, in theory, errors in the higher-level software could lead to this.Setting hstate to
SBI_HSM_STATE_START_PENDINGatomically and checking its previous values before changing *rscratch insbi_hsm_hart_start()guarantees that the current hart has exclusive access to *rscratch after that. If we moverscratch->next_arg1= arg1, etc., before hdata =sbi_scratch_offset_ptr(...), it would be possible for two harts to race and write different values to *rscratch for the same target hart. It is very unlikely to happen, but still.If it is somehow guaranteed that the current hart has exclusive access to *rscratch even at the beginning of
sbi_hsm_hart_start(), then yes, we can dropaux_state, etc., and use a simpler fix.Quoted 8 lines
smp_wmb()will then be paired withrmb()inatomic_read(&hdata->state)fromsbi_hsm_hart_wait()and it would make sure the writes are seen in correct order.So, should we worry about misbehaving OSes which could allow 2 or more harts to enter
sbi_hsm_hart_start()for the same target hart in parallel?What do you think?
I see your point but it is better to tackle the situation of 2 or more harts entering
sbi_hsm_hart_start()rather thanaux_statewhich less readable.How about this ?
1) Re-base on-top of the below patch
2) Update this patch to:Quoted 9 lines
a) Move "rscratch->next_*" updates before "hdata = sbi_scratch_offset_ptr(rscratch, hart_data_offset);" in sbi_hsm_hart_start() b) Add "start_ticket" which needs to be acquired (0->1) before "rscratch->next_*" updates in sbi_hsm_hart_start() and if a HART fails to acquire it then sbi_hsm_hart_start() fails c) Read the "scratch->next_" data and release "start_ticket" (1->0) in sbi_hsm_hart_start_finish() before doing sbi_hart_switch_mode().If '
start_ticket' is a global, rather than a per-hart variable, no hart would be allowed to runsbi_hsm_hart_start()till the secondary hart finishesinit_warm_startup()and releases the ticket. This could prevent the boot hart to callsbi_hsm_hart_start()for other harts, e.g. if the boot hart executessbi_hsm_hart_start()quickly, while the secondary hart is slow.I think, it would be sufficient to prevent entering
sbi_hsm_hart_start()only for the *same* target hart rather than for *all* target harts.If it is OK to add fields to 'struct
sbi_hsm_data', we could add 'start_ticket' there, instead of 'aux_data', and use it as you suggested.
Quoted 7 lines
Quoted 1 line
Would that be OK?
Sorry, I was not clear previously but I assumed "
start_ticket" to be per-HART. It should be added in 'structsbi_hsm_data'Regards,
Anup
Great, thanks! I will update and re-test the patches and will then send v3.
I do not see your patch with refactoring of sbi_hsm_hart_resume_finish, sbi_hsm_hart_start_finish, etc., in the mailing list - is it OK if I submit it as a part of this patch series?
Regards,
Evgenii
Quoted 146 lines
Quoted 145 lines
Quoted 136 lines
Regards,
Anupinclude/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);#endiflib/sbi/sbi_hsm.c +2
-1@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const structsbi_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);lib/sbi/sbi_hsm.c +3
-0@@ -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)lib/sbi/sbi_hsm.c +6
-1@@ -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(structsbi_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, u32suspend_type,lib/sbi/sbi_init.c +3
-4@@ -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;lib/sbi/sbi_init.c +5
-8@@ -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); }Quoted 2 lines
static atomic_t coldboot_lottery = ATOMIC_INITIALIZER(0);--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v2 1/2 lib: sbi: Wait in sbi_hsm_hart_wait() till the hart is prepared to start
Quoted 160 lines · On Sat, Mar 4, 2023 at 1:21?AM Evgenii Shatokhin <[email protected]> wrote:
Quoted 158 lines · Anup Patel
Quoted 146 lines · On Fri, Mar 3, 2023 at 8:31?PM Evgenii Shatokhin <[email protected]> wrote:
Quoted 127 lines · Anup Patel
Quoted 109 lines · Evgenii Shatokhin
Quoted 84 lines · Anup Patel
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 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,
...)
enterssbi_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 2 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.
Good catch. This is a valid issue which needs to be fixed.
Quoted 13 lines
This patch adds an auxiliary per-hart state variable,
aux_state, as well as its accessor functions, to synchronize execution of the boot hart and the starting secondary hart.Initially,
aux_stateis 0:sbi_scratch_alloc_offset()zeroes the allocated memory. The boot hart sets it to 1 after it has setnext_addr, etc., for the secondary hart, then sends the IPI to the latter.The secondary hart waits in
sbi_hsm_hart_wait()both foraux_stateto become non-zero and for an IPI to happen. This should be safe even if the secondary hart somehow gets an IPI unrelated tosbi_hsm_hart_start().Quoted 5 lines
Memory barriers are used to make sure the writes to
next_addr, etc., in the boot hart are visible to the secondary hart if it reads non-zeroaux_state.Only one bit of
aux_stateis actually used here, so, this per-hart variable can be used to hold other data in the future, if needed.Adding
aux_stateis an overly complicated solution in my opinion.Instead, you can simply do the following:
rscratch->next_arg1 = arg1; rscratch->next_addr = saddr; rscratch->next_mode = smode; /* * Barrier to ensure that the above writes have happened before * updating the HART state. */ smp_wmb();Quoted 2 lines
Before "hdata =
sbi_scratch_offset_ptr(rscratch, hart_data_offset);" insbi_hsm_hart_start().True, the proposed fix it is more complicated than I would like it to be, but there is a reason.
Should we worry about the scenario when two or more harts enter
sbi_hsm_hart_start()with the same target hartid? It must not happen during normal operation of the OS, but, in theory, errors in the higher-level software could lead to this.Setting hstate to
SBI_HSM_STATE_START_PENDINGatomically and checking its previous values before changing *rscratch insbi_hsm_hart_start()guarantees that the current hart has exclusive access to *rscratch after that. If we moverscratch->next_arg1= arg1, etc., before hdata =sbi_scratch_offset_ptr(...), it would be possible for two harts to race and write different values to *rscratch for the same target hart. It is very unlikely to happen, but still.If it is somehow guaranteed that the current hart has exclusive access to *rscratch even at the beginning of
sbi_hsm_hart_start(), then yes, we can dropaux_state, etc., and use a simpler fix.Quoted 8 lines
smp_wmb()will then be paired withrmb()inatomic_read(&hdata->state)fromsbi_hsm_hart_wait()and it would make sure the writes are seen in correct order.So, should we worry about misbehaving OSes which could allow 2 or more harts to enter
sbi_hsm_hart_start()for the same target hart in parallel?What do you think?
I see your point but it is better to tackle the situation of 2 or more harts entering
sbi_hsm_hart_start()rather thanaux_statewhich less readable.How about this ?
1) Re-base on-top of the below patch
2) Update this patch to:Quoted 9 lines
a) Move "rscratch->next_*" updates before "hdata = sbi_scratch_offset_ptr(rscratch, hart_data_offset);" in sbi_hsm_hart_start() b) Add "start_ticket" which needs to be acquired (0->1) before "rscratch->next_*" updates in sbi_hsm_hart_start() and if a HART fails to acquire it then sbi_hsm_hart_start() fails c) Read the "scratch->next_" data and release "start_ticket" (1->0) in sbi_hsm_hart_start_finish() before doing sbi_hart_switch_mode().If '
start_ticket' is a global, rather than a per-hart variable, no hart would be allowed to runsbi_hsm_hart_start()till the secondary hart finishesinit_warm_startup()and releases the ticket. This could prevent the boot hart to callsbi_hsm_hart_start()for other harts, e.g. if the boot hart executessbi_hsm_hart_start()quickly, while the secondary hart is slow.Quoted 7 lines
I think, it would be sufficient to prevent entering
sbi_hsm_hart_start()only for the *same* target hart rather than for *all* target harts.If it is OK to add fields to 'struct
sbi_hsm_data', we could add 'start_ticket' there, instead of 'aux_data', and use it as you suggested.Would that be OK?
Sorry, I was not clear previously but I assumed "
start_ticket" to be per-HART. It should be added in 'structsbi_hsm_data'
Quoted 8 lines
Quoted 2 lines
Regards,
AnupGreat, thanks! I will update and re-test the patches and will then send v3.
I do not see your patch with refactoring of
sbi_hsm_hart_resume_finish,sbi_hsm_hart_start_finish, etc., in the mailing list - is it OK if I submit it as a part of this patch series?
Yes, please include it as a separate patch in your series.
Thanks,
Anup
Quoted 152 lines
Regards,
EvgeniiQuoted 146 lines
Quoted 145 lines
Quoted 136 lines
Regards,
Anupinclude/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);#endiflib/sbi/sbi_hsm.c +2
-1@@ -110,7 +110,8 @@ int sbi_hsm_hart_interruptible_mask(const structsbi_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);lib/sbi/sbi_hsm.c +3
-0@@ -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)lib/sbi/sbi_hsm.c +6
-1@@ -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(structsbi_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, u32suspend_type,lib/sbi/sbi_init.c +3
-4@@ -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;lib/sbi/sbi_init.c +5
-8@@ -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); }Quoted 2 lines
static atomic_t coldboot_lottery = ATOMIC_INITIALIZER(0);--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v2 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.
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
@@ -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
PATCH v2 2/2 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 48 lines · Evgenii Shatokhin
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().Signed-off-by Evgenii Shatokhin <[email protected]>--- 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 +0
-0
lib/sbi/sbi_init.c static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid) · line 444
Quoted 9 lines
lib/sbi/sbi_init.c +3
-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);
This effectively does nothing in many cases because for a lot of platforms all HARTs enter OpenSBI at the same time and the HARTs entering warm boot path will call sbi_ipi_raw_clear() even before ipi_dev is provided by platform code.
Quoted 27 lines
lib/sbi/sbi_init.c +1
-0init_warm_startup (scratch, hartid); + } sbi_hart_switch_mode(hartid, scratch->next_arg1, scratch->next_addr,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
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Regards,
Anup
PATCH v2 2/2 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 52 lines · Anup Patel
Quoted 51 lines · Evgenii Shatokhin
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().Signed-off-by Evgenii Shatokhin <[email protected]>--- 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 +0
-0@@ -438,10 +438,12 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)if (hstate <0 )sbi_hart_hang ();
Quoted 11 lines
lib/sbi/sbi_init.c static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid) · line 444
Quoted 6 lines
lib/sbi/sbi_init.c +3
-2-if (hstate ==SBI_HSM_STATE_SUSPENDED ) +if (hstate ==SBI_HSM_STATE_SUSPENDED ) {init_warm_resume (scratch); -else + }else { +sbi_ipi_raw_clear (hartid);This effectively does nothing in many cases because for a lot of platforms all HARTs enter OpenSBI at the same time and the HARTs entering warm boot path will call
sbi_ipi_raw_clear()even beforeipi_devis provided by platform code.
Right, it would be a no-op then.
However, this is not the case when Linux 6.x boots, e.g. in QEMU with "-machine virt" or with other platforms which provide "cold" IPI init callbacks.
Here, the boot hart initializes ipi_dev early. For example, in QEMU with "-machine virt", it does the following:init_coldboot() => sbi_ipi_init() => ... => aclint_mswi_cold_init().
Other harts are waiting in sbi_hsm_init() at the moment and will only call sbi_ipi_init() after they leave sbi_hsm_hart_wait(), etc.
While they are waiting, their previous IPI remains pending. It can be easily demonstrated if one checks the relevant bits of CSR_MIP in sbi_hsm_hart_wait() right before the wfi loop. A delay in sbi_hsm_hart_start(), same as for the first issue, also helps make it visible.
csr_read(CSR_MIP) & (MIP_MSIP | MIP_MEIP) == 0x8 in my case at that point. That is, MIP_MSIP bit is set even before the boot hart has send an IPI to the target hart in sbi_hsm_hart_start(). Not good.
So, sbi_ipi_raw_clear(hartid) really helps in such cases, even if it is a no-op in other situations.
Quoted 28 lines
Quoted 28 lines
lib/sbi/sbi_init.c +1
-0init_warm_startup (scratch, hartid); + } sbi_hart_switch_mode(hartid, scratch->next_arg1, scratch->next_addr,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
Quoted 8 lines
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbiRegards,
Anup
Regards,
Evgenii
PATCH v2 2/2 lib: sbi: Clear IPIs before init_warm_startup in non-boot harts
Quoted 81 lines · Evgenii Shatokhin
Quoted 62 lines · Anup Patel
Quoted 60 lines · Evgenii Shatokhin
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 insbi_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().Signed-off-by Evgenii Shatokhin <[email protected]>---
? 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 +0
-0@@ -438,10 +438,12 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid) ???????? if (hstate < 0) ???????????????? sbi_hart_hang();Quoted 11 lines
lib/sbi/sbi_init.c static void __noreturn init_warmboot(struct · line 445
Quoted 6 lines
lib/sbi/sbi_init.c +3
-2-?????? if (hstate == SBI_HSM_STATE_SUSPENDED) +?????? if (hstate == SBI_HSM_STATE_SUSPENDED) { ???????????????? init_warm_resume(scratch); -?????? else +?????? } else { +?????????????? sbi_ipi_raw_clear(hartid);This effectively does nothing in many cases because for a lot of platforms all HARTs enter OpenSBI at the same time and the HARTs entering warm boot path will call
sbi_ipi_raw_clear()even beforeipi_devis provided by platform code.Right, it would be a no-op then.
However, this is not the case when Linux 6.x boots, e.g. in QEMU with "-machine virt" or with other platforms which provide "cold" IPI init callbacks.
Quoted 6 lines
Here, the boot hart initializes
ipi_devearly. For example, in QEMU with "-machine virt", it does the following:init_coldboot()=>sbi_ipi_init()=> ... =>aclint_mswi_cold_init().Other harts are waiting in
sbi_hsm_init()at the moment and will only callsbi_ipi_init()after they leavesbi_hsm_hart_wait(), etc.
Correction: the secondary harts are waiting in init_warmboot() at that moment rather than in sbi_hsm_init(). Sorry, my bad.
Still, the boot hart only wakes them up with wake_coldboot_harts() after it has called sbi_ipi_init(), etc. So, by the time the secondary harts get to init_warm_startup(), ipi_dev should already be initialized and sbi_ipi_raw_clear() should work.
Quoted 7 lines
While they are waiting, their previous IPI remains pending. It can be easily demonstrated if one checks the relevant bits of
CSR_MIPinsbi_hsm_hart_wait()right before the wfi loop. A delay insbi_hsm_hart_start(), same as for the first issue, also helps make it visible.
Quoted 45 lines
csr_read(CSR_MIP)& (MIP_MSIP|MIP_MEIP) ==0x8in my case at that point. That is,MIP_MSIPbit is set even before the boot hart has send an IPI to the target hart insbi_hsm_hart_start(). Not good.So,
sbi_ipi_raw_clear(hartid)really helps in such cases, even if it is a no-op in other situations.Quoted 36 lines
Quoted 32 lines
lib/sbi/sbi_init.c +1
-0???????????????? 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 · 8 lines
2.34.1 -- opensbi mailing list opensbi at lists.infradead.org http://lists.infradead.org/mailman/listinfo/opensbiRegards,
Anup
Regards,
Evgenii