Skip to content

Commit 7bb59df

Browse files
peterhurleyGustavo F. Padovan
authored andcommitted
Bluetooth: Fix hidp disconnect deadlocks and lost wakeup
Partial revert of commit aabf6f8. When the hidp session thread was converted from kernel_thread to kthread, the atomic/wakeups were replaced with kthread_stop. kthread_stop has blocking semantics which are inappropriate for the hidp session kthread. In addition, the kthread signals itself to terminate in hidp_process_hid_control() - it cannot do this with kthread_stop(). Lastly, a wakeup can be lost if the wakeup happens between checking for the loop exit condition and setting the current state to TASK_INTERRUPTIBLE. (Without appropriate synchronization mechanisms, the task state should not be changed between the condition test and the yield - via schedule() - as this creates a race between the wakeup and resetting the state back to interruptible.) Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
1 parent 7ac2881 commit 7bb59df

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

‎net/bluetooth/hidp/core.c‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ static void hidp_idle_timeout(unsigned long arg)
464464
{
465465
struct hidp_session *session = (struct hidp_session *) arg;
466466

467-
kthread_stop(session->task);
467+
atomic_inc(&session->terminate);
468+
wake_up_process(session->task);
468469
}
469470

470471
static void hidp_set_timer(struct hidp_session *session)
@@ -535,7 +536,8 @@ static void hidp_process_hid_control(struct hidp_session *session,
535536
skb_queue_purge(&session->ctrl_transmit);
536537
skb_queue_purge(&session->intr_transmit);
537538

538-
kthread_stop(session->task);
539+
atomic_inc(&session->terminate);
540+
wake_up_process(current);
539541
}
540542
}
541543

@@ -706,9 +708,8 @@ static int hidp_session(void *arg)
706708
add_wait_queue(sk_sleep(intr_sk), &intr_wait);
707709
session->waiting_for_startup = 0;
708710
wake_up_interruptible(&session->startup_queue);
709-
while (!kthread_should_stop()) {
710-
set_current_state(TASK_INTERRUPTIBLE);
711-
711+
set_current_state(TASK_INTERRUPTIBLE);
712+
while (!atomic_read(&session->terminate)) {
712713
if (ctrl_sk->sk_state != BT_CONNECTED ||
713714
intr_sk->sk_state != BT_CONNECTED)
714715
break;
@@ -726,6 +727,7 @@ static int hidp_session(void *arg)
726727
hidp_process_transmit(session);
727728

728729
schedule();
730+
set_current_state(TASK_INTERRUPTIBLE);
729731
}
730732
set_current_state(TASK_RUNNING);
731733
remove_wait_queue(sk_sleep(intr_sk), &intr_wait);
@@ -1060,7 +1062,8 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
10601062
err_add_device:
10611063
hid_destroy_device(session->hid);
10621064
session->hid = NULL;
1063-
kthread_stop(session->task);
1065+
atomic_inc(&session->terminate);
1066+
wake_up_process(session->task);
10641067

10651068
unlink:
10661069
hidp_del_timer(session);
@@ -1111,7 +1114,8 @@ int hidp_del_connection(struct hidp_conndel_req *req)
11111114
skb_queue_purge(&session->ctrl_transmit);
11121115
skb_queue_purge(&session->intr_transmit);
11131116

1114-
kthread_stop(session->task);
1117+
atomic_inc(&session->terminate);
1118+
wake_up_process(session->task);
11151119
}
11161120
} else
11171121
err = -ENOENT;

‎net/bluetooth/hidp/hidp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ struct hidp_session {
142142
uint ctrl_mtu;
143143
uint intr_mtu;
144144

145+
atomic_t terminate;
145146
struct task_struct *task;
146147

147148
unsigned char keys[8];

0 commit comments

Comments
 (0)