Netdev List
 help / color / mirror / Atom feed
From: Bin Luo via B4 Relay <devnull+luob87709.gmail.com@kernel.org>
To: David Heidelberg <david@ixit.cz>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,  Simon Horman <horms@kernel.org>
Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
	 linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH net v3 2/2] nfc: rawsock: avoid synchronous target deactivation in rawsock_destruct()
Date: Sun, 06 Dec 2026 10:52:11 -0000	[thread overview]
Message-ID: <20261206105211.379-3-luob87709@gmail.com> (raw)
In-Reply-To: <20261206105211.379-1-luob87709@gmail.com>

From: Bin Luo <luob87709@gmail.com>

rawsock_destruct() currently calls nfc_deactivate_target(), which
acquires device_lock(&dev->dev) and, for NCI devices, invokes
nci_request(ndev, nci_rf_deactivate_req, ..., NCI_RF_DEACTIVATE_TIMEOUT)
to synchronously wait up to 30 seconds for an RF_DEACTIVATE_RSP/NTF
exchange processed on ndev->rx_wq.

When rawsock_release() drops its reference while an nfc_data_exchange()
is still in flight, rawsock_data_exchange_complete() drops the final
socket reference directly from ndev->rx_wq (inside nci_rx_work()).
That invokes rawsock_destruct() -> nfc_deactivate_target() ->
nci_request() on ndev->rx_wq itself, causing ndev->rx_wq to block for
the full 30-second NCI_RF_DEACTIVATE_TIMEOUT waiting on a response that
only ndev->rx_wq can process:

  nci: __nci_request: wait_for_completion_interruptible_timeout failed 0

Deactivating immediately in rawsock_release() without waiting for an
in-flight nfc_data_exchange() would race with driver completion paths
such as pn533_data_exchange_complete() operating on dev->resp_q.

Track whether a data exchange is currently in flight under
sk->sk_write_queue.lock:

- In rawsock_release(), after cancelling tx_work, deactivate the target
  synchronously if no data exchange is in flight; otherwise mark
  deact_pending so deactivation waits for the in-flight completion.
- In rawsock_data_exchange_complete() (on both success and error
  paths), clear tx_in_flight; if deact_pending is set, transfer the
  in-flight socket reference to a dedicated unbound rawsock_wq work
  item (rawsock_deact_work) that calls nfc_deactivate_target() outside
  ndev->rx_wq and then drops the socket reference.
- Keep nfc_put_device() in rawsock_destruct() so the nfc_dev reference
  remains valid until deactivation finishes, and destroy rawsock_wq in
  rawsock_exit() before unregistering the protocol.

Tested in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS) with:
- tools/testing/selftests/nci/nci_dev (8/8 passed)
- deferred deactivation invariant tests (10/10 in-flight close deferred
  until after completion, 20/20 idle close synchronous, 5/5 error path)
- 300 single-frame + 300 multi-frame virtual_ncidev teardown race loops
  (600/600 deactivations, 0 timeouts, 0 KASAN/LOCKDEP/WARNING reports)

