From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796030113; b=zWPjHd+4404C97gMYQaa1qpC0f/ZF8VtdS9eNKQw+ivH+kurqjToUU3JkXfBswkzi4As rkvzGh/qIr/xmznqYa6ZZkPSUPRpwcUeflUR/+1ZC5sOSXIIqAeYy4d7tgkuV2E3HwBrD Zkd0Frn00gXJrFM0AbksmJb4YScNaWecM0xexMuTh8koFq3x5679CwCpcD9CUev3APoMe z5ygYmB3S4MHRfeckGTzEGpsG2pf0QCSpV+eKCtEVnwURjrt/AtjFFcpLG8TtJaQ2dOwO +RskZ8CatpkkHBPHJ64uqGsOm5k17+RX+8yipKl7EsnYq+ubIehvkBm69ai7ouRAyVg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796030113; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=kM2sn9ATh7w0wdaS7vxSX76oLWsyiiOS9UY/7eBfv/4=; b=hMvFw4R2f+KBzNhN9IMowd6n9z95CB7ntor2KKmuWt5Gge6HCPE8tiqT5OZPG9ojTQoy z5ALhtQNSujeA6+O+KxHeQB7sl4ZJXXTuaL8/KH8mj0sEoFT8hF26pPw9K4rEKZGCjKIw 6wWBaA/cXc08osKS20YzhxQFPatsSxgkRl6r5KsOvXdXTNlcFwwxDYqJpICup/BT/lrN2 gBVLD2WyCbpIafs2AUIDloPNSf37Kgw/Dkvxquwinz6LM5WUjZoTBotxRg2x87JF4rziA 0ozibzEfKj+NwxGvOA+sb9IvIgzE50drGhAw7F3ZiLELciDdhpzNxsgt0d2uOMwsZHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=octane.security; spf=pass smtp.mailfrom=octane.security; dkim=pass header.d=octane.security header.i=@octane.security header.a=rsa-sha256 header.s=google header.b=eMO6vibN; arc=none smtp.remote-ip=74.125.82.179 Received: from mail-dy1-f179.google.com (mail-dy1-f179.google.com [74.125.82.179]) (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 EA3B3606A32D for ; Mon, 30 Nov 2026 09:15:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EA3B3606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=octane.security Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=octane.security Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=octane.security header.i=@octane.security header.a=rsa-sha256 header.s=google header.b=eMO6vibN Received: by mail-dy1-f179.google.com with SMTP id 5a478bee46e88-38431138772so13710252eec.1 for ; Mon, 30 Nov 2026 01:15:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=octane.security; s=google; t=1796030112; x=1796634912; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=kM2sn9ATh7w0wdaS7vxSX76oLWsyiiOS9UY/7eBfv/4=; b=eMO6vibNAsZ8etmzi/NsZY+C9PcOJHgCcT/EX4wiSkeZ+7KQMJs8Vc8xr1bChqUa/a eNDH1dNbW43pKFHw31LCjDNrSm0IusENGSyIs3j1n6uVcsxevO5pO43HnRgjfcvbTqdB 3JeqLd51GcAvcYjD/yXPm2ZG5mS229PlLu1MbsolQzEq88fYejCm/ozBsgJNuTHh3tA0 Micxd74+IieNxnLnQ8FYZkohZzwPsRiyUY1WxW+NhUNasAoGilAW6scTI+Q8J8WZ5RT8 VCdoTkn38USXKFxwrZgkLNm1MCNx/E6HjMoQTYSXcBgCeBfKYpi9heDkY1n+SBi2aifX 3Hgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796030112; x=1796634912; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=kM2sn9ATh7w0wdaS7vxSX76oLWsyiiOS9UY/7eBfv/4=; b=hqS55sa8gIDtjD7y1JCfqHmBF6ohG3lZic6NiJFR2YkpW//rtLXQjlckHbnloLSbfq uDF1i7bTIZOpnsV5JAlNsSUsNRJg2LgefDiGdlywXh+WXs60jWswQy/GnZuzjEShSNfD GdcD29NBytGYq9I/WSOdhkqKjNIEzfmmy2bNHWMZ1/aUHMfEvhi6Zvu0mcXRP8Q5ExP/ EUB/x8P/reIFaOkSkTsZgwaR8kiMvIX6p6fOnkhUqHkqezokf6AThHETTsJvKBzH6PtS rX6CAas60eDhj9ORh7PpBYDizHlK9NWJ5B5B5LDpD5R0mSar3Nk5/Z2Zt+IqQDX4QlTB riMg== X-Gm-Message-State: APuU4Z9oubPcdjMoUVi7Qtd1HlHNqDnBZpY3w01epbNHwLBsAzf/aJ0D 17lJP2JzpgWZu6n8pdwfCSmPO6V0H5ETxlNSwTXyU3x32omAtPTPQttVaVL7HQ5g7GybS/T+e2K 6/FRh X-Gm-Gg: AZFdTZcEUACwWr0yb+np8UA25w/vxwaqhhpSFkyGRYTKts1G33T3qvkyShvf6fD5YbY qkSKTE6S22XoFNmn4RuP+waNma2ThtSn7fmC6Pr+JgXeu85RuNnNM8SJneZr8Nul3GdkAFS+o7x lgFdaDtiOmxkmp/sGup39f8SWGewPZ0oRYoJscfbFI5SnuXjhY7JKfJ3ptE7my24HZJUF/uwv4U YfiQl1XTycg47n0rkaIDpkjuK1ilPM33myISjixP7zmgNAqtjoCVbySq9/bI8xWmfBxVXwjxnSX Imk0zGA8xsTZXGWtZWDqx9HBn+dIRg3/7y9o6Kcwh2p7ydPPA0swIJq0O8ZtvRgyCw6C/AL4Mnr iHQnFVzM0b/K4emED6jAocQGSZpOwYIh9a+fYKY1mQGBqQvfNVKCxgc7SV9zei75UG1iQe3T2ly Bp6h5KszkoAOdf1XetSCTWOC0CBmj9pUB1BCqVN7nafN995Wti/Vd/nbH8reuJ+AyAf82Mn78nD YUt7L4HyqnZ6irM97hZpuE7PkVIGBNckhYRYzmxTKi6u0T7pdT+tygmx1hJGsMnojjR5Wy40vfb 3ufaNaKl8WqO9cykDX64Ae2Domf/Cfv0NjJKmBKfVB6X5YC/wTbUNvSu+ytKn1Y9EGXsvQZSZwY T0Dd6JouNBN+As6EyB2zcdCt5zmeJKEZLx4mTLbVlTpGwyxSXiQ== X-Received: by 2002:a05:693c:65c7:b0:386:cffd:bda6 with SMTP id 5a478bee46e88-386cffdd34fmr14160007eec.5.1796030111668; Mon, 30 Nov 2026 01:15:11 -0800 (PST) Received: from localhost.localdomain ([103.47.169.47]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-386ffc4ab65sm13869349eec.22.2026.11.30.01.15.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Nov 2026 01:15:11 -0800 (PST) From: Shubham Antil To: netdev@vger.kernel.org Cc: Simon Horman , gio@octane.security, David Heidelberg , oe-linux-nfc@lists.linux.dev Subject: [PATCH net v9 3/4] nfc: nci: uart: quiesce callbacks during teardown Date: Mon, 30 Nov 2026 14:44:55 +0530 Message-ID: <20261130091456.62379-4-shubham@octane.security> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261130091456.62379-1-shubham@octane.security> References: <20261130091456.62379-1-shubham@octane.security> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The UART writer can lose a wakeup while handing ownership back to a producer, and teardown has no state preventing new work after cancellation begins. A partial skb can also be owned only by the worker while another context observes the queue as empty. Protect transmit state, the queue, and tx_skb with one lock, preserve wakeups across partial writes, and balance each tx_start() with exactly one tx_done(). Queue admission and wakeup now share one critical section and use the unlocked skb queue helpers, avoiding repeated per-frame lock round trips. Add bounded flush, abort, receive-stop, and transmit-stop helpers so a driver can preserve complete frames before reset and block callbacks before releasing transport-private state. Wait until every byte of the preceding frame has been accepted by the ordered tty queue before adding the reset frame. Report a partial receive frame as a flush error so a close-time exchange cannot reuse its suffix. Reject new frames after quiescing starts and discard parser state outside active NCI states. Discovered during an AI-assisted source audit using OpenAI Codex, gpt-5.6-sol. The teardown issue was reproduced on a clean stock v7.3-rc1 kernel with 318 KASAN slab-use-after-free reports in nci_uart_write_work() after nci_uart_tty_close() freed tx_skb. Patch development and test planning used the same assistant. Confirmed with repeated pty-backed NCI UART open, transmit, reset, and hangup cycles. No physical NFC controller was available. Fixes: 9961127d4bce ("NFC: nci: add generic uart support") Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol) Signed-off-by: Shubham Antil --- include/net/nfc/nci_core.h | 11 ++ net/nfc/nci/uart.c | 366 ++++++++++++++++++++++++++++++++++--- 2 files changed, 350 insertions(+), 27 deletions(-) diff --git a/include/net/nfc/nci_core.h b/include/net/nfc/nci_core.h index 43b38aeab..6d6693042 100644 --- a/include/net/nfc/nci_core.h +++ b/include/net/nfc/nci_core.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -446,6 +447,7 @@ struct nci_uart; struct nci_uart_ops { int (*open)(struct nci_uart *nci_uart); + /* Quiesce callbacks before releasing transport-private state. */ void (*close)(struct nci_uart *nci_uart); int (*recv)(struct nci_uart *nci_uart, struct sk_buff *skb); int (*send)(struct nci_uart *nci_uart, struct sk_buff *skb); @@ -462,8 +464,11 @@ struct nci_uart { /* Dynamic data */ struct nci_dev *ndev; spinlock_t rx_lock; + spinlock_t tx_lock; /* protects tx state, skb, and queue */ struct work_struct write_work; + wait_queue_head_t tx_wait; struct tty_struct *tty; + unsigned long flags; unsigned long tx_state; struct sk_buff_head tx_q; struct sk_buff *tx_skb; @@ -475,5 +480,11 @@ struct nci_uart { int nci_uart_register(struct nci_uart *nu); void nci_uart_unregister(struct nci_uart *nu); void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl); +void nci_uart_set_ready(struct nci_uart *nu); +int nci_uart_rx_flush(struct nci_uart *nu); +void nci_uart_rx_stop(struct nci_uart *nu); +void nci_uart_tx_stop(struct nci_uart *nu); +void nci_uart_tx_abort(struct nci_uart *nu); +int nci_uart_tx_flush(struct nci_uart *nu); #endif /* __NCI_CORE_H */ diff --git a/net/nfc/nci/uart.c b/net/nfc/nci/uart.c index bc2016e62..2d9ee9484 100644 --- a/net/nfc/nci/uart.c +++ b/net/nfc/nci/uart.c @@ -34,76 +34,348 @@ /* TX states */ #define NCI_UART_SENDING 1 #define NCI_UART_TX_WAKEUP 2 +#define NCI_UART_CLOSING 3 +#define NCI_UART_TX_STARTED 4 + +/* UART flags */ +#define NCI_UART_READY 0 static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX]; static DEFINE_MUTEX(nci_uart_drivers_lock); static DEFINE_MUTEX(nci_uart_selection_lock); -static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu) +static struct sk_buff *nci_uart_dequeue_locked(struct nci_uart *nu) { struct sk_buff *skb = nu->tx_skb; if (!skb) - skb = skb_dequeue(&nu->tx_q); + skb = __skb_dequeue(&nu->tx_q); else nu->tx_skb = NULL; return skb; } -static inline int nci_uart_queue_empty(struct nci_uart *nu) +static bool nci_uart_queue_empty_locked(struct nci_uart *nu) { if (nu->tx_skb) - return 0; + return false; return skb_queue_empty(&nu->tx_q); } -static int nci_uart_tx_wakeup(struct nci_uart *nu) +static bool nci_uart_tx_idle(struct nci_uart *nu) +{ + unsigned long flags; + bool idle; + + spin_lock_irqsave(&nu->tx_lock, flags); + idle = nci_uart_queue_empty_locked(nu) && + !test_bit(NCI_UART_SENDING, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + + return idle; +} + +static void nci_uart_tx_wakeup_locked(struct nci_uart *nu) { - if (test_and_set_bit(NCI_UART_SENDING, &nu->tx_state)) { + if (test_bit(NCI_UART_CLOSING, &nu->tx_state) || + !test_bit_acquire(NCI_UART_READY, &nu->flags)) + return; + + if (test_bit(NCI_UART_SENDING, &nu->tx_state)) { set_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); - return 0; + return; } + if (nci_uart_queue_empty_locked(nu)) + return; + set_bit(NCI_UART_SENDING, &nu->tx_state); schedule_work(&nu->write_work); +} + +static int nci_uart_tx_wakeup(struct nci_uart *nu) +{ + unsigned long flags; + + spin_lock_irqsave(&nu->tx_lock, flags); + nci_uart_tx_wakeup_locked(nu); + spin_unlock_irqrestore(&nu->tx_lock, flags); + + return 0; +} + +/** + * nci_uart_tx_stop - stop UART transmit callbacks + * @nu: NCI UART transport + * + * Prevent new transmit work, wait for an active worker, and balance an + * outstanding tx_start() callback. This function may sleep and the caller + * must keep @nu->tty and transport-private state valid until it returns. + * Queued skbs are left for the caller to release and the closing state stays + * set until the transport instance is either freed or explicitly unwound. + */ +void nci_uart_tx_stop(struct nci_uart *nu) +{ + unsigned long flags; + bool tx_started; + + spin_lock_irqsave(&nu->tx_lock, flags); + set_bit(NCI_UART_CLOSING, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + + cancel_work_sync(&nu->write_work); + clear_bit(TTY_DO_WRITE_WAKEUP, &nu->tty->flags); + + spin_lock_irqsave(&nu->tx_lock, flags); + tx_started = test_and_clear_bit(NCI_UART_TX_STARTED, + &nu->tx_state); + clear_bit(NCI_UART_SENDING, &nu->tx_state); + clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + + if (tx_started && nu->ops.tx_done) + nu->ops.tx_done(nu); + wake_up_all(&nu->tx_wait); +} +EXPORT_SYMBOL_GPL(nci_uart_tx_stop); + +/** + * nci_uart_tx_flush - finish queued UART frames before a controller reset + * @nu: NCI UART transport + * + * Wait until the transport has handed every byte of each complete frame to + * the tty driver. Tty writes preserve byte ordering, so the reset frame can + * then be queued behind those bytes without waiting for the hardware FIFO to + * empty. The caller must first prevent producers from submitting new frames + * and must keep the tty valid. + * + * Return: 0 when every frame was accepted, or a negative error on timeout or + * after transport shutdown. + */ +int nci_uart_tx_flush(struct nci_uart *nu) +{ + long timeout = msecs_to_jiffies(NCI_RESET_TIMEOUT); + long retry = max_t(long, 1, msecs_to_jiffies(20)); + unsigned long deadline = jiffies + timeout; + unsigned long flags; + bool closing; + + spin_lock_irqsave(&nu->tx_lock, flags); + closing = test_bit(NCI_UART_CLOSING, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + if (closing) + return -ESHUTDOWN; + + for (;;) { + long remaining; + + nci_uart_tx_wakeup(nu); + if (nci_uart_tx_idle(nu)) + break; + remaining = deadline - jiffies; + if (remaining <= 0) + return -ETIMEDOUT; + + /* + * Line-discipline close holds ldisc_sem write-locked, so the tty + * cannot deliver write_wakeup. Retry a partial write here rather + * than depending on that callback to reschedule write_work. + */ + wait_event_timeout(nu->tx_wait, nci_uart_tx_idle(nu), + min(retry, remaining)); + } return 0; } +EXPORT_SYMBOL_GPL(nci_uart_tx_flush); + +/** + * nci_uart_tx_abort - discard a transport which cannot preserve framing + * @nu: NCI UART transport + * + * Stop the writer, flush the tty driver's output buffer, and release the + * unsent suffix and queued frames. Transmit remains stopped so a later NCI + * open cannot append bytes to the incomplete frame. Recovery requires the + * line discipline to be closed and selected again. + */ +void nci_uart_tx_abort(struct nci_uart *nu) +{ + struct sk_buff *skb; + unsigned long flags; + + nci_uart_tx_stop(nu); + tty_driver_flush_buffer(nu->tty); + + spin_lock_irqsave(&nu->tx_lock, flags); + skb = nu->tx_skb; + nu->tx_skb = NULL; + spin_unlock_irqrestore(&nu->tx_lock, flags); + kfree_skb(skb); + skb_queue_purge(&nu->tx_q); + wake_up_all(&nu->tx_wait); +} +EXPORT_SYMBOL_GPL(nci_uart_tx_abort); static void nci_uart_write_work(struct work_struct *work) { struct nci_uart *nu = container_of(work, struct nci_uart, write_work); struct tty_struct *tty = nu->tty; struct sk_buff *skb; + unsigned long flags; + bool tx_started; restart: + spin_lock_irqsave(&nu->tx_lock, flags); + if (test_bit(NCI_UART_CLOSING, &nu->tx_state)) { + tx_started = test_and_clear_bit(NCI_UART_TX_STARTED, + &nu->tx_state); + clear_bit(NCI_UART_SENDING, &nu->tx_state); + clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + if (tx_started && nu->ops.tx_done) + nu->ops.tx_done(nu); + wake_up_all(&nu->tx_wait); + return; + } clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); + tx_started = !test_and_set_bit(NCI_UART_TX_STARTED, + &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); - if (nu->ops.tx_start) + if (tx_started && nu->ops.tx_start) nu->ops.tx_start(nu); - while ((skb = nci_uart_dequeue(nu))) { + for (;;) { int len; + spin_lock_irqsave(&nu->tx_lock, flags); + skb = nci_uart_dequeue_locked(nu); + spin_unlock_irqrestore(&nu->tx_lock, flags); + if (!skb) + break; + set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); len = tty->ops->write(tty, skb->data, skb->len); skb_pull(skb, len); if (skb->len) { + spin_lock_irqsave(&nu->tx_lock, flags); nu->tx_skb = skb; + spin_unlock_irqrestore(&nu->tx_lock, flags); break; } kfree_skb(skb); } - if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state)) + spin_lock_irqsave(&nu->tx_lock, flags); + if (test_bit(NCI_UART_CLOSING, &nu->tx_state)) { + tx_started = test_and_clear_bit(NCI_UART_TX_STARTED, + &nu->tx_state); + clear_bit(NCI_UART_SENDING, &nu->tx_state); + clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + if (tx_started && nu->ops.tx_done) + nu->ops.tx_done(nu); + wake_up_all(&nu->tx_wait); + return; + } + if (test_and_clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state)) { + spin_unlock_irqrestore(&nu->tx_lock, flags); goto restart; + } + if (!nci_uart_queue_empty_locked(nu)) { + clear_bit(NCI_UART_SENDING, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + wake_up_all(&nu->tx_wait); + return; + } + tx_started = test_and_clear_bit(NCI_UART_TX_STARTED, + &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); - if (nu->ops.tx_done && nci_uart_queue_empty(nu)) + if (tx_started && nu->ops.tx_done) nu->ops.tx_done(nu); + spin_lock_irqsave(&nu->tx_lock, flags); + if (test_bit(NCI_UART_CLOSING, &nu->tx_state)) { + clear_bit(NCI_UART_SENDING, &nu->tx_state); + clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + wake_up_all(&nu->tx_wait); + return; + } + if (test_and_clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state) || + !nci_uart_queue_empty_locked(nu)) { + spin_unlock_irqrestore(&nu->tx_lock, flags); + goto restart; + } clear_bit(NCI_UART_SENDING, &nu->tx_state); + clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state); + spin_unlock_irqrestore(&nu->tx_lock, flags); + wake_up_all(&nu->tx_wait); +} + +/** + * nci_uart_set_ready - admit UART callbacks after transport setup + * @nu: NCI UART transport + * + * Publish fully initialized transport state and schedule any frame queued + * while the transport was opening. The line discipline and transmit locks + * must already be initialized. + */ +void nci_uart_set_ready(struct nci_uart *nu) +{ + /* Publish transport fields before callbacks observe READY. */ + smp_mb__before_atomic(); + set_bit(NCI_UART_READY, &nu->flags); + nci_uart_tx_wakeup(nu); +} +EXPORT_SYMBOL_GPL(nci_uart_set_ready); + +/** + * nci_uart_rx_flush - discard an incomplete UART receive frame + * @nu: NCI UART transport + * + * Clear parser state without changing callback admission. Return an error + * if a frame was incomplete, so the caller can skip any close-time exchange + * which would otherwise be parsed after the discarded prefix. + * + * Return: 0 if the parser was idle, or -EBADMSG if a partial frame was lost. + */ +int nci_uart_rx_flush(struct nci_uart *nu) +{ + int ret = 0; + + spin_lock(&nu->rx_lock); + if (nu->rx_skb) + ret = -EBADMSG; + kfree_skb(nu->rx_skb); + nu->rx_skb = NULL; + nu->rx_packet_len = -1; + spin_unlock(&nu->rx_lock); + + return ret; +} +EXPORT_SYMBOL_GPL(nci_uart_rx_flush); + +/** + * nci_uart_rx_stop - stop UART receive callbacks + * @nu: NCI UART transport + * + * Withdraw receive callback admission and discard an incomplete frame before + * transport-private state is released. The caller must keep @nu valid until + * any line-discipline callback already holding rx_lock has completed. + */ +void nci_uart_rx_stop(struct nci_uart *nu) +{ + spin_lock(&nu->rx_lock); + clear_bit(NCI_UART_READY, &nu->flags); + kfree_skb(nu->rx_skb); + nu->rx_skb = NULL; + nu->rx_packet_len = -1; + spin_unlock(&nu->rx_lock); } +EXPORT_SYMBOL_GPL(nci_uart_rx_stop); static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver) { @@ -131,14 +403,22 @@ static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver) nu->tty = tty; skb_queue_head_init(&nu->tx_q); INIT_WORK(&nu->write_work, nci_uart_write_work); + init_waitqueue_head(&nu->tx_wait); + spin_lock_init(&nu->tx_lock); spin_lock_init(&nu->rx_lock); ret = nu->ops.open(nu); if (ret) { + nci_uart_rx_stop(nu); + nci_uart_tx_stop(nu); + kfree_skb(nu->tx_skb); + nu->tx_skb = NULL; + skb_queue_purge(&nu->tx_q); module_put(nu->owner); kfree(nu); return ret; } + nci_uart_set_ready(nu); tty->disc_data = nu; return 0; @@ -181,22 +461,20 @@ static void nci_uart_tty_close(struct tty_struct *tty) { struct nci_uart *nu = tty->disc_data; - /* Detach from the tty */ - tty->disc_data = NULL; - if (!nu) return; - kfree_skb(nu->tx_skb); - kfree_skb(nu->rx_skb); + nu->ops.close(nu); + nci_uart_rx_stop(nu); + nci_uart_tx_stop(nu); + kfree_skb(nu->tx_skb); + nu->tx_skb = NULL; skb_queue_purge(&nu->tx_q); - nu->ops.close(nu); - nu->tty = NULL; module_put(nu->owner); - - cancel_work_sync(&nu->write_work); + tty->disc_data = NULL; + nu->tty = NULL; kfree(nu); } @@ -218,7 +496,8 @@ static void nci_uart_tty_wakeup(struct tty_struct *tty) clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); - if (tty != nu->tty) + if (tty != nu->tty || + !test_bit_acquire(NCI_UART_READY, &nu->flags)) return; nci_uart_tx_wakeup(nu); @@ -290,6 +569,17 @@ static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data, return 0; } +static bool nci_uart_rx_active(struct nci_uart *nu) +{ + if (!nu->ndev) + return false; + + /* Firmware and close-time reset responses bypass the normal UP gate. */ + return READ_ONCE(nu->ndev->nfc_dev->fw_download_in_progress) || + test_bit(NCI_UP, &nu->ndev->flags) || + test_bit(NCI_INIT, &nu->ndev->flags); +} + /* nci_uart_tty_receive() * * Called by tty low level driver when receive data is @@ -307,13 +597,23 @@ static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data, { struct nci_uart *nu = tty->disc_data; - if (!nu || tty != nu->tty) - return; + if (!nu || tty != nu->tty || + !test_bit_acquire(NCI_UART_READY, &nu->flags)) + goto unthrottle; spin_lock(&nu->rx_lock); + if (!test_bit(NCI_UART_READY, &nu->flags) || + !nci_uart_rx_active(nu)) { + kfree_skb(nu->rx_skb); + nu->rx_skb = NULL; + nu->rx_packet_len = -1; + spin_unlock(&nu->rx_lock); + goto unthrottle; + } nci_uart_default_recv_buf(nu, data, count); spin_unlock(&nu->rx_lock); +unthrottle: tty_unthrottle(tty); } @@ -367,11 +667,23 @@ static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file, static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb) { - /* Queue TX packet */ - skb_queue_tail(&nu->tx_q, skb); + unsigned long flags; + int ret = 0; - /* Try to start TX (if possible) */ - nci_uart_tx_wakeup(nu); + /* Queue TX packet */ + spin_lock_irqsave(&nu->tx_lock, flags); + if (test_bit(NCI_UART_CLOSING, &nu->tx_state) || + !test_bit(NCI_UART_READY, &nu->flags)) { + ret = -ESHUTDOWN; + } else { + __skb_queue_tail(&nu->tx_q, skb); + nci_uart_tx_wakeup_locked(nu); + } + spin_unlock_irqrestore(&nu->tx_lock, flags); + if (ret) { + kfree_skb(skb); + return ret; + } return 0; } -- 2.54.0