From: Hugo Villeneuve <hugo@hugovil.com>
To: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Hui Peng <benquike@gmail.com>, Jiri Slaby <jirislaby@kernel.org>,
John Ogness <john.ogness@linutronix.de>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-serial <linux-serial@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
stable@vger.kernel.org
Subject: Re: [PATCH v7 1/2] serial: core: fix baud rate fallback in uart_get_baud_rate()
Date: Wed, 7 Oct 2026 16:27:46 -0400 [thread overview]
Message-ID: <20261007162746.6987a62beef9233a7d19b9c9@hugovil.com> (raw)
In-Reply-To: <a846724d-0080-1df4-e78d-179e589c3aa2@linux.intel.com>
Hi Ilpo,
On Wed, 7 Oct 2026 23:09:26 +0300 (EEST)
Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> wrote:
> On Wed, 7 Oct 2026, Hugo Villeneuve wrote:
>
> > Hi Ilpo,
> >
> > On Wed, 7 Oct 2026 13:38:55 +0300 (EEST)
> > Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> wrote:
> >
> > > On Tue, 6 Oct 2026, Hugo Villeneuve wrote:
> > >
> > > > On Thu, 1 Oct 2026 12:46:05 +0300 (EEST)
> > > > Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> wrote:
> > > >
> > > > > On Thu, 1 Oct 2026, Greg Kroah-Hartman wrote:
> > > > >
> > > > > > On Wed, Sep 30, 2026 at 12:59:00PM +0000, Hui Peng wrote:
> > > > > > > When uart_get_baud_rate() is called with a baud rate exceeding the port's
> > > > > > > maximum supported speed (port->uartclk / 16), it clips baud to [min,
> > > > > > > max - 1] and encodes it into termios via tty_termios_encode_baud_rate().
> > > > > > >
> > > > > > > However, because the loop bound is for (try = 0; try < 2; try++), the
> > > > > > > loop terminates immediately after try == 1 without re-evaluating
> > > > > >
> > > > > > But try == 1 should keep the loop going as it is < 2, right? What am I
> > > > > > missing here? Do I need more coffee?
> > > > > >
> > > > > > > baud = tty_termios_baud_rate(termios) for the clipped rate, hitting
> > > > > > > WARN_ON(1) and returning 0, which then triggers a fatal divide-by-zero
> > > > > > > (Oops: divide error) in uart_get_divisor():
> > > > > > >
> > > > > > > WARNING: drivers/tty/serial/serial_core.c:548 at uart_get_baud_rate+0x136/0x260
> > > > > > > [ ... ]
> > > > > > > divide error: 0000 [#1] PREEMPT SMP KASAN
> > > > > > >
> > > > > > > Increase the retry count in uart_get_baud_rate() from 2 to 3 iterations so
> > > > > > > that clipped baud rates are re-evaluated in the third iteration.
> > > > > >
> > > > > > What is the magic 2 here, and why turning it into a magic 3 somehow fix
> > > > > > things?
> > > > >
> > > > > Hi Greg & Hui,
> > > > >
> > > > > First of all, I'm withdrawing my Reviewed-by from this!!!
> > > > >
> > > > > Lets hope the submitter can finally get his/her act together and not make
> > > > > unlisted changes between versions or send non-sense.
> > > > >
> > > > > While the code change is still fine, it seems the submitter (or more
> > > > > likely AI) has changed the changelog from what I read when I reviewed
> > > > > this. And the new one is way worse than it used to be so not being able
> > > > > to follow what's going on is very understandable given the lackluster
> > > > > explanation that remains.
> > > > >
> > > > >
> > > > > What you're missing is that the baud returns happens within the loop, so:
> > > > >
> > > > > try == 0: use new, if baud is out of bound and old is available, switch to
> > > > > old
> > > > > try == 1: if old is also out of bounds, there's the last resort rule
> > > > > towards the end of the loop which is applied forcing baud to the
> > > > > accetable range.
> > > >
> > > > Hi all,
> > > > is it at all possible that old is out of bounds in the first place?
> > >
> > > Apparently it is, given the WARNING above (unless that too is fabricate
> > > by LLM which seems well within possiblities given the track record of this
> > > particular submitter).
> > >
> > > > If yes, is it something that should be fixed?
> > >
> > > I agree.
> > >
> > > I think the scenario here is (but take it with grain of salt, as the
> > > information seems to be constantly changing thanks to LLM/submitter
> > > failing to keep one's act to together) [1]:
> > >
> > > Tested in QEMU against Linux 7.3.0-rc3 by setting /dev/ttyS1 to B115200,
> > > lowering baud_base to 9600 (max = 9600) via TIOCSSERIAL, and calling
> > > tcsetattr() with B57600 (old = B115200), reproducing the WARNING and
> > > Oops: divide error in uart_get_divisor() on the unfixed kernel and
> > > verifying clean execution with 0 warnings/faults with the fix applied.
> > >
> > > So when rate gets lowered, it should alter the termios to prevent what is
> > > here seem as "old" having an invalid value.
> >
> > That makes sense...
> >
> > >
> > > [1] https://lore.kernel.org/linux-serial/20260930041216.155911-3-benquike@gmail.com/
> > >
> > > > > try == 2: loop exits => WARN_ON(1) triggers.
> > > > >
> > > > > What we'd want to happen with try == 2, is for it to use the return baud
> > > > > which is within the loop body.
> > > >
> > > > Then should the "return 0" statement be modified to "return baud", and
> > > > possibly the WARN_ON() removed? Then you wouldn't need to increase
> > > > max try?
> > >
> > > IMO, the entire loop constructs feels somewhat artificial in this case so
> > > I'd prefer to kill the loop entirely. But such refactoring is not going to
> > > be a minimal fix to the issue.
> >
> > Yes, when I updated uart_get_baud_rate() a few months ago, the loop
> > also felt a little bit weird, but I did not remove it because I assumed
> > it was working ok if we assumed, like I did, that the old baud rate was
> > always valid.
> >
> > I tried in the past to simplify it, make it more logical and
> > understandable by normal humans :) But I always end up with
> > something not so simple and obvious because of all the special cases
> > (B0, spd_* flags, etc).
>
> Trying to all that in the same function surely gets problematic and
> repetivive. But how about adding another function/helper that is just
> called multiple times to avoid duplicating them?
That is exactly what I have been trying all day :) I implemented a
uart_validate_baud_rate() helper. Still testing all corner cases...
> > I am still trying though, maybe I will have better luck this time :)
> i.
--
Hugo Villeneuve
next prev parent reply other threads:[~2026-10-07 20:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-30 12:58 [PATCH v7 0/2] serial: core: fix baud rate fallback loop and baud_base overflow Hui Peng
2026-09-30 12:59 ` [PATCH v7 1/2] serial: core: fix baud rate fallback in uart_get_baud_rate() Hui Peng
2026-10-01 8:54 ` Greg Kroah-Hartman
2026-10-01 9:46 ` Ilpo Järvinen
2026-10-06 15:29 ` Hugo Villeneuve
2026-10-07 10:38 ` Ilpo Järvinen
2026-10-07 17:06 ` Hugo Villeneuve
2026-10-07 20:09 ` Ilpo Järvinen
2026-10-07 20:27 ` Hugo Villeneuve [this message]
2026-09-30 12:59 ` [PATCH v7 2/2] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info() Hui Peng
2026-09-30 14:09 ` Andy Shevchenko
2026-10-01 8:55 ` Greg Kroah-Hartman
2026-10-06 15:36 ` Hugo Villeneuve
2026-10-01 8:50 ` [PATCH v7 0/2] serial: core: fix baud rate fallback loop and baud_base overflow Greg Kroah-Hartman
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=20261007162746.6987a62beef9233a7d19b9c9@hugovil.com \
--to=hugo@hugovil.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=benquike@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=jirislaby@kernel.org \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=stable@vger.kernel.org \
/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®