Skip to content

Commit 08c30ac

Browse files
Dean JenkinsGustavo Padovan
authored andcommitted
Bluetooth: Remove RFCOMM session refcnt
Previous commits have improved the handling of the RFCOMM session timer and the RFCOMM session pointers such that freed RFCOMM session structures should no longer be erroneously accessed. The RFCOMM session refcnt now has no purpose and will be deleted by this commit. Note that the RFCOMM session is now deleted as soon as the RFCOMM control channel link is no longer required. This makes the lifetime of the RFCOMM session deterministic and absolute. Previously with the refcnt, there was uncertainty about when the session structure would be deleted because the relative refcnt prevented the session structure from being deleted at will. It was noted that the refcnt could malfunction under very heavy real-time processor loading in embedded SMP environments. This could cause premature RFCOMM session deletion or double session deletion that could result in kernel crashes. Removal of the refcnt prevents this issue. There are 4 connection / disconnection RFCOMM session scenarios: host initiated control link ---> host disconnected control link host initiated ctrl link ---> remote device disconnected ctrl link remote device initiated ctrl link ---> host disconnected ctrl link remote device initiated ctrl link ---> remote device disc'ed ctrl link The control channel connection procedures are independent of the disconnection procedures. Strangely, the RFCOMM session refcnt was applying special treatment so erroneously combining connection and disconnection events. This commit fixes this issue by removing some session code that used the "initiator" member of the session structure that was intended for use with the data channels. Signed-off-by: Dean Jenkins <Dean_Jenkins@mentor.com> Acked-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
1 parent 8ff52f7 commit 08c30ac

2 files changed

Lines changed: 5 additions & 45 deletions

File tree

‎include/net/bluetooth/rfcomm.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ struct rfcomm_session {
158158
struct timer_list timer;
159159
unsigned long state;
160160
unsigned long flags;
161-
atomic_t refcnt;
162161
int initiator;
163162

164163
/* Default DLC parameters */
@@ -276,12 +275,6 @@ static inline void rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
276275
void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src,
277276
bdaddr_t *dst);
278277

279-
static inline void rfcomm_session_hold(struct rfcomm_session *s)
280-
{
281-
if (s)
282-
atomic_inc(&s->refcnt);
283-
}
284-
285278
/* ---- RFCOMM sockets ---- */
286279
struct sockaddr_rc {
287280
sa_family_t rc_family;

‎net/bluetooth/rfcomm/core.c‎

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,6 @@ static void rfcomm_schedule(void)
108108
wake_up_process(rfcomm_thread);
109109
}
110110

111-
static struct rfcomm_session *rfcomm_session_put(struct rfcomm_session *s)
112-
{
113-
if (s && atomic_dec_and_test(&s->refcnt))
114-
s = rfcomm_session_del(s);
115-
116-
return s;
117-
}
118-
119111
/* ---- RFCOMM FCS computation ---- */
120112

121113
/* reversed, 8-bit, poly=0x07 */
@@ -251,16 +243,14 @@ static void rfcomm_session_set_timer(struct rfcomm_session *s, long timeout)
251243
{
252244
BT_DBG("session %p state %ld timeout %ld", s, s->state, timeout);
253245

254-
if (!mod_timer(&s->timer, jiffies + timeout))
255-
rfcomm_session_hold(s);
246+
mod_timer(&s->timer, jiffies + timeout);
256247
}
257248

258249
static void rfcomm_session_clear_timer(struct rfcomm_session *s)
259250
{
260251
BT_DBG("session %p state %ld", s, s->state);
261252

262-
if (del_timer_sync(&s->timer))
263-
rfcomm_session_put(s);
253+
del_timer_sync(&s->timer);
264254
}
265255

266256
/* ---- RFCOMM DLCs ---- */
@@ -338,8 +328,6 @@ static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d)
338328
{
339329
BT_DBG("dlc %p session %p", d, s);
340330

341-
rfcomm_session_hold(s);
342-
343331
rfcomm_session_clear_timer(s);
344332
rfcomm_dlc_hold(d);
345333
list_add(&d->list, &s->dlcs);
@@ -358,8 +346,6 @@ static void rfcomm_dlc_unlink(struct rfcomm_dlc *d)
358346

359347
if (list_empty(&s->dlcs))
360348
rfcomm_session_set_timer(s, RFCOMM_IDLE_TIMEOUT);
361-
362-
rfcomm_session_put(s);
363349
}
364350

