<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux/drivers/tty/tty_buffer.c, branch v4.3</title>
<subtitle>Mirror of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
</subtitle>
<id>https://git.shady.money/linux/atom?h=v4.3</id>
<link rel='self' href='https://git.shady.money/linux/atom?h=v4.3'/>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/'/>
<updated>2015-10-04T18:03:40Z</updated>
<entry>
<title>tty: fix data race on tty_buffer.commit</title>
<updated>2015-10-04T18:03:40Z</updated>
<author>
<name>Dmitry Vyukov</name>
<email>dvyukov@google.com</email>
</author>
<published>2015-09-17T15:17:10Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=facd885c75067c2a88b72622dfb0fa4b9510da5e'/>
<id>urn:sha1:facd885c75067c2a88b72622dfb0fa4b9510da5e</id>
<content type='text'>
Race on buffer data happens when newly committed data is
picked up by an old flush work in the following scenario:
__tty_buffer_request_room does a plain write of tail-&gt;commit,
no barriers were executed before that.
At this point flush_to_ldisc reads this new value of commit,
and reads buffer data, no barriers in between.
The committed buffer data is not necessary visible to flush_to_ldisc.

Similar bug happens when tty_schedule_flip commits data.

Update commit with smp_store_release and read commit with
smp_load_acquire, as it is commit that signals data readiness.
This is orthogonal to the existing synchronization on tty_buffer.next,
which is required to not dismiss a buffer with unconsumed data.

The data race was found with KernelThreadSanitizer (KTSAN).

Signed-off-by: Dmitry Vyukov &lt;dvyukov@google.com&gt;
Reviewed-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>tty: fix data race in tty_buffer_flush</title>
<updated>2015-10-04T18:03:40Z</updated>
<author>
<name>Dmitry Vyukov</name>
<email>dvyukov@google.com</email>
</author>
<published>2015-09-17T15:17:09Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=9e6b7cd7e77d4ca43b57c726d9bfa86d06e0567f'/>
<id>urn:sha1:9e6b7cd7e77d4ca43b57c726d9bfa86d06e0567f</id>
<content type='text'>
tty_buffer_flush frees not acquired buffers.
As the result, for example, read of b-&gt;size in tty_buffer_free
can return garbage value which will lead to a huge buffer
hanging in the freelist. This is just the benignest
manifestation of freeing of a not acquired object.
If the object is passed to kfree, heap can be corrupted.

Acquire visibility over the buffer before freeing it.

The data race was found with KernelThreadSanitizer (KTSAN).

Signed-off-by: Dmitry Vyukov &lt;dvyukov@google.com&gt;
Reviewed-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>tty: fix data race in flush_to_ldisc</title>
<updated>2015-10-04T18:03:40Z</updated>
<author>
<name>Dmitry Vyukov</name>
<email>dvyukov@google.com</email>
</author>
<published>2015-09-17T15:17:08Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=7098296a362a96051fa120abf48f0095818b99cd'/>
<id>urn:sha1:7098296a362a96051fa120abf48f0095818b99cd</id>
<content type='text'>
flush_to_ldisc reads port-&gt;itty and checks that it is not NULL,
concurrently release_tty sets port-&gt;itty to NULL. It is possible
that flush_to_ldisc loads port-&gt;itty once, ensures that it is
not NULL, but then reloads it again and uses. The second load
can already return NULL, which will cause a crash.

Use READ_ONCE to read port-&gt;itty.

The data race was found with KernelThreadSanitizer (KTSAN).

Signed-off-by: Dmitry Vyukov &lt;dvyukov@google.com&gt;
Reviewed-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>tty: buffers: Move hidden buffer index advance into outer loop</title>
<updated>2015-07-24T01:23:56Z</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2015-07-13T00:50:50Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=af5554f957f8a7e28b9826a9c4ad2a7dedd15a78'/>
<id>urn:sha1:af5554f957f8a7e28b9826a9c4ad2a7dedd15a78</id>
<content type='text'>
The advance of the 'read' buffer index belongs in the outer
flip buffer consume loop, with the other buffer index arithmetic.

No functional change.

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>tty: Replace smp_rmb/smp_wmb with smp_load_acquire/smp_store_release</title>
<updated>2015-07-24T01:23:56Z</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2015-07-13T00:50:49Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=069f38b4983efaea92cbe7cc0cacc057af55739a'/>
<id>urn:sha1:069f38b4983efaea92cbe7cc0cacc057af55739a</id>
<content type='text'>
Clarify flip buffer producer/consumer operation; the use of
smp_load_acquire() and smp_store_release() more clearly indicates
which memory access requires a barrier.

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>Merge 4.1-rc4 into tty-next</title>
<updated>2015-05-18T21:08:58Z</updated>
<author>
<name>Greg Kroah-Hartman</name>
<email>gregkh@linuxfoundation.org</email>
</author>
<published>2015-05-18T21:08:58Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=02730d3c053a9af1d402e1c8dc8bbbc5a1340406'/>
<id>urn:sha1:02730d3c053a9af1d402e1c8dc8bbbc5a1340406</id>
<content type='text'>
This resolves some tty driver merge issues.

Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>pty: Fix input race when closing</title>
<updated>2015-05-10T17:26:37Z</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2015-04-13T17:24:34Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=1a48632ffed61352a7810ce089dc5a8bcd505a60'/>
<id>urn:sha1:1a48632ffed61352a7810ce089dc5a8bcd505a60</id>
<content type='text'>
A read() from a pty master may mistakenly indicate EOF (errno == -EIO)
after the pty slave has closed, even though input data remains to be read.
For example,

       pty slave       |        input worker        |    pty master
                       |                            |
                       |                            |   n_tty_read()