Fixes: 23b7869c0fd0 ("NFC: add the NFC socket raw protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo <luob87709@gmail.com>
---
Changes in v3:
- Fix race with in-flight driver completions (such as pn533 resp_q
  handling) by tracking tx_in_flight under sk_write_queue.lock and
  deferring nfc_deactivate_target() to a dedicated unbound rawsock_wq
  work item when close() occurs while an exchange is still in flight.
- Normalize From:/Signed-off-by: author metadata.

Changes in v2:
- Resend cleanly via b4 web relay and include both dependent rawsock
  patches as a threaded series.

 net/nfc/nfc.h     |  3 ++
 net/nfc/rawsock.c | 73 ++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/net/nfc/nfc.h b/net/nfc/nfc.h
index 82c5dfd..2819ccd 100644
--- a/net/nfc/nfc.h
+++ b/net/nfc/nfc.h
@@ -29,7 +29,10 @@ struct nfc_rawsock {
 	struct nfc_dev *dev;
 	u32 target_idx;
 	struct work_struct tx_work;
+	struct work_struct deact_work;
 	bool tx_work_scheduled;
+	bool tx_in_flight;
+	bool deact_pending;
 };

 struct nfc_sock_list {
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 2fa0f05..b8aa2de 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -20,6 +20,18 @@ static struct nfc_sock_list raw_sk_list = {
 	.lock = __RW_LOCK_UNLOCKED(raw_sk_list.lock)
 };

+static struct workqueue_struct *rawsock_wq;
+
+static void rawsock_deact_work(struct work_struct *work)
+{
+	struct nfc_rawsock *rsk = container_of(work, struct nfc_rawsock,
+					       deact_work);
+	struct sock *sk = &rsk->sk;
+
+	nfc_deactivate_target(rsk->dev, rsk->target_idx, NFC_TARGET_MODE_IDLE);
+	sock_put(sk);
+}
+
 static void nfc_sock_link(struct nfc_sock_list *l, struct sock *sk)
 {
 	write_lock(&l->lock);
@@ -68,6 +80,8 @@ static int rawsock_release(struct socket *sock)
 		nfc_sock_unlink(&raw_sk_list, sk);

 	if (sk->sk_state = TCP_ESTABLISHED) {
+		bool do_deact = false;
+
 		/* Prevent rawsock_tx_work from starting new transmits and
 		 * wait for any in-progress work to finish.  This must happen
 		 * before the socket is orphaned to avoid a race where
@@ -78,6 +92,18 @@ static int rawsock_release(struct socket *sock)
 		__rawsock_write_queue_purge(sk);
 		spin_unlock_bh(&sk->sk_write_queue.lock);
 		cancel_work_sync(&nfc_rawsock(sk)->tx_work);
+
+		spin_lock_bh(&sk->sk_write_queue.lock);
+		if (nfc_rawsock(sk)->tx_in_flight)
+			nfc_rawsock(sk)->deact_pending = true;
+		else
+			do_deact = true;
+		spin_unlock_bh(&sk->sk_write_queue.lock);
+
+		if (do_deact)
+			nfc_deactivate_target(nfc_rawsock(sk)->dev,
+					      nfc_rawsock(sk)->target_idx,
+					      NFC_TARGET_MODE_IDLE);
 	}

 	sock_orphan(sk);
@@ -153,6 +179,7 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
 					   int err)
 {
 	struct sock *sk = (struct sock *) context;
+	bool defer_deact = false;

 	BUG_ON(in_hardirq());

@@ -170,14 +197,22 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
 		goto error_skb;

 	spin_lock_bh(&sk->sk_write_queue.lock);
-	if (!(sk->sk_shutdown & SEND_SHUTDOWN) &&
-	    !skb_queue_empty(&sk->sk_write_queue))
+	nfc_rawsock(sk)->tx_in_flight = false;
+	if (nfc_rawsock(sk)->deact_pending) {
+		nfc_rawsock(sk)->tx_work_scheduled = false;
+		defer_deact = true;
+	} else if (!(sk->sk_shutdown & SEND_SHUTDOWN) &&
+		   !skb_queue_empty(&sk->sk_write_queue)) {
 		schedule_work(&nfc_rawsock(sk)->tx_work);
-	else
+	} else {
 		nfc_rawsock(sk)->tx_work_scheduled = false;
+	}
 	spin_unlock_bh(&sk->sk_write_queue.lock);

-	sock_put(sk);
+	if (defer_deact)
+		queue_work(rawsock_wq, &nfc_rawsock(sk)->deact_work);
+	else
+		sock_put(sk);
 	return;

 error_skb:
@@ -185,7 +220,15 @@ error_skb:

 error:
 	rawsock_report_error(sk, err);
-	sock_put(sk);
+	spin_lock_bh(&sk->sk_write_queue.lock);
+	nfc_rawsock(sk)->tx_in_flight = false;
+	defer_deact = nfc_rawsock(sk)->deact_pending;
+	spin_unlock_bh(&sk->sk_write_queue.lock);
+
+	if (defer_deact)
+		queue_work(rawsock_wq, &nfc_rawsock(sk)->deact_work);
+	else
+		sock_put(sk);
 }

 static void rawsock_tx_work(struct work_struct *work)
@@ -211,6 +254,7 @@ static void rawsock_tx_work(struct work_struct *work)
 		spin_unlock_bh(&sk->sk_write_queue.lock);
 		return;
 	}
+	nfc_rawsock(sk)->tx_in_flight = true;
 	spin_unlock_bh(&sk->sk_write_queue.lock);
 	kcov_remote_start_common(skb_get_kcov_handle(skb));

@@ -219,6 +263,9 @@ static void rawsock_tx_work(struct work_struct *work)
 			       rawsock_data_exchange_complete, sk);
 	if (rc) {
 		rawsock_report_error(sk, rc);
+		spin_lock_bh(&sk->sk_write_queue.lock);
+		nfc_rawsock(sk)->tx_in_flight = false;
+		spin_unlock_bh(&sk->sk_write_queue.lock);
 		sock_put(sk);
 	}
 	kcov_remote_stop();
@@ -332,12 +379,8 @@ static void rawsock_destruct(struct sock *sk)
 {
 	pr_debug("sk=%p\n", sk);

-	if (sk->sk_state = TCP_ESTABLISHED) {
-		nfc_deactivate_target(nfc_rawsock(sk)->dev,
-				      nfc_rawsock(sk)->target_idx,
-				      NFC_TARGET_MODE_IDLE);
+	if (sk->sk_state = TCP_ESTABLISHED)
 		nfc_put_device(nfc_rawsock(sk)->dev);
-	}

 	skb_queue_purge(&sk->sk_receive_queue);

@@ -377,7 +420,10 @@ static int rawsock_create(struct net *net, struct socket *sock,
 		nfc_sock_link(&raw_sk_list, sk);
 	else {
 		INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
+		INIT_WORK(&nfc_rawsock(sk)->deact_work, rawsock_deact_work);
 		nfc_rawsock(sk)->tx_work_scheduled = false;
+		nfc_rawsock(sk)->tx_in_flight = false;
+		nfc_rawsock(sk)->deact_pending = false;
 	}

 	return 0;
@@ -437,12 +483,19 @@ int __init rawsock_init(void)
 {
 	int rc;

+	rawsock_wq = alloc_workqueue("nfc_rawsock", WQ_UNBOUND, 0);
+	if (!rawsock_wq)
+		return -ENOMEM;
+
 	rc = nfc_proto_register(&rawsock_nfc_proto);
+	if (rc)
+		destroy_workqueue(rawsock_wq);

 	return rc;
 }

 void rawsock_exit(void)
 {
+	destroy_workqueue(rawsock_wq);
 	nfc_proto_unregister(&rawsock_nfc_proto);
 }
--
2.43.0




      parent reply	other threads:[~2026-12-06 10:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 10:52 [PATCH net v3 0/2] nfc: rawsock: fix teardown races and destruct-time deactivation deadlock Bin Luo via B4 Relay
2026-12-06 10:52 ` [PATCH net v3 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown Bin Luo via B4 Relay
2026-12-06 10:52 ` Bin Luo via B4 Relay [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=20261206105211.379-3-luob87709@gmail.com \
    --to=devnull+luob87709.gmail.com@kernel.org \
    --cc=davem@davemloft.net \
    --cc=david@ixit.cz \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luob87709@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    /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