<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux-stable.git/drivers/tty/vt/selection.c, branch linux-4.9.y</title>
<subtitle>Linux kernel stable tree</subtitle>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/'/>
<entry>
<title>vt: selection, introduce vc_is_sel</title>
<updated>2020-04-02T15:20:39+00:00</updated>
<author>
<name>Jiri Slaby</name>
<email>jslaby@suse.cz</email>
</author>
<published>2020-02-19T07:39:43+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=fc3d6dd88e21c84550aa807d4cb1182bd161f383'/>
<id>fc3d6dd88e21c84550aa807d4cb1182bd161f383</id>
<content type='text'>
commit dce05aa6eec977f1472abed95ccd71276b9a3864 upstream.

Avoid global variables (namely sel_cons) by introducing vc_is_sel. It
checks whether the parameter is the current selection console. This will
help putting sel_cons to a struct later.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Link: https://lore.kernel.org/r/20200219073951.16151-1-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit dce05aa6eec977f1472abed95ccd71276b9a3864 upstream.

Avoid global variables (namely sel_cons) by introducing vc_is_sel. It
checks whether the parameter is the current selection console. This will
help putting sel_cons to a struct later.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Link: https://lore.kernel.org/r/20200219073951.16151-1-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</pre>
</div>
</content>
</entry>
<entry>
<title>vt: selection, push sel_lock up</title>
<updated>2020-03-11T06:53:12+00:00</updated>
<author>
<name>Jiri Slaby</name>
<email>jslaby@suse.cz</email>
</author>
<published>2020-02-28T11:54:06+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=e5be0e24ffc7f5783a3864b5b958088ed15be9e8'/>
<id>e5be0e24ffc7f5783a3864b5b958088ed15be9e8</id>
<content type='text'>
commit e8c75a30a23c6ba63f4ef6895cbf41fd42f21aa2 upstream.

sel_lock cannot nest in the console lock. Thanks to syzkaller, the
kernel states firmly:

&gt; WARNING: possible circular locking dependency detected
&gt; 5.6.0-rc3-syzkaller #0 Not tainted
&gt; ------------------------------------------------------
&gt; syz-executor.4/20336 is trying to acquire lock:
&gt; ffff8880a2e952a0 (&amp;tty-&gt;termios_rwsem){++++}, at: tty_unthrottle+0x22/0x100 drivers/tty/tty_ioctl.c:136
&gt;
&gt; but task is already holding lock:
&gt; ffffffff89462e70 (sel_lock){+.+.}, at: paste_selection+0x118/0x470 drivers/tty/vt/selection.c:374
&gt;
&gt; which lock already depends on the new lock.
&gt;
&gt; the existing dependency chain (in reverse order) is:
&gt;
&gt; -&gt; #2 (sel_lock){+.+.}:
&gt;        mutex_lock_nested+0x1b/0x30 kernel/locking/mutex.c:1118
&gt;        set_selection_kernel+0x3b8/0x18a0 drivers/tty/vt/selection.c:217
&gt;        set_selection_user+0x63/0x80 drivers/tty/vt/selection.c:181
&gt;        tioclinux+0x103/0x530 drivers/tty/vt/vt.c:3050
&gt;        vt_ioctl+0x3f1/0x3a30 drivers/tty/vt/vt_ioctl.c:364

This is ioctl(TIOCL_SETSEL).
Locks held on the path: console_lock -&gt; sel_lock

&gt; -&gt; #1 (console_lock){+.+.}:
&gt;        console_lock+0x46/0x70 kernel/printk/printk.c:2289
&gt;        con_flush_chars+0x50/0x650 drivers/tty/vt/vt.c:3223
&gt;        n_tty_write+0xeae/0x1200 drivers/tty/n_tty.c:2350
&gt;        do_tty_write drivers/tty/tty_io.c:962 [inline]
&gt;        tty_write+0x5a1/0x950 drivers/tty/tty_io.c:1046

This is write().
Locks held on the path: termios_rwsem -&gt; console_lock

