Skip to content

Commit e7f3880

Browse files
peterhurleygregkh
authored andcommitted
tty: Fix recursive deadlock in tty_perform_flush()
tty_perform_flush() can deadlock when called while holding a line discipline reference. By definition, all ldisc drivers hold a ldisc reference, so calls originating from ldisc drivers must not block for a ldisc reference. The deadlock can occur when: CPU 0 | CPU 1 | tty_ldisc_ref(tty) | .... | <line discipline halted> tty_ldisc_ref_wait(tty) | | CPU 0 cannot progess because it cannot obtain an ldisc reference with the line discipline has been halted (thus no new references are granted). CPU 1 cannot progress because an outstanding ldisc reference has not been released. An in-tree call-tree audit of tty_perform_flush() [1] shows 5 ldisc drivers calling tty_perform_flush() indirectly via n_tty_ioctl_helper() and 2 ldisc drivers calling directly. A single tty driver safely uses the function. [1] Recursive usage: /* These functions are line discipline ioctls and thus * recursive wrt line discipline references */ tty_perform_flush() - ./drivers/tty/tty_ioctl.c n_tty_ioctl_helper() hci_uart_tty_ioctl(default) - drivers/bluetooth/hci_ldisc.c (N_HCI) n_hdlc_tty_ioctl(default) - drivers/tty/n_hdlc.c (N_HDLC) gsmld_ioctl(default) - drivers/tty/n_gsm.c (N_GSM0710) n_tty_ioctl(default) - drivers/tty/n_tty.c (N_TTY) gigaset_tty_ioctl(default) - drivers/isdn/gigaset/ser-gigaset.c (N_GIGASET_M101) ppp_synctty_ioctl(TCFLSH) - drivers/net/ppp/pps_synctty.c ppp_asynctty_ioctl(TCFLSH) - drivers/net/ppp/ppp_async.c Non-recursive use: tty_perform_flush() - drivers/tty/tty_ioctl.c ipw_ioctl(TCFLSH) - drivers/tty/ipwireless/tty.c /* This function is a tty i/o ioctl method, which * is invoked by tty_ioctl() */ Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent be39711 commit e7f3880

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

‎drivers/net/ppp/ppp_async.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ ppp_asynctty_ioctl(struct tty_struct *tty, struct file *file,
314314
/* flush our buffers and the serial port's buffer */
315315
if (arg == TCIOFLUSH || arg == TCOFLUSH)
316316
ppp_async_flush_output(ap);
317-
err = tty_perform_flush(tty, arg);
317+
err = n_tty_ioctl_helper(tty, file, cmd, arg);
318318
break;
319319

320320
case FIONREAD:

‎drivers/net/ppp/ppp_synctty.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
355355
/* flush our buffers and the serial port's buffer */
356356
if (arg == TCIOFLUSH || arg == TCOFLUSH)
357357
ppp_sync_flush_output(ap);
358-
err = tty_perform_flush(tty, arg);
358+
err = n_tty_ioctl_helper(tty, file, cmd, arg);
359359
break;
360360

361361
case FIONREAD:

‎drivers/tty/tty_ioctl.c‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,14 +1122,12 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
11221122
}
11231123
EXPORT_SYMBOL_GPL(tty_mode_ioctl);
11241124

1125-
int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1125+
1126+
/* Caller guarantees ldisc reference is held */
1127+
static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
11261128
{
1127-
struct tty_ldisc *ld;
1128-
int retval = tty_check_change(tty);
1129-
if (retval)
1130-
return retval;
1129+
struct tty_ldisc *ld = tty->ldisc;
11311130

1132-
ld = tty_ldisc_ref_wait(tty);
11331131
switch (arg) {
11341132
case TCIFLUSH:
11351133
if (ld && ld->ops->flush_buffer) {
@@ -1147,12 +1145,24 @@ int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
11471145
tty_driver_flush_buffer(tty);
11481146
break;
11491147
default:
1150-
tty_ldisc_deref(ld);
11511148
return -EINVAL;
11521149
}
1153-
tty_ldisc_deref(ld);
11541150
return 0;
11551151
}
1152+
1153+
int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1154+
{
1155+
struct tty_ldisc *ld;
1156+
int retval = tty_check_change(tty);
1157+
if (retval)
1158+
return retval;
1159+
1160+
ld = tty_ldisc_ref_wait(tty);
1161+
retval = __tty_perform_flush(tty, arg);
1162+
if (ld)
1163+
tty_ldisc_deref(ld);
1164+
return retval;
1165+
}
11561166
EXPORT_SYMBOL_GPL(tty_perform_flush);
11571167

11581168
int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
@@ -1191,7 +1201,7 @@ int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
11911201
}
11921202
return 0;
11931203
case TCFLSH:
1194-
return tty_perform_flush(tty, arg);
1204+
return __tty_perform_flush(tty, arg);
11951205
default:
11961206
/* Try the mode commands */
11971207
return tty_mode_ioctl(tty, file, cmd, arg);

0 commit comments

Comments
 (0)