Netdev List
 help / color / mirror / Atom feed
From: Shubham Antil <shubham@octane.security>
To: netdev@vger.kernel.org
Cc: Simon Horman <horms@kernel.org>,
	gio@octane.security, David Heidelberg <david@ixit.cz>,
	oe-linux-nfc@lists.linux.dev
Subject: [PATCH net v9 4/4] nfc: nfcmrvl: adopt the NCI UART lifecycle helpers
Date: Mon, 30 Nov 2026 14:44:56 +0530	[thread overview]
Message-ID: <20261130091456.62379-5-shubham@octane.security> (raw)
In-Reply-To: <20261130091456.62379-1-shubham@octane.security>

The Marvell UART open path registers its NCI device before publishing the
transport back-pointers, and its close path can release private state
while generic UART callbacks are still admitted.

Publish the back-pointers before NCI registration, make the NCI ready
hook open callback admission immediately before public registration, and
withdraw admission before releasing private state. Flush complete frames
before the close-time reset and treat a partial receive frame as lost
framing, so the reset is skipped rather than parsed after a delayed
suffix. Abort a transport whose framing cannot be preserved, halt the
controller while its reset GPIO is still valid, reject firmware download
on that poisoned UART, and clear the published pointers during unwind.

The generic ready call remains an idempotent fallback after a transport
ready hook. This keeps transports without an early-ready requirement
working without reopening admission after it has been withdrawn.

Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. A high-concurrency pre-fix run reproduced a
general-protection fault in nfcmrvl_nci_uart_tx_start() while
nu->drv_data was still NULL. Patch development and test planning used the
same assistant.
Confirmed with repeated NCI UART registration, device-open, reset, and
hangup cycles using a pty-backed user-space controller. No physical NFC
controller was available.

Fixes: e097dc624f78 ("NFC: nfcmrvl: add UART driver")
Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol)
Signed-off-by: Shubham Antil <shubham@octane.security>
---
 drivers/nfc/nfcmrvl/main.c    | 46 +++++++++++++++++++++----
 drivers/nfc/nfcmrvl/nfcmrvl.h |  4 +++
 drivers/nfc/nfcmrvl/uart.c    | 63 +++++++++++++++++++++++++++++++----
 3 files changed, 101 insertions(+), 12 deletions(-)

diff --git a/drivers/nfc/nfcmrvl/main.c b/drivers/nfc/nfcmrvl/main.c
index 6efa83219..bc0d80ab1 100644
--- a/drivers/nfc/nfcmrvl/main.c
+++ b/drivers/nfc/nfcmrvl/main.c
@@ -22,13 +22,12 @@ static int nfcmrvl_nci_open(struct nci_dev *ndev)
 	if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
 		return 0;
 
-	/* Reset possible fault of previous session */
-	clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
-
 	err = priv->if_ops->nci_open(priv);
 
 	if (err)
 		clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);
+	else
+		clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
 
 	return err;
 }
@@ -67,6 +66,24 @@ static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
 	return priv->if_ops->nci_send(priv, skb);
 }
 
