Bluetooth: L2CAP: add annotations for l2cap_chan list locking

Add context analysis annotations for l2cap_conn::chan_l and chan_list
locking.

Add corresponding required annotations to accessors and callers.

This is not complete chan_l annotation, l2cap_chan::list and
l2cap_chan_del() locking is currently not fully correct, and needs
separate fix + annotations.

Signed-off-by: Pauli Virtanen <pav@iki.fi>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This commit is contained in:
Pauli Virtanen 2026-08-29 17:20:04 +03:00 committed by Luiz Augusto von Dentz
parent 115e6a21c8
commit 889dfdde79
2 changed files with 61 additions and 7 deletions

View File

@ -668,7 +668,7 @@ struct l2cap_conn {
struct l2cap_chan *smp;
struct list_head chan_l;
struct list_head chan_l __guarded_by(&lock);
struct mutex lock;
struct kref ref;
struct list_head users;
@ -954,7 +954,8 @@ void l2cap_cleanup_sockets(void);
bool l2cap_is_socket(struct socket *sock);
void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan);
void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
__must_hold(&chan->lock) __must_hold(&chan->conn->lock);
void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
@ -975,7 +976,7 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan);
int l2cap_ertm_init(struct l2cap_chan *chan);
void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
__must_hold(&chan->lock);
__must_hold(&conn->lock) __must_hold(&chan->lock);
typedef void (*l2cap_chan_func_t)(struct l2cap_chan *chan, void *data);
void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
void *data);

View File