&gt; -&gt; #0 (&amp;tty-&gt;termios_rwsem){++++}:
&gt;        down_write+0x57/0x140 kernel/locking/rwsem.c:1534
&gt;        tty_unthrottle+0x22/0x100 drivers/tty/tty_ioctl.c:136
&gt;        mkiss_receive_buf+0x12aa/0x1340 drivers/net/hamradio/mkiss.c:902
&gt;        tty_ldisc_receive_buf+0x12f/0x170 drivers/tty/tty_buffer.c:465
&gt;        paste_selection+0x346/0x470 drivers/tty/vt/selection.c:389
&gt;        tioclinux+0x121/0x530 drivers/tty/vt/vt.c:3055
&gt;        vt_ioctl+0x3f1/0x3a30 drivers/tty/vt/vt_ioctl.c:364

This is ioctl(TIOCL_PASTESEL).
Locks held on the path: sel_lock -&gt; termios_rwsem

&gt; other info that might help us debug this:
&gt;
&gt; Chain exists of:
&gt;   &amp;tty-&gt;termios_rwsem --&gt; console_lock --&gt; sel_lock

Clearly. From the above, we have:
 console_lock -&gt; sel_lock
 sel_lock -&gt; termios_rwsem
 termios_rwsem -&gt; console_lock

Fix this by reversing the console_lock -&gt; sel_lock dependency in
ioctl(TIOCL_SETSEL). First, lock sel_lock, then console_lock.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Reported-by: syzbot+26183d9746e62da329b8@syzkaller.appspotmail.com
Fixes: 07e6124a1a46 ("vt: selection, close sel_buffer race")
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200228115406.5735-2-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit e8c75a30a23c6ba63f4ef6895cbf41fd42f21aa2 upstream.

sel_lock cannot nest in the console lock. Thanks to syzkaller, the
kernel states firmly:

&gt; WARNING: possible circular locking dependency detected
&gt; 5.6.0-rc3-syzkaller #0 Not tainted
&gt; ------------------------------------------------------
&gt; syz-executor.4/20336 is trying to acquire lock:
&gt; ffff8880a2e952a0 (&amp;tty-&gt;termios_rwsem){++++}, at: tty_unthrottle+0x22/0x100 drivers/tty/tty_ioctl.c:136
&gt;
&gt; but task is already holding lock:
&gt; ffffffff89462e70 (sel_lock){+.+.}, at: paste_selection+0x118/0x470 drivers/tty/vt/selection.c:374
&gt;
&gt; which lock already depends on the new lock.
&gt;
&gt; the existing dependency chain (in reverse order) is:
&gt;
&gt; -&gt; #2 (sel_lock){+.+.}:
&gt;        mutex_lock_nested+0x1b/0x30 kernel/locking/mutex.c:1118
&gt;        set_selection_kernel+0x3b8/0x18a0 drivers/tty/vt/selection.c:217
&gt;        set_selection_user+0x63/0x80 drivers/tty/vt/selection.c:181
&gt;        tioclinux+0x103/0x530 drivers/tty/vt/vt.c:3050
&gt;        vt_ioctl+0x3f1/0x3a30 drivers/tty/vt/vt_ioctl.c:364

This is ioctl(TIOCL_SETSEL).
Locks held on the path: console_lock -&gt; sel_lock

&gt; -&gt; #1 (console_lock){+.+.}:
&gt;        console_lock+0x46/0x70 kernel/printk/printk.c:2289
&gt;        con_flush_chars+0x50/0x650 drivers/tty/vt/vt.c:3223
&gt;        n_tty_write+0xeae/0x1200 drivers/tty/n_tty.c:2350
&gt;        do_tty_write drivers/tty/tty_io.c:962 [inline]
&gt;        tty_write+0x5a1/0x950 drivers/tty/tty_io.c:1046

This is write().
Locks held on the path: termios_rwsem -&gt; console_lock

&gt; -&gt; #0 (&amp;tty-&gt;termios_rwsem){++++}:
&gt;        down_write+0x57/0x140 kernel/locking/rwsem.c:1534
&gt;        tty_unthrottle+0x22/0x100 drivers/tty/tty_ioctl.c:136
&gt;        mkiss_receive_buf+0x12aa/0x1340 drivers/net/hamradio/mkiss.c:902
&gt;        tty_ldisc_receive_buf+0x12f/0x170 drivers/tty/tty_buffer.c:465
&gt;        paste_selection+0x346/0x470 drivers/tty/vt/selection.c:389
&gt;        tioclinux+0x121/0x530 drivers/tty/vt/vt.c:3055
&gt;        vt_ioctl+0x3f1/0x3a30 drivers/tty/vt/vt_ioctl.c:364

