From 823929849a3920740d0332ab91c87108c6a81be5 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 20 Nov 2026 16:17:25 -0500 Subject: [PATCH 1/2] supervisor: don't queue heap callbacks during heap teardown stop_mp() unlinks queued callbacks on the heap, then runs finalisers and frees the heap. Until a finaliser stops its hardware, an interrupt can queue a callback that lives in a heap object again, and it would still be pending after the heap is freed. On ESP32-S3, the I2SOut on_sent interrupt did this and caused hard faults at soft reload (#11822). background_callback_reset() now sets a flag that makes background_callback_add() drop callbacks on the heap until background_callback_heap_ready() is called after the next gc_init(). gc_ptr_on_heap() moves to ITCM, because on i.MX RT the USB interrupt can queue a callback during a flash write. Co-Authored-By: Claude Opus 5.5 --- main.c | 1 + py/gc.c | 5 ++++- supervisor/background_callback.h | 15 ++++++++++++++- supervisor/shared/background_callback.c | 20 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index ff7e5dc6f3e..0cea7ca01dc 100644 --- a/main.c +++ b/main.c @@ -207,6 +207,7 @@ static void start_mp(safe_mode_t safe_mode) { _heap = _allocate_memory(safe_mode, "CIRCUITPY_HEAP_START_SIZE", CIRCUITPY_HEAP_START_SIZE, &heap_size); gc_init(_heap, _heap + heap_size); #endif + background_callback_heap_ready(); mp_init(); mp_obj_list_init((mp_obj_list_t *)mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) diff --git a/py/gc.c b/py/gc.c index 7f741c848b5..27fc735f467 100644 --- a/py/gc.c +++ b/py/gc.c @@ -542,7 +542,10 @@ bool gc_is_locked(void) { } // CIRCUITPY-CHANGE: additional function -bool gc_ptr_on_heap(const void *ptr) { +// In ITCM (RAM, on chips that have it) because background_callback_add_core() can call +// this from an interrupt that fires during a flash write. Code in flash can't run during +// a flash write. +bool PLACE_IN_ITCM(gc_ptr_on_heap)(const void *ptr) { for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) { if (ptr >= (void *)area->gc_pool_start // must be above start of pool && ptr < (void *)area->gc_pool_end) { // must be below end of pool diff --git a/supervisor/background_callback.h b/supervisor/background_callback.h index b26a683ad20..8fd22571a21 100644 --- a/supervisor/background_callback.h +++ b/supervisor/background_callback.h @@ -61,9 +61,22 @@ void background_callback_run_all(void); * interrupts are disabled. */ bool background_callback_pending(void); -/* During soft reset, remove all pending callbacks and clear the critical section flag */ +/* During soft reset, remove pending callbacks that are on the GC heap or whose data is on + * the GC heap, and cancel any background_callback_prevent() calls. + * + * Until background_callback_heap_ready() is called, background_callback_add() also drops + * any new callback that is on the heap or whose data is on the heap. Hardware owned by a + * heap object can keep interrupting until the object's finaliser stops it. A callback + * queued during teardown would still be pending after the heap is freed. + */ void background_callback_reset(void); +/* Let background_callback_add() queue callbacks on the GC heap again, after + * background_callback_reset() stopped it. Call once gc_init() has set up the new heap. + * Never fails. + */ +void background_callback_heap_ready(void); + /* Sometimes background callbacks must be blocked. Use these functions to * bracket the section of code where this is the case. These calls nest, and * begins must be balanced with ends. diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c index 65a0bcbf4c0..430be3d8a31 100644 --- a/supervisor/shared/background_callback.c +++ b/supervisor/shared/background_callback.c @@ -16,6 +16,11 @@ static volatile background_callback_t *volatile callback_head, *volatile callback_tail; +// True from background_callback_reset() until background_callback_heap_ready(), while +// the VM's heap is torn down. While it is set, background_callback_add_core() drops any +// callback that is on the heap or whose data is on the heap. +static bool reject_heap_callbacks; + #ifndef CALLBACK_CRITICAL_BEGIN #define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts()) #endif @@ -32,6 +37,14 @@ void PLACE_IN_ITCM(background_callback_add_core)(background_callback_t * cb) { CALLBACK_CRITICAL_END; return; } + // During heap teardown, hardware owned by a heap object (such as an I2SOut) keeps + // interrupting until the object's finaliser stops it. A callback queued during teardown + // would still be pending after the heap is freed. Drop any callback that is on the heap + // or whose data is on the heap. + if (reject_heap_callbacks && (gc_ptr_on_heap(cb) || gc_ptr_on_heap(cb->data))) { + CALLBACK_CRITICAL_END; + return; + } cb->next = 0; cb->prev = (background_callback_t *)callback_tail; if (callback_tail) { @@ -142,6 +155,13 @@ void background_callback_reset(void) { callback_head = new_head; callback_tail = new_tail; background_prevention_count = 0; + reject_heap_callbacks = true; + CALLBACK_CRITICAL_END; +} + +void background_callback_heap_ready(void) { + CALLBACK_CRITICAL_BEGIN; + reject_heap_callbacks = false; CALLBACK_CRITICAL_END; } From 3adad973b6f904b18e29bff9beedbe6859917895 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 20 Nov 2026 21:32:36 -0500 Subject: [PATCH 2/2] atmel-samd: allow turning off PDMIn; turn it off on two boards Guard common-hal/audiobusio/PDMIn.c and the EVSYS handler's call into it with CIRCUITPY_AUDIOBUSIO_PDMIN, as I2SOut.c is guarded with CIRCUITPY_AUDIOBUSIO_I2SOUT. metro_m0_express and sparkfun_redboard_turbo de_DE builds overflowed flash by 28 bytes. Turning off PDMIn leaves about 2.6 KB free. audiobusio.I2SOut is still available. Co-Authored-By: Claude Opus 5.5 --- ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk | 1 + .../boards/sparkfun_redboard_turbo/mpconfigboard.mk | 1 + ports/atmel-samd/common-hal/audiobusio/PDMIn.c | 4 ++++ ports/atmel-samd/supervisor/port.c | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk index d4801359cf8..85bac53dc76 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk @@ -10,6 +10,7 @@ SPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICES = "S25FL216K, GD25Q16C, W25Q16JVxQ" LONGINT_IMPL = MPZ +CIRCUITPY_AUDIOBUSIO_PDMIN = 0 CIRCUITPY_CODEOP = 0 CIRCUITPY_ERRNO = 0 CIRCUITPY_RAINBOWIO = 0 diff --git a/ports/atmel-samd/boards/sparkfun_redboard_turbo/mpconfigboard.mk b/ports/atmel-samd/boards/sparkfun_redboard_turbo/mpconfigboard.mk index b34dfb3d2e6..3b5e1c49108 100755 --- a/ports/atmel-samd/boards/sparkfun_redboard_turbo/mpconfigboard.mk +++ b/ports/atmel-samd/boards/sparkfun_redboard_turbo/mpconfigboard.mk @@ -10,6 +10,7 @@ SPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICES = "W25Q32FV" LONGINT_IMPL = MPZ +CIRCUITPY_AUDIOBUSIO_PDMIN = 0 CIRCUITPY_CODEOP = 0 CIRCUITPY_ERRNO = 0 CIRCUITPY_RAINBOWIO = 0 diff --git a/ports/atmel-samd/common-hal/audiobusio/PDMIn.c b/ports/atmel-samd/common-hal/audiobusio/PDMIn.c index 09452f70a8f..1fd055938ee 100644 --- a/ports/atmel-samd/common-hal/audiobusio/PDMIn.c +++ b/ports/atmel-samd/common-hal/audiobusio/PDMIn.c @@ -4,6 +4,8 @@ // // SPDX-License-Identifier: MIT +#if CIRCUITPY_AUDIOBUSIO_PDMIN + #include #include #include @@ -492,3 +494,5 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t *se return values_output; } + +#endif // CIRCUITPY_AUDIOBUSIO_PDMIN diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 2aeee5f57f7..b0c3175ff40 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -526,7 +526,7 @@ static void evsyshandler_common(void) { audio_dma_evsys_handler(); #endif - #if CIRCUITPY_AUDIOBUSIO + #if CIRCUITPY_AUDIOBUSIO_PDMIN pdmin_evsys_handler(); #endif }