Skip to contentSkip to conversation

Bluetooth: Fix "blocking ops when !TASK_RUNNING" with sock_accept

PATCH In review Johan Hedberg 2 participants 6 messages Jan 23, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bluetooth: Fix "blocking ops when !TASK_RUNNING" with sock_accept

From: Johan Hedberg <[email protected]>

Recent kernels have started giving the following style warnings:

[  +0.000237] WARNING: CPU: 1 PID: 701 at kernel/sched/core.c:7300 __might_sleep+0x65/0xa3()
[  +0.000407] do not call blocking ops when !TASK_RUNNING; state=1 set at [<f88b0955>] l2cap_sock_accept+0x97/0x1b4 [bluetooth]
[  +0.000611] Modules linked in: btusb hci_vhci rfcomm bluetooth
[  +0.000305] CPU: 1 PID: 701 Comm: l2cap-tester Not tainted 3.19.0-rc4+ #1304
[  +0.000318] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.7.5-20140709_153950- 04/01/2014
[  +0.000463]  00000000 00000000 f2e81c70 c13e57df f2e81c9c f2e81c8c c103372c 00001c84
[  +0.000586]  c104e4a1 00000001 f5e93390 f5e93390 f2e81ca4 c103376e 00000009 f2e81c9c
[  +0.000422]  c15da812 f2e81cb8 f2e81cdc c104e4a1 c15da2ae 00001c84 c15da812 00000001
[  +0.000869] Call Trace:
[  +0.000073]  [<c13e57df>] dump_stack+0x49/0x73
[  +0.000182]  [<c103372c>] warn_slowpath_common+0x89/0xa0
[  +0.000225]  [<c104e4a1>] ? __might_sleep+0x65/0xa3
[  +0.000204]  [<c103376e>] warn_slowpath_fmt+0x2b/0x2f
[  +0.000215]  [<c104e4a1>] __might_sleep+0x65/0xa3
[  +0.000212]  [<f88b0955>] ? l2cap_sock_accept+0x97/0x1b4 [bluetooth]
[  +0.000297]  [<f88b0955>] ? l2cap_sock_accept+0x97/0x1b4 [bluetooth]
[  +0.000284]  [<c130bfad>] lock_sock_nested+0x23/0x77
[  +0.000219]  [<f888b25d>] bt_accept_dequeue+0x68/0x11b [bluetooth]
[  +0.000274]  [<c1060165>] ? trace_hardirqs_on+0xb/0xd
[  +0.000224]  [<f88b0985>] l2cap_sock_accept+0xc7/0x1b4 [bluetooth]
[  +0.000498]  [<f88b0985>] ? l2cap_sock_accept+0xc7/0x1b4 [bluetooth]
[  +0.000494]  [<c1051849>] ? wake_up_state+0x11/0x11
[  +0.000202]  [<c1309c27>] SYSC_accept4+0xf3/0x1af
[  +0.000194]  [<c11912b5>] ? security_socket_accept+0x14/0x16
[  +0.000252]  [<c1309c27>] ? SYSC_accept4+0xf3/0x1af
[  +0.000201]  [<c1061507>] ? lock_release_non_nested+0x137/0x217
[  +0.000256]  [<c10cd5bc>] ? might_fault+0x44/0x8b
[  +0.000190]  [<c10cd5bc>] ? might_fault+0x44/0x8b
[  +0.000196]  [<c11da2b0>] ? _copy_from_user+0x44/0x4e
[  +0.000210]  [<c130b3ea>] SYSC_socketcall+0xff/0x38c
[  +0.000205]  [<c105fdcb>] ? mark_lock+0x1e/0x1c5
[  +0.000186]  [<c10606d8>] ? __lock_acquire+0x342/0xc89
[  +0.000215]  [<c1060926>] ? __lock_acquire+0x590/0xc89
[  +0.000217]  [<c105ea2e>] ? __lock_is_held+0x2e/0x44
[  +0.000205]  [<c11106c9>] ? fsnotify+0x452/0x494
[  +0.000186]  [<c1060165>] ? trace_hardirqs_on+0xb/0xd
[  +0.000403]  [<c10e4516>] ? fsnotify_modify+0x4a/0x55
[  +0.000497]  [<c10e4516>] ? fsnotify_modify+0x4a/0x55
[  +0.000220]  [<c10e48eb>] ? vfs_write+0xbb/0xc5
[  +0.000186]  [<c106013f>] ? trace_hardirqs_on_caller+0x15f/0x17a
[  +0.000273]  [<c130b6a8>] SyS_socketcall+0x13/0x15
[  +0.000200]  [<c13ea628>] sysenter_do_call+0x12/0x12

The problematic code is bt_accept_dequeue() which calls the blocking lock_sock() function. The simplest fix is to move setting TASK_INTERRUPTIBLE after the call to bt_accept_dequeue(). This patch makes this fix for our three socket types (L2CAP, RFCOMM & SCO).

---
Note: Due to my limited experience with waitqueues and playing with the task state, this can be considered a rather naive approach to the problem. However, the patch seems to work fine in practice and effectively removes the warning in question.

 net/bluetooth/l2cap_sock.c  | 4 ++--
 net/bluetooth/rfcomm/sock.c | 4 ++--
 net/bluetooth/sco.c         | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
net/bluetooth/l2cap_sock.c +2 -2
index 20206cd3acbc..8d723e6ae802 100644
@@ -316,8 +316,6 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
 	/* Wait for an incoming connection. (wake-one). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -327,6 +325,8 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
 		if (nsk)
 			break;
 
+		set_current_state(TASK_INTERRUPTIBLE);
+
 		if (!timeo) {
 			err = -EAGAIN;
 			break;
net/bluetooth/rfcomm/sock.c +2 -2
index d8a95755a8a8..8f492707f6d2 100644
@@ -487,8 +487,6 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int f
 	/* Wait for an incoming connection. (wake-one). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -498,6 +496,8 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int f
 		if (nsk)
 			break;
 
+		set_current_state(TASK_INTERRUPTIBLE);
+
 		if (!timeo) {
 			err = -EAGAIN;
 			break;
net/bluetooth/sco.c +2 -2
index 07ec7d23b843..ee9673e014cb 100644
@@ -632,8 +632,6 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flag
 	/* Wait for an incoming connection. (wake-one). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -643,6 +641,8 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flag
 		if (ch)
 			break;
 
+		set_current_state(TASK_INTERRUPTIBLE);
+
 		if (!timeo) {
 			err = -EAGAIN;
 			break;
Signature · 1 line
2.1.0
#1 lore raw

Hi,

Quoted 76 lines · Johan Hedberg

Recent kernels have started giving the following style warnings:

[  +0.000237] WARNING: CPU: 1 PID: 701 at kernel/sched/core.c:7300 __might_sleep+0x65/0xa3()
[  +0.000407] do not call blocking ops when !TASK_RUNNING; state=1 set at [<f88b0955>] l2cap_sock_accept+0x97/0x1b4 [bluetooth]
[  +0.000611] Modules linked in: btusb hci_vhci rfcomm bluetooth
[  +0.000305] CPU: 1 PID: 701 Comm: l2cap-tester Not tainted 3.19.0-rc4+ #1304
[  +0.000318] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.7.5-20140709_153950- 04/01/2014
[  +0.000463]  00000000 00000000 f2e81c70 c13e57df f2e81c9c f2e81c8c c103372c 00001c84
[  +0.000586]  c104e4a1 00000001 f5e93390 f5e93390 f2e81ca4 c103376e 00000009 f2e81c9c
[  +0.000422]  c15da812 f2e81cb8 f2e81cdc c104e4a1 c15da2ae 00001c84 c15da812 00000001
[  +0.000869] Call Trace:
[  +0.000073]  [<c13e57df>] dump_stack+0x49/0x73
[  +0.000182]  [<c103372c>] warn_slowpath_common+0x89/0xa0
[  +0.000225]  [<c104e4a1>] ? __might_sleep+0x65/0xa3
[  +0.000204]  [<c103376e>] warn_slowpath_fmt+0x2b/0x2f
[  +0.000215]  [<c104e4a1>] __might_sleep+0x65/0xa3
[  +0.000212]  [<f88b0955>] ? l2cap_sock_accept+0x97/0x1b4 [bluetooth]
[  +0.000297]  [<f88b0955>] ? l2cap_sock_accept+0x97/0x1b4 [bluetooth]
[  +0.000284]  [<c130bfad>] lock_sock_nested+0x23/0x77
[  +0.000219]  [<f888b25d>] bt_accept_dequeue+0x68/0x11b [bluetooth]
[  +0.000274]  [<c1060165>] ? trace_hardirqs_on+0xb/0xd
[  +0.000224]  [<f88b0985>] l2cap_sock_accept+0xc7/0x1b4 [bluetooth]
[  +0.000498]  [<f88b0985>] ? l2cap_sock_accept+0xc7/0x1b4 [bluetooth]
[  +0.000494]  [<c1051849>] ? wake_up_state+0x11/0x11
[  +0.000202]  [<c1309c27>] SYSC_accept4+0xf3/0x1af
[  +0.000194]  [<c11912b5>] ? security_socket_accept+0x14/0x16
[  +0.000252]  [<c1309c27>] ? SYSC_accept4+0xf3/0x1af
[  +0.000201]  [<c1061507>] ? lock_release_non_nested+0x137/0x217
[  +0.000256]  [<c10cd5bc>] ? might_fault+0x44/0x8b
[  +0.000190]  [<c10cd5bc>] ? might_fault+0x44/0x8b
[  +0.000196]  [<c11da2b0>] ? _copy_from_user+0x44/0x4e
[  +0.000210]  [<c130b3ea>] SYSC_socketcall+0xff/0x38c
[  +0.000205]  [<c105fdcb>] ? mark_lock+0x1e/0x1c5
[  +0.000186]  [<c10606d8>] ? __lock_acquire+0x342/0xc89
[  +0.000215]  [<c1060926>] ? __lock_acquire+0x590/0xc89
[  +0.000217]  [<c105ea2e>] ? __lock_is_held+0x2e/0x44
[  +0.000205]  [<c11106c9>] ? fsnotify+0x452/0x494
[  +0.000186]  [<c1060165>] ? trace_hardirqs_on+0xb/0xd
[  +0.000403]  [<c10e4516>] ? fsnotify_modify+0x4a/0x55
[  +0.000497]  [<c10e4516>] ? fsnotify_modify+0x4a/0x55
[  +0.000220]  [<c10e48eb>] ? vfs_write+0xbb/0xc5
[  +0.000186]  [<c106013f>] ? trace_hardirqs_on_caller+0x15f/0x17a
[  +0.000273]  [<c130b6a8>] SyS_socketcall+0x13/0x15
[  +0.000200]  [<c13ea628>] sysenter_do_call+0x12/0x12

The problematic code is bt_accept_dequeue() which calls the blocking lock_sock() function. The simplest fix is to move setting TASK_INTERRUPTIBLE after the call to bt_accept_dequeue(). This patch makes this fix for our three socket types (L2CAP, RFCOMM & SCO).

---
Note: Due to my limited experience with waitqueues and playing with the task state, this can be considered a rather naive approach to the problem. However, the patch seems to work fine in practice and effectively removes the warning in question.

 net/bluetooth/l2cap_sock.c  | 4 ++--
 net/bluetooth/rfcomm/sock.c | 4 ++--
 net/bluetooth/sco.c         | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
net/bluetooth/l2cap_sock.c +0 -2
index 20206cd3acbc..8d723e6ae802 100644
@@ -316,8 +316,6 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
 	/* Wait for an incoming connection. (wake-one). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;

net/bluetooth/l2cap_sock.c static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, · line 332

Quoted 8 lines
net/bluetooth/l2cap_sock.c +2 -0
@@ -327,6 +325,8 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
 		if (nsk)
 			break;
 
+		set_current_state(TASK_INTERRUPTIBLE);
+
 		if (!timeo) {
 			err = -EAGAIN;
 			break;

Szymon pointed out that this change is mostly reverting fixes from Peter Hurley from a few years back, i.e. the following commits:

	552b0d3cb9ff648aa503011ef50ca24019cd0f5f
	f9a3c20aa07462108fc6fd759dea956053f020bb
	950e2d51e866623e4c360280aa63b85ab66d3403

So my patch is probably not the right fix for the issue. Any further ideas on how to properly fix this are welcome. I didn't manage to find anything similar to our accept_q in other (non-Bluetooth) socket types, so I'm a bit lost why exactly it's needed in the first place.

Johan

#2 lore raw

Hi Johan,

Quoted 11 lines · Johan Hedberg

Szymon pointed out that this change is mostly reverting fixes from Peter Hurley from a few years back, i.e. the following commits:

	552b0d3cb9ff648aa503011ef50ca24019cd0f5f
	f9a3c20aa07462108fc6fd759dea956053f020bb
	950e2d51e866623e4c360280aa63b85ab66d3403

So my patch is probably not the right fix for the issue. Any further ideas on how to properly fix this are welcome. I didn't manage to find anything similar to our accept_q in other (non-Bluetooth) socket types, so I'm a bit lost why exactly it's needed in the first place.

Thanks for letting me know about this; I'll take care of it.

Regards,
Peter Hurley

#3 lore raw

PATCH 1/1 bluetooth: Fix nested sleeps

In reply to Johan Hedberg

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_wait_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().

---
 net/bluetooth/l2cap_sock.c  | 9 ++++-----
 net/bluetooth/rfcomm/sock.c | 9 ++++-----
 net/bluetooth/sco.c         | 8 +++-----
 3 files changed, 11 insertions(+), 15 deletions(-)
net/bluetooth/l2cap_sock.c +4 -5
index f65caf4..c41c689 100644
@@ -302,7 +302,7 @@ done:
 static int l2cap_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;
 	long timeo;
 	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). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -338,10 +336,11 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		release_sock(sk);
-		timeo = schedule_timeout(timeo);
+
+		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
+
 		lock_sock_nested(sk, L2CAP_NESTING_PARENT);
 	}
-	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(sk_sleep(sk), &wait);
 
 	if (err)
net/bluetooth/rfcomm/sock.c +4 -5
index 2348176..0f985ff 100644
@@ -468,7 +468,7 @@ done:
 
 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;
 	long timeo;
 	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). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -509,10 +507,11 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int f
 		}
 
 		release_sock(sk);
-		timeo = schedule_timeout(timeo);
+
+		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
+
 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
 	}
-	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(sk_sleep(sk), &wait);
 
 	if (err)
net/bluetooth/sco.c +3 -5
index 30e5ea3..b935003 100644
@@ -618,7 +618,7 @@ done:
 
 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;
 	long timeo;
 	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). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -654,10 +652,10 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flag
 		}
 
 		release_sock(sk);
-		timeo = schedule_timeout(timeo);
+
+		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
 		lock_sock(sk);
 	}
-	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(sk_sleep(sk), &wait);
 
 	if (err)
Signature · 1 line
2.2.2
#4 lore raw

PATCH v2 1/1 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().

---

v2: minor commit log fixes
* s/woken_wait_function/woken_wake_function/
* indent 'set TASK_RUNNING' under wait_woken to clarify wait_woken() performs that operation

 net/bluetooth/l2cap_sock.c  | 9 ++++-----
 net/bluetooth/rfcomm/sock.c | 9 ++++-----
 net/bluetooth/sco.c         | 8 +++-----
 3 files changed, 11 insertions(+), 15 deletions(-)
net/bluetooth/l2cap_sock.c +4 -5
index f65caf4..c41c689 100644
@@ -302,7 +302,7 @@ done:
 static int l2cap_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;
 	long timeo;
 	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). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -338,10 +336,11 @@ static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
 		}
 
 		release_sock(sk);
-		timeo = schedule_timeout(timeo);
+
+		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
+
 		lock_sock_nested(sk, L2CAP_NESTING_PARENT);
 	}
-	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(sk_sleep(sk), &wait);
 
 	if (err)
net/bluetooth/rfcomm/sock.c +4 -5
index 2348176..0f985ff 100644
@@ -468,7 +468,7 @@ done:
 
 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;
 	long timeo;
 	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). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -509,10 +507,11 @@ static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int f
 		}
 
 		release_sock(sk);
-		timeo = schedule_timeout(timeo);
+
+		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
+
 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
 	}
-	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(sk_sleep(sk), &wait);
 
 	if (err)
net/bluetooth/sco.c +3 -5
index 30e5ea3..b935003 100644
@@ -618,7 +618,7 @@ done:
 
 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;
 	long timeo;
 	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). */
 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
 	while (1) {
-		set_current_state(TASK_INTERRUPTIBLE);
-
 		if (sk->sk_state != BT_LISTEN) {
 			err = -EBADFD;
 			break;
@@ -654,10 +652,10 @@ static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flag
 		}
 
 		release_sock(sk);
-		timeo = schedule_timeout(timeo);
+
+		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
 		lock_sock(sk);
 	}
-	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(sk_sleep(sk), &wait);
 
 	if (err)
Signature · 1 line
2.2.2
author Applied
#5 lore raw

Re: [PATCH v2] bluetooth: Fix nested sleeps

Hi Peter,

Quoted 43 lines · Peter Hurley

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().

---

v2: minor commit log fixes
* s/woken_wait_function/woken_wake_function/
* indent 'set TASK_RUNNING' under wait_woken to clarify wait_woken() performs that operation

Quoted 4 lines
 net/bluetooth/l2cap_sock.c  | 9 ++++-----
 net/bluetooth/rfcomm/sock.c | 9 ++++-----
 net/bluetooth/sco.c         | 8 +++-----
 3 files changed, 11 insertions(+), 15 deletions(-)

Applied to bluetooth-next. Thanks!

Johan