Skip to content

Commit

Permalink
Bluetooth: L2CAP: Fix deadlock
Browse files Browse the repository at this point in the history
This fixes the following deadlock introduced by 39a92a5
("bluetooth/l2cap: sync sock recv cb and release")

============================================
WARNING: possible recursive locking detected
6.10.0-rc3-g4029dba6b6f1 #6823 Not tainted
--------------------------------------------
kworker/u5:0/35 is trying to acquire lock:
ffff888002ec2510 (&chan->lock#2/1){+.+.}-{3:3}, at:
l2cap_sock_recv_cb+0x44/0x1e0

but task is already holding lock:
ffff888002ec2510 (&chan->lock#2/1){+.+.}-{3:3}, at:
l2cap_get_chan_by_scid+0xaf/0xd0

other info that might help us debug this:
 Possible unsafe locking scenario:

       CPU0
       ----
  lock(&chan->lock#2/1);
  lock(&chan->lock#2/1);

 *** DEADLOCK ***

 May be due to missing lock nesting notation

3 locks held by kworker/u5:0/35:
 #0: ffff888002b8a940 ((wq_completion)hci0#2){+.+.}-{0:0}, at:
process_one_work+0x750/0x930
 #1: ffff888002c67dd0 ((work_completion)(&hdev->rx_work)){+.+.}-{0:0},
at: process_one_work+0x44e/0x930
 #2: ffff888002ec2510 (&chan->lock#2/1){+.+.}-{3:3}, at:
l2cap_get_chan_by_scid+0xaf/0xd0

To fix the original problem this introduces l2cap_chan_lock at
l2cap_conless_channel to ensure that l2cap_sock_recv_cb is called with
chan->lock held.

Fixes: 39a92a5 ("bluetooth/l2cap: sync sock recv cb and release")
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
  • Loading branch information
Vudentz committed Jun 28, 2024
1 parent 04608f9 commit 284f0e1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
3 changes: 3 additions & 0 deletions net/bluetooth/l2cap_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -6761,6 +6761,8 @@ static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,

BT_DBG("chan %p, len %d", chan, skb->len);

l2cap_chan_lock(chan);

if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
goto drop;

Expand All @@ -6777,6 +6779,7 @@ static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
}

drop:
l2cap_chan_unlock(chan);
l2cap_chan_put(chan);
free_skb:
kfree_skb(skb);
Expand Down
13 changes: 1 addition & 12 deletions net/bluetooth/l2cap_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1489,18 +1489,9 @@ static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
struct l2cap_pinfo *pi;
int err;

/* To avoid race with sock_release, a chan lock needs to be added here
* to synchronize the sock.
*/
l2cap_chan_hold(chan);
l2cap_chan_lock(chan);
sk = chan->data;

if (!sk) {
l2cap_chan_unlock(chan);
l2cap_chan_put(chan);
if (!sk)
return -ENXIO;
}

pi = l2cap_pi(sk);
lock_sock(sk);
Expand Down Expand Up @@ -1552,8 +1543,6 @@ static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)

done:
release_sock(sk);
l2cap_chan_unlock(chan);
l2cap_chan_put(chan);

return err;
}
Expand Down

0 comments on commit 284f0e1

Please sign in to comment.