Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dfb2fae7 authored by Peter Hurley's avatar Peter Hurley Committed by Johan Hedberg
Browse files

Bluetooth: Fix nested sleeps



l2cap/rfcomm/sco_sock_accept() are wait loops which may acquire
sleeping locks. Since both wait loops and sleeping locks use
task_struct.state to sleep and wake, the nested sleeping locks
destroy the wait loop state.

Use the newly-minted wait_woken() and DEFINE_WAIT_FUNC() for the
wait loop. DEFINE_WAIT_FUNC() allows an alternate wake function
to be specified; in this case, the predefined scheduler function,
woken_wake_function(). This wait construct ensures wakeups will
not be missed without requiring the wait loop to set the
task state before condition evaluation. How this works:

 CPU 0                            |  CPU 1
                                  |
                                  | is <condition> set?
                                  | no
set <condition>                   |
                                  |
wake_up_interruptible             |
  woken_wake_function             |
    set WQ_FLAG_WOKEN             |
    try_to_wake_up                |
                                  | wait_woken
                                  |   set TASK_INTERRUPTIBLE
                                  |   WQ_FLAG_WOKEN? yes
                                  |   set TASK_RUNNING
                                  |
                                  | - loop -
				  |
				  | is <condition> set?
                                  | yes - exit wait loop

Fixes "do not call blocking ops when !TASK_RUNNING" warnings
in l2cap_sock_accept(), rfcomm_sock_accept() and sco_sock_accept().

Signed-off-by: default avatarPeter Hurley <peter@hurleysoftware.com>
Signed-off-by: default avatarJohan Hedberg <johan.hedberg@intel.com>
parent a1443f5a
Loading
Loading
Loading
Loading
+4 −5
Original line number Original line Diff line number Diff line
@@ -302,7 +302,7 @@ done:
static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
			     int flags)
			     int flags)
{
{
	DECLARE_WAITQUEUE(wait, current);
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
	struct sock *sk = sock->sk, *nsk;
	struct sock *sk = sock->sk, *nsk;
	long timeo;
	long timeo;
	int err = 0;
	int err = 0;
@@ -316,8 +316,6 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
	/* Wait for an incoming connection. (wake-one). */
	/* Wait for an incoming connection. (wake-one). */
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
	while (1) {
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);

		if (sk->sk_state != BT_LISTEN) {
		if (sk->sk_state != BT_LISTEN) {
			err = -EBADFD;
			err = -EBADFD;
			break;
			break;
@@ -338,10 +336,11 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
		}
		}


		release_sock(sk);
		release_sock(sk);
		timeo = schedule_timeout(timeo);

		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);

		lock_sock_nested(sk, L2CAP_NESTING_PARENT);
		lock_sock_nested(sk, L2CAP_NESTING_PARENT);
	}
	}
	__set_current_state(TASK_RUNNING);
	remove_wait_queue(sk_sleep(sk), &wait);
	remove_wait_queue(sk_sleep(sk), &wait);


	if (err)
	if (err)
+4 −5
Original line number Original line Diff line number Diff line
@@ -468,7 +468,7 @@ done:


static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
{
{
	DECLARE_WAITQUEUE(wait, current);
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
	struct sock *sk = sock->sk, *nsk;
	struct sock *sk = sock->sk, *nsk;
	long timeo;
	long timeo;
	int err = 0;
	int err = 0;
@@ -487,8 +487,6 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int f
	/* Wait for an incoming connection. (wake-one). */
	/* Wait for an incoming connection. (wake-one). */
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
	while (1) {
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);

		if (sk->sk_state != BT_LISTEN) {
		if (sk->sk_state != BT_LISTEN) {
			err = -EBADFD;
			err = -EBADFD;
			break;
			break;
@@ -509,10 +507,11 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int f
		}
		}


		release_sock(sk);
		release_sock(sk);
		timeo = schedule_timeout(timeo);

		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);

		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
	}
	}
	__set_current_state(TASK_RUNNING);
	remove_wait_queue(sk_sleep(sk), &wait);
	remove_wait_queue(sk_sleep(sk), &wait);


	if (err)
	if (err)
+3 −5
Original line number Original line Diff line number Diff line
@@ -618,7 +618,7 @@ done:


static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
{
{
	DECLARE_WAITQUEUE(wait, current);
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
	struct sock *sk = sock->sk, *ch;
	struct sock *sk = sock->sk, *ch;
	long timeo;
	long timeo;
	int err = 0;
	int err = 0;
@@ -632,8 +632,6 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flag
	/* Wait for an incoming connection. (wake-one). */
	/* Wait for an incoming connection. (wake-one). */
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
	add_wait_queue_exclusive(sk_sleep(sk), &wait);
	while (1) {
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);

		if (sk->sk_state != BT_LISTEN) {
		if (sk->sk_state != BT_LISTEN) {
			err = -EBADFD;
			err = -EBADFD;
			break;
			break;
@@ -654,10 +652,10 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flag
		}
		}


		release_sock(sk);
		release_sock(sk);
		timeo = schedule_timeout(timeo);

		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
		lock_sock(sk);
		lock_sock(sk);
	}
	}
	__set_current_state(TASK_RUNNING);
	remove_wait_queue(sk_sleep(sk), &wait);
	remove_wait_queue(sk_sleep(sk), &wait);


	if (err)
	if (err)