+static void nfcmrvl_nci_ready(struct nci_dev *ndev)
+{
+	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
+
+	if (priv->if_ops->nci_ready)
+		priv->if_ops->nci_ready(priv);
+}
+
+static int nfcmrvl_nci_flush(struct nci_dev *ndev)
+{
+	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
+
+	if (priv->if_ops->nci_flush)
+		return priv->if_ops->nci_flush(priv);
+
+	return 0;
+}
+
 static int nfcmrvl_nci_setup(struct nci_dev *ndev)
 {
 	__u8 val = 1;
@@ -78,6 +95,12 @@ static int nfcmrvl_nci_setup(struct nci_dev *ndev)
 static int nfcmrvl_nci_fw_download(struct nci_dev *ndev,
 				   const char *firmware_name)
 {
+	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
+
+	if (priv->phy == NFCMRVL_PHY_UART &&
+	    test_bit(NFCMRVL_PHY_ERROR, &priv->flags))
+		return -EIO;
+
 	return nfcmrvl_fw_dnld_start(ndev, firmware_name);
 }
 
@@ -85,6 +108,8 @@ static const struct nci_ops nfcmrvl_nci_ops = {
 	.open = nfcmrvl_nci_open,
 	.close = nfcmrvl_nci_close,
 	.send = nfcmrvl_nci_send,
+	.ready = nfcmrvl_nci_ready,
+	.flush = nfcmrvl_nci_flush,
 	.setup = nfcmrvl_nci_setup,
 	.fw_download = nfcmrvl_nci_fw_download,
 };
@@ -154,19 +179,24 @@ struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
 
 	nci_set_drvdata(priv->ndev, priv);
 
+	/* Ensure that the controller is powered off before exposing it. */
+	nfcmrvl_chip_halt(priv);
+
+	if (priv->if_ops->nci_prepare)
+		priv->if_ops->nci_prepare(priv);
+
 	rc = nci_register_device(priv->ndev);
 	if (rc) {
 		nfc_err(dev, "nci_register_device failed %d\n", rc);
 		goto error_fw_dnld_deinit;
 	}
 
-	/* Ensure that controller is powered off */
-	nfcmrvl_chip_halt(priv);
-
 	nfc_info(dev, "registered with nci successfully\n");
 	return priv;
 
 error_fw_dnld_deinit:
+	if (priv->if_ops->nci_quiesce)
+		priv->if_ops->nci_quiesce(priv);
 	nfcmrvl_fw_dnld_deinit(priv);
 error_free_dev:
 	nci_free_device(priv->ndev);
@@ -181,6 +211,9 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
 	struct nci_dev *ndev = priv->ndev;
 
 	nci_unregister_device(ndev);
+	if (priv->if_ops->nci_quiesce)
+		priv->if_ops->nci_quiesce(priv);
+
 	if (priv->ndev->nfc_dev->fw_download_in_progress)
 		nfcmrvl_fw_dnld_abort(priv);
 
@@ -241,6 +274,7 @@ void nfcmrvl_chip_halt(struct nfcmrvl_private *priv)
 	if (priv->config.reset_gpio)
 		gpiod_set_value(priv->config.reset_gpio, 1);
 }
+EXPORT_SYMBOL_GPL(nfcmrvl_chip_halt);
 
 int nfcmrvl_parse_dt(struct device_node *node,
 		     struct nfcmrvl_platform_data *pdata)
diff --git a/drivers/nfc/nfcmrvl/nfcmrvl.h b/drivers/nfc/nfcmrvl/nfcmrvl.h
index 62fa77d58..085ea4953 100644
--- a/drivers/nfc/nfcmrvl/nfcmrvl.h
+++ b/drivers/nfc/nfcmrvl/nfcmrvl.h
@@ -112,6 +112,10 @@ struct nfcmrvl_if_ops {
 	int (*nci_open) (struct nfcmrvl_private *priv);
 	int (*nci_close) (struct nfcmrvl_private *priv);
 	int (*nci_send) (struct nfcmrvl_private *priv, struct sk_buff *skb);
+	void (*nci_ready)(struct nfcmrvl_private *priv);
+	int (*nci_flush)(struct nfcmrvl_private *priv);
+	void (*nci_prepare)(struct nfcmrvl_private *priv);
+	void (*nci_quiesce)(struct nfcmrvl_private *priv);
 	void (*nci_update_config)(struct nfcmrvl_private *priv,
 				  const void *param);
 };
diff --git a/drivers/nfc/nfcmrvl/uart.c b/drivers/nfc/nfcmrvl/uart.c
index 9aedd1687..8cecb48e7 100644
--- a/drivers/nfc/nfcmrvl/uart.c
+++ b/drivers/nfc/nfcmrvl/uart.c
@@ -28,11 +28,17 @@ static unsigned int break_control;
 
 static int nfcmrvl_uart_nci_open(struct nfcmrvl_private *priv)
 {
-	return 0;
+	struct nci_uart *nu = priv->drv_data;
+
+	/* A failed close-time flush poisons this line-discipline instance. */
+	return nci_uart_tx_flush(nu);
 }
 
 static int nfcmrvl_uart_nci_close(struct nfcmrvl_private *priv)
 {
+	if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags))
+		nfcmrvl_chip_halt(priv);
+
 	return 0;
 }
 
