This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 695d40366da serial/pl011: fix pl011_putc SMP race on IMSC causing RX 
input loss
695d40366da is described below

commit 695d40366dabf7f239c297fdbe57b2473c90cab3
Author: guanyi3 <[email protected]>
AuthorDate: Thu Feb 12 17:51:09 2026 +0800

    serial/pl011: fix pl011_putc SMP race on IMSC causing RX input loss
    
    The original pl011_putc() busy-waits for the TX FIFO to drain
    without any lock protection.  On SMP this allows two CPUs to race
    past the TXFF check simultaneously and write DR into a full FIFO,
    losing characters.
    
    More critically, because pl011_putc holds no lock while other paths
    (pl011_txint, pl011_rxint) do hold sport->lock when they
    read-modify-write the IMSC register, the unprotected pl011_putc
    creates a window where concurrent IMSC updates from the interrupt
    call-back chain can clobber each other, resulting in IMSC being
    zeroed.  Once IMSC == 0 the UART stops generating interrupts and
    RX input is lost while TX (polled via putc) continues to work.
    
    Fix by using a try-lock loop in pl011_putc: take sport->lock,
    check TXFF, if not full write DR and release; if full release
    immediately and retry.  This ensures the TXFF check and DR write
    are atomic while never busy-waiting with the lock held, so the
    interrupt path and other CPUs can always make progress.
    
    Signed-off-by: guanyi3 <[email protected]>
    
    Cherry-picked from open-vela/nuttx commit 
7a7f18cf1fa7bd58e68f413f7ae168b43a97c527
    
(https://github.com/open-vela/nuttx/commit/7a7f18cf1fa7bd58e68f413f7ae168b43a97c527).
    
    Signed-off-by: raiden00pl <[email protected]>
---
 drivers/serial/uart_pl011.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/drivers/serial/uart_pl011.c b/drivers/serial/uart_pl011.c
index d5e47cd16e6..24b3c9e64b2 100644
--- a/drivers/serial/uart_pl011.c
+++ b/drivers/serial/uart_pl011.c
@@ -675,13 +675,27 @@ static void pl011_send(FAR struct uart_dev_s *dev, int ch)
 
 static void pl011_putc(struct uart_dev_s *dev, int ch)
 {
-  FAR struct pl011_uart_port_s *sport = dev->priv;
+  FAR struct pl011_uart_port_s  *sport  = dev->priv;
+  FAR const struct pl011_config *config = &sport->config;
   irqstate_t flags;
 
-  flags = spin_lock_irqsave_notrace(&sport->lock);
-  while (!pl011_txempty(dev));
-  pl011_send(dev, ch);
-  spin_unlock_irqrestore_notrace(&sport->lock, flags);
+  for (; ; )
+    {
+      flags = spin_lock_irqsave_notrace(&sport->lock);
+
+      if (!(config->uart->fr & PL011_FR_TXFF))
+        {
+          config->uart->dr = ch;
+          spin_unlock_irqrestore_notrace(&sport->lock, flags);
+          break;
+        }
+
+      /* TX FIFO full – release the lock so the interrupt path and
+       * other CPUs can make progress, then retry.
+       */
+
+      spin_unlock_irqrestore_notrace(&sport->lock, flags);
+    }
 }
 
 /***************************************************************************
@@ -719,6 +733,7 @@ static bool pl011_rxavailable(FAR struct uart_dev_s *dev)
 static void pl011_rxint(FAR struct uart_dev_s *dev, bool enable)
 {
   FAR struct pl011_uart_port_s *sport = dev->priv;
+  irqstate_t flags = spin_lock_irqsave(&sport->lock);
 
   if (enable)
     {
@@ -728,6 +743,8 @@ static void pl011_rxint(FAR struct uart_dev_s *dev, bool 
enable)
     {
       pl011_irq_rx_disable(sport);
     }
+
+  spin_unlock_irqrestore(&sport->lock, flags);
 }
 
 /***************************************************************************

Reply via email to