Bluetooth: Rework the RFCOMM session refcnt
Subsystems (2)
v2 cover Bluetooth: Rework the RFCOMM session refcnt
This v2 patchset contains:
a) minor layout fixes for patches 2 and 3 based on comments from Gustavo
b) as per Gustavo's comment, Marcel's Ack has been added to get patch
This patchset consists of the following 6 patches that reworks the RFCOMM session refcnt by adding handling for a NULL RFCOMM session pointer when the session has been deleted followed by removing the now redundant refcnt mechanism:
----------------------------------------------------------------
Dean Jenkins (6):
Bluetooth: Avoid rfcomm_session_timeout using freed session
Bluetooth: Check rfcomm session and DLC exists on socket close
Bluetooth: Return RFCOMM session ptrs to avoid freed session
Bluetooth: Remove RFCOMM session refcnt
Bluetooth: Remove redundant call to rfcomm_send_disc
Bluetooth: Remove redundant RFCOMM BT_CLOSED settings
include/net/bluetooth/rfcomm.h | 6 ---- net/bluetooth/rfcomm/core.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------- 2 files changed, 76 insertions(+), 93 deletions(-)
For completeness, the original v1 patchset information is retained below:
The motivation for the change was RFCOMM kernel crashes in a 2.6.34 based ARM SMP kernel. It is noted that RFCOMM in kernel 3.7 still contains the RFCOMM session refcnt so the patches have been forward ported. Crashes were due to malfunctions of the RFCOMM session refcnt such as premature deletion of the session and double deletion of the session. Some crashes were caused by the interaction of 2 kernel threads under very high processor loading.
Note that the refcnt is incorrectly initialised, it should be set to 1 before allowing the session structure to be used, the old code used a value of 0 and this contributes to a weak implementation of the refcnt.
In kernels later than 2.6.34, the threading model for RFCOMM was modified to use work queues and this reduced the probability of a RFCOMM crash. This helps to explain why the reports of RFCOMM crashes went away. However, the implementation of the RFCOMM session refcnt is weak and is in fact logically incorrect by using the RFCOMM data channel "initiator" flag. The control channel disconnection procedures do not care about how the control channel was established eg. by host or remote device request.
The patches also make it clear when the RFCOMM session has been deleted by using a NULL pointer. The old code has a possibility to access freed RFCOMM session structures because there are multiple local copies of the pointer. The patches ensure that local copies of the pointer are set to NULL when the session has been deleted so avoiding any possibility of accessing freed memory.
Therefore, the patchset cleans up the disconnection of the RFCOMM session control channel and now the code is deterministic and is understandable by code review.
These patches were lightly tested against kernel 3.7.3 on a x86 PC. The design of the patches were proven on a commercial project using a modified embedded ARM SMP kernel 2.6.34.13. All previously observed RFCOMM control channel disconnection kernel crashes on ARM 2.6.34.13 went away with the patches applied.
Patch summaries are shown below:
0001: Bluetooth: Avoid rfcomm_session_timeout using freed session
Use del_timer_sync() instead of del_timer() as this ensures that rfcomm_session_timeout() is not running on a different CPU when rfcomm_session_put() is called. This avoids a race condition on SMP systems because potentially rfcomm_session_timeout() could reuse the freed RFCOMM session structure caused by the execution of rfcomm_session_put().
Note that this modification makes the reason for the RFCOMM session refcnt mechanism redundant.
0002: Bluetooth: Check rfcomm session and DLC exists on socket close
A race condition exists between near simultaneous asynchronous DLC data channel disconnection requests from the host and remote device. This causes the socket layer to request a socket shutdown at the same time the rfcomm core is processing the disconnect request from the remote device.
The socket layer retains a copy of a struct rfcomm_dlc d pointer. The d pointer refers to a copy of a struct rfcomm_session. When the socket layer thread performs a socket shutdown, the thread may wait on a rfcomm lock in rfcomm_dlc_close(). This means that whilst the thread waits, the rfcomm_session and/or rfcomm_dlc structures pointed to by d maybe freed due to rfcomm core handling. Consequently, when the rfcomm lock becomes available and the thread runs, a malfunction could occur as a freed rfcomm_session structure and/or a freed rfcomm_dlc structure will be erroneously accessed.
Therefore, after the rfcomm lock is acquired, check that the struct rfcomm_session is still valid by searching the rfcomm session list. If the session is valid then validate the d pointer by searching the rfcomm session list of active DLCs for the rfcomm_dlc structure pointed by d.
0003: Bluetooth: Return RFCOMM session ptrs to avoid freed session
Unfortunately, the design retains local copies of the s RFCOMM session pointer in various code blocks and this invites the erroneous access to a freed RFCOMM session structure.
Therefore, return the RFCOMM session pointer back up the call stack to avoid accessing a freed RFCOMM session structure. When the RFCOMM session is deleted, NULL is passed up the call stack.
If active DLCs exist when the rfcomm session is terminating, avoid a memory leak of rfcomm_dlc structures by ensuring that rfcomm_session_close() is used instead of rfcomm_session_del().
0004: 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.
0005: Bluetooth: Remove redundant call to rfcomm_send_disc
In rfcomm_session_del() remove the redundant call to rfcomm_send_disc() because it is not possible for the session to be in BT_CONNECTED state during deletion of the session.
0006: Bluetooth: Remove redundant RFCOMM BT_CLOSED settings
rfcomm_session_close() sets the RFCOMM session state to BT_CLOSED. However, in multiple places immediately before the function is called, the RFCOMM session is set to BT_CLOSED. Therefore, remove these unnecessary state settings.
PATCH v2 1/6 Bluetooth: Avoid rfcomm_session_timeout using freed session
Use del_timer_sync() instead of del_timer() as this ensures that rfcomm_session_timeout() is not running on a different CPU when rfcomm_session_put() is called. This avoids a race condition on SMP systems because potentially rfcomm_session_timeout() could reuse the freed RFCOMM session structure caused by the execution of rfcomm_session_put().
Note that this modification makes the reason for the RFCOMM session refcnt mechanism redundant.
--- net/bluetooth/rfcomm/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/rfcomm/core.c +1 -1
@@ -257,7 +257,7 @@ static void rfcomm_session_clear_timer(struct rfcomm_session *s) {BT_DBG ("session %p state %ld" , s, s->state); -if (timer_pending (&s->timer) &&del_timer (&s->timer)) +if (del_timer_sync (&s->timer))rfcomm_session_put (s); }
Signature · 1 line
1.7.10.1
PATCH v2 2/6 Bluetooth: Check rfcomm session and DLC exists on socket close
In reply to Dean JenkinsA race condition exists between near simultaneous asynchronous DLC data channel disconnection requests from the host and remote device. This causes the socket layer to request a socket shutdown at the same time the rfcomm core is processing the disconnect request from the remote device.
The socket layer retains a copy of a struct rfcomm_dlc d pointer. The d pointer refers to a copy of a struct rfcomm_session. When the socket layer thread performs a socket shutdown, the thread may wait on a rfcomm lock in rfcomm_dlc_close(). This means that whilst the thread waits, the rfcomm_session and/or rfcomm_dlc structures pointed to by d maybe freed due to rfcomm core handling. Consequently, when the rfcomm lock becomes available and the thread runs, a malfunction could occur as a freed rfcomm_session structure and/or a freed rfcomm_dlc structure will be erroneously accessed.
Therefore, after the rfcomm lock is acquired, check that the struct rfcomm_session is still valid by searching the rfcomm session list. If the session is valid then validate the d pointer by searching the rfcomm session list of active DLCs for the rfcomm_dlc structure pointed by d.
--- net/bluetooth/rfcomm/core.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
net/bluetooth/rfcomm/core.c +24 -2
@@ -493,12 +493,34 @@ static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)int rfcomm_dlc_close (struct rfcomm_dlc *d,int err) { -int r; +int r =0 ; +struct rfcomm_dlc *d_list; +struct rfcomm_session *s, *s_list; + +BT_DBG ("dlc %p state %ld dlci %d err %d" , d, d->state, d->dlci, err);rfcomm_lock (); - r =__rfcomm_dlc_close (d, err); + s = d->session; +if (!s) + goto no_session; + +/* after waiting on the mutex check the session still exists +* then check the dlc still exists +*/ +list_for_each_entry (s_list, &session_list, list) { +if (s_list == s) { +list_for_each_entry (d_list, &s->dlcs, list) { +if (d_list == d) { + r =__rfcomm_dlc_close (d, err); +break ; + } + } +break ; + } + } +no_session:rfcomm_unlock ();return r; }
Signature · 1 line
1.7.10.1
PATCH v2 3/6 Bluetooth: Return RFCOMM session ptrs to avoid freed session
In reply to Dean JenkinsUnfortunately, the design retains local copies of the s RFCOMM session pointer in various code blocks and this invites the erroneous access to a freed RFCOMM session structure.
Therefore, return the RFCOMM session pointer back up the call stack to avoid accessing a freed RFCOMM session structure. When the RFCOMM session is deleted, NULL is passed up the call stack.
If active DLCs exist when the rfcomm session is terminating, avoid a memory leak of rfcomm_dlc structures by ensuring that rfcomm_session_close() is used instead of rfcomm_session_del().
--- include/net/bluetooth/rfcomm.h | 3 +- net/bluetooth/rfcomm/core.c | 106 +++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 51 deletions(-)
include/net/bluetooth/rfcomm.h +2 -1
@@ -278,7 +278,8 @@ void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src,static inline void rfcomm_session_hold (struct rfcomm_session *s) { -atomic_inc (&s->refcnt); +if (s) +atomic_inc (&s->refcnt); }/* ---- RFCOMM sockets ---- */
net/bluetooth/rfcomm/core.c +56 -50
271 lines not on this page · raw message
Signature · 1 line
1.7.10.1
PATCH v2 4/6 Bluetooth: Remove RFCOMM session refcnt
In reply to Dean JenkinsPrevious 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.
--- include/net/bluetooth/rfcomm.h | 7 ------- net/bluetooth/rfcomm/core.c | 43 +++++----------------------------------- 2 files changed, 5 insertions(+), 45 deletions(-)
include/net/bluetooth/rfcomm.h +0 -7
@@ -158,7 +158,6 @@ struct rfcomm_session {struct timer_list timer;unsigned long state;unsigned long flags; -atomic_t refcnt;int initiator;/* Default DLC parameters */
@@ -276,12 +275,6 @@ static inline void rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)void rfcomm_session_getaddr (struct rfcomm_session *s,bdaddr_t *src,bdaddr_t *dst); -static inline void rfcomm_session_hold (struct rfcomm_session *s) -{ -if (s) -atomic_inc (&s->refcnt); -} -/* ---- RFCOMM sockets ---- */ struct sockaddr_rc {sa_family_t rc_family;
net/bluetooth/rfcomm/core.c +5 -38
133 lines not on this page · raw message
Signature · 1 line
1.7.10.1
PATCH v2 5/6 Bluetooth: Remove redundant call to rfcomm_send_disc
In reply to Dean JenkinsIn rfcomm_session_del() remove the redundant call to rfcomm_send_disc() because it is not possible for the session to be in BT_CONNECTED state during deletion of the session.
--- net/bluetooth/rfcomm/core.c | 3 --- 1 file changed, 3 deletions(-)
net/bluetooth/rfcomm/core.c +0 -3
@@ -627,9 +627,6 @@ static struct rfcomm_session *rfcomm_session_del(struct rfcomm_session *s)list_del (&s->list); -if (state ==BT_CONNECTED ) -rfcomm_send_disc (s,0 ); -rfcomm_session_clear_timer (s);sock_release (s->sock);kfree (s);
Signature · 1 line
1.7.10.1
PATCH v2 6/6 Bluetooth: Remove redundant RFCOMM BT_CLOSED settings
In reply to Dean Jenkinsrfcomm_session_close() sets the RFCOMM session state to BT_CLOSED. However, in multiple places immediately before the function is called, the RFCOMM session is set to BT_CLOSED. Therefore, remove these unnecessary state settings.
--- net/bluetooth/rfcomm/core.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
net/bluetooth/rfcomm/core.c +2 -5
@@ -659,10 +659,10 @@ static struct rfcomm_session *rfcomm_session_close(struct rfcomm_session *s,struct rfcomm_dlc *d;struct list_head *p, *n; -BT_DBG ("session %p state %ld err %d" , s, s->state, err); - s->state =BT_CLOSED ; +BT_DBG ("session %p state %ld err %d" , s, s->state, err); +/* Close all dlcs */ list_for_each_safe (p, n, &s->dlcs) { d = list_entry(p, struct rfcomm_dlc, list);
@@ -1188,7 +1188,6 @@ static struct rfcomm_session *rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci)else err =ECONNRESET ; - s->state =BT_CLOSED ; s =rfcomm_session_close (s, err); }return s;
@@ -1224,7 +1223,6 @@ static struct rfcomm_session *rfcomm_recv_disc(struct rfcomm_session *s,else err =ECONNRESET ; - s->state =BT_CLOSED ; s =rfcomm_session_close (s, err); }return s;
@@ -1921,7 +1919,6 @@ static struct rfcomm_session *rfcomm_check_connection(struct rfcomm_session *s)break ;case BT_CLOSED : - s->state =BT_CLOSED ; s =rfcomm_session_close (s, sk->sk_err);break ; }
Signature · 1 line
1.7.10.1
Re: [PATCH v2 6/6] Bluetooth: Remove redundant RFCOMM BT_CLOSED settings
Hi Dean,
Quoted 10 lines · Dean Jenkins
rfcomm_session_close()sets the RFCOMM session state toBT_CLOSED. However, in multiple places immediately before the function is called, the RFCOMM session is set toBT_CLOSED. Therefore, remove these unnecessary state settings.Signed-off-by Dean Jenkins <[email protected]>Acked-by Marcel Holtmann <[email protected]>--- net/bluetooth/rfcomm/core.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
Patches has been applied to bluetooth-next. Thanks for the nice work in RFCOMM.
Gustavo