All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: "Maciej W. Rozycki" <macro@orcam.me.uk>
Cc: sashiko-reviews@lists.linux.dev,
	Jiri Slaby <jirislaby@kernel.org>,
	Hugo Villeneuve <hvilleneuve@dimonoff.com>,
	Kees Cook <kees@kernel.org>, Lukas Wunner <lukas@wunner.de>,
	John Ogness <john.ogness@linutronix.de>,
	Jiayuan Chen <jiayuan.chen@shopee.com>,
	Gerhard Engleder <eg@keba.com>,
	linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/1] serial: core: Don't clobber the baud rate on hangup via B0
Date: Tue, 13 Oct 2026 15:16:45 +0200	[thread overview]
Message-ID: <2026101335-broadside-register-8a53@gregkh> (raw)
In-Reply-To: <alpine.DEB.2.21.2610121325280.55767@angie.orcam.me.uk>

On Mon, Oct 12, 2026 at 03:37:01PM +0100, Maciej W. Rozycki wrote:
> On Mon, 12 Oct 2026, sashiko-bot@kernel.org wrote:
> 
> > > @@ -514,10 +514,12 @@ uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
> > >  		 * Special case: B0 rate.
> > >  		 */
> > >  		if (baud == 0)
> > > -			return max(min, 9600);
> > > +			return port->hup_speed;
> > 
> > [Severity: High]
> > Does this risk a division by zero panic?
> > 
> > If a new struct uart_port is initialized, port->hup_speed defaults to 0.
> > If a B0 rate is requested before any valid rate is set (such as via
> > console=ttyS0,0 or an early tcsetattr with B0), this returns 0.
> 
>  Yeah, this has glimpsed in my mind and then I lost it.  Indeed we need to 
> keep the fallback 9600/min logic after all for the initial setup.  Easily 
> fixed since it's not in the hot path.
> 
> > >  
> > > -		if (baud >= min && baud <= max)
> > > +		if (baud >= min && baud <= max) {
> > > +			port->hup_speed = baud;
> > 
> > [Severity: Medium]
> > Could this result in a data race on port->hup_speed?
> > 
> > In the standard uart_set_termios() path, uart_get_baud_rate() is called
> > holding only port->mutex and termios_rwsem, not port->lock.
> > 
> > At the same time, atomic console write paths execute asynchronously from
> > hardirq or NMI holding only the nbcon lock. For example:
> > 
> > drivers/tty/serial/8250/8250_port.c:serial8250_console_restore() {
> > 	...
> > 	baud = serial8250_get_baud_rate(port, &termios, NULL);
> > 	...
> > }
> > 
> > This calls into uart_get_baud_rate() without holding port->mutex. A
> > concurrent tcsetattr() and atomic console write can trigger an unprotected
> > data race on port->hup_speed. A torn read or write on this variable could
> > yield a garbage baud rate and lead to hardware misconfiguration.
> 
>  This seems like the only exception across all the serial drivers in the 
> use of uart_get_baud_rate().  Since this is always called to restore the 
> most recent baud rate setting only, perhaps the path of least resistance 
> would be just to refrain from updating port->hup_speed if called with the 
> port lock held?
> 
>  Otherwise it would require rewriting all the 60+ call sites to use the 
> port lock to guard the whole sequence from a call to uart_get_baud_rate() 
> through to the write of the settings obtained to the port registers, which 
> seems an overkill to me and asking for trouble with some odd hardware.
> 
>  I'll experiment with the idea outlined or will be happy with any hints 
> otherwise.

That idea seems sane, thanks for working on this.

greg k-h

      reply	other threads:[~2026-10-13 13:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12  1:49 [PATCH v2 0/1] serial: core: Don't clobber the baud rate on hangup via B0 Maciej W. Rozycki
2026-10-12  1:49 ` [PATCH v2 1/1] " Maciej W. Rozycki
2026-10-12  2:10   ` sashiko-bot
2026-10-12 14:37     ` Maciej W. Rozycki
2026-10-13 13:16       ` Greg Kroah-Hartman [this message]

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=2026101335-broadside-register-8a53@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=eg@keba.com \
    --cc=hvilleneuve@dimonoff.com \
    --cc=jiayuan.chen@shopee.com \
    --cc=jirislaby@kernel.org \
    --cc=john.ogness@linutronix.de \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=macro@orcam.me.uk \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.