@ -44,8 +44,8 @@ bool enable_ecred = IS_ENABLED(CONFIG_BT_LE_L2CAP_ECRED);
static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
static LIST_HEAD(chan_list);
static DEFINE_RWLOCK(chan_list_lock);
static __guarded_by(&chan_list_lock) LIST_HEAD(chan_list);
static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
u8 code, u8 ident, u16 dlen, void *data);
@ -87,6 +87,7 @@ static inline u8 bdaddr_dst_type(struct hci_conn *hcon)
static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
u16 cid)
__must_hold(&conn->lock)
{
struct l2cap_chan *c;
@ -99,6 +100,7 @@ static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
u16 cid)
__must_hold(&conn->lock)
{
struct l2cap_chan *c;
@ -114,6 +116,7 @@ static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
*/
static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
u16 cid)
__must_hold(&conn->lock)
{
struct l2cap_chan *c;
@ -129,6 +132,7 @@ static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
*/
static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
u16 cid)
__must_hold(&conn->lock)
{
struct l2cap_chan *c;
@ -141,6 +145,7 @@ static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
u8 ident)
__must_hold(&conn->lock)
{
struct l2cap_chan *c;
@ -153,6 +158,7 @@ static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src,
u8 src_type)
__must_hold_shared(&chan_list_lock)
{
struct l2cap_chan *c;
@ -230,6 +236,7 @@ int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
}
static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
__must_hold(&conn->lock)
{
u16 cid, dyn_end;
@ -728,6 +735,7 @@ EXPORT_SYMBOL_GPL(l2cap_chan_del);
static void __l2cap_chan_list_id(struct l2cap_conn *conn, u16 id,
l2cap_chan_func_t func, void *data)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan, *l;
@ -739,6 +747,7 @@ static void __l2cap_chan_list_id(struct l2cap_conn *conn, u16 id,
static void __l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
void *data)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan;
@ -806,6 +815,9 @@ static void l2cap_chan_ecred_connect_reject(struct l2cap_chan *chan)
{
l2cap_state_change(chan, BT_DISCONN);
lockdep_assert_held(&chan->lock);
lockdep_assert_held(&chan->conn->lock);
__l2cap_ecred_conn_rsp_defer(chan);
}
@ -1423,6 +1435,7 @@ static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data)
}
static void l2cap_ecred_connect(struct l2cap_chan *chan)
__must_hold(&chan->conn->lock)
{
struct l2cap_conn *conn = chan->conn;
struct l2cap_ecred_conn_data data;
@ -1456,6 +1469,7 @@ static void l2cap_ecred_connect(struct l2cap_chan *chan)
}
static void l2cap_le_start(struct l2cap_chan *chan)
__must_hold(&chan->conn->lock)
{
struct l2cap_conn *conn = chan->conn;
@ -1476,6 +1490,7 @@ static void l2cap_le_start(struct l2cap_chan *chan)
}
static void l2cap_start_connection(struct l2cap_chan *chan)
__must_hold(&chan->conn->lock)
{
if (chan->conn->hcon->type == LE_LINK) {
l2cap_le_start(chan);
@ -1525,6 +1540,7 @@ static bool l2cap_check_enc_key_size(struct hci_conn *hcon,
}
static void l2cap_do_start(struct l2cap_chan *chan)
__must_hold(&chan->conn->lock)
{
struct l2cap_conn *conn = chan->conn;
@ -1591,6 +1607,7 @@ static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
/* ---- L2CAP connections ---- */
static void l2cap_conn_start(struct l2cap_conn *conn)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan, *tmp;
@ -1599,6 +1616,8 @@ 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);
if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
l2cap_chan_ready(chan);
l2cap_chan_unlock(chan);
@ -1715,6 +1734,8 @@ static void l2cap_conn_ready(struct l2cap_conn *conn)
l2cap_chan_lock(chan);
lockdep_assert_held(&chan->conn->lock);
if (hcon->type == LE_LINK) {
l2cap_le_start(chan);
} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
@ -1737,6 +1758,7 @@ static void l2cap_conn_ready(struct l2cap_conn *conn)
/* Notify sockets that we cannot guaranty reliability anymore */
static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan;
@ -3034,6 +3056,7 @@ static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
/* Copy frame to all raw sockets on that connection */
static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
__must_hold(&conn->lock)
{
struct sk_buff *nskb;
struct l2cap_chan *chan;
@ -4087,6 +4110,7 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
static inline int l2cap_command_rej(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
@ -4119,6 +4143,7 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,
*/
static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,
struct l2cap_chan *pchan)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan;
@ -4148,6 +4173,7 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn,
static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
u8 *data, u8 rsp_code)
__must_hold(&conn->lock)
__context_unsafe(/* conditional locking */)
{
struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
@ -4278,6 +4304,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
static int l2cap_connect_req(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
__must_hold(&conn->lock)
{
if (cmd_len < sizeof(struct l2cap_conn_req))
return -EPROTO;
@ -4289,6 +4316,7 @@ static int l2cap_connect_req(struct l2cap_conn *conn,
static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
u16 scid, dcid, result, status;
@ -4406,6 +4434,7 @@ static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
static inline int l2cap_config_req(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
u16 dcid, flags;
@ -4519,6 +4548,7 @@ static inline int l2cap_config_req(struct l2cap_conn *conn,
static inline int l2cap_config_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
u16 scid, flags, result;
@ -4628,6 +4658,7 @@ static inline int l2cap_config_rsp(struct l2cap_conn *conn,
static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
struct l2cap_disconn_rsp rsp;
@ -4669,6 +4700,7 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
u16 dcid, scid;
@ -4756,6 +4788,7 @@ static inline int l2cap_information_req(struct l2cap_conn *conn,
static inline int l2cap_information_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
u16 type, result;
@ -4863,6 +4896,7 @@ static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
struct hci_conn *hcon = conn->hcon;
@ -4969,6 +5003,7 @@ static void l2cap_put_ident(struct l2cap_conn *conn, u8 code, u8 id)
static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
int err = 0;
@ -5030,6 +5065,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
static int l2cap_le_connect_req(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
struct l2cap_le_conn_rsp rsp;
@ -5178,6 +5214,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
static inline int l2cap_le_credits(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_le_credits *pkt;
struct l2cap_chan *chan;
@ -5227,6 +5264,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_ecred_conn_req *req = (void *) data;
DEFINE_RAW_FLEX(struct l2cap_ecred_conn_rsp, pdu, dcid, L2CAP_ECRED_MAX_CID);
@ -5399,6 +5437,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_ecred_conn_rsp *rsp = (void *) data;
struct hci_conn *hcon = conn->hcon;
@ -5526,6 +5565,7 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,
static inline int l2cap_ecred_reconf_req(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_ecred_reconf_req *req = (void *) data;
struct l2cap_ecred_reconf_rsp rsp;
@ -5624,6 +5664,7 @@ static inline int l2cap_ecred_reconf_req(struct l2cap_conn *conn,
static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan, *tmp;
struct l2cap_ecred_reconf_rsp *rsp = (void *)data;
@ -5664,6 +5705,7 @@ static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn,
static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
struct l2cap_chan *chan;
@ -5691,6 +5733,7 @@ static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
struct l2cap_cmd_hdr *cmd, u16 cmd_len,
u8 *data)
__must_hold(&conn->lock)
{
int err = 0;
@ -5755,6 +5798,7 @@ static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
struct sk_buff *skb)
__must_hold(&conn->lock)
{
struct hci_conn *hcon = conn->hcon;
struct l2cap_cmd_hdr *cmd;
@ -5813,6 +5857,7 @@ static inline void l2cap_sig_send_mtu_rej(struct l2cap_conn *conn, u8 ident)
static inline void l2cap_sig_channel(struct l2cap_conn *conn,
struct sk_buff *skb)
__must_hold(&conn->lock)
{
struct hci_conn *hcon = conn->hcon;
struct l2cap_cmd_hdr *cmd;
@ -7058,6 +7103,7 @@ static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
struct sk_buff *skb)
__must_hold(&conn->lock)
{
struct l2cap_chan *chan;
@ -7166,6 +7212,7 @@ static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
}
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
__must_hold(&conn->lock)
{
struct l2cap_hdr *lh = (void *) skb->data;
struct hci_conn *hcon = conn->hcon;
@ -7275,9 +7322,9 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP)))
conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR;
mutex_init(&conn->lock);
INIT_LIST_HEAD(&conn->chan_l);
scoped_guard(mutex_init, &conn->lock) {
INIT_LIST_HEAD(&conn->chan_l);
}
INIT_LIST_HEAD(&conn->users);
INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
@ -7493,6 +7540,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
__l2cap_chan_add(conn, chan);
lockdep_assert_held(&chan->conn->lock);
/* l2cap_chan_add takes its own ref so we can drop this one */
hci_conn_drop(hcon);
@ -7715,6 +7764,8 @@ static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
}
static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
__must_hold(&chan->lock)
__must_hold(&chan->conn->lock)
{
if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
return;
@ -7747,6 +7798,8 @@ 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);
BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
state_to_string(chan->state));