@@ -44,6 +50,51 @@ static int nfcmrvl_uart_nci_send(struct nfcmrvl_private *priv,
 	return nu->ops.send(nu, skb);
 }
 
+static void nfcmrvl_uart_nci_ready(struct nfcmrvl_private *priv)
+{
+	struct nci_uart *nu = priv->drv_data;
+
+	nci_uart_set_ready(nu);
+}
+
+static int nfcmrvl_uart_nci_flush(struct nfcmrvl_private *priv)
+{
+	struct nci_uart *nu = priv->drv_data;
+	int rx_ret;
+	int ret;
+
+	ret = nci_uart_tx_flush(nu);
+	rx_ret = nci_uart_rx_flush(nu);
+	if (!ret)
+		ret = rx_ret;
+	if (ret) {
+		set_bit(NFCMRVL_PHY_ERROR, &priv->flags);
+		nci_uart_tx_abort(nu);
+	}
+
+	return ret;
+}
+
+static void nfcmrvl_uart_nci_prepare(struct nfcmrvl_private *priv)
+{
+	struct nci_uart *nu = priv->drv_data;
+
+	priv->support_fw_dnld = true;
+	nu->drv_data = priv;
+	nu->ndev = priv->ndev;
+}
+
+static void nfcmrvl_uart_nci_quiesce(struct nfcmrvl_private *priv)
+{
+	struct nci_uart *nu = priv->drv_data;
+
+	nci_uart_rx_stop(nu);
+	nci_uart_tx_stop(nu);
+	nfcmrvl_chip_halt(priv);
+	nu->drv_data = NULL;
+	nu->ndev = NULL;
+}
+
 static void nfcmrvl_uart_nci_update_config(struct nfcmrvl_private *priv,
 					   const void *param)
 {
@@ -58,6 +109,10 @@ static const struct nfcmrvl_if_ops uart_ops = {
 	.nci_open = nfcmrvl_uart_nci_open,
 	.nci_close = nfcmrvl_uart_nci_close,
 	.nci_send = nfcmrvl_uart_nci_send,
+	.nci_ready = nfcmrvl_uart_nci_ready,
+	.nci_flush = nfcmrvl_uart_nci_flush,
+	.nci_prepare = nfcmrvl_uart_nci_prepare,
+	.nci_quiesce = nfcmrvl_uart_nci_quiesce,
 	.nci_update_config = nfcmrvl_uart_nci_update_config
 };
 
@@ -136,11 +191,6 @@ static int nfcmrvl_nci_uart_open(struct nci_uart *nu)
 	if (IS_ERR(priv))
 		return PTR_ERR(priv);
 
-	priv->support_fw_dnld = true;
-
-	nu->drv_data = priv;
-	nu->ndev = priv->ndev;
-
 	return 0;
 }
 
@@ -199,6 +249,7 @@ static struct nci_uart nfcmrvl_nci_uart = {
 		.tx_done	= nfcmrvl_nci_uart_tx_done,
 	}
 };
+
 module_driver(nfcmrvl_nci_uart, nci_uart_register, nci_uart_unregister);
 
 MODULE_AUTHOR("Marvell International Ltd.");
-- 
2.54.0


      parent reply	other threads:[~2026-11-30  9:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  9:14 [PATCH net v9 0/4] nfc: nci: uart: split lifecycle teardown fixes Shubham Antil
2026-11-30  9:14 ` [PATCH net v9 1/4] nfc: keep devices shut down until registration completes Shubham Antil
2026-11-30  9:14 ` [PATCH net v9 2/4] nfc: nci: coordinate transport shutdown in the core Shubham Antil
2026-11-30  9:14 ` [PATCH net v9 3/4] nfc: nci: uart: quiesce callbacks during teardown Shubham Antil
2026-11-30  9:14 ` Shubham Antil [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130091456.62379-5-shubham@octane.security \
    --to=shubham@octane.security \
    --cc=david@ixit.cz \
    --cc=gio@octane.security \
    --cc=horms@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox