Skip to content

Commit f9adf42

Browse files
JeffyCNgregkh
authored andcommitted
Bluetooth: cmtp: fix possible might sleep error in cmtp_session
commit f06d977309d09253c744e54e75c5295ecc52b7b4 upstream. It looks like cmtp_session has same pattern as the issue reported in old rfcomm: while (1) { set_current_state(TASK_INTERRUPTIBLE); if (condition) break; // may call might_sleep here schedule(); } __set_current_state(TASK_RUNNING); Which fixed at: dfb2fae Bluetooth: Fix nested sleeps So let's fix it at the same way, also follow the suggestion of: https://lwn.net/Articles/628628/ Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com> Reviewed-by: Brian Norris <briannorris@chromium.org> Reviewed-by: AL Yu-Chen Cho <acho@suse.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Cc: Jiri Slaby <jslaby@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 172bbb8 commit f9adf42

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

net/bluetooth/cmtp/core.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@ static int cmtp_session(void *arg)
281281
struct cmtp_session *session = arg;
282282
struct sock *sk = session->sock->sk;
283283
struct sk_buff *skb;
284-
wait_queue_t wait;
284+
DEFINE_WAIT_FUNC(wait, woken_wake_function);
285285

286286
BT_DBG("session %p", session);
287287

288288
set_user_nice(current, -15);
289289

290-
init_waitqueue_entry(&wait, current);
291290
add_wait_queue(sk_sleep(sk), &wait);
292291
while (1) {
293-
set_current_state(TASK_INTERRUPTIBLE);
292+
/* Ensure session->terminate is updated */
293+
smp_mb__before_atomic();
294294

295295
if (atomic_read(&session->terminate))
296296
break;
@@ -307,9 +307,8 @@ static int cmtp_session(void *arg)
307307

308308
cmtp_process_transmit(session);
309309

310-
schedule();
310+
wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
311311
}
312-
__set_current_state(TASK_RUNNING);
313312
remove_wait_queue(sk_sleep(sk), &wait);
314313

315314
down_write(&cmtp_session_sem);
@@ -394,7 +393,7 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
394393
err = cmtp_attach_device(session);
395394
if (err < 0) {
396395
atomic_inc(&session->terminate);
397-
wake_up_process(session->task);
396+
wake_up_interruptible(sk_sleep(session->sock->sk));
398397
up_write(&cmtp_session_sem);
399398
return err;
400399
}
@@ -432,7 +431,11 @@ int cmtp_del_connection(struct cmtp_conndel_req *req)
432431

433432
/* Stop session thread */
434433
atomic_inc(&session->terminate);
435-
wake_up_process(session->task);
434+
435+
/* Ensure session->terminate is updated */
436+
smp_mb__after_atomic();
437+
438+
wake_up_interruptible(sk_sleep(session->sock->sk));
436439
} else
437440
err = -ENOENT;
438441

0 commit comments

Comments
 (0)