[PATCH 10/14] Bluetooth: Add lock context annotations for connection aliases
From: Bart Van Assche
Date: Mon Nov 02 2026 - 17:38:04 EST
Replace lockdep_assert_held() with lockdep_assume_ctx_held() where
hdev->lock, an L2CAP connection lock or an L2CAP channel lock is
accessed through a pointer alias.
Currently, lockdep_assert_held() implicitly assumes lock ownership for
Clang context analysis. Prepare for removing the context assumption from
lockdep_assert_held(). In Bluetooth code, functions access members
guarded by hdev->lock (such as l2cap_data) or call functions requiring
hdev->lock through connection pointer aliases (hcon->hdev->lock,
conn->hdev->lock, link->conn->hdev->lock). Similarly, L2CAP functions
call functions that require chan->conn->lock while holding conn->lock,
and l2cap_ecred_rsp_defer() relies on rsp->locked_chan->lock,
rsp->locked_chan->conn->lock, chan->lock and chan->conn->lock being
held.
Replace lockdep_assert_held() with lockdep_assume_ctx_held() at these
alias access points. This informs Clang context analysis that the lock
is held and preserves the runtime lockdep checks.
Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
net/bluetooth/6lowpan.c | 2 +-
net/bluetooth/iso.c | 8 +++----
net/bluetooth/l2cap_core.c | 48 +++++++++++++++++++-------------------
net/bluetooth/mgmt.c | 2 +-
net/bluetooth/smp.c | 2 +-
5 files changed, 31 insertions(+), 31 deletions(-)
diff --git a/net/bluetooth/6lowpan.c b/net/bluetooth/6lowpan.c
index 8c51c20f6c4b..0a1a067d4870 100644
--- a/net/bluetooth/6lowpan.c
+++ b/net/bluetooth/6lowpan.c
@@ -1019,7 +1019,7 @@ static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
return -ENOENT;
}
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
*conn = l2cap_conn_hold_unless_zero(hcon->l2cap_data);
diff --git a/net/bluetooth/iso.c b/net/bluetooth/iso.c
index c1fdd9846eeb..bedac9b0682e 100644
--- a/net/bluetooth/iso.c
+++ b/net/bluetooth/iso.c
@@ -466,7 +466,7 @@ static int iso_connect_bis(struct sock *sk)
iso_pi(sk)->bc_sid = hcon->sid;
}
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
conn = iso_conn_add(hcon);
if (!conn) {
@@ -572,7 +572,7 @@ static int iso_connect_cis(struct sock *sk)
}
}
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
/* The socket lock keeps the current attachment and its hcon stable. */
if (iso_pi(sk)->conn && iso_pi(sk)->conn->hcon != hcon) {
@@ -1390,7 +1390,7 @@ static int iso_listen_bis(struct sock *sk)
goto unlock;
}
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
conn = iso_conn_add(hcon);
if (!conn) {
@@ -2657,7 +2657,7 @@ static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
list_for_each_entry_safe(link, t, &hcon->link_list,
list) {
- lockdep_assert_held(&link->conn->hdev->lock);
+ lockdep_assume_ctx_held(&link->conn->hdev->lock);
iso_conn_del(link->conn, bt_to_errno(status));
}
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 4dd56e0074fd..6b3381ae574e 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1657,7 +1657,7 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
l2cap_chan_ready(chan);
@@ -1775,7 +1775,7 @@ static void l2cap_conn_ready(struct l2cap_conn *conn)
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
if (hcon->type == LE_LINK) {
l2cap_le_start(chan);
@@ -1932,7 +1932,7 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)
l2cap_chan_hold(chan);
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
l2cap_chan_del(chan, err);
@@ -4063,8 +4063,8 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags))
return;
- lockdep_assert_held(&rsp->locked_chan->lock);
- lockdep_assert_held(&rsp->locked_chan->conn->lock);
+ lockdep_assume_ctx_held(&rsp->locked_chan->lock);
+ lockdep_assume_ctx_held(&rsp->locked_chan->conn->lock);
l2cap_chan_hold(chan);
@@ -4076,8 +4076,8 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data)
if (context_unsafe(!locked && chan != rsp->locked_chan))
goto done;
- lockdep_assert_held(&chan->lock);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
/* Reset ident so only one response is sent */
chan->ident = 0;
@@ -4275,7 +4275,7 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,
__l2cap_chan_add(conn, chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
if (pchan->ops->new_connection &&
pchan->ops->new_connection(pchan, chan) < 0) {
@@ -4478,7 +4478,7 @@ static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
/* Only accept a response to a connection request we sent and that
* is still outstanding. A channel waiting in BT_CONNECT for security
@@ -4589,7 +4589,7 @@ static inline int l2cap_config_req(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2 &&
chan->state != BT_CONNECTED) {
@@ -4711,7 +4711,7 @@ static inline int l2cap_config_rsp(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
/* Connected channels may also receive reconfiguration responses. */
if (chan->state != BT_CONFIG && chan->state != BT_CONNECTED)
@@ -4829,7 +4829,7 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
rsp.dcid = cpu_to_le16(chan->scid);
rsp.scid = cpu_to_le16(chan->dcid);
@@ -4871,7 +4871,7 @@ static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
if (chan->state != BT_DISCONN) {
l2cap_chan_unlock(chan);
@@ -5085,7 +5085,7 @@ static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
switch (result) {
case L2CAP_CR_LE_SUCCESS:
@@ -5305,7 +5305,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
bacpy(&chan->src, &conn->hcon->src);
bacpy(&chan->dst, &conn->hcon->dst);
@@ -5538,7 +5538,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
bacpy(&chan->src, &conn->hcon->src);
bacpy(&chan->dst, &conn->hcon->dst);
@@ -5629,7 +5629,7 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,
l2cap_chan_hold(chan);
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
/* Check that there is a dcid for each pending channel */
if (cmd_len < sizeof(dcid)) {
@@ -5858,7 +5858,7 @@ static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn,
continue;
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
l2cap_chan_del(chan, ECONNRESET);
@@ -5889,7 +5889,7 @@ static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
goto done;
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
l2cap_chan_del(chan, ECONNREFUSED);
l2cap_chan_unlock(chan);
l2cap_chan_put(chan);
@@ -7285,7 +7285,7 @@ static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
BT_DBG("chan %p, len %d", chan, skb->len);
@@ -7665,7 +7665,7 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
goto done;
}
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
conn = l2cap_conn_add(hcon);
if (!conn) {
@@ -7711,7 +7711,7 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
__l2cap_chan_add(conn, chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
/* l2cap_chan_add takes its own ref so we can drop this one */
hci_conn_drop(hcon);
@@ -7969,7 +7969,7 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
list_for_each_entry(chan, &conn->chan_l, list) {
l2cap_chan_lock(chan);
- lockdep_assert_held(&chan->conn->lock);
+ lockdep_assume_ctx_held(&chan->conn->lock);
BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
state_to_string(chan->state));
@@ -8141,7 +8141,7 @@ int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle,
return -ENOENT;
}
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
hci_conn_enter_active_mode(hcon, BT_POWER_FORCE_ACTIVE_OFF);
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 3b9c82bd2f02..022e7f41a91a 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -3902,7 +3902,7 @@ static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
}
if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
- lockdep_assert_held(&conn->hdev->lock);
+ lockdep_assume_ctx_held(&conn->hdev->lock);
err = smp_user_confirm_reply(conn, mgmt_op, passkey);
if (!err)
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 5dac8b2eae42..21640632368b 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -2446,7 +2446,7 @@ int smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr,
if (!hcon)
goto done;
- lockdep_assert_held(&hcon->hdev->lock);
+ lockdep_assume_ctx_held(&hcon->hdev->lock);
conn = hcon->l2cap_data;
if (!conn)