From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793259471; b=PwQmIrMsham0HWZtoyUrPXBFuUju0W3fgUo64ePXANFJETE2hZX26Iipg93tYuS2Bwpr Dpfs/FSlSGdUauuH2VCE3xIPdgM7FRJdl6EsLAvz0CYWVZmla6n786LIluncowCIX8K2j d26I9U94Zn3/gfpb0LPeB6kiJV18eF58b20y9FVukwwFpmWR75O8iPN5zGFLXmIEL/vFp sNFakxS6IynzjmOlk4104TnQeIk52vORG/wOO6vNoBQR83cr2IOFKoiaAvBZNmNC48dny G68g7RzmO4zNEuBU+P2SF106nTIXUUQJK9lTvGfKsMPvMaWumS5ZDVz+itRcLLOC4tA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793259471; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: References:In-Reply-To:To:Cc:X-Mailer; bh=tekWrSBddWH/mxS51gI4XCBrQci+CpUsgVszjFogEtc=; b=nrVuzO1Bf1p7vIlVX7AlTfW3EgOGRDmBVqJSWjmXLSuYUIItHekZJjAavu+V+Ojig0CX lUMkywvGhptdvAdEG2JPBzAB2uMVjRfNE1crT0Tc1PguY41CSHVMFbHyMTnBCM03k3655 6/3XJQ1nQ3mytP+cKa6mz+6MxdLdmyu22RZAZiCtRzjXh43+7s4epbUF2rjp1y0Kl9t19 no6VOnoIJ/EisJNPJ8SHfzFs1SNUN4jH4N/g8G0Hd3ko5/5lui3fiEWrEWaKyOXM6/x/W Si/MERwd+W/qFGtYZn68mrldd6b1I+fkzgzhHocyT5a2HSpiC5m0zB27LpeeOm6Q5xA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=LSxTJi8i; arc=none smtp.remote-ip=209.85.167.182 Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 581A860A260C for ; Thu, 29 Oct 2026 07:37:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 581A860A260C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=LSxTJi8i Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-4ed6d69bdfaso1178595b6e.3 for ; Thu, 29 Oct 2026 00:37:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793259469; x=1793864269; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tekWrSBddWH/mxS51gI4XCBrQci+CpUsgVszjFogEtc=; b=LSxTJi8iaturqWg3yVav2ryaWJvumb1EmPA7vyhec48Ci/BOBaynH5GRoVZhCVkldQ p6xhBbTabAPe/Q/xjqnBkYB1CsAyqFcTDyQqRvtnrTi1/XVlGv/O6FMQbFUPbPOdjiFE jNAcDnWJpmVKoebJuP/6xjuLmVXFFvfj5PHIWvZ0mtO4MlGr44Q7cSMnSg1MuS0fsFJs 4OWn6dyZ89BccwgT2q+V9jRExjDMGhS+SQDMzWJbudV798wLTv3VtRvMJgNO+2szCa6F 5aeWTMxGiqzx8aOpx08527n5WuXswMybU73E4Xvmk969cuvpejD+AsNSqU2rS22rTvQz 4aMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793259469; x=1793864269; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=tekWrSBddWH/mxS51gI4XCBrQci+CpUsgVszjFogEtc=; b=XBLt1hXeY3XOCn4o5T9JQ06nuxaoOvYLasi4GyQe2hoed7JcUld3bx90spIxuhUOnB ps51fUbnMWKnQZUzni9KKRW40RxM9yMvYSo7yhoutlNRJTXWDmbNQgaznFki8ea24xme iIey7Z0XFlJR4xslMaZI8d0DlfeL7EbsO4XV/JgE25x1x/h70CoKF679H4AyLttXbaCI EDw9YAP28fjEG7HQql2K9kTIozVwoEHL4dO82n4CGfnhdk07EU54u16ij0+YRzLgQXm1 40+nVC2YCXfQ71jGGU7uL7JS6YwW8BRH2ACvyAKm76Wfg0jEsrsaHuWqVyrJ4V/7bPB7 btjQ== X-Gm-Message-State: AFq9FYLk3ecW3Te2FhKF+d+eAJeG0LV1pt25pAFMbVuY+PB7e0+VD6vL tIZ/qh4JKaHoBUgahNoSD6m3N9DnK+FL9IAZmsIJQOHCbavSZxt6EIShDnkpzD11 X-Gm-Gg: AYBFou2l30CZBHyH40msabC0UzUONaswWR+4EDmLC6H8amO0PfReP1eTczO1eTDA+6w APrf00Ymfy0h1z8ESo3TSkBNbjfFkD3GmZUD7OUhVwM5JxNKqtvvcZa4kVJGmjuI0WjUZIFujur dnTOT3/g6DLdpW0QKyyeeEXi+F+8lvT7LbTjXLwwNqF2rkBT8my/yXPfcf/BHkqPASTqagzCwr2 aXR8E5NDEltPOtCZwToH65lpP7PPhrEoNJIoIXK8cZ/9buOcJKKIeum5c2cgpa1rV9jzhUwQ2DK te1HC1nR9191H0s2vYMiGtF6kiQdKiZT6Fqh90dDso6i1HLOL5Vo4KmN2uWueK1wxwCScgHq57h 9t4ghtGLrLkbonmiAa/WJN18ZU4+BAxToR1Yh7XNYVlulzeQhJvYsZxa4X9/JhueBa7U7IITPgZ W/TTtQOAq3lYccuk510Scuu6PnGUyZPz459w+5BdDHTtRyLevwfFzk/u5JFMHmFcQIe58EuGkXZ KkZotDajJyHTwMmmpUOzx7V0TZoQ2STh9dYbSM25v09L7D4iy/ySkDHGR/+hn0SbMe/QZzWhVMs 26P7mJBaB0mQaPJCcJLf16eFoaRJX5RQTcuut/aCwP8uzSZfNAD6EUYKxjfJwdZcvOiawwfOHtk T7xLr+u6uhDT2V1ocKJ67LMV3+SvTedLwcFlVkvdfNOpyQbNBYWa/s03vBHjdM+5TDjJqFT/Or7 awgYB6p0k9Hgs4laf+BOOo+BldF6uO0zXR2LDUHgQpqEhu4UBsPXgD X-Received: by 2002:a05:6808:bc7:b0:55c:859:adc with SMTP id 5614622812f47-5638378ff28mr3065513b6e.55.1793259469207; Thu, 29 Oct 2026 00:37:49 -0700 (PDT) Received: from [127.0.1.1] (174-29-1-49.hlrn.qwest.net. [174.29.1.49]) by smtp.gmail.com with ESMTPSA id 5614622812f47-563c3638604sm3321976b6e.1.2026.10.29.00.37.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2026 00:37:48 -0700 (PDT) From: James Hilliard Date: Thu, 29 Oct 2026 01:36:59 -0600 Subject: [PATCH v8 3/6] serial: core: allow drivers to reserve flushed transmit storage X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261029-submit-serial-8250-dma-cancel-v1-v8-3-3fd56ba352ed@gmail.com> References: <20261029-submit-serial-8250-dma-cancel-v1-v8-0-3fd56ba352ed@gmail.com> In-Reply-To: <20261029-submit-serial-8250-dma-cancel-v1-v8-0-3fd56ba352ed@gmail.com> To: Greg Kroah-Hartman , Jiri Slaby , Raul E Rangel , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Andy Shevchenko , Heikki Krogerus , Tony Lindgren , Sebastian Andrzej Siewior , Richard Genoud , Nicolas Ferre , Alexandre Belloni , Claudiu Beznea , Jacky Huang , Shan-Chun Hung , =?utf-8?q?Pali_Roh=C3=A1r?= , Frank Li , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , "Maciej W. Rozycki" , Timur Tabi Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, linux-arm-kernel@lists.infradead.org, imx@lists.linux.dev, linuxppc-dev@lists.ozlabs.org, James Hilliard X-Mailer: b4 0.15.2 uart_flush_buffer() resets the transmit FIFO before calling the driver's flush callback. An asynchronous DMA transfer can still access that storage after the callback returns, while a new write can immediately reuse it. Add uart_xmit_block() and uart_xmit_unblock() so a driver can reserve the flushed storage until its transfer and completion callbacks have retired. Serialize the reservation with FIFO producers using the port lock. While the storage is reserved, write_room() reports no space and write() and put_char() accept no data. Defer write-ready notifications until the reservation is released, preserving one-shot TTY wakeups. Keep the flush wakeup under the port lock so it cannot race another reservation. Suppress ordinary TX starts while the FIFO storage is blocked. Preserve high-priority flow-control characters, which use separate storage. Releasing the reservation wakes writers through the existing TTY wakeup path. This preserves immediate logical flushing while applying write backpressure until the storage is reusable. Track port readiness separately from DMA ownership. Retirement releases the reservation even during startup or shutdown, while writes and wakeups stay gated until successful startup or resume. Reopening or restarting a port wakes deferred writers with either a new or retained FIFO. Restart queued output when reusing a FIFO after suspension. Keep its write-ready notification pending until the retained output drains below the normal wakeup threshold. Publish resume readiness under the same port lock as the TX restart, without clearing any reservation. A concurrent flush can then reserve a new transfer without resume releasing its storage prematurely. Assert the port-lock requirement at uart_write_wakeup(), alongside the reservation helpers, so callers cannot inspect readiness or release a one-shot notification concurrently with a reservation change. Signed-off-by: James Hilliard --- drivers/tty/serial/serial_core.c | 103 +++++++++++++++++++++++++++++++++++---- include/linux/serial_core.h | 18 ++++++- 2 files changed, 110 insertions(+), 11 deletions(-) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index c0deec0e6af4..11bd1260cdc6 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -100,6 +100,11 @@ static inline struct uart_port *uart_port_check(struct uart_state *state) return state->uart_port; } +static bool uart_xmit_writable(const struct uart_state *state) +{ + return state->xmit_ready && !state->xmit_blocked; +} + /** * uart_write_wakeup - schedule write processing * @port: port to be processed @@ -114,15 +119,79 @@ static inline struct uart_port *uart_port_check(struct uart_state *state) void uart_write_wakeup(struct uart_port *port) { struct uart_state *state = port->state; + + lockdep_assert_held_once(&port->lock); + /* * This means you called this function _after_ the port was * closed. No cookie for you. */ BUG_ON(!state); + if (!uart_xmit_writable(state)) + return; tty_port_tty_wakeup(&state->port); } EXPORT_SYMBOL(uart_write_wakeup); +/* Publish readiness without releasing any transfer's storage reservation. */ +static void uart_xmit_enable(struct uart_port *port) +{ + struct uart_state *state = port->state; + + lockdep_assert_held_once(&port->lock); + if (state->xmit_ready) + return; + state->xmit_ready = true; + /* Retained output must drain before consuming a write-ready notification. */ + if (kfifo_len(&state->port.xmit_fifo) < WAKEUP_CHARS) + uart_write_wakeup(port); +} + +/** + * uart_xmit_block - reserve flushed transmit storage + * @port: port whose transmit storage is still in use + * + * A driver may call this from ->flush_buffer() if DMA still owns the old + * transmit data. The logical FIFO has already been emptied, but writers + * must not overwrite its storage until uart_xmit_unblock() is called. + * write_room() reports zero and write()/put_char() accept no data meanwhile. + * Write wakeups are deferred until the reservation is released. + * + * The driver must unblock after retiring the transfer, including on shutdown. + * Serial core separately gates writes and wakeups until startup or resume + * has made the port ready. + * Repeated calls are idempotent, not nested reservations. + * + * Context: @port->lock held. + */ +void uart_xmit_block(struct uart_port *port) +{ + lockdep_assert_held_once(&port->lock); + port->state->xmit_blocked = true; +} +EXPORT_SYMBOL_GPL(uart_xmit_block); + +/** + * uart_xmit_unblock - release reserved transmit storage and wake writers + * @port: port whose storage is available again + * + * The old transfer must no longer access the FIFO storage, and any required + * DMA synchronization must have completed before this call. + * Retirement is recorded even while the port is inactive. Writes and wakeups + * remain deferred until startup or resume has made the port ready. + * + * Context: @port->lock held. + */ +void uart_xmit_unblock(struct uart_port *port) +{ + lockdep_assert_held_once(&port->lock); + if (!port->state->xmit_blocked) + return; + port->state->xmit_blocked = false; + uart_write_wakeup(port); +} +EXPORT_SYMBOL_GPL(uart_xmit_unblock); + static void uart_stop(struct tty_struct *tty) { struct uart_state *state = tty->driver_data; @@ -141,7 +210,10 @@ static void __uart_start(struct uart_state *state) struct serial_port_device *port_dev; int err; - if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port)) + if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port) || !state->xmit_ready) + return; + /* High-priority flow-control characters have their own storage. */ + if (state->xmit_blocked && !port->x_char) return; port_dev = port->port_dev; @@ -375,6 +447,11 @@ static int uart_startup(struct tty_struct *tty, struct uart_state *state, return -EIO; serial_base_port_startup(uport); + scoped_guard(uart_port_lock_irq, uport) { + uart_xmit_enable(uport); + if (!kfifo_is_empty(&port->xmit_fifo) || uport->x_char) + __uart_start(state); + } return 0; } @@ -397,8 +474,11 @@ static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) if (tty) set_bit(TTY_IO_ERROR, &tty->flags); - if (uport) + if (uport) { + scoped_guard(uart_port_lock_irq, uport) + state->xmit_ready = false; serial_base_port_shutdown(uport); + } if (tty_port_initialized(port)) { tty_port_set_initialized(port, false); @@ -593,7 +673,7 @@ static int uart_put_char(struct tty_struct *tty, u8 c) return 0; } - if (port) + if (port && uart_xmit_writable(state)) ret = kfifo_put(&state->port.xmit_fifo, c); uart_port_unlock_deref(port, flags); return ret; @@ -624,7 +704,7 @@ static ssize_t uart_write(struct tty_struct *tty, const u8 *buf, size_t count) return 0; } - if (port) + if (port && uart_xmit_writable(state)) ret = kfifo_in(&state->port.xmit_fifo, buf, count); __uart_start(state); @@ -640,7 +720,7 @@ static unsigned int uart_write_room(struct tty_struct *tty) unsigned int ret; port = uart_port_ref_lock(state, &flags); - if (!state->port.xmit_buf) + if (!state->port.xmit_buf || !uart_xmit_writable(state)) ret = 0; else ret = kfifo_avail(&state->port.xmit_fifo); @@ -682,8 +762,9 @@ static void uart_flush_buffer(struct tty_struct *tty) kfifo_reset(&state->port.xmit_fifo); if (port->ops->flush_buffer) port->ops->flush_buffer(port); + /* Serialize the wakeup with reservation changes, including another flush. */ + uart_write_wakeup(port); uart_port_unlock_deref(port, flags); - tty_port_tty_wakeup(&state->port); } /* @@ -1748,8 +1829,10 @@ static void uart_tty_port_shutdown(struct tty_port *port) if (WARN(!uport, "detached port still initialized!\n")) return; - scoped_guard(uart_port_lock_irq, uport) + scoped_guard(uart_port_lock_irq, uport) { + state->xmit_ready = false; uport->ops->stop_rx(uport); + } serial_base_port_shutdown(uport); uart_port_shutdown(port); @@ -2391,9 +2474,10 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) unsigned int mctrl; tty_port_set_suspended(port, true); - tty_port_set_initialized(port, false); scoped_guard(uart_port_lock_irq, uport) { + state->xmit_ready = false; + tty_port_set_initialized(port, false); ops->stop_tx(uport); if (!(uport->rs485.flags & SER_RS485_ENABLED)) ops->set_mctrl(uport, 0); @@ -2498,8 +2582,9 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) if (!(uport->rs485.flags & SER_RS485_ENABLED)) ops->set_mctrl(uport, uport->mctrl); ops->start_tx(uport); + tty_port_set_initialized(port, true); + uart_xmit_enable(uport); } - tty_port_set_initialized(port, true); } else { /* * Failed to resume - maybe hardware went away? diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 4eeb8253752a..41598a38a408 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -208,8 +208,17 @@ struct gpio_desc; * Flush any write buffers, reset any DMA state and stop any ongoing DMA * transfers. * - * This will be called whenever the @port->state->xmit circular buffer is - * cleared. + * This is called after the transmit FIFO has been cleared, with @port->lock + * still held. A driver whose asynchronous transfer can still access the + * flushed storage must call uart_xmit_block() before returning. This keeps + * write_room() at zero and prevents write() and put_char() from reusing the + * storage until the driver calls uart_xmit_unblock(). + * + * Release the reservation only after the transfer and its callbacks have + * retired and any required DMA synchronization has completed. Shutdown must + * also retire the transfer and release the reservation before returning, + * since serial core then frees the transmit buffer. Serial core separately + * gates writes and wakeups until startup or resume has made the port ready. * * Locking: @port->lock taken. * Interrupts: locally disabled. @@ -834,6 +843,9 @@ enum uart_pm_state { */ struct uart_state { struct tty_port port; + /* TX readiness and storage ownership; protected by the UART port lock. */ + bool xmit_ready; + bool xmit_blocked; enum uart_pm_state pm_state; @@ -909,6 +921,8 @@ struct uart_driver { }; void uart_write_wakeup(struct uart_port *port); +void uart_xmit_block(struct uart_port *port); +void uart_xmit_unblock(struct uart_port *port); /** * enum UART_TX_FLAGS -- flags for uart_port_tx_flags() -- 2.53.0