Skip to content

Commit dd8edd7

Browse files
committed
Merge tag 'tty-4.1-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
Pull tty/serial fixes from Greg KH: "Here's some TTY and serial driver fixes for reported issues. All of these have been in linux-next successfully" * tag 'tty-4.1-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty: pty: Fix input race when closing tty/n_gsm.c: fix a memory leak when gsmtty is removed Revert "serial/amba-pl011: Leave the TX IRQ alone when the UART is not open" serial: omap: Fix error handling in probe earlycon: Revert log warnings
2 parents 3f4741b + 1a48632 commit dd8edd7

File tree

10 files changed

+64
-34
lines changed

10 files changed

+64
-34
lines changed

Documentation/serial/tty.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ TTY_IO_ERROR If set, causes all subsequent userspace read/write
198198

199199
TTY_OTHER_CLOSED Device is a pty and the other side has closed.
200200

201+
TTY_OTHER_DONE Device is a pty and the other side has closed and
202+
all pending input processing has been completed.
203+
201204
TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
202205
smaller chunks.
203206

drivers/tty/n_gsm.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,15 +3170,14 @@ static int gsmtty_break_ctl(struct tty_struct *tty, int state)
31703170
return gsmtty_modem_update(dlci, encode);
31713171
}
31723172

3173-
static void gsmtty_remove(struct tty_driver *driver, struct tty_struct *tty)
3173+
static void gsmtty_cleanup(struct tty_struct *tty)
31743174
{
31753175
struct gsm_dlci *dlci = tty->driver_data;
31763176
struct gsm_mux *gsm = dlci->gsm;
31773177

31783178
dlci_put(dlci);
31793179
dlci_put(gsm->dlci[0]);
31803180
mux_put(gsm);
3181-
driver->ttys[tty->index] = NULL;
31823181
}
31833182

31843183
/* Virtual ttys for the demux */
@@ -3199,7 +3198,7 @@ static const struct tty_operations gsmtty_ops = {
31993198
.tiocmget = gsmtty_tiocmget,
32003199
.tiocmset = gsmtty_tiocmset,
32013200
.break_ctl = gsmtty_break_ctl,
3202-
.remove = gsmtty_remove,
3201+
.cleanup = gsmtty_cleanup,
32033202
};
32043203

32053204

drivers/tty/n_hdlc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
600600
add_wait_queue(&tty->read_wait, &wait);
601601

602602
for (;;) {
603-
if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
603+
if (test_bit(TTY_OTHER_DONE, &tty->flags)) {
604604
ret = -EIO;
605605
break;
606606
}
@@ -828,7 +828,7 @@ static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
828828
/* set bits for operations that won't block */
829829
if (n_hdlc->rx_buf_list.head)
830830
mask |= POLLIN | POLLRDNORM; /* readable */
831-
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
831+
if (test_bit(TTY_OTHER_DONE, &tty->flags))
832832
mask |= POLLHUP;
833833
if (tty_hung_up_p(filp))
834834
mask |= POLLHUP;

drivers/tty/n_tty.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,18 @@ static inline int input_available_p(struct tty_struct *tty, int poll)
19491949
return ldata->commit_head - ldata->read_tail >= amt;
19501950
}
19511951

