linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <jhovold@gmail.com>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: Chris Ruehl <chris.ruehl@gtsys.com.hk>,
	Alan Stern <stern@rowland.harvard.edu>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	linux-usb@vger.kernel.org, linux-serial@vger.kernel.org,
	Alan Cox <alan@linux.intel.com>, Johan Hovold <jhovold@gmail.com>
Subject: [RFC 0/4] tty: port hangup and close issues
Date: Wed, 13 Feb 2013 18:27:22 +0100	[thread overview]
Message-ID: <1360776446-31371-1-git-send-email-jhovold@gmail.com> (raw)
In-Reply-To: <20130213142513.GA21078@localhost>

As mentioned in a previous mail there is a problem in the tty layer
which affects the USB-serial drivers. In summary, a call to dtr_rts()
can be made even after the device has been disconnected and the tty
device unregistered.

This can be worked around in usb-serial core by checking the
disconnected flag in dtr_rts() as we do in activate().

In my setup it can be almost perfectly reproduced by keeping the
usb-serial device open, while repeatedly opening and closing the port.
When the device is disconnected any open ports are properly hung up but
there is a window during disconnect where we can get a second open. This
call will fail in the serial driver because the disconnected flag is
checked in activate(). TTY core still proceeds to close the
never-successfully opened port, which involves the call to dtr_rts().

[ Note that this closing of an uninitialised port seems to be a bug in
  itself, which these patches aim to fix. ]

If the driver has port drain delay set, things get even worse, as we
then reschedule in tty_port_close_start() and usb-serial disconnect() can
proceed with unregistering the device. When scheduled back in, the device
is gone and the call to dtr_rts() will oops (as the usb-serial port
data is now NULL).

[ Note also that there has been a recent bug report, where dtr_rts() is
  called after tty device unregister when apparently only using minicom.
  This could indicate that there are more than one way to trigger this
  problem. ]

While trying to fix this bug I came across two related issues. First, it
turns out that dtr_rts() is normally not called at all during hang up,
and second, that the closing of uninitialised ports needlessly honours
drain delay.

1) With an open port (each open raising DTR/RST), dtr_rts() is never
   called to lower DTR/RTS when we get a hangup. This is because
   any open port reference will get the hung_up_tty_fops, which is later
   checked for in in tty_port_close_start before calling dtr_rts(). [ So
   it is only in the above somewhat construed case, with a second failed
   open that dtr_rts() is even called after a hangup. ]

   I propose to fix this by moving the HUPCL (DTR/RTS) handling from
   tty_port_close_start() to tty_shutdown() as well as protecting it
   with the ASYNC_INITIALISED flag. This way the signals are lowered on
   hangup as well as on the last close of a tty device (as before). This
   is also how things are currently handled in several serial drivers. 

   I've verified that all serial drivers that rely on
   tty_port_close_start() directly (rather than through tty_port_close())
   lowers DTR/RTS as part of their close() except for mxser.c and
   n_gsm.c, which I would need to fix.

   [ Note that some drivers currently seems broken with respect to this,
     as comments indicate that they do not expect tty_port_close_start to
     change the modem status. ]

   Note also that this would fix the usb-serial driver oops as a
   side-effect, as tty_port_shutdown() completes before the device is
   unregistered, and it would not be called again due to
   ASYNC_INITIALIZED.

2) Looking at tty_port_close_start() it seems to me that we should never
   do tty-driver callbacks on ports that have never been opened (as
   mentioned above). Currently, only the call to tty_wait_until_sent()
   is protected by the ASYNC_INITIALIZED flag, but I suggest this to be 
   extended to all driver callbacks as well as the drain-delay handling.

   As things stand today, a port with drain delay set could hang
   needlessly for up to two seconds on every failed open. The final
   patch below fixes this.

   Note that only protecting the driver call-backs could also in itself
   be enough to prevent the oops described above if it's decided best to
   keep HUPCL handling in tty_port_close_start().

The first, and third patches in the series are essentially clean-ups,
while the second patch fixes HUPCL handling and the last fixes the
closing of uninitialised ports.

Johan

   
Johan Hovold (4):
  tty: clean up port shutdown
  tty: fix DTR/RTS not being dropped on hang up
  tty: clean up port drain-delay handling
  tty: fix close of uninitialised ports

 drivers/tty/tty_port.c | 72 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 28 deletions(-)

