hci_ldsic nested locking problem
Subsystems (2)
Hi,
when 8250 driver calls uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() calls tty->ops->write() to actually send the characters, but that call will try to acquire the same port lock again.
Looking at other line disciplines that looks like a bug in hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?
Is it legal to call tty->ops->write() from within ->write_wakeup() ?
cheers
Signature · 1 line
balbi
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.
Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?
It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...
and recurse
Alan
.
---Sent from Boxer | http://getboxer.com
Quoted 12 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is alreadytaken. hci_ldisc.c's implementation of ->write_wakeup() calls
tty->ops->write()to actually send the characters, but that call willtry to acquire the same port lock again.
Quoted 8 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c.Am I correct to assume that ->write_wakeup() is supposed to *just*
wakeup the bottom half so we handle ->write() in another context ?
Is it legal to call
tty->ops->write()from within ->write_wakeup() ?
It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...
and recurse
Alan
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 16 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
Quoted 7 lines
write write_wakeup write write wakeup ...and recurse
cool, so there really is a bug in hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
Signature · 1 line
balbi
Quoted 17 lines · Felipe Balbi
Quoted 16 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
Quoted 7 lines
Quoted 7 lines
write write_wakeup write write wakeup ...and recurse
then we need updates to Documentation:
Documentation/serial/tty.txt::
| Driver Side Interfaces: | | receive_buf() - Hand buffers of bytes from the driver to the ldisc | for processing. Semantics currently rather | mysterious 8( | | write_wakeup() - May be called at any point between open and close. | The TTY_DO_WRITE_WAKEUP flag indicates if a call | is needed but always races versus calls. Thus the | ldisc must be careful about setting order and to | handle unexpected calls. Must not sleep. | | The driver is forbidden from calling this directly | from the ->write call from the ldisc as the ldisc | is permitted to call the driver write method from | this function. In such a situation defer it.
documentation says ldisc is allowed to call ->write() from ->write_wakeup(). huh ?
Signature · 1 line
balbi
[ +cc Huang Shijie ]
Quoted 23 lines · Felipe Balbi
Quoted 22 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...
Quoted 4 lines
Quoted 1 line
and recurse
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
hci_uart_tx_wakeup() should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.
I'd fix it but I have no way to test it.
Regards,
Peter Hurley
[ +cc Huang Shijie ]
Quoted 21 lines · Felipe Balbi
then we need updates to Documentation:
Documentation/serial/tty.txt::| Driver Side Interfaces: | | receive_buf() - Hand buffers of bytes from the driver to the ldisc | for processing. Semantics currently rather | mysterious 8( | | write_wakeup() - May be called at any point between open and close. | The TTY_DO_WRITE_WAKEUP flag indicates if a call | is needed but always races versus calls. Thus the | ldisc must be careful about setting order and to | handle unexpected calls. Must not sleep. | | The driver is forbidden from calling this directly | from the ->write call from the ldisc as the ldisc | is permitted to call the driver write method from | this function. In such a situation defer it.
Quoted 2 lines
documentation says ldisc is allowed to call ->write() from ->write_wakeup(). huh ?
Patch submitted but never applied.
http://www.spinics.net/lists/linux-serial/msg11144.html
Regards,
Peter Hurley
Felipe Balbi
Acked-byQuoted 24 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 21 lines · Felipe Balbi
then we need updates to Documentation:
Documentation/serial/tty.txt::| Driver Side Interfaces: | | receive_buf() - Hand buffers of bytes from the driver to the ldisc | for processing. Semantics currently rather | mysterious 8( | | write_wakeup() - May be called at any point between open and close. | The TTY_DO_WRITE_WAKEUP flag indicates if a call | is needed but always races versus calls. Thus the | ldisc must be careful about setting order and to | handle unexpected calls. Must not sleep. | | The driver is forbidden from calling this directly | from the ->write call from the ldisc as the ldisc | is permitted to call the driver write method from | this function. In such a situation defer it.
Quoted 6 lines
Quoted 2 lines
documentation says ldisc is allowed to call ->write() from ->write_wakeup(). huh ?
Patch submitted but never applied.
Thank you. For that patch:
Signature · 1 line
balbi
Quoted 28 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 25 lines · Felipe Balbi
Quoted 23 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...and recurse
Quoted 7 lines
Quoted 2 lines
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
hci_uart_tx_wakeup()should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.I'd fix it but I have no way to test it.
here's a build-tested only patch which is waiting for testing from other colleagues who've got a platform to reproduce the problem:
drivers/bluetooth/hci_ldisc.c +13 -5
@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;
@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); + +return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; + restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);
@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)
@@ -289,6 +296,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);
drivers/bluetooth/hci_uart.h +1 -0
@@ -68,6 +68,7 @@ struct hci_uart {unsigned long hdev_flags;struct work_struct init_ready; +struct work_struct write_work;struct hci_uart_proto *proto;void *priv;
Signature · 1 line
balbi
Quoted 32 lines · Felipe Balbi
Quoted 31 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 27 lines · Felipe Balbi
Quoted 16 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
Quoted 7 lines
write write_wakeup write write wakeup ...and recurse
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
Quoted 7 lines
Quoted 4 lines
hci_uart_tx_wakeup()should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.I'd fix it but I have no way to test it.
here's a build-tested only patch which is waiting for testing from other colleagues who've got a platform to reproduce the problem:
Where's the cancel_work_sync() on teardown?
Regards,
Peter Hurley
Quoted 63 lines
drivers/bluetooth/hci_ldisc.c +13
-5@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); + +return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; + restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)@@ -289,6 +296,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);drivers/bluetooth/hci_uart.h +1
-0@@ -68,6 +68,7 @@ struct hci_uart {unsigned long hdev_flags;struct work_struct init_ready; +struct work_struct write_work;struct hci_uart_proto *proto;void *priv;
Quoted 36 lines · Peter Hurley
Quoted 35 lines · Felipe Balbi
Quoted 34 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 23 lines · Felipe Balbi
Quoted 22 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...Quoted 4 lines
Quoted 1 line
and recurse
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
hci_uart_tx_wakeup()should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I'd fix it but I have no way to test it.
here's a build-tested only patch which is waiting for testing from other colleagues who've got a platform to reproduce the problem:
Where's the
cancel_work_sync()on teardown?
here, as a patch too this time:
From 3ee6b74833f154df64a6164476b854846206a3f2 Mon Sep 17 00:00:00 2001
From: Felipe Balbi <[email protected]>
Date: Thu, 20 Mar 2014 13:20:10 -0500
Subject: [PATCH] bluetooth: hci_ldisc: fix deadlock condition
LDISCs shouldn't call tty->ops->write() from within ->write_wakeup().
->write_wakeup() is called with port lock taken and IRQs disabled, tty->ops->write() will try to acquire the same port lock and we will deadlock.
--- drivers/bluetooth/hci_ldisc.c | 20 +++++++++++++++----- drivers/bluetooth/hci_uart.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)
drivers/bluetooth/hci_ldisc.c +15 -5
@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;
@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); + +return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; + restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);
@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)
@@ -281,6 +288,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);
@@ -318,6 +326,8 @@ static void hci_uart_tty_close(struct tty_struct *tty)if (hdev)hci_uart_close (hdev); +cancel_work_sync (&hy->write_work); +if (test_and_clear_bit (HCI_UART_PROTO_SET , &hu->flags)) {if (hdev) {if (test_bit (HCI_UART_REGISTERED , &hu->flags))
drivers/bluetooth/hci_uart.h +1 -0
@@ -68,6 +68,7 @@ struct hci_uart {unsigned long hdev_flags;struct work_struct init_ready; +struct work_struct write_work;struct hci_uart_proto *proto;void *priv;
Signature · 5 lines
1.9.1.286.g5172cb3 -- balbi
Greg KH
maintainerQuoted 28 lines · Felipe Balbi
Quoted 27 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 23 lines · Felipe Balbi
then we need updates to Documentation:
Documentation/serial/tty.txt::| Driver Side Interfaces: | | receive_buf() - Hand buffers of bytes from the driver to the ldisc | for processing. Semantics currently rather | mysterious 8( | | write_wakeup() - May be called at any point between open and close. | The TTY_DO_WRITE_WAKEUP flag indicates if a call | is needed but always races versus calls. Thus the | ldisc must be careful about setting order and to | handle unexpected calls. Must not sleep. | | The driver is forbidden from calling this directly | from the ->write call from the ldisc as the ldisc | is permitted to call the driver write method from | this function. In such a situation defer it.documentation says ldisc is allowed to call ->write() from ->write_wakeup(). huh ?
Quoted 7 lines
Quoted 3 lines
Patch submitted but never applied.
Thank you. For that patch:
Acked-by Felipe Balbi <[email protected]>
Can someone resend it, this is lost in my tree for some reason...
Quoted 31 lines · Greg KH
Quoted 30 lines · Felipe Balbi
Quoted 29 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 21 lines · Felipe Balbi
then we need updates to Documentation:
Documentation/serial/tty.txt::| Driver Side Interfaces: | | receive_buf() - Hand buffers of bytes from the driver to the ldisc | for processing. Semantics currently rather | mysterious 8( | | write_wakeup() - May be called at any point between open and close. | The TTY_DO_WRITE_WAKEUP flag indicates if a call | is needed but always races versus calls. Thus the | ldisc must be careful about setting order and to | handle unexpected calls. Must not sleep. | | The driver is forbidden from calling this directly | from the ->write call from the ldisc as the ldisc | is permitted to call the driver write method from | this function. In such a situation defer it.Quoted 2 lines
documentation says ldisc is allowed to call ->write() from ->write_wakeup(). huh ?
Patch submitted but never applied.
Quoted 7 lines
Quoted 5 lines
Thank you. For that patch:
Acked-by Felipe Balbi <[email protected]>Can someone resend it, this is lost in my tree for some reason...
Apologies if my mailer mangles this.
--- >% ---
From: Huang Shijie <[email protected]>
In the uart_handle_cts_change(), uart_write_wakeup() is called after we call @uart_port->ops->start_tx().
The Documentation/serial/driver tells us:
-----------------------------------------------
start_tx(port)
Start transmitting characters.
Locking: port->lock taken.
Interrupts: locally disabled.
-----------------------------------------------
So when the uart_write_wakeup() is called, the port->lock is taken by the upper. See the following callstack:
|_ uart_write_wakeup |_ tty_wakeup |_ ld->ops->write_wakeup
With the port->lock held, we call the @write_wakeup. Some implemetation of the @write_wakeup does not notice that the port->lock is held, and it still tries to send data with uart_write() which will try to grab the prot->lock. A dead lock occurs, see the following log caught in the Bluetooth by uart:
--------------------------------------------------------------------
BUG: spinlock lockup suspected on CPU#0, swapper/0/0 lock: 0xdc3f4410, .magic: dead4ead, .owner: swapper/0/0, .owner_cpu: 0 CPU: 0 PID: 0 Comm: swapper/0 Tainted: G W 3.10.17-16839-ge4a1bef #1320 [<80014cbc>] (unwind_backtrace+0x0/0x138) from [<8001251c>] (show_stack+0x10/0x14) [<8001251c>] (show_stack+0x10/0x14) from [<802816ac>] (do_raw_spin_lock+0x108/0x184) [<802816ac>] (do_raw_spin_lock+0x108/0x184) from [<806a22b0>] (_raw_spin_lock_irqsave+0x54/0x60) [<806a22b0>] (_raw_spin_lock_irqsave+0x54/0x60) from [<802f5754>] (uart_write+0x38/0xe0) [<802f5754>] (uart_write+0x38/0xe0) from [<80455270>] (hci_uart_tx_wakeup+0xa4/0x168) [<80455270>] (hci_uart_tx_wakeup+0xa4/0x168) from [<802dab18>] (tty_wakeup+0x50/0x5c) [<802dab18>] (tty_wakeup+0x50/0x5c) from [<802f81a4>] (imx_rtsint+0x50/0x80) [<802f81a4>] (imx_rtsint+0x50/0x80) from [<802f88f4>] (imx_int+0x158/0x17c) [<802f88f4>] (imx_int+0x158/0x17c) from [<8007abe0>] (handle_irq_event_percpu+0x50/0x194) [<8007abe0>] (handle_irq_event_percpu+0x50/0x194) from [<8007ad60>] (handle_irq_event+0x3c/0x5c) --------------------------------------------------------------------
This patch adds more limits to the @write_wakeup, the one who wants to implemet the @write_wakeup should follow the limits which avoid the deadlock.
--- include/linux/tty_ldisc.h | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
include/linux/tty_ldisc.h +4 -1
@@ -91,7 +91,10 @@* This function is called by the low-level tty driver to signal * that line discpline should try to send more characters to the * low-level driver for transmission. If the line discpline does -* not have any more data to send, it can just return. +* not have any more data to send, it can just return. If the line +* discipline does have some data to send, please arise a tasklet +* or workqueue to do the real data transfer. Do not send data in +* this hook, it may leads to a deadlock. * * int (*hangup)(struct tty_struct *) *
-- 1.7.2.rc3
Quoted 115 lines · Felipe Balbi
Quoted 36 lines · Peter Hurley
Quoted 35 lines · Felipe Balbi
Quoted 34 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 23 lines · Felipe Balbi
Quoted 22 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...Quoted 4 lines
Quoted 1 line
and recurse
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
hci_uart_tx_wakeup()should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I'd fix it but I have no way to test it.
here's a build-tested only patch which is waiting for testing from other colleagues who've got a platform to reproduce the problem:
Where's the
cancel_work_sync()on teardown?here, as a patch too this time:
From
3ee6b74833f154df64a6164476b854846206a3f2Mon Sep 17 00:00:00 2001
From: Felipe Balbi <[email protected]>
Date: Thu, 20 Mar 2014 13:20:10 -0500
Subject: [PATCH] bluetooth:hci_ldisc: fix deadlock conditionLDISCs shouldn't call
tty->ops->write()from within ->write_wakeup().->write_wakeup() is called with port lock taken and IRQs disabled,
tty->ops->write()will try to acquire the same port lock and we will deadlock.Signed-off-by Felipe Balbi <[email protected]>--- drivers/bluetooth/hci_ldisc.c | 20 +++++++++++++++----- drivers/bluetooth/hci_uart.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)drivers/bluetooth/hci_ldisc.c +13
-5@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); + +return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; + restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)@@ -281,6 +288,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);
drivers/bluetooth/hci_ldisc.c static void hci_uart_tty_close(struct tty_struct *tty) · line 329
Quoted 4 lines
drivers/bluetooth/hci_ldisc.c +1
-0@@ -318,6 +326,8 @@ static void hci_uart_tty_close(struct tty_struct *tty)if (hdev)hci_uart_close (hdev); +cancel_work_sync (&hy->write_work);
forgot to commit, darn it
Signature · 1 line
balbi
Hi,
Quoted 7 lines · Felipe Balbi
Review static void hci_uart_tty_close(struct tty_struct *tty) · line 329
Quoted 5 lines
(quoted diff) +1
-0@@ -318,6 +326,8 @@ static void hci_uart_tty_close(struct tty_struct *tty) if (hdev) hci_uart_close(hdev); + cancel_work_sync(&hy->write_work);forgot to commit, darn it
here it is:
From eaf7ff6f2d224f202369e4820b76a03fa664fab0 Mon Sep 17 00:00:00 2001
From: Felipe Balbi <[email protected]>
Date: Thu, 20 Mar 2014 13:20:10 -0500
Subject: [PATCH] bluetooth: hci_ldisc: fix deadlock condition
LDISCs shouldn't call tty->ops->write() from within ->write_wakeup().
->write_wakeup() is called with port lock taken and IRQs disabled, tty->ops->write() will try to acquire the same port lock and we will deadlock.
--- drivers/bluetooth/hci_ldisc.c | 20 +++++++++++++++----- drivers/bluetooth/hci_uart.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)
drivers/bluetooth/hci_ldisc.c +15 -5
@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;
@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); + +return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; + restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);
@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)
@@ -281,6 +288,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);
@@ -318,6 +326,8 @@ static void hci_uart_tty_close(struct tty_struct *tty)if (hdev)hci_uart_close (hdev); +cancel_work_sync (&hu->write_work); +if (test_and_clear_bit (HCI_UART_PROTO_SET , &hu->flags)) {if (hdev) {if (test_bit (HCI_UART_REGISTERED , &hu->flags))
drivers/bluetooth/hci_uart.h +1 -0
@@ -68,6 +68,7 @@ struct hci_uart {unsigned long hdev_flags;struct work_struct init_ready; +struct work_struct write_work;struct hci_uart_proto *proto;void *priv;
Signature · 5 lines
1.9.1.286.g5172cb3 -- balbi
Peter Hurley
Reviewed-byQuoted 37 lines · Felipe Balbi
Quoted 36 lines · Peter Hurley
Quoted 35 lines · Felipe Balbi
Quoted 34 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 23 lines · Felipe Balbi
Quoted 22 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...Quoted 4 lines
Quoted 1 line
and recurse
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
hci_uart_tx_wakeup()should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I'd fix it but I have no way to test it.
here's a build-tested only patch which is waiting for testing from other colleagues who've got a platform to reproduce the problem:
Where's the
cancel_work_sync()on teardown?here, as a patch too this time:
Thanks. Minor edits below but, strictly speaking, not necessary.
Quoted 5 lines
From
3ee6b74833f154df64a6164476b854846206a3f2Mon Sep 17 00:00:00 2001
From: Felipe Balbi <[email protected]>
Date: Thu, 20 Mar 2014 13:20:10 -0500
Subject: [PATCH] bluetooth:hci_ldisc: fix deadlock condition
Quoted 7 lines
LDISCs shouldn't call
tty->ops->write()from within ->write_wakeup().->write_wakeup() is called with port lock taken and IRQs disabled,
tty->ops->write()will try to acquire the same port lock and we will deadlock.
I know you found it independently but ?
Quoted 27 lines
Signed-off-by Felipe Balbi <[email protected]>--- drivers/bluetooth/hci_ldisc.c | 20 +++++++++++++++----- drivers/bluetooth/hci_uart.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)drivers/bluetooth/hci_ldisc.c +2
-4@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); +
drivers/bluetooth/hci_ldisc.c int hci_uart_tx_wakeup(struct hci_uart *hu) · line 139
Quoted 10 lines
drivers/bluetooth/hci_ldisc.c +10
-0+return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; +
+ /* FIXME: if bad skb length or tty->ops->write() returns < 0 ??? */
Quoted 41 lines
drivers/bluetooth/hci_ldisc.c +3
-1restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)@@ -281,6 +288,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);@@ -318,6 +326,8 @@ static void hci_uart_tty_close(struct tty_struct *tty)if (hdev)hci_uart_close (hdev); +cancel_work_sync (&hy->write_work); +if (test_and_clear_bit (HCI_UART_PROTO_SET , &hu->flags)) {if (hdev) {if (test_bit (HCI_UART_REGISTERED , &hu->flags))drivers/bluetooth/hci_uart.h +1
-0@@ -68,6 +68,7 @@ struct hci_uart {unsigned long hdev_flags;struct work_struct init_ready; +struct work_struct write_work;struct hci_uart_proto *proto;void *priv;
Hi,
Quoted 60 lines · Peter Hurley
Quoted 37 lines · Felipe Balbi
Quoted 36 lines · Peter Hurley
Quoted 35 lines · Felipe Balbi
Quoted 34 lines · Peter Hurley
[ +cc Huang Shijie ]
Quoted 23 lines · Felipe Balbi
Quoted 22 lines · Alan Cox
Quoted 7 lines · Felipe Balbi
Hi,
when 8250 driver calls
uart_write_wakeup(), the tty port lock is already taken. hci_ldisc.c's implementation of ->write_wakeup() callstty->ops->write()to actually send the characters, but that call will try to acquire the same port lock again.Quoted 5 lines
Looking at other line disciplines that looks like a bug in
hci_ldisc.c. Am I correct to assume that ->write_wakeup() is supposed to *just* wakeup the bottom half so we handle ->write() in another context ?Is it legal to call
tty->ops->write()from within ->write_wakeup() ?It isn't because you might send all the bytes and go
write write_wakeup write write wakeup ...Quoted 4 lines
Quoted 1 line
and recurse
cool, so there really is a bug in
hci_ldisc. Marcel, any tips on how do you want this to be sorted out ?
hci_uart_tx_wakeup()should perform the I/O as work. FWIW, this was reported by Huang Shijie back on Dec 6.Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I'd fix it but I have no way to test it.
here's a build-tested only patch which is waiting for testing from other colleagues who've got a platform to reproduce the problem:
Where's the
cancel_work_sync()on teardown?here, as a patch too this time:
Thanks. Minor edits below but, strictly speaking, not necessary.
Reviewed-by Peter Hurley <[email protected]>Quoted 8 lines
From
3ee6b74833f154df64a6164476b854846206a3f2Mon Sep 17 00:00:00 2001
From: Felipe Balbi <[email protected]>
Date: Thu, 20 Mar 2014 13:20:10 -0500
Subject: [PATCH] bluetooth:hci_ldisc: fix deadlock conditionLDISCs shouldn't call
tty->ops->write()from within ->write_wakeup().
Quoted 8 lines
Quoted 4 lines
->write_wakeup() is called with port lock taken and IRQs disabled,
tty->ops->write()will try to acquire the same port lock and we will deadlock.I know you found it independently but ?
Reported-by Huang Shijie <[email protected]>
I will never add any *-by tags without seeing it in the mailing list. Now I can add it to the patch and send it as a real patch (git send-email it).
Quoted 82 lines
Quoted 37 lines
Signed-off-by Felipe Balbi <[email protected]>--- drivers/bluetooth/hci_ldisc.c | 20 +++++++++++++++----- drivers/bluetooth/hci_uart.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)drivers/bluetooth/hci_ldisc.c +12
-4@@ -118,10 +118,6 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)int hci_uart_tx_wakeup (struct hci_uart *hu) { -struct tty_struct *tty = hu->tty; -struct hci_dev *hdev = hu->hdev; -struct sk_buff *skb; -if (test_and_set_bit (HCI_UART_SENDING , &hu->tx_state)) {set_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);return 0 ;@@ -129,6 +125,18 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)BT_DBG ("" ); +schedule_work (&hu->write_work); + +return 0 ; +} + +static void hci_uart_write_work (struct work_struct *work) +{ + struct hci_uart *hu = container_of(work, struct hci_uart, init_ready); +struct tty_struct *tty = hu->tty; +struct hci_dev *hdev = hu->hdev; +struct sk_buff *skb; +(quoted diff) +1
-0+ /* FIXME: if bad skb length or tty->ops->write() returns < 0 ??? */Quoted 42 lines
drivers/bluetooth/hci_ldisc.c +3
-1restart:clear_bit (HCI_UART_TX_WAKEUP , &hu->tx_state);@@ -153,7 +161,6 @@ restart: goto restart;clear_bit (HCI_UART_SENDING , &hu->tx_state); -return 0 ; }static void hci_uart_init_work (struct work_struct *work)@@ -281,6 +288,7 @@ static int hci_uart_tty_open(struct tty_struct *tty) tty->receive_room =65536 ;INIT_WORK (&hu->init_ready, hci_uart_init_work); +INIT_WORK (&hu->write_work, hci_uart_write_work);spin_lock_init (&hu->rx_lock);@@ -318,6 +326,8 @@ static void hci_uart_tty_close(struct tty_struct *tty)if (hdev)hci_uart_close (hdev); +cancel_work_sync (&hy->write_work); +if (test_and_clear_bit (HCI_UART_PROTO_SET , &hu->flags)) {if (hdev) {if (test_bit (HCI_UART_REGISTERED , &hu->flags))drivers/bluetooth/hci_uart.h +1
-0@@ -68,6 +68,7 @@ struct hci_uart {unsigned long hdev_flags;struct work_struct init_ready; +struct work_struct write_work;struct hci_uart_proto *proto;void *priv;
Signature · 1 line
balbi