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

Open
#4,620 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
28/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
embedded-iot

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
7.3k
Forks
1.6k
Avg merge
1d 19h
Merged PRs (30d)
67

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hathach/tinyusb

All issues in hathach/tinyusb

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.