linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
Cc: Jiri Slaby <jslaby@suse.com>, Rob Herring <robh@kernel.org>,
	linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org
Subject: Re: [PATCH 2/2] tty: add retry to tty_init_dev() to workaround a race condition
Date: Wed, 20 Nov 2019 16:44:50 +0100	[thread overview]
Message-ID: <20191120154450.GB3004157@kroah.com> (raw)
In-Reply-To: <20191120151709.14148-2-sudipm.mukherjee@gmail.com>

On Wed, Nov 20, 2019 at 03:17:09PM +0000, Sudip Mukherjee wrote:
> There seems to be a race condition in tty drivers and I could see on
> many boot cycles a NULL pointer dereference as tty_init_dev() tries to
> do 'tty->port->itty = tty' even though tty->port is NULL.
> 'tty->port' will be set by the driver and if the driver has not yet done
> it before we open the tty device we can get to this situation. By adding
> some extra debug prints, I noticed that tty_port_link_device() is
> initialising 'driver->ports[index]' just few microseconds after I
> get the warning.
> So, add one retry so that tty_init_dev() will return -EAGAIN on its first
> try if 'tty->port' is not set yet, and then tty_open() will try to open
> it again.
> 
> Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
> ---
>  drivers/tty/pty.c                   |  2 +-
>  drivers/tty/serdev/serdev-ttyport.c |  2 +-
>  drivers/tty/tty_io.c                | 20 ++++++++++++++------
>  include/linux/tty.h                 |  3 ++-
>  4 files changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
> index 00099a8439d2..22e8c40d9f9c 100644
> --- a/drivers/tty/pty.c
> +++ b/drivers/tty/pty.c
> @@ -842,7 +842,7 @@ static int ptmx_open(struct inode *inode, struct file *filp)
>  
>  
>  	mutex_lock(&tty_mutex);
> -	tty = tty_init_dev(ptm_driver, index);
> +	tty = tty_init_dev(ptm_driver, index, 0);

Horrible naming scheme for this new "flag".

Look at that call here, can you instantly tell what this call is doing
with "0"?  I sure can not :(

If you really want to do this, you make a different function,
tty_init_dev_retry() and then have that pass in a retry flag in the tty
core, so that any users always know what they are doing here.

But, this really feels like a race in the code somewhere:

> --- a/drivers/tty/tty_io.c
> +++ b/drivers/tty/tty_io.c
> @@ -1295,6 +1295,7 @@ static int tty_reopen(struct tty_struct *tty)
>   *	tty_init_dev		-	initialise a tty device
>   *	@driver: tty driver we are opening a device on
>   *	@idx: device index
> + *	@retry: retry count if driver has not set tty->port yet

Why would tty->port not be set up already?  The caller has control over
this, what is not happening correctly to cause this?

thanks,

greg k-h

  reply	other threads:[~2019-11-20 15:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-20 15:17 [PATCH 1/2] tty: remove unused argument from tty_open_by_driver() Sudip Mukherjee
2019-11-20 15:17 ` [PATCH 2/2] tty: add retry to tty_init_dev() to workaround a race condition Sudip Mukherjee
2019-11-20 15:44   ` Greg Kroah-Hartman [this message]
2019-11-20 15:55     ` Sudip Mukherjee
2019-11-25  6:27   ` Dan Carpenter

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=20191120154450.GB3004157@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sudipm.mukherjee@gmail.com \
    /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).