mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alan Cox <alan@lxorguk.ukuu.org.uk>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Daniel Mack <daniel@caiaq.de>,
	Kernel development list <linux-kernel@vger.kernel.org>,
	USB list <linux-usb@vger.kernel.org>
Subject: Re: [PATCH] [usb-serial] fix Ooops on uplug
Date: Tue, 21 Jul 2009 20:21:23 +0100	[thread overview]
Message-ID: <20090721202123.404339aa@lxorguk.ukuu.org.uk> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0907211423420.2088-100000@iolanthe.rowland.org>

> > 	file->f_ops = &hung_up_tty_ops;
> > 
> > 		(so the USB close will never be called)
> 
> That doesn't make sense.  The driver's open method has been called, so 
> of course the driver expects its close method to be called.

The driver close method yes - but that uses tty_port_close_start() which
sees the port was hung up and leaves well alone.

> > 	ldisc hangup
> > 	tty->ops->hangup (no-op on USB serial)
> 
> What do you mean?  There is a serial_hangup method in usb-serial.c
> and it does get called; see below.

Thats me not reading carefully. It isn't just a resource free it does
stuff. That calls drv->close() so in fact the USB layer for want of a
better word "fakes" the close.

> [  283.624088]  [<f08b09d7>] serial_hangup+0x45/0x66 [usbserial]
> [  283.624187]  [<c112018c>] do_tty_hangup+0x28c/0x2b9

So we passed

        /* This breaks for file handles being sent over AF_UNIX sockets ?
        */ list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
                if (filp->f_op->write == redirected_tty_write)
                        cons_filp = filp;
                if (filp->f_op->write != tty_write)
                        continue;
                closecount++;
                tty_fasync(-1, filp, 0);        /* can't block */
                filp->f_op = &hung_up_tty_fops;
        }

and changed the fops. As you say my theory was completely wrong

> Close the open file descriptor:
> [  291.227977] tty_release_dev of tty2 (tty count=2)...
> [  291.230492] tty_release_dev of ttyUSB0 (tty count=1)...
> [  291.230630] serial_close port 107 (ef7fd920)
> 
> That line was inserted in serial_close.  As you can see, the port 
> number is wrong because the port structure has already been 
> deallocated by port_free.  And that leads to the following corruption.

Bingo - and that in turn means that the tty layer doesn't realise the
port has been hung up which makes tty_port_close_start do random things
which causes us to double free.

So in fact we need to delay the resource free until the tty layer has
really finished with it as the port resource contains the tty layer port.

We can't just skip freeing the resources in the hangup method as
tty_port_close_start() will return 0 and leak them on a hangup.

Alan: does this make sense

	Take an extra tty layer reference to the usb_serial at open time

	Put that reference in the tty shutdown() hook which is called
	when the tty struct gets its final kref_put (ie after the close,
	and if there is any outstanding other use eg in an IRQ handler on
	another processor).

Am I understanding the usb_serial_port lifetime correctly ?


  reply	other threads:[~2009-07-21 19:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20090720155502.50b84ae9@lxorguk.ukuu.org.uk>
2009-07-20 17:51 ` Daniel Mack
2009-07-20 22:48   ` Alan Cox
2009-07-20 21:58     ` Daniel Mack
2009-07-20 23:45       ` Alan Cox
2009-07-21 15:53         ` Alan Stern
2009-07-21 15:56           ` Daniel Mack
2009-07-21 16:00             ` Alan Cox
2009-07-21 16:11               ` Alan Stern
2009-07-21 16:18                 ` Alan Cox
2009-07-21 16:16           ` Alan Cox
2009-07-21 16:19             ` Daniel Mack
2009-07-21 16:28               ` Alan Cox
2009-07-21 18:36             ` Alan Stern
2009-07-21 19:21               ` Alan Cox [this message]
2009-07-21 21:38                 ` Alan Stern
2009-07-21 22:55                   ` Alan Cox
2009-07-22 14:44                     ` Alan Stern
2009-07-22 16:17                       ` Alan Cox
2009-07-22 18:21                         ` Alan Stern
2009-07-22 22:35                           ` Alan Cox
2009-07-22 22:45                             ` Alan Stern
2009-07-22 22:48                               ` Alan Cox
2009-07-20 23:32   ` Alan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090721202123.404339aa@lxorguk.ukuu.org.uk \
    --to=alan@lxorguk.ukuu.org.uk \
    --cc=daniel@caiaq.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®