micropython/ports
Damien George ac5e0b9f62 rp2/mpthreadport: Fix race with IRQ when entering atomic section.
Prior to this commit there is a potential deadlock in
mp_thread_begin_atomic_section(), when obtaining the atomic_mutex, in the
following 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 commit bcbdee2357
- 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 free

The solution in this commit is to use mutex enter/exit functions that also
atomically disable/restore interrupts.

Fixes issues #12980 and #13288.

Signed-off-by: Damien George <damien@micropython.org>
2024-01-05 12:33:10 +11:00
..
bare-arm shared/libc/string0: Don't deref args for n==0 case. 2023-11-07 16:01:50 +11:00
cc3200 ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
embed all: Rename *umodule*.c to remove the "u" prefix. 2023-06-08 17:54:17 +10:00
esp32 extmod/nimble: Do not set GAP device name after sync. 2023-12-22 16:07:02 +11:00
esp8266 ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
mimxrt ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
minimal ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
nrf nrf/main: Add /flash and /flash/lib to sys.path. 2023-12-22 11:15:19 +11:00
pic16bit ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
powerpc ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
qemu-arm ports: Switch build to use common lib/libm list of source files. 2023-12-08 15:42:41 +11:00
renesas-ra ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
rp2 rp2/mpthreadport: Fix race with IRQ when entering atomic section. 2024-01-05 12:33:10 +11:00
samd ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
stm32 ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
unix ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
webassembly ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
windows ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00
zephyr ports: Fix sys.stdout.buffer.write() return value. 2023-12-22 10:32:46 +11:00