mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Måns Rullgård" <mans@mansr.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christian Riesch <christian.riesch@omicron.at>,
	Jiri Slaby <jslaby@suse.cz>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] n_tty: Add memory barrier to fix race condition in receive path
Date: Thu, 06 Nov 2014 22:54:13 +0000	[thread overview]
Message-ID: <yw1xfvdwj5yi.fsf@unicorn.mansr.com> (raw)
In-Reply-To: <20141106223114.GA1701@kroah.com> (Greg Kroah-Hartman's message of "Thu, 6 Nov 2014 14:31:14 -0800")

Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:

> On Thu, Nov 06, 2014 at 10:12:54PM +0000, Måns Rullgård wrote:
>> Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:
>> 
>> > On Thu, Nov 06, 2014 at 09:38:59PM +0000, Måns Rullgård wrote:
>> >> Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:
>> >> 
>> >> > On Thu, Nov 06, 2014 at 09:01:36PM +0000, Måns Rullgård wrote:
>> >> >> Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:
>> >> >> 
>> >> >> > On Thu, Nov 06, 2014 at 08:49:01PM +0000, Måns Rullgård wrote:
>> >> >> >> Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:
>> >> >> >> 
>> >> >> >> > On Thu, Nov 06, 2014 at 12:39:59PM +0100, Christian Riesch wrote:
>> >> >> >> >> The current implementation of put_tty_queue() causes a race condition
>> >> >> >> >> when re-arranged by the compiler.
>> >> >> >> >> 
>> >> >> >> >> On my build with gcc 4.8.3, cross-compiling for ARM, the line
>> >> >> >> >> 
>> >> >> >> >> 	*read_buf_addr(ldata, ldata->read_head++) = c;
>> >> >> >> >> 
>> >> >> >> >> was re-arranged by the compiler to something like
>> >> >> >> >> 
>> >> >> >> >> 	x = ldata->read_head
>> >> >> >> >> 	ldata->read_head++
>> >> >> >> >> 	*read_buf_addr(ldata, x) = c;
>> >> >> >> >> 
>> >> >> >> >> which causes a race condition. Invalid data is read if data is read
>> >> >> >> >> before it is actually written to the read buffer.
>> >> >> >> >
>> >> >> >> > Really?  A compiler can rearange things like that and expect things to
>> >> >> >> > actually work?  How is that valid?
>> >> >> >> 
>> >> >> >> This is actually required by the C spec.  There is a sequence point
>> >> >> >> before a function call, after the arguments have been evaluated.  Thus
>> >> >> >> all side-effects, such as the post-increment, must be complete before
>> >> >> >> the function is called, just like in the example.
>> >> >> >> 
>> >> >> >> There is no "re-arranging" here.  The code is simply wrong.
>> >> >> >
>> >> >> > Ah, ok, time to dig out the C spec...
>> >> >> >
>> >> >> > Anyway, because of this, no need for the wmb() calls, just rearrange the
>> >> >> > logic and all should be good, right?  Christian, can you test that
>> >> >> > instead?
>> >> >> 
>> >> >> Weakly ordered SMP systems probably need some kind of barrier.  I didn't
>> >> >> look at it carefully.
>> >> >
>> >> > It shouldn't need a barier, as it is a sequence point with the function
>> >> > call.  Well, it's an inline function, but that "shouldn't" matter here,
>> >> > right?
>> >> 
>> >> Sequence points say nothing about the order in which stores become
>> >> visible to other CPUs.  That's why there are barrier instructions.
>> >
>> > Yes, but "order" matters.
>> >
>> > If I write code that does:
>> >
>> > 100	x = ldata->read_head;
>> > 101	&ldata->read_head[x & SOME_VALUE] = y;
>> > 102	ldata->read_head++;
>> >
>> > the compiler can not reorder lines 102 and 101 just because it feels
>> > like it, right?  Or is it time to go spend some reading of the C spec
>> > again...
>> 
>> The compiler can't.  The hardware can.  All the hardware promises is
>> that at some unspecified time in the future, both memory locations will
>> have the correct values.  Another CPU might see 'read_head' updated
>> before it sees the corresponding data value.  A wmb() between the writes
>> forces the CPU to complete preceding stores before it begins subsequent
>> ones.
>
> Yes, sorry, I'm not talking about other CPUs and what they see, I'm
> talking about the local one.  I'm not assuming that this is SMP "safe"
> at all.  If it is supposed to be, then yes, we do have problems, but
> there should be a lock _somewhere_ protecting this.

Within the confines of a single CPU + memory, barriers are never needed.
The moment another CPU or master-capable peripheral enters the mix,
proper ordering must be enforced somehow.

If the buffer is already protected by a lock of some kind, this will
provide the necessary barriers, so nothing further is necessary.  If
it's a lock-less design, there will need to be barriers somewhere.

>From the patch context, I can't tell which category this case falls
into, and I'm far too lazy to read the entire file.

-- 
Måns Rullgård
mans@mansr.com

  reply	other threads:[~2014-11-06 22:54 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-06 11:39 Christian Riesch
2014-11-06 20:38 ` Greg Kroah-Hartman
2014-11-06 20:49   ` Måns Rullgård
2014-11-06 20:56     ` Greg Kroah-Hartman
2014-11-06 21:01       ` Måns Rullgård
2014-11-06 21:17         ` Greg Kroah-Hartman
2014-11-06 21:38           ` Måns Rullgård
2014-11-06 22:02             ` Greg Kroah-Hartman
2014-11-06 22:12               ` Måns Rullgård
2014-11-06 22:31                 ` Greg Kroah-Hartman
2014-11-06 22:54                   ` Måns Rullgård [this message]
2014-11-07  6:50                     ` Christian Riesch
2014-11-07 13:45                   ` Peter Hurley
2014-12-30 19:02                     ` Denis Du
2014-12-30 19:18                       ` Peter Hurley
2014-11-06 21:40       ` Christian Riesch
2014-11-10  7:51       ` Christian Riesch
2014-11-10  9:25         ` Måns Rullgård
2014-11-10  9:38           ` 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=yw1xfvdwj5yi.fsf@unicorn.mansr.com \
    --to=mans@mansr.com \
    --cc=christian.riesch@omicron.at \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.cz \
    --cc=linux-kernel@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®