Linux kernel -stable discussions
 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 v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release()
Date: Fri, 04 Dec 2026 14:06:53 +0800	[thread overview]
Message-ID: <20261204060651.488-3-luob87709@gmail.com> (raw)
In-Reply-To: <20261204060651.488-1-luob87709@gmail.com>

From: "Bin Luo (University of Electronic Science and Technology of China)" <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

Move nfc_deactivate_target() into rawsock_release() (process context,
after cancelling tx_work) so deactivation never runs inside sk_destruct
from an asynchronous completion context.  Keep nfc_put_device() in
rawsock_destruct() so the nfc_dev reference stays valid until any
in-flight completion drops its socket reference.

Discovered by AI-assisted code review and reproduced in QEMU KVM on an
unmodified kernel using /dev/virtual_nci without delay injection (an
AF_NFC SOCK_SEQPACKET socket sends one frame and closes concurrently
with the NCI data response).  Tested against the reproducer across
3 x 200 iterations in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS) with zero
timeouts, and against tools/testing/selftests/nci/nci_dev (8/8 passed).

Fixes: 23b7869c0fd0 ("NFC: add the NFC socket raw protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
Changes in v2:
- Resend cleanly via b4 web relay (v1 patch was line-wrapped by Gmail)
  and include both dependent rawsock patches as a threaded series.

 net/nfc/rawsock.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 291f509..e2a9a2b 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -78,6 +78,10 @@ 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);
+
+		nfc_deactivate_target(nfc_rawsock(sk)->dev,
+				      nfc_rawsock(sk)->target_idx,
+				      NFC_TARGET_MODE_IDLE);
 	}
 
 	sock_orphan(sk);
@@ -332,12 +336,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);
 
-- 
2.43.0




      parent reply	other threads:[~2026-12-04  6:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261204060651.488-1-luob87709@gmail.com>
2026-12-04  6:06 ` [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown Bin Luo via B4 Relay
2026-12-04  6:06 ` 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=20261204060651.488-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