This is ioctl(TIOCL_PASTESEL).
Locks held on the path: sel_lock -&gt; termios_rwsem

&gt; other info that might help us debug this:
&gt;
&gt; Chain exists of:
&gt;   &amp;tty-&gt;termios_rwsem --&gt; console_lock --&gt; sel_lock

Clearly. From the above, we have:
 console_lock -&gt; sel_lock
 sel_lock -&gt; termios_rwsem
 termios_rwsem -&gt; console_lock

Fix this by reversing the console_lock -&gt; sel_lock dependency in
ioctl(TIOCL_SETSEL). First, lock sel_lock, then console_lock.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Reported-by: syzbot+26183d9746e62da329b8@syzkaller.appspotmail.com
Fixes: 07e6124a1a46 ("vt: selection, close sel_buffer race")
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200228115406.5735-2-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</pre>
</div>
</content>
</entry>
<entry>
<title>vt: selection, push console lock down</title>
<updated>2020-03-11T06:53:12+00:00</updated>
<author>
<name>Jiri Slaby</name>
<email>jslaby@suse.cz</email>
</author>
<published>2020-02-28T11:54:05+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=ccd35863147dd447110b726a0d4911ab686aade9'/>
<id>ccd35863147dd447110b726a0d4911ab686aade9</id>
<content type='text'>
commit 4b70dd57a15d2f4685ac6e38056bad93e81e982f upstream.

We need to nest the console lock in sel_lock, so we have to push it down
a bit. Fortunately, the callers of set_selection_* just lock the console
lock around the function call. So moving it down is easy.

In the next patch, we switch the order.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Fixes: 07e6124a1a46 ("vt: selection, close sel_buffer race")
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200228115406.5735-1-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 4b70dd57a15d2f4685ac6e38056bad93e81e982f upstream.

We need to nest the console lock in sel_lock, so we have to push it down
a bit. Fortunately, the callers of set_selection_* just lock the console
lock around the function call. So moving it down is easy.

In the next patch, we switch the order.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Fixes: 07e6124a1a46 ("vt: selection, close sel_buffer race")
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200228115406.5735-1-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</pre>
</div>
</content>
</entry>
<entry>
<title>vt: selection, close sel_buffer race</title>
<updated>2020-03-11T06:53:11+00:00</updated>
<author>
<name>Jiri Slaby</name>
<email>jslaby@suse.cz</email>
</author>
<published>2020-02-10T08:11:31+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=290a9381ccc16131c6ccc19940589141985db6b1'/>
<id>290a9381ccc16131c6ccc19940589141985db6b1</id>
<content type='text'>
commit 07e6124a1a46b4b5a9b3cacc0c306b50da87abf5 upstream.

syzkaller reported this UAF:
BUG: KASAN: use-after-free in n_tty_receive_buf_common+0x2481/0x2940 drivers/tty/n_tty.c:1741
Read of size 1 at addr ffff8880089e40e9 by task syz-executor.1/13184

CPU: 0 PID: 13184 Comm: syz-executor.1 Not tainted 5.4.7 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
Call Trace:
...
 kasan_report+0xe/0x20 mm/kasan/common.c:634
 n_tty_receive_buf_common+0x2481/0x2940 drivers/tty/n_tty.c:1741
 tty_ldisc_receive_buf+0xac/0x190 drivers/tty/tty_buffer.c:461
 paste_selection+0x297/0x400 drivers/tty/vt/selection.c:372
 tioclinux+0x20d/0x4e0 drivers/tty/vt/vt.c:3044
 vt_ioctl+0x1bcf/0x28d0 drivers/tty/vt/vt_ioctl.c:364
 tty_ioctl+0x525/0x15a0 drivers/tty/tty_io.c:2657
 vfs_ioctl fs/ioctl.c:47 [inline]

It is due to a race between parallel paste_selection (TIOCL_PASTESEL)
and set_selection_user (TIOCL_SETSEL) invocations. One uses sel_buffer,
while the other frees it and reallocates a new one for another
selection. Add a mutex to close this race.

