From: Christian Riesch <christian.riesch@omicron.at>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
<linux-kernel@vger.kernel.org>
Cc: "Måns Rullgård" <mans@mansr.com>,
"Peter Hurley" <peter@hurleysoftware.com>,
"Christian Riesch" <christian.riesch@omicron.at>,
stable@vger.kernel.org
Subject: [PATCH v2] n_tty: Fix read_buf race condition, increment read_head after pushing data
Date: Tue, 11 Nov 2014 08:24:17 +0100 [thread overview]
Message-ID: <a2e126c9-3001-4255-91c9-6311b54988c5@EXC02-ATKLA.omicron.at> (raw)
Commit 19e2ad6a09f0c06dbca19c98e5f4584269d913dd ("n_tty: Remove overflow
tests from receive_buf() path") moved the increment of read_head into
the arguments list of read_buf_addr(). Function calls represent a
sequence point in C. Therefore read_head is incremented before the
character c is placed in the buffer. Since the circular read buffer is
a lock-less design since commit 6d76bd2618535c581f1673047b8341fd291abc67
("n_tty: Make N_TTY ldisc receive path lockless"), this creates a race
condition that leads to communication errors.
This patch modifies the code to increment read_head _after_ the data
is placed in the buffer and thus fixes the race for non-SMP machines.
To fix the problem for SMP machines, memory barriers must be added in
a separate patch.
Signed-off-by: Christian Riesch <christian.riesch@omicron.at>
Cc: <stable@vger.kernel.org>
---
This is version 2 of the patch in [1].
Changes for v2:
- Rewrote commit message. Since I did not know better, I blamed the compiler
in v1, but actually the code was wrong. See the discussion in [1].
- Removed memory barriers. For non-SMP machines they are not required,
for SMP machines more brainwork and discussions are needed.
Best regards,
Christian
[1] https://lkml.org/lkml/2014/11/6/216
drivers/tty/n_tty.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 2e900a9..b09f326 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -321,7 +321,9 @@ static void n_tty_check_unthrottle(struct tty_struct *tty)
static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
{
- *read_buf_addr(ldata, ldata->read_head++) = c;
+ *read_buf_addr(ldata, ldata->read_head) = c;
+ /* increment read_head _after_ placing the character in the buffer */
+ ldata->read_head++;
}
/**
--
1.7.9.5
next reply other threads:[~2014-11-11 7:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-11 7:24 Christian Riesch [this message]
2014-11-11 13:04 ` [PATCH v2] n_tty: Fix read_buf race condition, increment read_head after pushing data Måns Rullgård
2014-11-12 7:28 ` Christian Riesch
2014-11-12 11:53 ` Måns Rullgård
2014-11-12 20:03 ` Christian Riesch
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=a2e126c9-3001-4255-91c9-6311b54988c5@EXC02-ATKLA.omicron.at \
--to=christian.riesch@omicron.at \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mans@mansr.com \
--cc=peter@hurleysoftware.com \
--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;
as well as URLs for NNTP newsgroup(s).