Bluetooth: Convert IRK list to RCU

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.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
This commit is contained in:
Johan Hedberg 2014-11-13 14:37:48 +02:00 committed by Marcel Holtmann
parent 970d0f1b28
commit adae20cb2d
3 changed files with 38 additions and 29 deletions

View File

@ -121,6 +121,7 @@ struct smp_ltk {
struct smp_irk { struct smp_irk {
struct list_head list; struct list_head list;
struct rcu_head rcu;
bdaddr_t rpa; bdaddr_t rpa;
bdaddr_t bdaddr; bdaddr_t bdaddr;
u8 addr_type; u8 addr_type;

View File

@ -748,16 +748,15 @@ static const struct file_operations white_list_fops = {
static int identity_resolving_keys_show(struct seq_file *f, void *ptr) static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
{ {
struct hci_dev *hdev = f->private; struct hci_dev *hdev = f->private;
struct list_head *p, *n; struct smp_irk *irk;
hci_dev_lock(hdev); rcu_read_lock();
list_for_each_safe(p, n, &hdev->identity_resolving_keys) { list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
struct smp_irk *irk = list_entry(p, struct smp_irk, list);
seq_printf(f, "%pMR (type %u) %*phN %pMR\n", seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
&irk->bdaddr, irk->addr_type, &irk->bdaddr, irk->addr_type,
16, irk->val, &irk->rpa); 16, irk->val, &irk->rpa);
} }
hci_dev_unlock(hdev); rcu_read_unlock();
return 0; return 0;
} }
@ -3114,11 +3113,11 @@ void hci_smp_ltks_clear(struct hci_dev *hdev)
void hci_smp_irks_clear(struct hci_dev *hdev) void hci_smp_irks_clear(struct hci_dev *hdev)
{ {
struct smp_irk *k, *tmp; struct smp_irk *k;
list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) { list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) {
list_del(&k->list); list_del_rcu(&k->list);
kfree(k); kfree_rcu(k, rcu);
} }
} }
@ -3221,18 +3220,23 @@ struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa)
{ {
struct smp_irk *irk; struct smp_irk *irk;
list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { rcu_read_lock();
if (!bacmp(&irk->rpa, rpa)) list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
return irk; if (!bacmp(&irk->rpa, rpa)) {
} rcu_read_unlock();
list_for_each_entry(irk, &hdev->identity_resolving_keys, list) {
if (smp_irk_matches(hdev, irk->val, rpa)) {
bacpy(&irk->rpa, rpa);
return irk; return irk;
} }
} }
list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
if (smp_irk_matches(hdev, irk->val, rpa)) {
bacpy(&irk->rpa, rpa);
rcu_read_unlock();
return irk;
}
}
rcu_read_unlock();
return NULL; return NULL;
} }
@ -3245,11 +3249,15 @@ struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0) if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0)
return NULL; return NULL;
list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { rcu_read_lock();
list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
if (addr_type == irk->addr_type && if (addr_type == irk->addr_type &&
bacmp(bdaddr, &irk->bdaddr) == 0) bacmp(bdaddr, &irk->bdaddr) == 0) {
rcu_read_unlock();
return irk; return irk;
}
} }
rcu_read_unlock();
return NULL; return NULL;
} }
@ -3344,7 +3352,7 @@ struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
bacpy(&irk->bdaddr, bdaddr); bacpy(&irk->bdaddr, bdaddr);
irk->addr_type = addr_type; irk->addr_type = addr_type;
list_add(&irk->list, &hdev->identity_resolving_keys); list_add_rcu(&irk->list, &hdev->identity_resolving_keys);
} }
memcpy(irk->val, val, 16); memcpy(irk->val, val, 16);
@ -3390,16 +3398,16 @@ int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type)
void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type) void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type)
{ {
struct smp_irk *k, *tmp; struct smp_irk *k;
list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) { list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) {
if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type) if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type)
continue; continue;
BT_DBG("%s removing %pMR", hdev->name, bdaddr); BT_DBG("%s removing %pMR", hdev->name, bdaddr);
list_del(&k->list); list_del_rcu(&k->list);
kfree(k); kfree_rcu(k, rcu);
} }
} }

View File

@ -393,8 +393,8 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
} }
if (smp->remote_irk) { if (smp->remote_irk) {
list_del(&smp->remote_irk->list); list_del_rcu(&smp->remote_irk->list);
kfree(smp->remote_irk); kfree_rcu(smp->remote_irk, rcu);
} }
} }
@ -655,8 +655,8 @@ static void smp_notify_keys(struct l2cap_conn *conn)
* just remove it. * just remove it.
*/ */
if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
list_del(&smp->remote_irk->list); list_del_rcu(&smp->remote_irk->list);
kfree(smp->remote_irk); kfree_rcu(smp->remote_irk, rcu);
smp->remote_irk = NULL; smp->remote_irk = NULL;
} }
} }
@ -1696,7 +1696,7 @@ int smp_register(struct hci_dev *hdev)
BT_DBG("%s", hdev->name); 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)) { if (IS_ERR(tfm_aes)) {
int err = PTR_ERR(tfm_aes); int err = PTR_ERR(tfm_aes);
BT_ERR("Unable to create crypto context"); BT_ERR("Unable to create crypto context");