Skip to contentSkip to conversation

Bluetooth: More lockdep warning fixes with the help of RCU

PATCH In review Johan Hedberg 2 participants 6 messages Nov 13, 2014 – Nov 15, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Bluetooth: More lockdep warning fixes with the help of RCU

Hi,

This patch set converts the LTK and IRK lists to use RCU which eliminates the need to always hold the hdev lock while accessing these lists. This helps us fix more lockdep warnings arising from hdev locking in smp.c.

What this patch set doesn't fix is the general problem of the SMP context holding references to list entries without any guarantee that those references stay valid if some other thread goes and removes the entries. For this the proper fix would probably be to introduce kref for the entries to ensure that external references stay valid. However, since that requires considerably more code it's better to first merge most lists into a single "remote device info" list that's protected both by kref and RCU.

Johan

----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Convert LTK list to RCU
Bluetooth: Convert IRK list to RCU
Bluetooth: Remove unnecessary hdev locking in smp.c
Bluetooth: Add debug logs to help track locking issues

 include/net/bluetooth/hci_core.h |  2 +
 net/bluetooth/hci_core.c         | 88 ++++++++++++++++++++++----------------
 net/bluetooth/hci_event.c        |  4 +-
 net/bluetooth/l2cap_core.c       |  3 +-
 net/bluetooth/l2cap_sock.c       | 10 ++++-
 net/bluetooth/smp.c              | 27 ++++--------
 6 files changed, 75 insertions(+), 59 deletions(-)
#1 lore raw

PATCH 1/4 Bluetooth: Convert LTK list to RCU

From: Johan Hedberg <[email protected]>

This patch set converts the hdev->long_term_keys list to use RCU to eliminate the need to use hci_dev_lock/unlock.

---
 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c         | 42 +++++++++++++++++++++++-----------------
 net/bluetooth/hci_event.c        |  4 ++--
 net/bluetooth/smp.c              | 10 ++++------
 4 files changed, 31 insertions(+), 26 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
