-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix multicore_lockout features so that the victim core cannot become stuck in an infinite loop if the lockout attempt times out #2467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kilograham
merged 5 commits into
raspberrypi:develop
from
jwhitham:2454-flash-safe-execute-lockout-forever-fix
May 20, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e91da36
Fix issue 2454 flash safe execute lockout
jwhitham eadf08b
Update documentation for multicore_lockout_end functions
jwhitham 5551101
Simplification, remove magic number (not required)
jwhitham d928e45
Review improvements
jwhitham 38de127
Restore use of non-zero magic number
jwhitham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,25 +202,25 @@ void multicore_launch_core1_raw(void (*entry)(void), uint32_t *sp, uint32_t vect | |
| irq_set_enabled(irq_num, enabled); | ||
| } | ||
|
|
||
| #define LOCKOUT_MAGIC_START 0x73a8831eu | ||
| #define LOCKOUT_MAGIC_END (~LOCKOUT_MAGIC_START) | ||
|
|
||
| static mutex_t lockout_mutex; | ||
| static bool lockout_in_progress; | ||
| static io_rw_32 lockout_request_id = 0; | ||
|
|
||
| // note this method is in RAM because lockout is used when writing to flash | ||
| // it only makes inline calls | ||
| static void __isr __not_in_flash_func(multicore_lockout_handler)(void) { | ||
| multicore_fifo_clear_irq(); | ||
| while (multicore_fifo_rvalid()) { | ||
| if (sio_hw->fifo_rd == LOCKOUT_MAGIC_START) { | ||
| uint32_t request_id = sio_hw->fifo_rd; | ||
| if (request_id == lockout_request_id) { | ||
| // valid lockout request received | ||
| uint32_t save = save_and_disable_interrupts(); | ||
| multicore_fifo_push_blocking_inline(LOCKOUT_MAGIC_START); | ||
| while (multicore_fifo_pop_blocking_inline() != LOCKOUT_MAGIC_END) { | ||
| tight_loop_contents(); // not tight but endless potentially | ||
| multicore_fifo_push_blocking_inline(request_id); | ||
| // wait for the lockout to expire | ||
| while (request_id == lockout_request_id) { | ||
| // when lockout_request_id is updated, the other CPU core calls __sev | ||
| __wfe(); | ||
| } | ||
| restore_interrupts_from_disabled(save); | ||
| multicore_fifo_push_blocking_inline(LOCKOUT_MAGIC_END); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -257,7 +257,7 @@ void multicore_lockout_victim_deinit(void) { | |
| } | ||
| } | ||
|
|
||
| static bool multicore_lockout_handshake(uint32_t magic, absolute_time_t until) { | ||
| static bool multicore_lockout_handshake(uint32_t request_id, absolute_time_t until) { | ||
| uint irq_num = SIO_FIFO_IRQ_NUM(get_core_num()); | ||
| bool enabled = irq_is_enabled(irq_num); | ||
| if (enabled) irq_set_enabled(irq_num, false); | ||
|
|
@@ -267,7 +267,7 @@ static bool multicore_lockout_handshake(uint32_t magic, absolute_time_t until) { | |
| if (next_timeout_us < 0) { | ||
| break; | ||
| } | ||
| multicore_fifo_push_timeout_us(magic, (uint64_t)next_timeout_us); | ||
| multicore_fifo_push_timeout_us(request_id, (uint64_t)next_timeout_us); | ||
| next_timeout_us = absolute_time_diff_us(get_absolute_time(), until); | ||
| if (next_timeout_us < 0) { | ||
| break; | ||
|
|
@@ -276,22 +276,36 @@ static bool multicore_lockout_handshake(uint32_t magic, absolute_time_t until) { | |
| if (!multicore_fifo_pop_timeout_us((uint64_t)next_timeout_us, &word)) { | ||
| break; | ||
| } | ||
| if (word == magic) { | ||
| if (word == request_id) { | ||
| rc = true; | ||
| } | ||
| } while (!rc); | ||
| if (enabled) irq_set_enabled(irq_num, true); | ||
| return rc; | ||
| } | ||
|
|
||
| static uint32_t update_lockout_request_id() { | ||
|
||
| // generate new number and then update shared variable | ||
| uint32_t new_request_id = lockout_request_id + 1; | ||
| lockout_request_id = new_request_id; | ||
kilograham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // notify other core | ||
| __sev(); | ||
| return new_request_id; | ||
| } | ||
|
|
||
| static bool multicore_lockout_start_block_until(absolute_time_t until) { | ||
| check_lockout_mutex_init(); | ||
| if (!mutex_enter_block_until(&lockout_mutex, until)) { | ||
| return false; | ||
| } | ||
| hard_assert(!lockout_in_progress); | ||
| bool rc = multicore_lockout_handshake(LOCKOUT_MAGIC_START, until); | ||
| lockout_in_progress = rc; | ||
| // generate a new request_id number | ||
| uint32_t request_id = update_lockout_request_id(); | ||
| // attempt to lock out | ||
| bool rc = multicore_lockout_handshake(request_id, until); | ||
| if (!rc) { | ||
| // lockout failed - cancel it | ||
| update_lockout_request_id(); | ||
| } | ||
| mutex_exit(&lockout_mutex); | ||
| return rc; | ||
| } | ||
|
|
@@ -309,13 +323,10 @@ static bool multicore_lockout_end_block_until(absolute_time_t until) { | |
| if (!mutex_enter_block_until(&lockout_mutex, until)) { | ||
| return false; | ||
| } | ||
| assert(lockout_in_progress); | ||
| bool rc = multicore_lockout_handshake(LOCKOUT_MAGIC_END, until); | ||
| if (rc) { | ||
| lockout_in_progress = false; | ||
| } | ||
| // lockout finished - cancel it | ||
| update_lockout_request_id(); | ||
| mutex_exit(&lockout_mutex); | ||
| return rc; | ||
| return true; | ||
| } | ||
|
|
||
| bool multicore_lockout_end_timeout_us(uint64_t timeout_us) { | ||
|
|
@@ -402,4 +413,4 @@ void multicore_doorbell_unclaim(uint doorbell_num, uint core_mask) { | |
| } | ||
|
|
||
|
|
||
| #endif | ||
| #endif | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small style point, we would probably use
volatile uint32_texplicitly here vsio_rw_32which is really intended for memory mapped IOThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, replaced.