From: Cen Zhang <zzzccc427@gmail.com>
To: gregkh@linuxfoundation.org, jirislaby@kernel.org
Cc: peter@hurleysoftware.com, linux-kernel@vger.kernel.org,
linux-serial@vger.kernel.org, baijiaju1990@gmail.com,
Cen Zhang <zzzccc427@gmail.com>
Subject: [PATCH] tty: n_tty: order lockless input availability checks
Date: Mon, 4 May 2026 15:23:21 +0800 [thread overview]
Message-ID: <20260504072321.928921-1-zzzccc427@gmail.com> (raw)
The N_TTY read buffer uses release/acquire ordering for its
lockless ring indices. Input producers release-publish canon_head and
commit_head after updating the buffer and delimiter flags, and readers
acquire those heads before copying data. Readers also release-publish
read_tail before producers use it to calculate room.
chars_in_buffer() and input_available_p() sample the same indices
for availability and flow-control decisions, but use plain loads. That
can miss the ordering used by the data-copy paths and can also let
poll() observe termios-synthesized availability with weaker ordering
than normal receive-side publication.
Use acquire loads for the lockless head/tail samples in those
helpers. When n_tty_set_termios() updates canonical/noncanonical
availability, publish the updated heads with release stores as well.
Keep the cached icanon bit as an intentionally lockless mode snapshot
and annotate that access.
Fixes: 70aca71f92ca ("n_tty: Fix unordered accesses to lockless read buffer")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
drivers/tty/n_tty.c | 32 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index e6a0f5b40d0a..56b0cd96a453 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -213,9 +213,17 @@ static void n_tty_kick_worker(const struct tty_struct *tty)
static ssize_t chars_in_buffer(const struct tty_struct *tty)
{
const struct n_tty_data *ldata = tty->disc_data;
- size_t head = ldata->icanon ? ldata->canon_head : ldata->commit_head;
+ bool icanon = data_race((int)ldata->icanon); /* lockless snapshot */
+ size_t head;
+ size_t tail;
- return head - ldata->read_tail;
+ if (icanon)
+ head = smp_load_acquire(&ldata->canon_head); /* producer publish */
+ else
+ head = smp_load_acquire(&ldata->commit_head); /* producer publish */
+ tail = smp_load_acquire(&ldata->read_tail); /* consumer publish */
+
+ return head - tail;
}
/**
@@ -1779,14 +1787,14 @@ static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old
bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
ldata->line_start = ldata->read_tail;
if (!L_ICANON(tty) || !read_cnt(ldata)) {
- ldata->canon_head = ldata->read_tail;
+ smp_store_release(&ldata->canon_head, ldata->read_tail); /* publish */
ldata->push = 0;
} else {
set_bit(MASK(ldata->read_head - 1), ldata->read_flags);
- ldata->canon_head = ldata->read_head;
+ smp_store_release(&ldata->canon_head, ldata->read_head); /* publish */
ldata->push = 1;
}
- ldata->commit_head = ldata->read_head;
+ smp_store_release(&ldata->commit_head, ldata->read_head); /* publish */
ldata->erasing = 0;
ldata->lnext = 0;
}
@@ -1908,11 +1916,17 @@ static inline int input_available_p(const struct tty_struct *tty, int poll)
{
const struct n_tty_data *ldata = tty->disc_data;
int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
+ bool icanon = data_race((int)ldata->icanon); /* lockless snapshot */
+ size_t tail = smp_load_acquire(&ldata->read_tail); /* consumer publish */
+ size_t head;
- if (ldata->icanon && !L_EXTPROC(tty))
- return ldata->canon_head != ldata->read_tail;
- else
- return ldata->commit_head - ldata->read_tail >= amt;
+ if (icanon && !L_EXTPROC(tty)) {
+ head = smp_load_acquire(&ldata->canon_head); /* producer publish */
+ return head != tail;
+ }
+
+ head = smp_load_acquire(&ldata->commit_head); /* producer publish */
+ return head - tail >= amt;
}
/**
--
2.43.0
next reply other threads:[~2026-05-04 7:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-04 7:23 Cen Zhang [this message]
2026-05-04 7:34 ` [PATCH] tty: n_tty: order lockless input availability checks Greg KH
2026-05-04 7:47 ` Cen Zhang
2026-05-04 7:53 ` Greg KH
2026-05-04 8:11 ` Cen Zhang
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=20260504072321.928921-1-zzzccc427@gmail.com \
--to=zzzccc427@gmail.com \
--cc=baijiaju1990@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=peter@hurleysoftware.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