Skip to content

Commit e359a4e

Browse files
peterhurleygregkh
authored andcommitted
tty: Remove tty_hung_up_p() tests from tty drivers' open()
Since at least before 2.6.30, it has not been possible to observe a hung up file pointer in a tty driver's open() method unless/until the driver open() releases the tty_lock() (eg., before blocking). This is because tty_open() adds the file pointer while holding the tty_lock() _and_ doesn't release the lock until after calling the tty driver's open() method. [ Before tty_lock(), this was lock_kernel(). ] Since __tty_hangup() first waits on the tty_lock() before enumerating and hanging up the open file pointers, either __tty_hangup() will wait for the tty_lock() or tty_open() will not yet have added the file pointer. For example, CPU 0 | CPU 1 | tty_open | __tty_hangup .. | .. tty_lock | .. tty_reopen | tty_lock / blocks .. | tty_add_file(tty, filp) | .. | tty->ops->open(tty, filp) | tty_port_open | tty_port_block_til_ready | .. | while (1) | .. | tty_unlock | / unblocks schedule | for each filp on tty->tty_files | f_ops = tty_hung_up_fops; | .. | tty_unlock tty_lock | .. | tty_unlock | Note that since tty_port_block_til_ready() and similar drop the tty_lock while blocking, when woken, the file pointer must then be tested for having been hung up. Also, fix bit-rotted drivers that used extra_count to track the port->count bump. CC: Mikael Starvik <starvik@axis.com> CC: Samuel Ortiz <samuel@sortiz.org> CC: "David S. Miller" <davem@davemloft.net> Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Acked-by: Jesper Nilsson <jesper.nilsson@axis.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5fda7a0 commit e359a4e

10 files changed

Lines changed: 21 additions & 61 deletions

File tree

‎drivers/char/pcmcia/synclink_cs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ static int mgslpc_open(struct tty_struct *tty, struct file * filp)
25102510
__FILE__, __LINE__, tty->driver->name, port->count);
25112511