pty_write()            |                            |     input avail? no
  add data             |                            |     sleep
  schedule worker  ---&gt;|                            |     .
                       |---&gt; flush_to_ldisc()       |     .
pty_close()            |       fill read buffer     |     .
  wait for worker      |       wakeup reader    ---&gt;|     .
                       |       read buffer full?    |---&gt; input avail ? yes
                       |&lt;---   yes - exit worker    |     copy 4096 bytes to user
  TTY_OTHER_CLOSED &lt;---|                            |&lt;--- kick worker
                       |                            |

		                **** New read() before worker starts ****

                       |                            |   n_tty_read()
                       |                            |     input avail? no
                       |                            |     TTY_OTHER_CLOSED? yes
                       |                            |     return -EIO

Several conditions are required to trigger this race:
1. the ldisc read buffer must become full so the input worker exits
2. the read() count parameter must be &gt;= 4096 so the ldisc read buffer
   is empty
3. the subsequent read() occurs before the kicked worker has processed
   more input

However, the underlying cause of the race is that data is pipelined, while
tty state is not; ie., data already written by the pty slave end is not
yet visible to the pty master end, but state changes by the pty slave end
are visible to the pty master end immediately.

Pipeline the TTY_OTHER_CLOSED state through input worker to the reader.
1. Introduce TTY_OTHER_DONE which is set by the input worker when
   TTY_OTHER_CLOSED is set and either the input buffers are flushed or
   input processing has completed. Readers/polls are woken when
   TTY_OTHER_DONE is set.
2. Reader/poll checks TTY_OTHER_DONE instead of TTY_OTHER_CLOSED.
3. A new input worker is started from pty_close() after setting
   TTY_OTHER_CLOSED, which ensures the TTY_OTHER_DONE state will be
   set if the last input worker is already finished (or just about to
   exit).

Remove tty_flush_to_ldisc(); no in-tree callers.

Fixes: 52bce7f8d4fc ("pty, n_tty: Simplify input processing on final close")
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=96311
BugLink: http://bugs.launchpad.net/bugs/1429756
Cc: &lt;stable@vger.kernel.org&gt; # 3.19+
Reported-by: Andy Whitcroft &lt;apw@canonical.com&gt;
Reported-by: H.J. Lu &lt;hjl.tools@gmail.com&gt;
Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>tty: tty_buffer.c: move assignment out of if () block</title>
<updated>2015-05-10T17:04:18Z</updated>
<author>
<name>Greg Kroah-Hartman</name>
<email>gregkh@linuxfoundation.org</email>
</author>
<published>2015-04-30T09:22:17Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=e16cb0a72fc291847adf10ec26b7879c69f12327'/>
<id>urn:sha1:e16cb0a72fc291847adf10ec26b7879c69f12327</id>
<content type='text'>
We should not be doing assignments within an if () block
so fix up the code to not do this.

change was created using Coccinelle.

CC: Jiri Slaby &lt;jslaby@suse.cz&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>pty: Fix buffer flush deadlock</title>
<updated>2015-02-02T18:11:27Z</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2015-01-17T20:42:05Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=1d1d14da12e79a6c05fbe1a975401f0f56c93316'/>
<id>urn:sha1:1d1d14da12e79a6c05fbe1a975401f0f56c93316</id>
<content type='text'>
The pty driver does not clear its write buffer when commanded.
This is to avoid an apparent deadlock between parallel flushes from
both pty ends; specifically when handling either BRK or INTR input.
However, parallel flushes from this source is not possible since
the pty master can never be set to BRKINT or ISIG. Parallel flushes
from other sources are possible but these do not threaten deadlocks.

Annotate the tty buffer mutex for lockdep to represent the nested
tty_buffer locking which occurs when the pty slave is processing input
(its buffer mutex held) and receives INTR or BRK and acquires the
linked tty buffer mutex via tty_buffer_flush().

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>tty: Flush ldisc buffer atomically with tty flip buffers</title>
<updated>2014-11-06T02:50:43Z</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2014-11-05T17:13:09Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=86c80a8e2ab443e9c4261b3499de4ce808399104'/>
<id>urn:sha1:86c80a8e2ab443e9c4261b3499de4ce808399104</id>
<content type='text'>
tty_ldisc_flush() first clears the line discipline input buffer,
then clears the tty flip buffers. However, this allows for existing
data in the tty flip buffers to be added after the ldisc input
buffer has been cleared, but before the flip buffers have been cleared.

Add an optional ldisc parameter to tty_buffer_flush() to allow
tty_ldisc_flush() to pass the ldisc to clear.

NB: Initially, the plan was to do this automatically in
tty_buffer_flush(). However, an audit of the behavior of existing
line disciplines showed that performing a ldisc buffer flush on
ioctl(TCFLSH) was not always the outcome. For example, some line
disciplines have flush_buffer() methods but not ioctl() methods,
so a -&gt;flush_buffer() command would be unexpected.

Reviewed-by: Alan Cox &lt;alan@linux.intel.com&gt;
Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
</entry>
</feed>
