linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hvc_console: Fix a race between hvc_close and hvc_remove
@ 2010-02-26 11:46 Amit Shah
  2010-02-27  1:33 ` Alan Cox
  0 siblings, 1 reply; 2+ messages in thread
From: Amit Shah @ 2010-02-26 11:46 UTC (permalink / raw)
  To: linux-kernel; +Cc: Amit Shah, linuxppc-dev, Rusty Russell, Alan Cox

Alan pointed out a race in the code where hvc_remove is invoked. The
recent virtio_console work is the first user of hvc_remove().

Alan describes it thus:

The hvc_console assumes that a close and remove call can't occur at the
same time.

In addition tty_hangup(tty) is problematic as tty_hangup is asynchronous
itself....

So this can happen

        hvc_close                               hvc_remove
        hung up ? - no
                                                lock
                                                tty = hp->tty
                                                unlock
        lock
	hp->tty = NULL
        unlock
	notify del
        kref_put the hvc struct
        close completes
        tty is destroyed
                                                tty_hangup dead tty
                                                tty->ops will be NULL
                                                NULL->...

This patch adds some tty krefs and also converts to using tty_vhangup()
before putting the tty kref.

Reported-by: Alan Cox <alan@lxorguk.ukuu.org.uk>
Signed-off-by: Amit Shah <amit.shah@redhat.com>
CC: Alan Cox <alan@lxorguk.ukuu.org.uk>
CC: linuxppc-dev@ozlabs.org
CC: Rusty Russell <rusty@rustcorp.com.au>
---

I can't be sure if this is all that's needed. tty people, please take
a look!

 drivers/char/hvc_console.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/drivers/char/hvc_console.c b/drivers/char/hvc_console.c
index d8dac58..3983e32 100644
--- a/drivers/char/hvc_console.c
+++ b/drivers/char/hvc_console.c
@@ -106,6 +106,7 @@ static struct hvc_struct *hvc_get_by_index(int index)
 		spin_lock_irqsave(&hp->lock, flags);
 		if (hp->index == index) {
 			kref_get(&hp->kref);
+			tty_kref_get(hp->tty);
 			spin_unlock_irqrestore(&hp->lock, flags);
 			spin_unlock(&hvc_structs_lock);
 			return hp;
@@ -390,6 +391,7 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
 	}
 
 	kref_put(&hp->kref, destroy_hvc_struct);
+	tty_kref_put(tty);
 }
 
 static void hvc_hangup(struct tty_struct *tty)
@@ -806,6 +808,7 @@ int hvc_remove(struct hvc_struct *hp)
 	unsigned long flags;
 	struct tty_struct *tty;
 
+	tty_kref_get(hp->tty);
 	spin_lock_irqsave(&hp->lock, flags);
 	tty = hp->tty;
 
@@ -830,7 +833,9 @@ int hvc_remove(struct hvc_struct *hp)
 	 * cleaned up the hvc_struct.
 	 */
 	if (tty)
-		tty_hangup(tty);
+		tty_vhangup(tty);
+
+	tty_kref_put(hp->tty);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(hvc_remove);
-- 
1.6.2.5

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] hvc_console: Fix a race between hvc_close and hvc_remove
  2010-02-26 11:46 [PATCH] hvc_console: Fix a race between hvc_close and hvc_remove Amit Shah
@ 2010-02-27  1:33 ` Alan Cox
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Cox @ 2010-02-27  1:33 UTC (permalink / raw)
  To: Amit Shah; +Cc: linuxppc-dev, Rusty Russell, linux-kernel

> +	tty_kref_get(hp->tty);
>  	spin_lock_irqsave(&hp->lock, flags);
>  	tty = hp->tty;
>  
> @@ -830,7 +833,9 @@ int hvc_remove(struct hvc_struct *hp)
>  	 * cleaned up the hvc_struct.
>  	 */
>  	if (tty)
> -		tty_hangup(tty);
> +		tty_vhangup(tty);
> +
> +	tty_kref_put(hp->tty);

You need to deref hp->tty, take the kref under the lock (and indeed do
all assignments to it that way too), then tty_kref_put(tty), otherwise
what stops hp->tty changing during the remove ?

Take a look how tty_port_tty_get() and tty_port.c does it ...

Alan

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-02-27  1:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-26 11:46 [PATCH] hvc_console: Fix a race between hvc_close and hvc_remove Amit Shah
2010-02-27  1:33 ` Alan Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).