The mutex takes care properly of sel_buffer and sel_buffer_lth only. The
other selection global variables (like sel_start, sel_end, and sel_cons)
are protected only in set_selection_user. The other functions need quite
some more work to close the races of the variables there. This is going
to happen later.

This likely fixes (I am unsure as there is no reproducer provided) bug
206361 too. It was marked as CVE-2020-8648.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Reported-by: syzbot+59997e8d5cbdc486e6f6@syzkaller.appspotmail.com
References: https://bugzilla.kernel.org/show_bug.cgi?id=206361
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200210081131.23572-2-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 07e6124a1a46b4b5a9b3cacc0c306b50da87abf5 upstream.

syzkaller reported this UAF:
BUG: KASAN: use-after-free in n_tty_receive_buf_common+0x2481/0x2940 drivers/tty/n_tty.c:1741
Read of size 1 at addr ffff8880089e40e9 by task syz-executor.1/13184

CPU: 0 PID: 13184 Comm: syz-executor.1 Not tainted 5.4.7 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
Call Trace:
...
 kasan_report+0xe/0x20 mm/kasan/common.c:634
 n_tty_receive_buf_common+0x2481/0x2940 drivers/tty/n_tty.c:1741
 tty_ldisc_receive_buf+0xac/0x190 drivers/tty/tty_buffer.c:461
 paste_selection+0x297/0x400 drivers/tty/vt/selection.c:372
 tioclinux+0x20d/0x4e0 drivers/tty/vt/vt.c:3044
 vt_ioctl+0x1bcf/0x28d0 drivers/tty/vt/vt_ioctl.c:364
 tty_ioctl+0x525/0x15a0 drivers/tty/tty_io.c:2657
 vfs_ioctl fs/ioctl.c:47 [inline]

It is due to a race between parallel paste_selection (TIOCL_PASTESEL)
and set_selection_user (TIOCL_SETSEL) invocations. One uses sel_buffer,
while the other frees it and reallocates a new one for another
selection. Add a mutex to close this race.

The mutex takes care properly of sel_buffer and sel_buffer_lth only. The
other selection global variables (like sel_start, sel_end, and sel_cons)
are protected only in set_selection_user. The other functions need quite
some more work to close the races of the variables there. This is going
to happen later.

This likely fixes (I am unsure as there is no reproducer provided) bug
206361 too. It was marked as CVE-2020-8648.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Reported-by: syzbot+59997e8d5cbdc486e6f6@syzkaller.appspotmail.com
References: https://bugzilla.kernel.org/show_bug.cgi?id=206361
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200210081131.23572-2-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</pre>
</div>
</content>
</entry>
<entry>
<title>vt: selection, handle pending signals in paste_selection</title>
<updated>2020-02-28T14:42:45+00:00</updated>
<author>
<name>Jiri Slaby</name>
<email>jslaby@suse.cz</email>
</author>
<published>2020-02-10T08:11:30+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=5827cbb5746289ec91f78028091587d1bc244ae2'/>
<id>5827cbb5746289ec91f78028091587d1bc244ae2</id>
<content type='text'>
commit 687bff0cd08f790d540cfb7b2349f0d876cdddec upstream.

When pasting a selection to a vt, the task is set as INTERRUPTIBLE while
waiting for a tty to unthrottle. But signals are not handled at all.
Normally, this is not a problem as tty_ldisc_receive_buf receives all
the goods and a user has no reason to interrupt the task.

There are two scenarios where this matters:
1) when the tty is throttled and a signal is sent to the process, it
   spins on a CPU until the tty is unthrottled. schedule() does not
   really echedule, but returns immediately, of course.
2) when the sel_buffer becomes invalid, KASAN prevents any reads from it
   and the loop simply does not proceed and spins forever (causing the
   tty to throttle, but the code never sleeps, the same as above). This
   sometimes happens as there is a race in the sel_buffer handling code.

So add signal handling to this ioctl (TIOCL_PASTESEL) and return -EINTR
in case a signal is pending.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200210081131.23572-1-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 687bff0cd08f790d540cfb7b2349f0d876cdddec upstream.

When pasting a selection to a vt, the task is set as INTERRUPTIBLE while
waiting for a tty to unthrottle. But signals are not handled at all.
Normally, this is not a problem as tty_ldisc_receive_buf receives all
the goods and a user has no reason to interrupt the task.

