Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.2k
rp2: fix atomic section#13312
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
rp2: fix atomic section#13312
Uh oh!
There was an error while loading.Please reload this page.
Conversation
mendenm commentedJan 1, 2024
It works! At least the simple test script runs both processors and reports results form them. Excellent. |
Uh oh!
There was an error while loading.Please reload this page.
d062a6f
to130b4a9
CompareI have changed this to use a new set of mutex functions that also disable/restore interrupts when obtaining/releasing the mutex. |
130b4a9
to3586586
CompareNew approach looks good to me! |
Using the multicore lockout feature in the general atomic section makes itmuch more difficult to get correct.Signed-off-by: Damien George <damien@micropython.org>
These allow entering/exiting a mutex and also disabling/restoringinterrupts, in an atomic way.Signed-off-by: Damien George <damien@micropython.org>
Prior to this commit there is a potential deadlock inmp_thread_begin_atomic_section(), when obtaining the atomic_mutex, in thefollowing situation:- main thread calls mp_thread_begin_atomic_section() (for whatever reason, doesn't matter)- the second core is running so the main thread grabs the mutex via the call mp_thread_mutex_lock(&atomic_mutex, 1), and this succeeds- before the main thread has a chance to run save_and_disable_interrupts() a USB IRQ comes in and the main thread jumps off to process this IRQ- that USB processing triggers a call to the dcd_event_handler() wrapper from commitbcbdee2- that then calls mp_sched_schedule_node()- that then attempts to obtain the atomic section, calling mp_thread_begin_atomic_section()- that call then blocks trying to obtain atomic_mutex- core0 is now deadlocked on itself, because the main thread has the mutex but the IRQ handler (which preempted the main thread) is blocked waiting for the mutex, which will never be freeThe solution in this commit is to use mutex enter/exit functions that alsoatomically disable/restore interrupts.Fixes issuesmicropython#12980 andmicropython#13288.Signed-off-by: Damien George <damien@micropython.org>
3586586
todc2a4e3
Compare
Uh oh!
There was an error while loading.Please reload this page.
This is an attempt tofix#12980 and#13288.
There are two fixes to theMICROPY_BEGIN_ATOMIC_SECTION
/MICROPY_END_ATOMIC_SECTION
on the rp2 port:Use a recursive mutex to prevent deadlocks on the same thread, in the case an IRQ races against the thread level (on the same core) when acquiring the atomic section (mutex may be taken but IRQs not yet disabled, then the IRQ waits forever trying to acquire the mutux).Unlock the mutex if it was locked, not ifcore1_entry
is still non-null, to fix the case where the second core finishes (and setscore1_entry
toNULL
) while the first core is in the middle of an atomic operation.Edit: the two changes are now: