CV-Bowen opened a new pull request, #22361:
URL: https://github.com/apache/nuttx/pull/22361

   ## Summary
   
   Locking fixes for the rpmsg uart (`drivers/serial/uart_rpmsg.c`), in
   the order they were made:
   
   - `store received data in a critical section`: `uart_read()` holds
     `dev->recv.lock` while it echoes a character, and the echo waits for
     the peer to acknowledge the previous write, which the same rpmsg work
     delivers; the endpoint callback blocked on `recv.lock`:
   
     ```
     nsh: uart_read()                 rpmsg work: uart_rpmsg_ept_cb()
       holds dev->recv.lock             nxmutex_lock(&dev->recv.lock)
       uart_putxmitchar()                 blocks
         waits for the write ack  <---  never delivered
     ```
   - `take the uart spinlock instead of a critical section`: the uart_rpmsg
     part of 6b1092ae312 ("drivers/serial: using small lock to replease
     enter/leave_critical_section"), which master took without it.
   - `track the flow control state with an atomic`: with the spinlock,
     `uart_rpmsg_rxflowcontrol()` (reached from the receive path when the
     rx buffer is full) must not take the `priv->lock` mutex.
   - `release priv->lock while waiting for a tx buffer`:
     `rpmsg_get_tx_payload_buffer()` dispatches rx work on the waiting
     thread, and the callback took `priv->lock` again:
   
     ```
     #2  nxmutex_lock (mutex.h:280)
     #3  uart_rpmsg_ept_cb (uart_rpmsg.c:378)
     #9  rpmsg_tickwait (rpmsg.c:423)
     #12 rpmsg_get_tx_payload_buffer (rpmsg.c:218)
     #13 uart_rpmsg_dmasend (uart_rpmsg.c:192)
     #15 uart_rpmsg_dmatxavail (uart_rpmsg.c:275)
     ```
   - `end the dma transfer when the send fails`: a failed
     `rpmsg_send_nocopy()` left `dmatx.length` set, and no transfer was
     started again.
   - `include/nuttx/spinlock.h: use the count in the stub
     rspin_restorelock()`: without `CONFIG_SPINLOCK` the stub dropped its
     argument, so the next commit warned (`-Wunused-but-set-variable`) on
     sim.
   - `serialize the tx path with the uart spinlock`: serial.c calls
     `uart_rpmsg_dmatxavail()` with the uart spinlock held, and it took the
     `priv->lock` mutex, which may sleep with the spinlock held; on SMP
     another CPU then spins on `dev->lock` forever. `priv->lock` is removed:
     the tx path uses the recursive `dev->lock`, released with
     `rspin_breaklock()` around the wait for a tx buffer.
   
   ## Impact
   
   - `drivers/serial/uart_rpmsg.c` only, plus the `CONFIG_SPINLOCK=n` stub
     of `rspin_restorelock()`. No API, Kconfig or wire format change.
   
   ## Testing
   
   Host: Ubuntu 22.04.5 LTS x86_64, QEMU version 10.2.1, sim: cc (Ubuntu 
13.1.0-8ubuntu1~22.04) 13.1.0, qemu-armv8a: aarch64-none-elf-gcc (Arm GNU 
Toolchain 12.2.Rel1 (Build arm-12.24)) 12.2.1 20221205
   
   Every commit builds sim:rpserver, sim:rpproxy, qemu-armv8a:rpserver and
   qemu-armv8a:rpproxy with 0 warnings (sim:rpserver/rpproxy enable
   `RPMSG_UART`: the proxy's nsh is reached as `/dev/ttyproxy` on the
   server).
   
   Stress on sim:rpserver + sim:rpproxy, through `cu /dev/ttyproxy`: 15
   rounds of `ls -l /proc` and `cat /proc/meminfo` on the proxy, each
   followed by `rpmsg ping all 5 512 1 0` on the server: 45/45 pass, no
   assertion.
   
   Not reproduced here: the SMP deadlock (no in-tree SMP configuration with
   `RPMSG_UART`) and the failed `rpmsg_send_nocopy()`.
   
   No regression in rpmsg: sim:rpserver/rpproxy and 
qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, 
rpmsgfs; 15/15; the sim proxy cases run over `/dev/ttyproxy`). qemu-armv8a was 
built with LIBC_ENVPATH and BINFMT_CHECKEXEC disabled, which current master 
needs for nxinit to start nsh there; unrelated to this change.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to