There are two scenarios where this matters:
1) when the tty is throttled and a signal is sent to the process, it
   spins on a CPU until the tty is unthrottled. schedule() does not
   really echedule, but returns immediately, of course.
2) when the sel_buffer becomes invalid, KASAN prevents any reads from it
   and the loop simply does not proceed and spins forever (causing the
   tty to throttle, but the code never sleeps, the same as above). This
   sometimes happens as there is a race in the sel_buffer handling code.

So add signal handling to this ioctl (TIOCL_PASTESEL) and return -EINTR
in case a signal is pending.

Signed-off-by: Jiri Slaby &lt;jslaby@suse.cz&gt;
Cc: stable &lt;stable@vger.kernel.org&gt;
Link: https://lore.kernel.org/r/20200210081131.23572-1-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;

</pre>
</div>
</content>
</entry>
<entry>
<title>tty: Replace TTY_THROTTLED bit tests with tty_throttled()</title>
<updated>2016-04-30T16:26:55+00:00</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2016-04-10T00:11:36+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=97ef38b8210d7459d4cb51668cdf3983772ac6b7'/>
<id>97ef38b8210d7459d4cb51668cdf3983772ac6b7</id>
<content type='text'>
Abstract TTY_THROTTLED bit tests with tty_throttled().

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Abstract TTY_THROTTLED bit tests with tty_throttled().

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>tty: Prepare for destroying line discipline on hangup</title>
<updated>2016-01-27T23:01:44+00:00</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2016-01-11T06:41:01+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=e55afd11a48354c810caf6b6ad4c103016a88230'/>
<id>e55afd11a48354c810caf6b6ad4c103016a88230</id>
<content type='text'>
tty file_operations (read/write/ioctl) wait for the ldisc reference
indefinitely (until ldisc lifetime events, such as hangup or TIOCSETD,
finish). Since hangup now destroys the ldisc and does not instance
another copy, file_operations must now be prepared to receive a NULL
ldisc reference from tty_ldisc_ref_wait():

CPU 0                                   CPU 1
-----                                   -----
(*f_op-&gt;read)() =&gt; tty_read()
                                        __tty_hangup()
                                        ...
                                        f_op = &amp;hung_up_tty_fops;
                                        ...
                                        tty_ldisc_hangup()
                                           tty_ldisc_lock()
                                           tty_ldisc_kill()
                                              tty-&gt;ldisc = NULL
                                           tty_ldisc_unlock()
ld = tty_ldisc_ref_wait()
/* ld == NULL */

Instead, the action taken now is to return the same value as if the
tty had been hungup a moment earlier:

CPU 0                                   CPU 1
-----                                   -----
                                        __tty_hangup()
                                        ...
                                        f_op = &amp;hung_up_tty_fops;
(*f_op-&gt;read)() =&gt; hung_up_tty_read()
return 0;
                                        ...
                                        tty_ldisc_hangup()
                                           tty_ldisc_lock()
                                           tty_ldisc_kill()
                                              tty-&gt;ldisc = NULL
                                           tty_ldisc_unlock()

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
tty file_operations (read/write/ioctl) wait for the ldisc reference
indefinitely (until ldisc lifetime events, such as hangup or TIOCSETD,
finish). Since hangup now destroys the ldisc and does not instance
another copy, file_operations must now be prepared to receive a NULL
ldisc reference from tty_ldisc_ref_wait():

CPU 0                                   CPU 1
-----                                   -----
(*f_op-&gt;read)() =&gt; tty_read()
                                        __tty_hangup()
                                        ...
                                        f_op = &amp;hung_up_tty_fops;
                                        ...
                                        tty_ldisc_hangup()
                                           tty_ldisc_lock()
                                           tty_ldisc_kill()
                                              tty-&gt;ldisc = NULL
                                           tty_ldisc_unlock()
ld = tty_ldisc_ref_wait()
/* ld == NULL */

Instead, the action taken now is to return the same value as if the
tty had been hungup a moment earlier:

CPU 0                                   CPU 1
-----                                   -----
                                        __tty_hangup()
                                        ...
                                        f_op = &amp;hung_up_tty_fops;
(*f_op-&gt;read)() =&gt; hung_up_tty_read()
return 0;
                                        ...
                                        tty_ldisc_hangup()
                                           tty_ldisc_lock()
                                           tty_ldisc_kill()
                                              tty-&gt;ldisc = NULL
                                           tty_ldisc_unlock()

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>tty: vt: Fix !TASK_RUNNING diagnostic warning from paste_selection()</title>
<updated>2015-07-24T01:08:29+00:00</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2015-07-13T00:47:35+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=61e86cc90af49cecef9c54ccea1f572fbcb695ac'/>
<id>61e86cc90af49cecef9c54ccea1f572fbcb695ac</id>
<content type='text'>
Pasting text with gpm on a VC produced warning [1]. Reset task state
to TASK_RUNNING in the paste_selection() loop, if the loop did not
sleep.

[1]
WARNING: CPU: 6 PID: 1960 at /home/peter/src/kernels/mainline/kernel/sched/core.c:7286 __might_sleep+0x7f/0x90()
do not call blocking ops when !TASK_RUNNING; state=1 set at [&lt;ffffffff8151805e&gt;] paste_selection+0x9e/0x1a0
Modules linked in: btrfs xor raid6_pq ufs qnx4 hfsplus hfs minix ntfs msdos jfs xfs libcrc32c .....
CPU: 6 PID: 1960 Comm: gpm Not tainted 4.1.0-rc7+tty-xeon+debug #rc7+tty
Hardware name: Dell Inc. Precision WorkStation T5400  /0RW203, BIOS A11 04/30/2012
 ffffffff81c9c0a0 ffff8802b0fd3ac8 ffffffff8185778a 0000000000000001
 ffff8802b0fd3b18 ffff8802b0fd3b08 ffffffff8108039a ffffffff82ae8510
 ffffffff81c9ce00 0000000000000015 0000000000000000 0000000000000000
Call Trace:
 [&lt;ffffffff8185778a&gt;] dump_stack+0x4f/0x7b
 [&lt;ffffffff8108039a&gt;] warn_slowpath_common+0x8a/0xc0
 [&lt;ffffffff81080416&gt;] warn_slowpath_fmt+0x46/0x50
 [&lt;ffffffff810ddced&gt;] ? __lock_acquire+0xe2d/0x13a0
 [&lt;ffffffff8151805e&gt;] ? paste_selection+0x9e/0x1a0
 [&lt;ffffffff8151805e&gt;] ? paste_selection+0x9e/0x1a0
 [&lt;ffffffff810ad4ff&gt;] __might_sleep+0x7f/0x90
 [&lt;ffffffff8185f76a&gt;] down_read+0x2a/0xa0
 [&lt;ffffffff810bb1d8&gt;] ? sched_clock_cpu+0xb8/0xe0
 [&lt;ffffffff8150d1dc&gt;] n_tty_receive_buf_common+0x4c/0xba0
 [&lt;ffffffff810dc875&gt;] ? mark_held_locks+0x75/0xa0
 [&lt;ffffffff81861c95&gt;] ? _raw_spin_unlock_irqrestore+0x65/0x80
 [&lt;ffffffff810b49a1&gt;] ? get_parent_ip+0x11/0x50
 [&lt;ffffffff8150dd44&gt;] n_tty_receive_buf2+0x14/0x20
 [&lt;ffffffff81518117&gt;] paste_selection+0x157/0x1a0
 [&lt;ffffffff810b77b0&gt;] ? wake_up_state+0x20/0x20
 [&lt;ffffffff815203f8&gt;] tioclinux+0xb8/0x2c0
 [&lt;ffffffff81515bfe&gt;] vt_ioctl+0xaee/0x11a0
 [&lt;ffffffff810baf75&gt;] ? sched_clock_local+0x25/0x90
 [&lt;ffffffff810bbe11&gt;] ? vtime_account_user+0x91/0xa0
 [&lt;ffffffff8150810c&gt;] tty_ioctl+0x20c/0xe20
 [&lt;ffffffff810bbe11&gt;] ? vtime_account_user+0x91/0xa0
 [&lt;ffffffff810b49a1&gt;] ? get_parent_ip+0x11/0x50
 [&lt;ffffffff810b4a69&gt;] ? preempt_count_sub+0x49/0x50
 [&lt;ffffffff811ab71c&gt;] ? context_tracking_exit+0x5c/0x290
 [&lt;ffffffff811ab71c&gt;] ? context_tracking_exit+0x5c/0x290
 [&lt;ffffffff81248b98&gt;] do_vfs_ioctl+0x318/0x570
 [&lt;ffffffff810dca8d&gt;] ? trace_hardirqs_on+0xd/0x10
 [&lt;ffffffff810dc9b5&gt;] ? trace_hardirqs_on_caller+0x115/0x1e0
 [&lt;ffffffff81254acc&gt;] ? __fget_light+0x6c/0xa0
 [&lt;ffffffff81248e71&gt;] SyS_ioctl+0x81/0xa0
 [&lt;ffffffff81862832&gt;] system_call_fastpath+0x16/0x7a

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Pasting text with gpm on a VC produced warning [1]. Reset task state
to TASK_RUNNING in the paste_selection() loop, if the loop did not
sleep.