25122512
/* If port is closing, signal caller to try again */
2513-
if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2513+
if (port->flags & ASYNC_CLOSING){
25142514
wait_event_interruptible_tty(tty, port->close_wait,
25152515
!(port->flags & ASYNC_CLOSING));
25162516
retval = ((port->flags & ASYNC_HUP_NOTIFY) ?

‎drivers/staging/dgrp/dgrp_tty.c‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -631,16 +631,6 @@ static int dgrp_tty_open(struct tty_struct *tty, struct file *file)
631631
un->un_tty = tty;
632632
tty->driver_data = un;
633633

634-
/*
635-
* If we are in the middle of hanging up,
636-
* then return an error
637-
*/
638-
if (tty_hung_up_p(file)) {
639-
retval = ((un->un_flag & UN_HUP_NOTIFY) ?
640-
-EAGAIN : -ERESTARTSYS);
641-
goto done;
642-
}
643-
644634
/*
645635
* If the port is in the middle of closing, then block
646636
* until it is done, then try again.

‎drivers/tty/cyclades.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ static int cy_open(struct tty_struct *tty, struct file *filp)
15791579
/*
15801580
* If the port is the middle of closing, bail out now
15811581
*/
1582-
if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1582+
if (info->port.flags & ASYNC_CLOSING) {
15831583
wait_event_interruptible_tty(tty, info->port.close_wait,
15841584
!(info->port.flags & ASYNC_CLOSING));
15851585
return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;

‎drivers/tty/serial/crisv10.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,14 +3831,13 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
38313831
DECLARE_WAITQUEUE(wait, current);
38323832
unsigned long flags;
38333833
int retval;
3834-
int do_clocal = 0, extra_count = 0;
3834+
int do_clocal = 0;
38353835

38363836
/*
38373837
* If the device is in the middle of being closed, then block
38383838
* until it's done, and then try again.
38393839
*/
3840-
if (tty_hung_up_p(filp) ||
3841-
(info->port.flags & ASYNC_CLOSING)) {
3840+
if (info->port.flags & ASYNC_CLOSING) {
38423841
wait_event_interruptible_tty(tty, info->port.close_wait,
38433842
!(info->port.flags & ASYNC_CLOSING));
38443843
#ifdef SERIAL_DO_RESTART
@@ -3879,10 +3878,7 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
38793878
info->line, info->port.count);
38803879
#endif
38813880
local_irq_save(flags);
3882-
if (!tty_hung_up_p(filp)) {
3883-
extra_count++;
3884-
info->port.count--;
3885-
}
3881+
info->port.count--;
38863882
local_irq_restore(flags);
38873883
info->port.blocked_open++;
38883884
while (1) {
@@ -3921,7 +3917,7 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
39213917
}
39223918
set_current_state(TASK_RUNNING);
39233919
remove_wait_queue(&info->port.open_wait, &wait);
3924-
if (extra_count)
3920+
if (!tty_hung_up_p(filp))
39253921
info->port.count++;
39263922
info->port.blocked_open--;
39273923
#ifdef SERIAL_DEBUG_OPEN
@@ -3976,8 +3972,7 @@ rs_open(struct tty_struct *tty, struct file * filp)
39763972
/*
39773973
* If the port is in the middle of closing, bail out now
39783974
*/
3979-
if (tty_hung_up_p(filp) ||
3980-
(info->port.flags & ASYNC_CLOSING)) {
3975+
if (info->port.flags & ASYNC_CLOSING) {
39813976
wait_event_interruptible_tty(tty, info->port.close_wait,
39823977
!(info->port.flags & ASYNC_CLOSING));
39833978
#ifdef SERIAL_DO_RESTART

‎drivers/tty/serial/serial_core.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,14 +1583,6 @@ static int uart_open(struct tty_struct *tty, struct file *filp)
15831583
(state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
15841584
tty_port_tty_set(port, tty);
15851585

1586-
/*
1587-
* If the port is in the middle of closing, bail out now.
1588-
*/
1589-
if (tty_hung_up_p(filp)) {
1590-
retval = -EAGAIN;
1591-
goto err_dec_count;
1592-
}
1593-
15941586
/*
15951587
* Start up the serial port.
15961588
*/

‎drivers/tty/synclink.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,7 +3267,6 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
32673267
DECLARE_WAITQUEUE(wait, current);
32683268
int retval;
32693269
bool do_clocal = false;
3270-
bool extra_count = false;
32713270
unsigned long flags;
32723271
int dcd;
32733272
struct tty_port *port = &info->port;
@@ -3300,10 +3299,7 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
33003299
__FILE__,__LINE__, tty->driver->name, port->count );
33013300

33023301
spin_lock_irqsave(&info->irq_spinlock, flags);
3303-
if (!tty_hung_up_p(filp)) {
3304-
extra_count = true;
3305-
port->count--;
3306-
}
3302+
port->count--;
33073303
spin_unlock_irqrestore(&info->irq_spinlock, flags);
33083304
port->blocked_open++;
33093305

@@ -3342,7 +3338,7 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
33423338
remove_wait_queue(&port->open_wait, &wait);
33433339

33443340
/* FIXME: Racy on hangup during close wait */
3345-
if (extra_count)
3341+
if (!tty_hung_up_p(filp))
33463342
port->count++;
33473343
port->blocked_open--;
33483344

@@ -3403,7 +3399,7 @@ static int mgsl_open(struct tty_struct *tty, struct file * filp)
34033399
__FILE__,__LINE__,tty->driver->name, info->port.count);
34043400

34053401
/* If port is closing, signal caller to try again */
3406-
if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
3402+
if (info->port.flags & ASYNC_CLOSING){
34073403
wait_event_interruptible_tty(tty, info->port.close_wait,
34083404
!(info->port.flags & ASYNC_CLOSING));
34093405
retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?

‎drivers/tty/synclink_gt.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ static int open(struct tty_struct *tty, struct file *filp)
673673
DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
674674

675675
/* If port is closing, signal caller to try again */
676-
if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
676+
if (info->port.flags & ASYNC_CLOSING){
677677
wait_event_interruptible_tty(tty, info->port.close_wait,
678678
!(info->port.flags & ASYNC_CLOSING));
679679
retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
@@ -3273,7 +3273,6 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
32733273
DECLARE_WAITQUEUE(wait, current);
32743274
int retval;
32753275
bool do_clocal = false;
3276-
bool extra_count = false;
32773276
unsigned long flags;
32783277
int cd;
32793278
struct tty_port *port = &info->port;
@@ -3300,10 +3299,7 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
33003299
add_wait_queue(&port->open_wait, &wait);
33013300

33023301
spin_lock_irqsave(&info->lock, flags);
3303-
if (!tty_hung_up_p(filp)) {
3304-
extra_count = true;
3305-
port->count--;
3306-
}
3302+
port->count--;
33073303
spin_unlock_irqrestore(&info->lock, flags);
33083304
port->blocked_open++;
33093305

@@ -3338,7 +3334,7 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
33383334
set_current_state(TASK_RUNNING);
33393335
remove_wait_queue(&port->open_wait, &wait);
33403336

3341-
if (extra_count)
3337+
if (!tty_hung_up_p(filp))
33423338
port->count++;
33433339
port->blocked_open--;
33443340

‎drivers/tty/synclinkmp.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ static int open(struct tty_struct *tty, struct file *filp)
753753
__FILE__,__LINE__,tty->driver->name, info->port.count);
754754

755755
/* If port is closing, signal caller to try again */
756-
if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
756+
if (info->port.flags & ASYNC_CLOSING){
757757
wait_event_interruptible_tty(tty, info->port.close_wait,
758758
!(info->port.flags & ASYNC_CLOSING));
759759
retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
@@ -3288,7 +3288,6 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
32883288
DECLARE_WAITQUEUE(wait, current);
32893289
int retval;
32903290
bool do_clocal = false;
3291-
bool extra_count = false;
32923291
unsigned long flags;
32933292
int cd;
32943293
struct tty_port *port = &info->port;
@@ -3322,10 +3321,7 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
33223321
__FILE__,__LINE__, tty->driver->name, port->count );
33233322

33243323
spin_lock_irqsave(&info->lock, flags);
3325-
if (!tty_hung_up_p(filp)) {
3326-
extra_count = true;
3327-
port->count--;
3328-
}
3324+
port->count--;
33293325
spin_unlock_irqrestore(&info->lock, flags);
33303326
port->blocked_open++;
33313327

@@ -3362,8 +3358,7 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
33623358

33633359
set_current_state(TASK_RUNNING);
33643360
remove_wait_queue(&port->open_wait, &wait);
3365-
3366-
if (extra_count)
3361+
if (!tty_hung_up_p(filp))
33673362
port->count++;
33683363
port->blocked_open--;
33693364

‎drivers/tty/tty_port.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ int tty_port_block_til_ready(struct tty_port *port,
365365
DEFINE_WAIT(wait);
366366

367367
/* block if port is in the process of being closed */
368-
if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
368+
if (port->flags & ASYNC_CLOSING) {
369369
wait_event_interruptible_tty(tty, port->close_wait,
370370
!(port->flags & ASYNC_CLOSING));
371371
if (port->flags & ASYNC_HUP_NOTIFY)
@@ -399,8 +399,7 @@ int tty_port_block_til_ready(struct tty_port *port,
399399

400400
/* The port lock protects the port counts */
401401
spin_lock_irqsave(&port->lock, flags);
402-
if (!tty_hung_up_p(filp))
403-
port->count--;
402+
port->count--;
404403
port->blocked_open++;
405404
spin_unlock_irqrestore(&port->lock, flags);
406405

@@ -593,8 +592,7 @@ int tty_port_open(struct tty_port *port, struct tty_struct *tty,
593592
struct file *filp)
594593
{
595594
spin_lock_irq(&port->lock);
596-
if (!tty_hung_up_p(filp))
597-
++port->count;
595+
++port->count;
598596
spin_unlock_irq(&port->lock);
599597
tty_port_tty_set(port, tty);
600598

‎net/irda/ircomm/ircomm_tty.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
320320
__FILE__, __LINE__, tty->driver->name, port->count);
321321

322322
spin_lock_irqsave(&port->lock, flags);
323-
if (!tty_hung_up_p(filp))
324-
port->count--;
323+
port->count--;
325324
port->blocked_open++;
326325
spin_unlock_irqrestore(&port->lock, flags);
327326

@@ -458,8 +457,7 @@ static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
458457
/*
459458
* If the port is the middle of closing, bail out now
460459
*/
461-
if (tty_hung_up_p(filp) ||
462-
test_bit(ASYNCB_CLOSING, &self->port.flags)) {
460+
if (test_bit(ASYNCB_CLOSING, &self->port.flags)) {
463461

464462
/* Hm, why are we blocking on ASYNC_CLOSING if we
465463
* do return -EAGAIN/-ERESTARTSYS below anyway?

0 commit comments

Comments
 (0)