mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cen Zhang <zzzccc427@gmail.com>
To: marcel@holtmann.org, luiz.dentz@gmail.com
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, baijiaju1990@gmail.com,
	jjzuming@gmail.com, zzzccc427@gmail.com
Subject: [PATCH 2/2] Bluetooth: RFCOMM: Close DLC on TTY registration failure
Date: Wed, 25 Nov 2026 23:22:18 +0800	[thread overview]
Message-ID: <pm-bluetooth-objects-candidate-0218-followup-v2-88edfec8c72ed860e626-2@gmail.com> (raw)
In-Reply-To: <pm-bluetooth-objects-candidate-0218-followup-v2-88edfec8c72ed860e626-0@gmail.com>

The TTY takes ownership of the DLC before tty_port_register_device().
If registration fails, dropping the port references detaches the device
but leaves the DLC open. In the reuse case the original socket is
already BT_CLOSED, so it will not close the DLC on release.

Start DLC teardown while the registration reference still pins the
port and its DLC. Purge any unconsumed receive data when the port is
finally destroyed, after detaching the device as the DLC owner.

Commit 77bc0f253ff9f ("Bluetooth: RFCOMM: serialize RFCOMM_CONNINFO
 dlc->session dereference") moved session reads under rfcomm_mutex.
The handoff therefore no longer needs to wait on the socket lock for
RFCOMM_CONNINFO. Use WRITE_ONCE() to publish BT_CLOSED. The DLC lock
continues to serialize that update with the old socket state callback.

Fixes: 56bc551ef79c ("Bluetooth: RFCOMM: Close the socket before transferring its DLC")
Link: https://lkml.iu.edu/hypermail/linux/kernel/2611.2/24606.html
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
This patch was written with assistance from Codex (OpenAI).

 net/bluetooth/rfcomm/tty.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 745cf1e4bb2c3..43a03837e371b 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -96,6 +96,7 @@ static void rfcomm_dev_destruct(struct tty_port *port)
 	list_del(&dev->list);
 	mutex_unlock(&rfcomm_dev_lock);
 
+	skb_queue_purge(&dev->pending);
 	kfree(dev);
 
 	/* It's safe to call module_put() here because socket still
@@ -293,12 +294,8 @@ static struct rfcomm_dev *__rfcomm_dev_add(struct rfcomm_dev_req *req,
 			atomic_sub(skb->len, &sk->sk_rmem_alloc);
 		}
 
-		/* Finish RFCOMM_CONNINFO before replacing the callback
-		 * which serializes session teardown with the socket.
-		 */
-		lock_sock(sk);
-		sk->sk_state = BT_CLOSED;
-		release_sock(sk);
+		/* Serialize with the DLC state callback. */
+		WRITE_ONCE(sk->sk_state, BT_CLOSED);
 	}
 
 	dlc->data_ready   = rfcomm_dev_data_ready;
@@ -347,6 +344,8 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
 			dev->id, NULL);
 	if (IS_ERR(tty)) {
 		err = PTR_ERR(tty);
+		/* Close the DLC while the port still pins it. */
+		rfcomm_dlc_close(dlc, 0);
 		if (!test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status))
 			tty_port_put(&dev->port); /* Drop the initial reference. */
 		tty_port_put(&dev->port); /* Drop the registration reference. */

      parent reply	other threads:[~2026-11-25 15:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 15:22 [PATCH 0/2] Bluetooth: RFCOMM: Fix listener shutdown and TTY setup Cen Zhang
2026-11-25 15:22 ` [PATCH 1/2] Bluetooth: RFCOMM: Reject listen after socket shutdown Cen Zhang
2026-11-25 15:22 ` Cen Zhang [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=pm-bluetooth-objects-candidate-0218-followup-v2-88edfec8c72ed860e626-2@gmail.com \
    --to=zzzccc427@gmail.com \
    --cc=baijiaju1990@gmail.com \
    --cc=jjzuming@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --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

all inboxes | Powered by JetHome®