[1]
WARNING: CPU: 6 PID: 1960 at /home/peter/src/kernels/mainline/kernel/sched/core.c:7286 __might_sleep+0x7f/0x90()
do not call blocking ops when !TASK_RUNNING; state=1 set at [&lt;ffffffff8151805e&gt;] paste_selection+0x9e/0x1a0
Modules linked in: btrfs xor raid6_pq ufs qnx4 hfsplus hfs minix ntfs msdos jfs xfs libcrc32c .....
CPU: 6 PID: 1960 Comm: gpm Not tainted 4.1.0-rc7+tty-xeon+debug #rc7+tty
Hardware name: Dell Inc. Precision WorkStation T5400  /0RW203, BIOS A11 04/30/2012
 ffffffff81c9c0a0 ffff8802b0fd3ac8 ffffffff8185778a 0000000000000001
 ffff8802b0fd3b18 ffff8802b0fd3b08 ffffffff8108039a ffffffff82ae8510
 ffffffff81c9ce00 0000000000000015 0000000000000000 0000000000000000
Call Trace:
 [&lt;ffffffff8185778a&gt;] dump_stack+0x4f/0x7b
 [&lt;ffffffff8108039a&gt;] warn_slowpath_common+0x8a/0xc0
 [&lt;ffffffff81080416&gt;] warn_slowpath_fmt+0x46/0x50
 [&lt;ffffffff810ddced&gt;] ? __lock_acquire+0xe2d/0x13a0
 [&lt;ffffffff8151805e&gt;] ? paste_selection+0x9e/0x1a0
 [&lt;ffffffff8151805e&gt;] ? paste_selection+0x9e/0x1a0
 [&lt;ffffffff810ad4ff&gt;] __might_sleep+0x7f/0x90
 [&lt;ffffffff8185f76a&gt;] down_read+0x2a/0xa0
 [&lt;ffffffff810bb1d8&gt;] ? sched_clock_cpu+0xb8/0xe0
 [&lt;ffffffff8150d1dc&gt;] n_tty_receive_buf_common+0x4c/0xba0
 [&lt;ffffffff810dc875&gt;] ? mark_held_locks+0x75/0xa0
 [&lt;ffffffff81861c95&gt;] ? _raw_spin_unlock_irqrestore+0x65/0x80
 [&lt;ffffffff810b49a1&gt;] ? get_parent_ip+0x11/0x50
 [&lt;ffffffff8150dd44&gt;] n_tty_receive_buf2+0x14/0x20
 [&lt;ffffffff81518117&gt;] paste_selection+0x157/0x1a0
 [&lt;ffffffff810b77b0&gt;] ? wake_up_state+0x20/0x20
 [&lt;ffffffff815203f8&gt;] tioclinux+0xb8/0x2c0
 [&lt;ffffffff81515bfe&gt;] vt_ioctl+0xaee/0x11a0
 [&lt;ffffffff810baf75&gt;] ? sched_clock_local+0x25/0x90
 [&lt;ffffffff810bbe11&gt;] ? vtime_account_user+0x91/0xa0
 [&lt;ffffffff8150810c&gt;] tty_ioctl+0x20c/0xe20
 [&lt;ffffffff810bbe11&gt;] ? vtime_account_user+0x91/0xa0
 [&lt;ffffffff810b49a1&gt;] ? get_parent_ip+0x11/0x50
 [&lt;ffffffff810b4a69&gt;] ? preempt_count_sub+0x49/0x50
 [&lt;ffffffff811ab71c&gt;] ? context_tracking_exit+0x5c/0x290
 [&lt;ffffffff811ab71c&gt;] ? context_tracking_exit+0x5c/0x290
 [&lt;ffffffff81248b98&gt;] do_vfs_ioctl+0x318/0x570
 [&lt;ffffffff810dca8d&gt;] ? trace_hardirqs_on+0xd/0x10
 [&lt;ffffffff810dc9b5&gt;] ? trace_hardirqs_on_caller+0x115/0x1e0
 [&lt;ffffffff81254acc&gt;] ? __fget_light+0x6c/0xa0
 [&lt;ffffffff81248e71&gt;] SyS_ioctl+0x81/0xa0
 [&lt;ffffffff81862832&gt;] system_call_fastpath+0x16/0x7a

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>tty: Fix unsafe vt paste_selection()</title>
<updated>2013-07-23T23:47:10+00:00</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2013-06-15T13:36:15+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=a7c8d58c79853adeebf0a1ddc9c63e433b4d97f1'/>
<id>a7c8d58c79853adeebf0a1ddc9c63e433b4d97f1</id>
<content type='text'>
Convert the tty_buffer_flush() exclusion mechanism to a
public interface - tty_buffer_lock/unlock_exclusive() - and use
the interface to safely write the paste selection to the line
discipline.

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Convert the tty_buffer_flush() exclusion mechanism to a
public interface - tty_buffer_lock/unlock_exclusive() - and use
the interface to safely write the paste selection to the line
discipline.

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>tty: Make ldisc input flow control concurrency-friendly</title>
<updated>2013-07-23T23:42:59+00:00</updated>
<author>
<name>Peter Hurley</name>
<email>peter@hurleysoftware.com</email>
</author>
<published>2013-06-15T13:14:15+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=24a89d1cb69b6c488cf16d98dd02e7820f62b40c'/>
<id>24a89d1cb69b6c488cf16d98dd02e7820f62b40c</id>
<content type='text'>
Although line discipline receiving is single-producer/single-consumer,
using tty-&gt;receive_room to manage flow control creates unnecessary
critical regions requiring additional lock use.

