zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
11k stars 6.69k forks source link

drivers: usb: mcux: ip3511: udc_mcux_handler_setup() locks mutex in ISR context #77985

Closed henrikbrixandersen closed 2 months ago

henrikbrixandersen commented 3 months ago

Describe the bug While developing downstream firmware, I have encountered an issue with the NXP MCUX IP3511 USB device driver (drivers/usb/udc/udc_mcux_ip3511.c) attempting to lock a mutex in ISR context.

To Reproduce Steps to reproduce the behavior:

  1. west build -b lpcxpresso55s16/lpc55s16 samples/subsys/usb/hid-keyboard/ -- -DCONFIG_ASSERT=y
  2. west flash
  3. See errors below

Expected behavior Drivers should not attempt to lock mutexes in ISR context.

Impact Unsupported behaviour.

Logs and console output Console output:

*** Booting Zephyr OS build v3.7.0-2094-gca48767be48c ***
[00:00:00.000,000] <inf> main: HID keyboard sample is initialized
[00:00:00.205,000] <inf> main: USBD message: Bus reset
ASSERTION FAIL [!arch_is_in_isr()] @ WEST_TOPDIR/zephyr/kernel/mutex.c:111
    mutexes cannot be used inside ISRs
[00:00:00.247,000] <err> os: r0/a1:  0x00000004  r1/a2:  0x0000006f  r2/a3:  0x00000003
[00:00:00.247,000] <err> os: r3/a4:  0x00000004 r12/ip:  0x000000f7 r14/lr:  0x0000909b
[00:00:00.247,000] <err> os:  xpsr:  0x0100003f
[00:00:00.247,000] <err> os: Faulting instruction address (r15/pc): 0x0000c392
[00:00:00.247,000] <err> os: >>> ZEPHYR FATAL ERROR 4: Kernel panic on CPU 0
[00:00:00.247,000] <err> os: Fault during interrupt handling

[00:00:00.247,000] <err> os: Current thread: 0x20000b90 (unknown)
[00:00:00.313,000] <err> os: Halting system

Failing instruction is in assert.c line 44:

$ addr2line -e build/zephyr/zephyr.elf 0x0000c392
/home/brix/Projects/zephyrproject/zephyr/lib/os/assert.c:44

GDB backtrace:

(gdb) break assert.c:44
Breakpoint 1 at 0xc394: file /home/brix/Projects/zephyrproject/zephyr/lib/os/assert.c, line 48.
(gdb) cont
Continuing.

Breakpoint 1, assert_print (fmt=0x20000a10 <usbd_thread_data> "\034\004") at /home/brix/Projects/zephyrproject/zephyr/lib/os/assert.c:51
51      va_start(ap, fmt);
(gdb) bt
#0  assert_print (fmt=0x20000a10 <usbd_thread_data> "\034\004") at /home/brix/Projects/zephyrproject/zephyr/lib/os/assert.c:51
#1  0x0000908c in z_impl_k_mutex_lock (mutex=0x2000020c <udc_data_0+132>, timeout=...) at /home/brix/Projects/zephyrproject/zephyr/kernel/mutex.c:111
#2  0x00006354 in udc_ep_buf_alloc (dev=dev@entry=0xf0d0 <__device_dts_ord_46>, ep=<optimized out>, size=size@entry=8) at /home/brix/Projects/zephyrproject/zephyr/drivers/usb/udc/udc_common.c:654
#3  0x0000d434 in udc_ctrl_alloc (dev=dev@entry=0xf0d0 <__device_dts_ord_46>, ep=ep@entry=0 '\000', size=size@entry=8) at /home/brix/Projects/zephyrproject/zephyr/drivers/usb/udc/udc_common.c:678
#4  0x000067c4 in udc_mcux_handler_setup (setup=0x200101c0 <s_SetupAndEpReservedData+64>, dev=0xf0d0 <__device_dts_ord_46>) at /home/brix/Projects/zephyrproject/zephyr/drivers/usb/udc/udc_mcux_ip3511.c:172
#5  USB_DeviceNotificationTrigger (handle=<optimized out>, msg=msg@entry=0x20002864 <z_interrupt_stacks+1988>) at /home/brix/Projects/zephyrproject/zephyr/drivers/usb/udc/udc_mcux_ip3511.c:466
#6  0x0000e988 in USB_DeviceLpc3511IpInterruptToken (errorStatus=<optimized out>, isSetup=1 '\001', endpointIndex=0 '\000', lpc3511IpState=0x2000127c <s_UsbDeviceLpc3511IpState+364>)
    at /home/brix/Projects/zephyrproject/modules/hal/nxp/mcux/middleware/mcux-sdk-middleware-usb/device/usb_device_lpcip3511.c:1422
#7  USB_DeviceLpcIp3511IsrFunction (deviceHandle=<optimized out>) at /home/brix/Projects/zephyrproject/modules/hal/nxp/mcux/middleware/mcux-sdk-middleware-usb/device/usb_device_lpcip3511.c:2832
#8  0x000031d2 in _isr_wrapper () at /home/brix/Projects/zephyrproject/zephyr/arch/arm/core/cortex_m/isr_wrapper.c:77
#9  <signal handler called>
#10 0x0000c85c in arch_cpu_idle () at /home/brix/Projects/zephyrproject/zephyr/arch/arm/core/cortex_m/cpu_idle.c:99
#11 0x00008d6c in k_cpu_idle () at /home/brix/Projects/zephyrproject/zephyr/include/zephyr/kernel.h:5998
#12 idle (unused1=<optimized out>, unused2=<optimized out>, unused3=<optimized out>) at /home/brix/Projects/zephyrproject/zephyr/kernel/idle.c:74
#13 0x000015f6 in z_thread_entry (entry=0x8d4d <idle>, p1=0x200013e8 <_kernel>, p2=0x0 <_vector_table>, p3=0x0 <_vector_table>) at /home/brix/Projects/zephyrproject/zephyr/lib/os/thread_entry.c:48
#14 0x00000000 in ?? ()

Environment (please complete the following information):

Additional context This should be caught by in-tree ztests (which automatically enable CONFIG_ASSERT=y) on the USB driver level.

henrikbrixandersen commented 3 months ago

CC: @jfischer-no, @tmon-nordic