1952+
static inline int check_other_done(struct tty_struct *tty)
1953+
{
1954+
int done = test_bit(TTY_OTHER_DONE, &tty->flags);
1955+
if (done) {
1956+
/* paired with cmpxchg() in check_other_closed(); ensures
1957+
* read buffer head index is not stale
1958+
*/
1959+
smp_mb__after_atomic();
1960+
}
1961+
return done;
1962+
}
1963+
19521964
/**
19531965
* copy_from_read_buf - copy read data directly
19541966
* @tty: terminal device
@@ -2167,7 +2179,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
21672179
struct n_tty_data *ldata = tty->disc_data;
21682180
unsigned char __user *b = buf;
21692181
DEFINE_WAIT_FUNC(wait, woken_wake_function);
2170-
int c;
2182+
int c, done;
21712183
int minimum, time;
21722184
ssize_t retval = 0;
21732185
long timeout;
@@ -2235,8 +2247,10 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
22352247
((minimum - (b - buf)) >= 1))
22362248
ldata->minimum_to_wake = (minimum - (b - buf));
22372249

2250+
done = check_other_done(tty);
2251+
22382252
if (!input_available_p(tty, 0)) {
2239-
if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2253+
if (done) {
22402254
retval = -EIO;
22412255
break;
22422256
}
@@ -2443,12 +2457,12 @@ static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
24432457

24442458
poll_wait(file, &tty->read_wait, wait);
24452459
poll_wait(file, &tty->write_wait, wait);
2460+
if (check_other_done(tty))
2461+
mask |= POLLHUP;
24462462
if (input_available_p(tty, 1))
24472463
mask |= POLLIN | POLLRDNORM;
24482464
if (tty->packet && tty->link->ctrl_status)
24492465
mask |= POLLPRI | POLLIN | POLLRDNORM;
2450-
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2451-
mask |= POLLHUP;
24522466
if (tty_hung_up_p(file))
24532467
mask |= POLLHUP;
24542468
if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {

drivers/tty/pty.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
5353
/* Review - krefs on tty_link ?? */
5454
if (!tty->link)
5555
return;
56-
tty_flush_to_ldisc(tty->link);
5756
set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
58-
wake_up_interruptible(&tty->link->read_wait);
57+
tty_flip_buffer_push(tty->link->port);
5958
wake_up_interruptible(&tty->link->write_wait);
6059
if (tty->driver->subtype == PTY_TYPE_MASTER) {
6160
set_bit(TTY_OTHER_CLOSED, &tty->flags);
@@ -243,7 +242,9 @@ static int pty_open(struct tty_struct *tty, struct file *filp)
243242
goto out;
244243

245244
clear_bit(TTY_IO_ERROR, &tty->flags);
245+
/* TTY_OTHER_CLOSED must be cleared before TTY_OTHER_DONE */
246246
clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
247+
clear_bit(TTY_OTHER_DONE, &tty->link->flags);
247248
set_bit(TTY_THROTTLED, &tty->flags);
248249
return 0;
249250

drivers/tty/serial/amba-pl011.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,9 @@ static int pl011_startup(struct uart_port *port)
16391639

16401640
writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
16411641

1642+
/* Assume that TX IRQ doesn't work until we see one: */
1643+
uap->tx_irq_seen = 0;
1644+
16421645
spin_lock_irq(&uap->port.lock);
16431646

16441647
/* restore RTS and DTR */
@@ -1702,7 +1705,7 @@ static void pl011_shutdown(struct uart_port *port)
17021705
spin_lock_irq(&uap->port.lock);
17031706
uap->im = 0;
17041707
writew(uap->im, uap->port.membase + UART011_IMSC);
1705-
writew(0xffff & ~UART011_TXIS, uap->port.membase + UART011_ICR);
1708+
writew(0xffff, uap->port.membase + UART011_ICR);
17061709
spin_unlock_irq(&uap->port.lock);
17071710

17081711
pl011_dma_shutdown(uap);

drivers/tty/serial/earlycon.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,8 @@ static int __init param_setup_earlycon(char *buf)
187187
return 0;
188188

189189
err = setup_earlycon(buf);
190-
if (err == -ENOENT) {
191-
pr_warn("no match for %s\n", buf);
192-
err = 0;
193-
} else if (err == -EALREADY) {
194-
pr_warn("already registered\n");
195-
err = 0;
196-
}
190+
if (err == -ENOENT || err == -EALREADY)
191+
return 0;
197192
return err;
198193
}
199194
early_param("earlycon", param_setup_earlycon);

