aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth
diff options
context:
space:
mode:
authorGustavo F. Padovan <padovan@profusion.mobi>2010-05-01 15:15:45 -0400
committerMarcel Holtmann <marcel@holtmann.org>2010-05-10 03:28:53 -0400
commitdfc909befbfe967bd7f46ef33b6969c1b7f3cf42 (patch)
treecec8545cce920fe194a2d167467a1ca500c2616d /net/bluetooth
parent6161c0382bbab883a634d284f7367a88bbe88534 (diff)
Bluetooth: Fix race condition on l2cap_ertm_send()
l2cap_ertm_send() can be called both from user context and bottom half context. The socket locks for that contexts are different, the user context uses a mutex(which can sleep) and the second one uses a spinlock_bh. That creates a race condition when we have interruptions on both contexts at the same time. The better way to solve this is to add a new spinlock to lock l2cap_ertm_send() and the vars it access. The other solution was to defer l2cap_ertm_send() with a workqueue, but we the sending process already has one defer on the hci layer. It's not a good idea add another one. The patch refactor the code to create l2cap_retransmit_frames(), then we encapulate the lock of l2cap_ertm_send() for some call. It also changes l2cap_retransmit_frame() to l2cap_retransmit_one_frame() to avoid confusion Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi> Reviewed-by: João Paulo Rechi Vita <jprvita@profusion.mobi> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net/bluetooth')
-rw-r--r--net/bluetooth/l2cap.c99
1 files changed, 66 insertions, 33 deletions
diff --git a/net/bluetooth/l2cap.c b/net/bluetooth/l2cap.c
index 9d514f9dbc0f..fe663e9c6684 100644
--- a/net/bluetooth/l2cap.c
+++ b/net/bluetooth/l2cap.c
@@ -1368,7 +1368,7 @@ static int l2cap_streaming_send(struct sock *sk)
1368 return 0; 1368 return 0;
1369} 1369}
1370 1370
1371static void l2cap_retransmit_frame(struct sock *sk, u8 tx_seq) 1371static void l2cap_retransmit_one_frame(struct sock *sk, u8 tx_seq)
1372{ 1372{
1373 struct l2cap_pinfo *pi = l2cap_pi(sk); 1373 struct l2cap_pinfo *pi = l2cap_pi(sk);
1374 struct sk_buff *skb, *tx_skb; 1374 struct sk_buff *skb, *tx_skb;
@@ -1467,10 +1467,29 @@ static int l2cap_ertm_send(struct sock *sk)
1467 return nsent; 1467 return nsent;
1468} 1468}
1469 1469
1470static int l2cap_retransmit_frames(struct sock *sk)
1471{
1472 struct l2cap_pinfo *pi = l2cap_pi(sk);
1473 int ret;
1474
1475 spin_lock_bh(&pi->send_lock);
1476
1477 if (!skb_queue_empty(TX_QUEUE(sk)))
1478 sk->sk_send_head = TX_QUEUE(sk)->next;
1479
1480 pi->next_tx_seq = pi->expected_ack_seq;
1481 ret = l2cap_ertm_send(sk);
1482
1483 spin_unlock_bh(&pi->send_lock);
1484
1485 return ret;
1486}
1487
1470static void l2cap_send_ack(struct l2cap_pinfo *pi) 1488static void l2cap_send_ack(struct l2cap_pinfo *pi)
1471{ 1489{
1472 struct sock *sk = (struct sock *)pi; 1490 struct sock *sk = (struct sock *)pi;
1473 u16 control = 0; 1491 u16 control = 0;
1492 int nframes;
1474 1493
1475 control |= pi->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 1494 control |= pi->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
1476 1495
@@ -1479,10 +1498,17 @@ static void l2cap_send_ack(struct l2cap_pinfo *pi)
1479 pi->conn_state |= L2CAP_CONN_RNR_SENT; 1498 pi->conn_state |= L2CAP_CONN_RNR_SENT;
1480 l2cap_send_sframe(pi, control); 1499 l2cap_send_sframe(pi, control);
1481 return; 1500 return;
1482 } else if (l2cap_ertm_send(sk) == 0) {
1483 control |= L2CAP_SUPER_RCV_READY;
1484 l2cap_send_sframe(pi, control);
1485 } 1501 }
1502
1503 spin_lock_bh(&pi->send_lock);
1504 nframes = l2cap_ertm_send(sk);
1505 spin_unlock_bh(&pi->send_lock);
1506
1507 if (nframes > 0)
1508 return;
1509
1510 control |= L2CAP_SUPER_RCV_READY;
1511 l2cap_send_sframe(pi, control);
1486} 1512}
1487 1513
1488static void l2cap_send_srejtail(struct sock *sk) 1514static void l2cap_send_srejtail(struct sock *sk)
@@ -1673,8 +1699,10 @@ static inline int l2cap_sar_segment_sdu(struct sock *sk, struct msghdr *msg, siz
1673 size += buflen; 1699 size += buflen;
1674 } 1700 }
1675 skb_queue_splice_tail(&sar_queue, TX_QUEUE(sk)); 1701 skb_queue_splice_tail(&sar_queue, TX_QUEUE(sk));
1702 spin_lock_bh(&pi->send_lock);
1676 if (sk->sk_send_head == NULL) 1703 if (sk->sk_send_head == NULL)
1677 sk->sk_send_head = sar_queue.next; 1704 sk->sk_send_head = sar_queue.next;
1705 spin_unlock_bh(&pi->send_lock);
1678 1706
1679 return size; 1707 return size;
1680} 1708}
@@ -1745,8 +1773,15 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct ms
1745 goto done; 1773 goto done;
1746 } 1774 }
1747 __skb_queue_tail(TX_QUEUE(sk), skb); 1775 __skb_queue_tail(TX_QUEUE(sk), skb);
1776
1777 if (pi->mode == L2CAP_MODE_ERTM)
1778 spin_lock_bh(&pi->send_lock);
1779
1748 if (sk->sk_send_head == NULL) 1780 if (sk->sk_send_head == NULL)
1749 sk->sk_send_head = skb; 1781 sk->sk_send_head = skb;
1782
1783 if (pi->mode == L2CAP_MODE_ERTM)
1784 spin_unlock_bh(&pi->send_lock);
1750 } else { 1785 } else {
1751 /* Segment SDU into multiples PDUs */ 1786 /* Segment SDU into multiples PDUs */
1752 err = l2cap_sar_segment_sdu(sk, msg, len); 1787 err = l2cap_sar_segment_sdu(sk, msg, len);
@@ -1754,10 +1789,13 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct ms
1754 goto done; 1789 goto done;
1755 } 1790 }
1756 1791
1757 if (pi->mode == L2CAP_MODE_STREAMING) 1792 if (pi->mode == L2CAP_MODE_STREAMING) {
1758 err = l2cap_streaming_send(sk); 1793 err = l2cap_streaming_send(sk);
1759 else 1794 } else {
1795 spin_lock_bh(&pi->send_lock);
1760 err = l2cap_ertm_send(sk); 1796 err = l2cap_ertm_send(sk);
1797 spin_unlock_bh(&pi->send_lock);
1798 }
1761 1799
1762 if (err >= 0) 1800 if (err >= 0)
1763 err = len; 1801 err = len;
@@ -2321,6 +2359,7 @@ static inline void l2cap_ertm_init(struct sock *sk)
2321 2359
2322 __skb_queue_head_init(SREJ_QUEUE(sk)); 2360 __skb_queue_head_init(SREJ_QUEUE(sk));
2323 __skb_queue_head_init(BUSY_QUEUE(sk)); 2361 __skb_queue_head_init(BUSY_QUEUE(sk));
2362 spin_lock_init(&l2cap_pi(sk)->send_lock);
2324 2363
2325 INIT_WORK(&l2cap_pi(sk)->busy_work, l2cap_busy_work); 2364 INIT_WORK(&l2cap_pi(sk)->busy_work, l2cap_busy_work);
2326} 2365}
@@ -3340,7 +3379,9 @@ static inline void l2cap_send_i_or_rr_or_rnr(struct sock *sk)
3340 if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY && pi->unacked_frames > 0) 3379 if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY && pi->unacked_frames > 0)
3341 __mod_retrans_timer(); 3380 __mod_retrans_timer();
3342 3381
3382 spin_lock_bh(&pi->send_lock);
3343 l2cap_ertm_send(sk); 3383 l2cap_ertm_send(sk);
3384 spin_unlock_bh(&pi->send_lock);
3344 3385
3345 if (!(pi->conn_state & L2CAP_CONN_LOCAL_BUSY) && 3386 if (!(pi->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
3346 pi->frames_sent == 0) { 3387 pi->frames_sent == 0) {
@@ -3857,12 +3898,8 @@ expected:
3857 if (rx_control & L2CAP_CTRL_FINAL) { 3898 if (rx_control & L2CAP_CTRL_FINAL) {
3858 if (pi->conn_state & L2CAP_CONN_REJ_ACT) 3899 if (pi->conn_state & L2CAP_CONN_REJ_ACT)
3859 pi->conn_state &= ~L2CAP_CONN_REJ_ACT; 3900 pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
3860 else { 3901 else
3861 if (!skb_queue_empty(TX_QUEUE(sk))) 3902 l2cap_retransmit_frames(sk);
3862 sk->sk_send_head = TX_QUEUE(sk)->next;
3863 pi->next_tx_seq = pi->expected_ack_seq;
3864 l2cap_ertm_send(sk);
3865 }
3866 } 3903 }
3867 3904
3868 err = l2cap_push_rx_skb(sk, skb, rx_control); 3905 err = l2cap_push_rx_skb(sk, skb, rx_control);
@@ -3907,12 +3944,8 @@ static inline void l2cap_data_channel_rrframe(struct sock *sk, u16 rx_control)
3907 3944
3908 if (pi->conn_state & L2CAP_CONN_REJ_ACT) 3945 if (pi->conn_state & L2CAP_CONN_REJ_ACT)
3909 pi->conn_state &= ~L2CAP_CONN_REJ_ACT; 3946 pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
3910 else { 3947 else
3911 if (!skb_queue_empty(TX_QUEUE(sk))) 3948 l2cap_retransmit_frames(sk);
3912 sk->sk_send_head = TX_QUEUE(sk)->next;
3913 pi->next_tx_seq = pi->expected_ack_seq;
3914 l2cap_ertm_send(sk);
3915 }
3916 3949
3917 } else { 3950 } else {
3918 if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) && 3951 if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
@@ -3920,10 +3953,13 @@ static inline void l2cap_data_channel_rrframe(struct sock *sk, u16 rx_control)
3920 __mod_retrans_timer(); 3953 __mod_retrans_timer();
3921 3954
3922 pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3955 pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3923 if (pi->conn_state & L2CAP_CONN_SREJ_SENT) 3956 if (pi->conn_state & L2CAP_CONN_SREJ_SENT) {
3924 l2cap_send_ack(pi); 3957 l2cap_send_ack(pi);
3925 else 3958 } else {
3959 spin_lock_bh(&pi->send_lock);
3926 l2cap_ertm_send(sk); 3960 l2cap_ertm_send(sk);
3961 spin_unlock_bh(&pi->send_lock);
3962 }
3927 } 3963 }
3928} 3964}
3929 3965
@@ -3940,17 +3976,10 @@ static inline void l2cap_data_channel_rejframe(struct sock *sk, u16 rx_control)
3940 if (rx_control & L2CAP_CTRL_FINAL) { 3976 if (rx_control & L2CAP_CTRL_FINAL) {
3941 if (pi->conn_state & L2CAP_CONN_REJ_ACT) 3977 if (pi->conn_state & L2CAP_CONN_REJ_ACT)
3942 pi->conn_state &= ~L2CAP_CONN_REJ_ACT; 3978 pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
3943 else { 3979 else
3944 if (!skb_queue_empty(TX_QUEUE(sk))) 3980 l2cap_retransmit_frames(sk);
3945 sk->sk_send_head = TX_QUEUE(sk)->next;
3946 pi->next_tx_seq = pi->expected_ack_seq;
3947 l2cap_ertm_send(sk);
3948 }
3949 } else { 3981 } else {
3950 if (!skb_queue_empty(TX_QUEUE(sk))) 3982 l2cap_retransmit_frames(sk);
3951 sk->sk_send_head = TX_QUEUE(sk)->next;
3952 pi->next_tx_seq = pi->expected_ack_seq;
3953 l2cap_ertm_send(sk);
3954 3983
3955 if (pi->conn_state & L2CAP_CONN_WAIT_F) 3984 if (pi->conn_state & L2CAP_CONN_WAIT_F)
3956 pi->conn_state |= L2CAP_CONN_REJ_ACT; 3985 pi->conn_state |= L2CAP_CONN_REJ_ACT;
@@ -3966,8 +3995,12 @@ static inline void l2cap_data_channel_srejframe(struct sock *sk, u16 rx_control)
3966 if (rx_control & L2CAP_CTRL_POLL) { 3995 if (rx_control & L2CAP_CTRL_POLL) {
3967 pi->expected_ack_seq = tx_seq; 3996 pi->expected_ack_seq = tx_seq;
3968 l2cap_drop_acked_frames(sk); 3997 l2cap_drop_acked_frames(sk);
3969 l2cap_retransmit_frame(sk, tx_seq); 3998 l2cap_retransmit_one_frame(sk, tx_seq);
3999
4000 spin_lock_bh(&pi->send_lock);
3970 l2cap_ertm_send(sk); 4001 l2cap_ertm_send(sk);
4002 spin_unlock_bh(&pi->send_lock);
4003
3971 if (pi->conn_state & L2CAP_CONN_WAIT_F) { 4004 if (pi->conn_state & L2CAP_CONN_WAIT_F) {
3972 pi->srej_save_reqseq = tx_seq; 4005 pi->srej_save_reqseq = tx_seq;
3973 pi->conn_state |= L2CAP_CONN_SREJ_ACT; 4006 pi->conn_state |= L2CAP_CONN_SREJ_ACT;
@@ -3977,9 +4010,9 @@ static inline void l2cap_data_channel_srejframe(struct sock *sk, u16 rx_control)
3977 pi->srej_save_reqseq == tx_seq) 4010 pi->srej_save_reqseq == tx_seq)
3978 pi->conn_state &= ~L2CAP_CONN_SREJ_ACT; 4011 pi->conn_state &= ~L2CAP_CONN_SREJ_ACT;
3979 else 4012 else
3980 l2cap_retransmit_frame(sk, tx_seq); 4013 l2cap_retransmit_one_frame(sk, tx_seq);
3981 } else { 4014 } else {
3982 l2cap_retransmit_frame(sk, tx_seq); 4015 l2cap_retransmit_one_frame(sk, tx_seq);
3983 if (pi->conn_state & L2CAP_CONN_WAIT_F) { 4016 if (pi->conn_state & L2CAP_CONN_WAIT_F) {
3984 pi->srej_save_reqseq = tx_seq; 4017 pi->srej_save_reqseq = tx_seq;
3985 pi->conn_state |= L2CAP_CONN_SREJ_ACT; 4018 pi->conn_state |= L2CAP_CONN_SREJ_ACT;