@@ -1491,10 +1491,14 @@ static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1491
1491
* canonical mode and don't have a newline yet!
1492
1492
*/
1493
1493
while (1 ) {
1494
+ int throttled ;
1494
1495
tty_set_flow_change (tty , TTY_THROTTLE_SAFE );
1495
1496
if (receive_room (tty ) >= TTY_THRESHOLD_THROTTLE )
1496
1497
break ;
1497
- if (!tty_throttle_safe (tty ))
1498
+ up_read (& tty -> termios_rwsem );
1499
+ throttled = tty_throttle_safe (tty );
1500
+ down_read (& tty -> termios_rwsem );
1501
+ if (!throttled )
1498
1502
break ;
1499
1503
}
1500
1504
__tty_set_flow_change (tty , 0 );
@@ -1503,7 +1507,9 @@ static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1503
1507
static void n_tty_receive_buf (struct tty_struct * tty , const unsigned char * cp ,
1504
1508
char * fp , int count )
1505
1509
{
1510
+ down_read (& tty -> termios_rwsem );
1506
1511
__receive_buf (tty , cp , fp , count );
1512
+ up_read (& tty -> termios_rwsem );
1507
1513
}
1508
1514
1509
1515
static int n_tty_receive_buf2 (struct tty_struct * tty , const unsigned char * cp ,
@@ -1512,13 +1518,17 @@ static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1512
1518
struct n_tty_data * ldata = tty -> disc_data ;
1513
1519
int room ;
1514
1520
1521
+ down_read (& tty -> termios_rwsem );
1522
+
1515
1523
tty -> receive_room = room = receive_room (tty );
1516
1524
if (!room )
1517
1525
ldata -> no_room = 1 ;
1518
1526
count = min (count , room );
1519
1527
if (count )
1520
1528
__receive_buf (tty , cp , fp , count );
1521
1529
1530
+ up_read (& tty -> termios_rwsem );
1531
+
1522
1532
return count ;
1523
1533
}
1524
1534
@@ -1934,6 +1944,8 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1934
1944
if (c < 0 )
1935
1945
return c ;
1936
1946
1947
+ down_read (& tty -> termios_rwsem );
1948
+
1937
1949
minimum = time = 0 ;
1938
1950
timeout = MAX_SCHEDULE_TIMEOUT ;
1939
1951
if (!ldata -> icanon ) {
@@ -1955,11 +1967,15 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1955
1967
* Internal serialization of reads.
1956
1968
*/
1957
1969
if (file -> f_flags & O_NONBLOCK ) {
1958
- if (!mutex_trylock (& ldata -> atomic_read_lock ))
1970
+ if (!mutex_trylock (& ldata -> atomic_read_lock )) {
1971
+ up_read (& tty -> termios_rwsem );
1959
1972
return - EAGAIN ;
1973
+ }
1960
1974
} else {
1961
- if (mutex_lock_interruptible (& ldata -> atomic_read_lock ))
1975
+ if (mutex_lock_interruptible (& ldata -> atomic_read_lock )) {
1976
+ up_read (& tty -> termios_rwsem );
1962
1977
return - ERESTARTSYS ;
1978
+ }
1963
1979
}
1964
1980
packet = tty -> packet ;
1965
1981
@@ -2009,7 +2025,11 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2009
2025
break ;
2010
2026
}
2011
2027
n_tty_set_room (tty );
2028
+ up_read (& tty -> termios_rwsem );
2029
+
2012
2030
timeout = schedule_timeout (timeout );
2031
+
2032
+ down_read (& tty -> termios_rwsem );
2013
2033
continue ;
2014
2034
}
2015
2035
__set_current_state (TASK_RUNNING );
@@ -2048,13 +2068,17 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2048
2068
* we won't get any more characters.
2049
2069
*/
2050
2070
while (1 ) {
2071
+ int unthrottled ;
2051
2072
tty_set_flow_change (tty , TTY_UNTHROTTLE_SAFE );
2052
2073
if (chars_in_buffer (tty ) > TTY_THRESHOLD_UNTHROTTLE )
2053
2074
break ;
2054
2075
if (!tty -> count )
2055
2076
break ;
2056
2077
n_tty_set_room (tty );
2057
- if (!tty_unthrottle_safe (tty ))
2078
+ up_read (& tty -> termios_rwsem );
2079
+ unthrottled = tty_unthrottle_safe (tty );
2080
+ down_read (& tty -> termios_rwsem );
2081
+ if (!unthrottled )
2058
2082
break ;
2059
2083
}
2060
2084
__tty_set_flow_change (tty , 0 );
@@ -2076,10 +2100,13 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2076
2100
retval = size ;
2077
2101
if (nr )
2078
2102
clear_bit (TTY_PUSH , & tty -> flags );
2079
- } else if (test_and_clear_bit (TTY_PUSH , & tty -> flags ))
2103
+ } else if (test_and_clear_bit (TTY_PUSH , & tty -> flags )) {
2104
+ up_read (& tty -> termios_rwsem );
2080
2105
goto do_it_again ;
2106
+ }
2081
2107
2082
2108
n_tty_set_room (tty );
2109
+ up_read (& tty -> termios_rwsem );
2083
2110
return retval ;
2084
2111
}
2085
2112
@@ -2120,6 +2147,8 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2120
2147
return retval ;
2121
2148
}
2122
2149
2150
+ down_read (& tty -> termios_rwsem );
2151
+
2123
2152
/* Write out any echoed characters that are still pending */
2124
2153
process_echoes (tty );
2125
2154
@@ -2173,13 +2202,18 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2173
2202
retval = - EAGAIN ;
2174
2203
break ;
2175
2204
}
2205
+ up_read (& tty -> termios_rwsem );
2206
+
2176
2207
schedule ();
2208
+
2209
+ down_read (& tty -> termios_rwsem );
2177
2210
}
2178
2211
break_out :
2179
2212
__set_current_state (TASK_RUNNING );
2180
2213
remove_wait_queue (& tty -> write_wait , & wait );
2181
2214
if (b - buf != nr && tty -> fasync )
2182
2215
set_bit (TTY_DO_WRITE_WAKEUP , & tty -> flags );
2216
+ up_read (& tty -> termios_rwsem );
2183
2217
return (b - buf ) ? b - buf : retval ;
2184
2218
}
2185
2219
0 commit comments