drivers/tty/serial/omap-serial.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,8 @@ static int serial_omap_probe(struct platform_device *pdev)
17351735
err_add_port:
17361736
pm_runtime_put(&pdev->dev);
17371737
pm_runtime_disable(&pdev->dev);
1738+
pm_qos_remove_request(&up->pm_qos_request);
1739+
device_init_wakeup(up->dev, false);
17381740
err_rs485:
17391741
err_port_line:
17401742
return ret;

drivers/tty/tty_buffer.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@
3737

3838
#define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
3939

40+
/*
41+
* If all tty flip buffers have been processed by flush_to_ldisc() or
42+
* dropped by tty_buffer_flush(), check if the linked pty has been closed.
43+
* If so, wake the reader/poll to process
44+
*/
45+
static inline void check_other_closed(struct tty_struct *tty)
46+
{
47+
unsigned long flags, old;
48+
49+
/* transition from TTY_OTHER_CLOSED => TTY_OTHER_DONE must be atomic */
50+
for (flags = ACCESS_ONCE(tty->flags);
51+
test_bit(TTY_OTHER_CLOSED, &flags);
52+
) {
53+
old = flags;
54+
__set_bit(TTY_OTHER_DONE, &flags);
55+
flags = cmpxchg(&tty->flags, old, flags);
56+
if (old == flags) {
57+
wake_up_interruptible(&tty->read_wait);
58+
break;
59+
}
60+
}
61+
}
4062

4163
/**
4264
* tty_buffer_lock_exclusive - gain exclusive access to buffer
@@ -229,6 +251,8 @@ void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
229251
if (ld && ld->ops->flush_buffer)
230252
ld->ops->flush_buffer(tty);
231253

254+
check_other_closed(tty);
255+
232256
atomic_dec(&buf->priority);
233257
mutex_unlock(&buf->lock);
234258
}
@@ -471,8 +495,10 @@ static void flush_to_ldisc(struct work_struct *work)
471495
smp_rmb();
472496
count = head->commit - head->read;
473497
if (!count) {
474-
if (next == NULL)
498+
if (next == NULL) {
499+
check_other_closed(tty);
475500
break;
501+
}
476502
buf->head = next;
477503
tty_buffer_free(port, head);
478504
continue;
@@ -488,19 +514,6 @@ static void flush_to_ldisc(struct work_struct *work)
488514
tty_ldisc_deref(disc);
489515
}
490516

491-
/**
492-
* tty_flush_to_ldisc
493-
* @tty: tty to push
494-
*
495-
* Push the terminal flip buffers to the line discipline.
496-
*
497-
* Must not be called from IRQ context.
498-
*/
499-
void tty_flush_to_ldisc(struct tty_struct *tty)
500-
{
501-
flush_work(&tty->port->buf.work);
502-
}
503-
504517
/**
505518
* tty_flip_buffer_push - terminal
506519
* @port: tty port to push

include/linux/tty.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ struct tty_file_private {
339339
#define TTY_EXCLUSIVE 3 /* Exclusive open mode */
340340
#define TTY_DEBUG 4 /* Debugging */
341341
#define TTY_DO_WRITE_WAKEUP 5 /* Call write_wakeup after queuing new */
342+
#define TTY_OTHER_DONE 6 /* Closed pty has completed input processing */
342343
#define TTY_LDISC_OPEN 11 /* Line discipline is open */
343344
#define TTY_PTY_LOCK 16 /* pty private */
344345
#define TTY_NO_WRITE_SPLIT 17 /* Preserve write boundaries to driver */
@@ -462,7 +463,6 @@ extern int tty_hung_up_p(struct file *filp);
462463
extern void do_SAK(struct tty_struct *tty);
463464
extern void __do_SAK(struct tty_struct *tty);
464465
extern void no_tty(void);
465-
extern void tty_flush_to_ldisc(struct tty_struct *tty);
466466
extern void tty_buffer_free_all(struct tty_port *port);
467467
extern void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld);
468468
extern void tty_buffer_init(struct tty_port *port);

0 commit comments

Comments
 (0)