index 4e39a5adfcab..a4adef22ad7c 100644
@@ -108,6 +108,7 @@ struct smp_csrk {
 
 struct smp_ltk {
 	struct list_head list;
+	struct rcu_head rcu;
 	bdaddr_t bdaddr;
 	u8 bdaddr_type;
 	u8 authenticated;
net/bluetooth/hci_core.c +24 -18

102 lines not on this page · raw message

net/bluetooth/hci_event.c +2 -2
index aec3b1dce1cc..09d76547d985 100644
@@ -4578,8 +4578,8 @@ static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	 */
 	if (ltk->type == SMP_STK) {
 		set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
-		list_del(&ltk->list);
-		kfree(ltk);
+		list_del_rcu(&ltk->list);
+		kfree_rcu(ltk, rcu);
 	} else {
 		clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
 	}
net/bluetooth/smp.c +4 -6
index 3dc5f0e66405..fd2dfe5222bc 100644
@@ -383,13 +383,13 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
 	/* If pairing failed clean up any keys we might have */
 	if (!complete) {
 		if (smp->ltk) {
-			list_del(&smp->ltk->list);
-			kfree(smp->ltk);
+			list_del_rcu(&smp->ltk->list);
+			kfree_rcu(smp->ltk, rcu);
 		}
 
 		if (smp->slave_ltk) {
-			list_del(&smp->slave_ltk->list);
-			kfree(smp->slave_ltk);
+			list_del_rcu(&smp->slave_ltk->list);
+			kfree_rcu(smp->slave_ltk, rcu);
 		}
 
 		if (smp->remote_irk) {
@@ -1321,7 +1321,6 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
 
 	skb_pull(skb, sizeof(*rp));
 
-	hci_dev_lock(hdev);
 	authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
 	ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
 			  authenticated, smp->tk, smp->enc_key_size,
@@ -1329,7 +1328,6 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
 	smp->ltk = ltk;
 	if (!(smp->remote_key_dist & KEY_DIST_MASK))
 		smp_distribute_keys(smp);
-	hci_dev_unlock(hdev);
 
 	return 0;
 }
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/4 Bluetooth: Convert IRK list to RCU

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch set converts the hdev->identity_resolving_keys list to use RCU to eliminate the need to use hci_dev_lock/unlock.

An additional change that must be done is to remove use of CRYPTO_ALG_ASYNC for the hdev-specific AES crypto context. The reason is that this context is used for matching RPAs and the loop that does the matching is under the RCU read lock, i.e. is an atomic section which cannot sleep.

---
 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c         | 46 +++++++++++++++++++++++-----------------
 net/bluetooth/smp.c              | 10 ++++-----
 3 files changed, 33 insertions(+), 24 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
index a4adef22ad7c..fe2d5f299e12 100644
@@ -121,6 +121,7 @@ struct smp_ltk {
 
 struct smp_irk {
 	struct list_head list;
+	struct rcu_head rcu;
 	bdaddr_t rpa;
 	bdaddr_t bdaddr;
 	u8 addr_type;
net/bluetooth/hci_core.c +27 -19

105 lines not on this page · raw message

net/bluetooth/smp.c +5 -5
index fd2dfe5222bc..7b610f615257 100644
@@ -393,8 +393,8 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
 		}
 
 		if (smp->remote_irk) {
-			list_del(&smp->remote_irk->list);
-			kfree(smp->remote_irk);
+			list_del_rcu(&smp->remote_irk->list);
+			kfree_rcu(smp->remote_irk, rcu);
 		}
 	}
 
@@ -655,8 +655,8 @@ static void smp_notify_keys(struct l2cap_conn *conn)
 		 * just remove it.
 		 */
 		if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
-			list_del(&smp->remote_irk->list);
-			kfree(smp->remote_irk);
+			list_del_rcu(&smp->remote_irk->list);
+			kfree_rcu(smp->remote_irk, rcu);
 			smp->remote_irk = NULL;
 		}
 	}
@@ -1696,7 +1696,7 @@ int smp_register(struct hci_dev *hdev)
 
 	BT_DBG("%s", hdev->name);
 
-	tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
+	tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
 	if (IS_ERR(tfm_aes)) {
 		int err = PTR_ERR(tfm_aes);
 		BT_ERR("Unable to create crypto context");
Signature · 1 line
2.1.0
#3 lore raw

PATCH 3/4 Bluetooth: Remove unnecessary hdev locking in smp.c

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

Now that the SMP related key lists are converted to RCU there is nothing in smp_cmd_sign_info() or smp_cmd_ident_addr_info() that would require taking the hdev lock (including the smp_distribute_keys call). This patch removes this unnecessary locking.

---
 net/bluetooth/smp.c | 7 -------
 1 file changed, 7 deletions(-)
net/bluetooth/smp.c +0 -7
index 7b610f615257..069b76e03b57 100644
@@ -1374,8 +1374,6 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
 
 	skb_pull(skb, sizeof(*info));
 
-	hci_dev_lock(hcon->hdev);
-
 	/* Strictly speaking the Core Specification (4.1) allows sending
 	 * an empty address which would force us to rely on just the IRK
 	 * as "identity information". However, since such
@@ -1403,8 +1401,6 @@ distribute:
 	if (!(smp->remote_key_dist & KEY_DIST_MASK))
 		smp_distribute_keys(smp);
 
-	hci_dev_unlock(hcon->hdev);
-
 	return 0;
 }
 
@@ -1413,7 +1409,6 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
 	struct smp_cmd_sign_info *rp = (void *) skb->data;
 	struct l2cap_chan *chan = conn->smp;
 	struct smp_chan *smp = chan->data;
-	struct hci_dev *hdev = conn->hcon->hdev;
 	struct smp_csrk *csrk;
 
 	BT_DBG("conn %p", conn);
@@ -1426,7 +1421,6 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
 
 	skb_pull(skb, sizeof(*rp));
 
-	hci_dev_lock(hdev);
 	csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
 	if (csrk) {
 		csrk->master = 0x01;
@@ -1434,7 +1428,6 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
 	}
 	smp->csrk = csrk;
 	smp_distribute_keys(smp);
-	hci_dev_unlock(hdev);
 
 	return 0;
 }
Signature · 1 line
2.1.0
#4 lore raw

PATCH 4/4 Bluetooth: Add debug logs to help track locking issues

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds some extra debug logs to L2CAP related code. These are mainly to help track locking issues but will probably be useful for debugging other types of issues as well.

---
 net/bluetooth/l2cap_core.c |  3 ++-
 net/bluetooth/l2cap_sock.c | 10 +++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)
net/bluetooth/l2cap_core.c +2 -1
index 15784d32108d..8e1273173020 100644
@@ -570,7 +570,8 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err)
 
 	__clear_chan_timer(chan);
 
-	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
+	BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err,
+	       state_to_string(chan->state));
 
 	chan->ops->teardown(chan, err);
 
net/bluetooth/l2cap_sock.c +9 -1
index a5aa9f92b5e2..b0efb7202957 100644
@@ -1102,6 +1102,8 @@ static int l2cap_sock_shutdown(struct socket *sock, int how)
 	chan = l2cap_pi(sk)->chan;
 	conn = chan->conn;
 
+	BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
+
 	if (conn)
 		mutex_lock(&conn->chan_lock);
 
@@ -1159,12 +1161,16 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
 {
 	struct sock *sk;
 
-	BT_DBG("parent %p", parent);
+	BT_DBG("parent %p state %s", parent,
+	       state_to_string(parent->sk_state));
 
 	/* Close not yet accepted channels */
 	while ((sk = bt_accept_dequeue(parent, NULL))) {
 		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
 
+		BT_DBG("child chan %p state %s", chan,
+		       state_to_string(chan->state));
+
 		l2cap_chan_lock(chan);
 		__clear_chan_timer(chan);
 		l2cap_chan_close(chan, ECONNRESET);
@@ -1252,6 +1258,8 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
 	struct sock *sk = chan->data;
 	struct sock *parent;
 
+	BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
+
 	/* This callback can be called both for server (BT_LISTEN)
 	 * sockets as well as "normal" ones. To avoid lockdep warnings
 	 * with child socket locking (through l2cap_sock_cleanup_listen)
Signature · 1 line
2.1.0
maintainer
#5 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 23 lines

This patch set converts the LTK and IRK lists to use RCU which eliminates the need to always hold the hdev lock while accessing these lists. This helps us fix more lockdep warnings arising from hdev locking in smp.c.

What this patch set doesn't fix is the general problem of the SMP context holding references to list entries without any guarantee that those references stay valid if some other thread goes and removes the entries. For this the proper fix would probably be to introduce kref for the entries to ensure that external references stay valid. However, since that requires considerably more code it's better to first merge most lists into a single "remote device info" list that's protected both by kref and RCU.

Johan

----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Convert LTK list to RCU
Bluetooth: Convert IRK list to RCU
Bluetooth: Remove unnecessary hdev locking in smp.c
Bluetooth: Add debug logs to help track locking issues

Quoted 7 lines
include/net/bluetooth/hci_core.h |  2 +
net/bluetooth/hci_core.c         | 88 ++++++++++++++++++++++----------------
net/bluetooth/hci_event.c        |  4 +-
net/bluetooth/l2cap_core.c       |  3 +-
net/bluetooth/l2cap_sock.c       | 10 ++++-
net/bluetooth/smp.c              | 27 ++++--------
6 files changed, 75 insertions(+), 59 deletions(-)

all 4 patches have been applied to bluetooth-next tree.

Regards

Marcel