-- 
1.8.1.1


  parent reply	other threads:[~2013-02-13 17:29 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <510B2C09.7020700@gtsys.com.hk>
2013-02-13 14:25 ` USB Ooops PL2303 when unplug while use (linux v3.7.3) Johan Hovold
2013-02-13 14:28   ` [PATCH] USB: serial: fix null-pointer dereferences on disconnect Johan Hovold
     [not found]     ` <1360765731-23164-1-git-send-email-jhovold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2013-02-13 14:34       ` Felipe Balbi
2013-02-13 14:48         ` Johan Hovold
2013-02-13 16:41     ` Greg KH
2013-02-13 16:53       ` [PATCH resend] " Johan Hovold
2013-02-13 17:27   ` Johan Hovold [this message]
2013-02-13 17:27     ` [RFC 1/4] tty: clean up port shutdown Johan Hovold
2013-02-13 17:27     ` [RFC 2/4] tty: fix DTR/RTS not being dropped on hang up Johan Hovold
     [not found]       ` <1360776446-31371-3-git-send-email-jhovold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2013-02-13 18:06         ` [RFC v2 " Johan Hovold
2013-02-13 19:32         ` [RFC " Peter Hurley
     [not found]           ` <1360783973.8499.48.camel-AsKIXgLx6sE@public.gmane.org>
2013-02-14  1:11             ` Peter Hurley
     [not found]               ` <1360804273.3385.23.camel-AsKIXgLx6sE@public.gmane.org>
2013-02-14  9:04                 ` Johan Hovold
2013-02-13 17:27     ` [RFC 3/4] tty: clean up port drain-delay handling Johan Hovold
2013-02-14  1:39       ` Peter Hurley
2013-02-14  9:12         ` Johan Hovold
2013-02-13 17:27     ` [RFC 4/4] tty: fix close of uninitialised ports Johan Hovold
2013-02-13 17:36     ` [RFC 0/4] tty: port hangup and close issues Alan Cox
     [not found]       ` <20130213173630.63e29b47-+KEw/ACL1GZE/aiTQr5FLb0Ud+EcFu5g@public.gmane.org>
2013-02-13 18:05         ` Johan Hovold
     [not found]     ` <1360776446-31371-1-git-send-email-jhovold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2013-02-20 16:02       ` [PATCH 0/4] TTY: port hangup and close fixes Johan Hovold
2013-02-20 16:02         ` [PATCH 1/4] TTY: clean up port shutdown Johan Hovold
2013-02-20 16:02         ` [PATCH 2/4] TTY: fix DTR not being dropped on hang up Johan Hovold
2013-02-23 14:18           ` Peter Hurley
2013-02-26 10:59             ` Johan Hovold
2013-02-20 16:02         ` [PATCH 3/4] TTY: clean up port drain-delay handling Johan Hovold
     [not found]         ` <1361376172-31860-1-git-send-email-jhovold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2013-02-20 16:02           ` [PATCH 4/4] TTY: fix close of uninitialised ports Johan Hovold
2013-02-21  1:53         ` [PATCH 0/4] TTY: port hangup and close fixes Peter Hurley
2013-02-13 18:40   ` USB Ooops PL2303 when unplug while use (linux v3.7.3) Peter Hurley
     [not found]     ` <1360780808.8499.43.camel-AsKIXgLx6sE@public.gmane.org>
2013-02-14 17:43       ` Johan Hovold
2013-02-22 10:03         ` USB: serial: port lifetimes (was: Re: USB Ooops PL2303 when unplug while use (linux v3.7.3)) Johan Hovold
2013-02-22 15:17           ` Alan Stern
2013-02-22 17:11             ` Johan Hovold
2013-02-22 17:35               ` Alan Stern
     [not found]                 ` <Pine.LNX.4.44L0.1302221218270.1365-100000-IYeN2dnnYyZXsRXLowluHWD2FQJk+8+b@public.gmane.org>
2013-02-22 17:44                   ` Greg KH
2013-02-22 18:21                 ` Peter Hurley
     [not found]                   ` <1361557284.5752.25.camel-AsKIXgLx6sE@public.gmane.org>
2013-02-22 18:57                     ` Alan Stern
2013-02-23 16:05                     ` Johan Hovold
2013-02-23 16:23                 ` Johan Hovold
2013-02-23 16:34                   ` Johan Hovold
2013-02-23 17:31                   ` Alan Stern
2013-02-23 18:12                     ` Johan Hovold
2013-02-23 21:20                       ` Alan Stern

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=1360776446-31371-1-git-send-email-jhovold@gmail.com \
    --to=jhovold@gmail.com \
    --cc=alan@linux.intel.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=chris.ruehl@gtsys.com.hk \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-serial@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;
as well as URLs for NNTP newsgroup(s).