Instead, introduce the optional .receive_buf2() ldisc method which
returns the # of bytes actually received. Serialization is guaranteed
by the caller.

In turn, the line discipline should schedule the buffer work item
whenever space becomes available; ie., when there is room to receive
data and receive_room() previously returned 0 (the buffer work
item stops processing if receive_buf2() returns 0). Note the
'no room' state need not be atomic despite concurrent use by two
threads because only the buffer work thread can set the state and
only the read() thread can clear the state.

Add n_tty_receive_buf2() as the receive_buf2() method for N_TTY.
Provide a public helper function, tty_ldisc_receive_buf(), to use
when directly accessing the receive_buf() methods.

Line disciplines not using input flow control can continue to set
tty-&gt;receive_room to a fixed value and only provide the receive_buf()
method.

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Although line discipline receiving is single-producer/single-consumer,
using tty-&gt;receive_room to manage flow control creates unnecessary
critical regions requiring additional lock use.

Instead, introduce the optional .receive_buf2() ldisc method which
returns the # of bytes actually received. Serialization is guaranteed
by the caller.

In turn, the line discipline should schedule the buffer work item
whenever space becomes available; ie., when there is room to receive
data and receive_room() previously returned 0 (the buffer work
item stops processing if receive_buf2() returns 0). Note the
'no room' state need not be atomic despite concurrent use by two
threads because only the buffer work thread can set the state and
only the read() thread can clear the state.

Add n_tty_receive_buf2() as the receive_buf2() method for N_TTY.
Provide a public helper function, tty_ldisc_receive_buf(), to use
when directly accessing the receive_buf() methods.

Line disciplines not using input flow control can continue to set
tty-&gt;receive_room to a fixed value and only provide the receive_buf()
method.

Signed-off-by: Peter Hurley &lt;peter@hurleysoftware.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
</pre>
</div>
</content>
</entry>
</feed>
