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 --- 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. */