Skip to content

Commit

Permalink
Bluetooth: Add extra device reference counting for connections
Browse files Browse the repository at this point in the history
The device model itself has no real usable reference counting at the
moment and this causes problems if parents are deleted before their
children. The device model itself handles the memory details of this
correctly, but the uevent order is not consistent. This causes various
problems for systems like HAL or even X.

So until device_put() does a proper cleanup, the device for Bluetooth
connection will be protected with an extra reference counting to ensure
the correct order of uevents when connections are terminated.

This is not an automatic feature. Higher Bluetooth layers like HIDP or
BNEP should grab this new reference to ensure that their uevents are
send before the ones from the parent device.

Based on a report by Brian Rogers <brian@xyzw.org>

Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
  • Loading branch information
holtmann committed Aug 22, 2009
1 parent 364f635 commit 9eba32b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/net/bluetooth/hci_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ struct hci_conn {
struct work_struct work_del;

struct device dev;
atomic_t devref;

struct hci_dev *hdev;
void *l2cap_data;
Expand Down Expand Up @@ -339,6 +340,9 @@ int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
void hci_conn_enter_active_mode(struct hci_conn *conn);
void hci_conn_enter_sniff_mode(struct hci_conn *conn);

void hci_conn_hold_device(struct hci_conn *conn);
void hci_conn_put_device(struct hci_conn *conn);

static inline void hci_conn_hold(struct hci_conn *conn)
{
atomic_inc(&conn->refcnt);
Expand Down
17 changes: 16 additions & 1 deletion net/bluetooth/hci_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
if (hdev->notify)
hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);

atomic_set(&conn->devref, 0);

hci_conn_init_sysfs(conn);

tasklet_enable(&hdev->tx_task);
Expand Down Expand Up @@ -288,7 +290,7 @@ int hci_conn_del(struct hci_conn *conn)

skb_queue_purge(&conn->data_q);

hci_conn_del_sysfs(conn);
hci_conn_put_device(conn);

hci_dev_put(hdev);

Expand Down Expand Up @@ -583,6 +585,19 @@ void hci_conn_check_pending(struct hci_dev *hdev)
hci_dev_unlock(hdev);
}

void hci_conn_hold_device(struct hci_conn *conn)
{
atomic_inc(&conn->devref);
}
EXPORT_SYMBOL(hci_conn_hold_device);

void hci_conn_put_device(struct hci_conn *conn)
{
if (atomic_dec_and_test(&conn->devref))
hci_conn_del_sysfs(conn);
}
EXPORT_SYMBOL(hci_conn_put_device);

int hci_get_conn_list(void __user *arg)
{
struct hci_conn_list_req req, *cl;
Expand Down
2 changes: 2 additions & 0 deletions net/bluetooth/hci_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *s
} else
conn->state = BT_CONNECTED;

hci_conn_hold_device(conn);
hci_conn_add_sysfs(conn);

if (test_bit(HCI_AUTH, &hdev->flags))
Expand Down Expand Up @@ -1693,6 +1694,7 @@ static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_bu
conn->handle = __le16_to_cpu(ev->handle);
conn->state = BT_CONNECTED;

hci_conn_hold_device(conn);
hci_conn_add_sysfs(conn);
break;

Expand Down

0 comments on commit 9eba32b

Please sign in to comment.