max3421 (ESP): the task-context interrupt handler can take mutexes hcd_deinit() deleted

Aperta
#4,620 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
28/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
embedded-iot

Direzione di ricerca

Start by reading hcd_deinit() in src/portable/analog/max3421/hcd_max3421.c:571-589 alongside max3421_intr_task() in hw/bsp/espressif/boards/family.c:230-237, then trace where max3421_intr_sem is given and where the GPIO interrupt is disabled. The fix either stops and drains the task before the mutexes are deleted, or has the handler check a cleared running flag before locking; note osal_mutex_lock under FreeRTOS is xSemaphoreTake in src/osal/osal_freertos.h:224-226. Done means tuh_deinit() with a device mounted no longer reaches a NULL mutex, validated on a dual-core ESP32 with a MAX3421 attached.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

follow-up Prio 📌

Found while reviewing https://github.com/hathach/tinyusb/pull/4619 at 9b6be7461; outside that PR's scope. Source
trace by review, not reproduced on hardware.

Problem

hcd_deinit() (src/portable/analog/max3421/hcd_max3421.c:571-589) deletes spi_mutex and handler_mutex and sets
both to NULL. On Espressif the MAX3421 interrupt is serviced from a task: max3421_intr_task()
(hw/bsp/espressif/boards/family.c:230-237) loops on max3421_intr_sem and calls
tuh_int_handler(BOARD_TUH_RHPORT, false). Nothing stops or drains that task before the mutexes are deleted.

Two ways the handler runs after deletion:

  • the semaphore was given before tuh_max3421_int_api(rhport, false) disabled the GPIO interrupt, so the task wakes
    after hcd_deinit() returned;
  • on a dual-core ESP32 the task runs on the other core while hcd_deinit() executes. With a device attached, FRAME
    IRQs fire every 1 ms, so the window is not narrow.

The handler then calls osal_mutex_lock(NULL, ...), which is xSemaphoreTake(NULL) (src/osal/osal_freertos.h:224-226):
an assert or crash during teardown.

handler_mutex was added by #4619; spi_mutex is deleted the same way on master 1384420308, so the hazard predates
it.

Not affected

  • usbh itself makes no HCD transfer call after hcd_deinit(): usbh_task_run() returns when !tuh_inited()
    (src/host/usbh.c:869-871).
  • No in-tree example calls tuh_deinit() on a MAX3421 build, so nothing in the tree triggers it today.

Severity: low.

Related: #4489 A3 — tuh_deinit() order of hcd_deinit() against the device-tree teardown; that is the usbh side, this
one is the ESP BSP's handler task.

Remaining work

In the ESP BSP, stop and drain max3421_intr_task before hcd_deinit() deletes the mutexes, or have the handler bail
out on a cleared running flag before locking. Validate on an ESP32 dual-core board with MAX3421 attached by calling
tuh_deinit() with a device mounted.

Why deferred

Lifecycle of a BSP task with no in-tree caller of tuh_deinit() on this configuration; it belongs in its own change
with hardware validation, not in #4619's device-removal handling.

Lingua principale
C
Stelle
7.3k
Fork
1.6k
Merge medio
1g 19h
PR unite (30g)
67

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di hathach/tinyusb

Tutte le issue di hathach/tinyusb

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.