There is a deadlock in rs_close(), which is shown
below:
(Thread 1) | (Thread 2)
| rs_open()
rs_close() | mod_timer()
spin_lock_bh() //(1) | (wait a time)
... | rs_poll()
del_timer_sync() | spin_lock() //(2)
(wait timer to stop) | ...
We hold timer_lock in position (1) of thread 1 and
use del_timer_sync() to wait timer to stop, but timer handler
also need timer_lock in position (2) of thread 2.
As a result, rs_close() will block forever.
This patch extracts del_timer_sync() from the protection of
spin_lock_bh(), which could let timer handler to obtain
the needed lock.
Signed-off-by: Duoming Zhou <[email protected]>
---
arch/xtensa/platforms/iss/console.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/xtensa/platforms/iss/console.c b/arch/xtensa/platforms/iss/console.c
index 81d7c7e8f7e..d431b61ae3c 100644
--- a/arch/xtensa/platforms/iss/console.c
+++ b/arch/xtensa/platforms/iss/console.c
@@ -51,8 +51,10 @@ static int rs_open(struct tty_struct *tty, struct file * filp)
static void rs_close(struct tty_struct *tty, struct file * filp)
{
spin_lock_bh(&timer_lock);
- if (tty->count == 1)
+ if (tty->count == 1) {
+ spin_unlock_bh(&timer_lock);
del_timer_sync(&serial_timer);
+ }
spin_unlock_bh(&timer_lock);
}
--
2.17.1
Hello!
On 4/7/22 9:37 AM, Duoming Zhou wrote:
> There is a deadlock in rs_close(), which is shown
> below:
>
> (Thread 1) | (Thread 2)
> | rs_open()
> rs_close() | mod_timer()
> spin_lock_bh() //(1) | (wait a time)
> ... | rs_poll()
> del_timer_sync() | spin_lock() //(2)
> (wait timer to stop) | ...
>
> We hold timer_lock in position (1) of thread 1 and
> use del_timer_sync() to wait timer to stop, but timer handler
> also need timer_lock in position (2) of thread 2.
> As a result, rs_close() will block forever.
>
> This patch extracts del_timer_sync() from the protection of
> spin_lock_bh(), which could let timer handler to obtain
> the needed lock.
>
> Signed-off-by: Duoming Zhou <[email protected]>
> ---
> arch/xtensa/platforms/iss/console.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/arch/xtensa/platforms/iss/console.c b/arch/xtensa/platforms/iss/console.c
> index 81d7c7e8f7e..d431b61ae3c 100644
> --- a/arch/xtensa/platforms/iss/console.c
> +++ b/arch/xtensa/platforms/iss/console.c
> @@ -51,8 +51,10 @@ static int rs_open(struct tty_struct *tty, struct file * filp)
> static void rs_close(struct tty_struct *tty, struct file * filp)
> {
> spin_lock_bh(&timer_lock);
> - if (tty->count == 1)
> + if (tty->count == 1) {
> + spin_unlock_bh(&timer_lock);
> del_timer_sync(&serial_timer);
> + }
> spin_unlock_bh(&timer_lock);
Double unlock iff tty->count == 1?
[...]
MBR, Sergey