365351
static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci)
@@ -678,8 +664,6 @@ static struct rfcomm_session *rfcomm_session_close(struct rfcomm_session *s,
678664

679665
BT_DBG("session %p state %ld err %d", s, s->state, err);
680666

681-
rfcomm_session_hold(s);
682-
683667
s->state = BT_CLOSED;
684668

685669
/* Close all dlcs */
@@ -690,7 +674,7 @@ static struct rfcomm_session *rfcomm_session_close(struct rfcomm_session *s,
690674
}
691675

692676
rfcomm_session_clear_timer(s);
693-
return rfcomm_session_put(s);
677+
return rfcomm_session_del(s);
694678
}
695679

696680
static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
@@ -1884,13 +1868,8 @@ static struct rfcomm_session *rfcomm_process_rx(struct rfcomm_session *s)
18841868
kfree_skb(skb);
18851869
}
18861870

1887-
if (s && (sk->sk_state == BT_CLOSED)) {
1888-
if (!s->initiator)
1889-
s = rfcomm_session_put(s);
1890-
1891-
if (s)
1892-
s = rfcomm_session_close(s, sk->sk_err);
1893-
}
1871+
if (s && (sk->sk_state == BT_CLOSED))
1872+
s = rfcomm_session_close(s, sk->sk_err);
18941873

18951874
return s;
18961875
}
@@ -1917,8 +1896,6 @@ static void rfcomm_accept_connection(struct rfcomm_session *s)
19171896

19181897
s = rfcomm_session_add(nsock, BT_OPEN);
19191898
if (s) {
1920-
rfcomm_session_hold(s);
1921-
19221899
/* We should adjust MTU on incoming sessions.
19231900
* L2CAP MTU minus UIH header and FCS. */
19241901
s->mtu = min(l2cap_pi(nsock->sk)->chan->omtu,
@@ -1967,7 +1944,6 @@ static void rfcomm_process_sessions(void)
19671944
if (test_and_clear_bit(RFCOMM_TIMED_OUT, &s->flags)) {
19681945
s->state = BT_DISCONN;
19691946
rfcomm_send_disc(s, 0);
1970-
rfcomm_session_put(s);
19711947
continue;
19721948
}
19731949

@@ -1976,8 +1952,6 @@ static void rfcomm_process_sessions(void)
19761952
continue;
19771953
}
19781954

1979-
rfcomm_session_hold(s);
1980-
19811955
switch (s->state) {
19821956
case BT_BOUND:
19831957
s = rfcomm_check_connection(s);
@@ -1990,8 +1964,6 @@ static void rfcomm_process_sessions(void)
19901964

19911965
if (s)
19921966
rfcomm_process_dlcs(s);
1993-
1994-
rfcomm_session_put(s);
19951967
}
19961968

19971969
rfcomm_unlock();
@@ -2041,7 +2013,6 @@ static int rfcomm_add_listener(bdaddr_t *ba)
20412013
if (!s)
20422014
goto failed;
20432015

2044-
rfcomm_session_hold(s);
20452016
return 0;
20462017
failed:
20472018
sock_release(sock);
@@ -2099,8 +2070,6 @@ static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt)
20992070
if (!s)
21002071
return;
21012072

2102-
rfcomm_session_hold(s);
2103-
21042073
list_for_each_safe(p, n, &s->dlcs) {
21052074
d = list_entry(p, struct rfcomm_dlc, list);
21062075

@@ -2132,8 +2101,6 @@ static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt)
21322101
set_bit(RFCOMM_AUTH_REJECT, &d->flags);
21332102
}
21342103

2135-
rfcomm_session_put(s);
2136-
21372104
rfcomm_schedule();
21382105
}
21392106

0 commit comments

Comments
 (0)