Skip to content

Commit

Permalink
tty: cumulate and document tty_struct::flow* members
Browse files Browse the repository at this point in the history
Group the flow flags under a single struct called flow. The new struct
contains 'stopped' and 'tco_stopped' bools which used to be bits in a
bitfield. The struct also contains the lock protecting them to
potentially share the same cache line.

Note that commit c545b66 (tty: Serialize tcflow() with other tty
flow control changes) added a padding to the original bitfield. It was
for the bitfield to occupy a whole 64b word to avoid interferring stores
on Alpha (cannot we evaporate this arch with weird implications to C
code yet?). But it doesn't work as expected as the padding
(tty_struct::unused) is aligned to a 8B boundary too and occupies some
bytes from the next word.

So make it reliable by:
1) setting __aligned of the struct -- that aligns the start, and
2) making 'unsigned long unused[0]' as the last member of the struct --
   pads the end.

This is also the perfect time to start the documentation of tty_struct
where all this lives. So we start by documenting what these bools
actually serve for. And why we do all the alignment dances. Only the few
up-to-date information from the Theodore's comment made it into this new
Kerneldoc comment.

Signed-off-by: Jiri Slaby <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Ulf Hansson <[email protected]>
Cc: Heiko Carstens <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Christian Borntraeger <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Vineet Gupta <[email protected]>
Cc: "Maciej W. Rozycki" <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
Jiri Slaby authored and gregkh committed May 13, 2021
1 parent 0f3dcf3 commit 6e94dbc
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 77 deletions.
4 changes: 2 additions & 2 deletions Documentation/networking/caif/caif.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ There are debugfs parameters provided for serial communication.

- 0x01 - tty->warned is on.
- 0x04 - tty->packed is on.
- 0x08 - tty->flow_stopped is on.
- 0x08 - tty->flow.tco_stopped is on.
- 0x10 - tty->hw_stopped is on.
- 0x20 - tty->stopped is on.
- 0x20 - tty->flow.stopped is on.

* last_tx_msg: Binary blob Prints the last transmitted frame.

Expand Down
8 changes: 4 additions & 4 deletions drivers/char/pcmcia/synclink_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
else
#endif
{
if (tty && (tty->stopped || tty->hw_stopped)) {
if (tty && (tty->flow.stopped || tty->hw_stopped)) {
tx_stop(info);
return;
}
Expand All @@ -1005,7 +1005,7 @@ static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
if (!info->tx_active)
return;
} else {
if (tty && (tty->stopped || tty->hw_stopped)) {
if (tty && (tty->flow.stopped || tty->hw_stopped)) {
tx_stop(info);
return;
}
Expand Down Expand Up @@ -1525,7 +1525,7 @@ static void mgslpc_flush_chars(struct tty_struct *tty)
if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
return;

if (info->tx_count <= 0 || tty->stopped ||
if (info->tx_count <= 0 || tty->flow.stopped ||
tty->hw_stopped || !info->tx_buf)
return;

Expand Down Expand Up @@ -1594,7 +1594,7 @@ static int mgslpc_write(struct tty_struct * tty,
ret += c;
}
start:
if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
if (info->tx_count && !tty->flow.stopped && !tty->hw_stopped) {
spin_lock_irqsave(&info->lock, flags);
if (!info->tx_active)
tx_start(info, tty);
Expand Down
2 changes: 1 addition & 1 deletion drivers/mmc/core/sdio_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
tty = tty_port_tty_get(&port->port);

if (tty == NULL || !kfifo_len(xmit) ||
tty->stopped || tty->hw_stopped) {
tty->flow.stopped || tty->hw_stopped) {
sdio_uart_stop_tx(port);
tty_kref_put(tty);
return;
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/caif/caif_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ static void ldisc_tx_wakeup(struct tty_struct *tty);
static inline void update_tty_status(struct ser_device *ser)
{
ser->tty_status =
ser->tty->stopped << 5 |
ser->tty->flow_stopped << 3 |
ser->tty->flow.stopped << 5 |
ser->tty->flow.tco_stopped << 3 |
ser->tty->packet << 2;
}
static inline void debugfs_init(struct ser_device *ser, struct tty_struct *tty)
Expand Down
2 changes: 1 addition & 1 deletion drivers/s390/char/tty3270.c
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty,
int i_msg, i;

spin_lock_bh(&tp->view.lock);
for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) {
for (i_msg = 0; !tty->flow.stopped && i_msg < count; i_msg++) {
if (tp->esc_state != 0) {
/* Continue escape sequence. */
tty3270_escape_sequence(tp, buf[i_msg]);
Expand Down
2 changes: 1 addition & 1 deletion drivers/staging/fwserial/fwserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ static int fwtty_tx(struct fwtty_port *port, bool drain)

/* try to write as many dma transactions out as possible */
n = -EAGAIN;
while (!tty->stopped && !tty->hw_stopped &&
while (!tty->flow.stopped && !tty->hw_stopped &&
!test_bit(STOP_TX, &port->flags)) {
txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
if (!txn) {
Expand Down
8 changes: 4 additions & 4 deletions drivers/tty/amiserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static __inline__ void rtsdtr_ctrl(int bits)
* ------------------------------------------------------------
* rs_stop() and rs_start()
*
* This routines are called before setting or resetting tty->stopped.
* This routines are called before setting or resetting tty->flow.stopped.
* They enable or disable transmitter interrupts, as necessary.
* ------------------------------------------------------------
*/
Expand Down Expand Up @@ -309,7 +309,7 @@ static void transmit_chars(struct serial_state *info)
return;
}
if (info->xmit.head == info->xmit.tail
|| info->tport.tty->stopped
|| info->tport.tty->flow.stopped
|| info->tport.tty->hw_stopped) {
info->IER &= ~UART_IER_THRI;
custom.intena = IF_TBE;
Expand Down Expand Up @@ -768,7 +768,7 @@ static void rs_flush_chars(struct tty_struct *tty)
unsigned long flags;

if (info->xmit.head == info->xmit.tail
|| tty->stopped
|| tty->flow.stopped
|| tty->hw_stopped
|| !info->xmit.buf)
return;
Expand Down Expand Up @@ -812,7 +812,7 @@ static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count
local_irq_restore(flags);

if (info->xmit.head != info->xmit.tail
&& !tty->stopped
&& !tty->flow.stopped
&& !tty->hw_stopped
&& !(info->IER & UART_IER_THRI)) {
info->IER |= UART_IER_THRI;
Expand Down
4 changes: 2 additions & 2 deletions drivers/tty/moxa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ static int moxa_write_room(struct tty_struct *tty)
{
struct moxa_port *ch;

if (tty->stopped)
if (tty->flow.stopped)
return 0;
ch = tty->driver_data;
if (ch == NULL)
Expand Down Expand Up @@ -1374,7 +1374,7 @@ static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
clear_bit(EMPTYWAIT, &p->statusflags);
tty_wakeup(tty);
}
if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
if (test_bit(LOWWAIT, &p->statusflags) && !tty->flow.stopped &&
MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
clear_bit(LOWWAIT, &p->statusflags);
tty_wakeup(tty);
Expand Down
12 changes: 6 additions & 6 deletions drivers/tty/mxser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int cou
total += c;
}

if (info->xmit_cnt && !tty->stopped) {
if (info->xmit_cnt && !tty->flow.stopped) {
if (!tty->hw_stopped ||
(info->type == PORT_16550A) ||
(info->board->chip_flag)) {
Expand Down Expand Up @@ -1149,7 +1149,7 @@ static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
info->xmit_head &= SERIAL_XMIT_SIZE - 1;
info->xmit_cnt++;
spin_unlock_irqrestore(&info->slock, flags);
if (!tty->stopped) {
if (!tty->flow.stopped) {
if (!tty->hw_stopped ||
(info->type == PORT_16550A) ||
info->board->chip_flag) {
Expand All @@ -1169,7 +1169,7 @@ static void mxser_flush_chars(struct tty_struct *tty)
struct mxser_port *info = tty->driver_data;
unsigned long flags;

if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
if (info->xmit_cnt <= 0 || tty->flow.stopped || !info->port.xmit_buf ||
(tty->hw_stopped && info->type != PORT_16550A &&
!info->board->chip_flag))
return;
Expand Down Expand Up @@ -1917,7 +1917,7 @@ static void mxser_unthrottle(struct tty_struct *tty)
/*
* mxser_stop() and mxser_start()
*
* This routines are called before setting or resetting tty->stopped.
* This routines are called before setting or resetting tty->flow.stopped.
* They enable or disable transmitter interrupts, as necessary.
*/
static void mxser_stop(struct tty_struct *tty)
Expand Down Expand Up @@ -1963,7 +1963,7 @@ static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termi

/* Handle sw stopped */
if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
tty->stopped = 0;
tty->flow.stopped = 0;

if (info->board->chip_flag) {
spin_lock_irqsave(&info->slock, flags);
Expand Down Expand Up @@ -2175,7 +2175,7 @@ static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port
if (port->port.xmit_buf == NULL)
return;

if (port->xmit_cnt <= 0 || tty->stopped ||
if (port->xmit_cnt <= 0 || tty->flow.stopped ||
(tty->hw_stopped &&
(port->type != PORT_16550A) &&
(!port->board->chip_flag))) {
Expand Down
8 changes: 4 additions & 4 deletions drivers/tty/n_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
}
}

if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
start_tty(tty);
process_echoes(tty);
}
Expand Down Expand Up @@ -1398,7 +1398,7 @@ static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
{
struct n_tty_data *ldata = tty->disc_data;

if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
start_tty(tty);
process_echoes(tty);
}
Expand Down Expand Up @@ -1427,7 +1427,7 @@ static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
if (c == STOP_CHAR(tty))
stop_tty(tty);
else if (c == START_CHAR(tty) ||
(tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
(tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) &&
c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
c != SUSP_CHAR(tty))) {
start_tty(tty);
Expand Down Expand Up @@ -1797,7 +1797,7 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
* Fix tty hang when I_IXON(tty) is cleared, but the tty
* been stopped by STOP_CHAR(tty) before it.
*/
if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
start_tty(tty);
process_echoes(tty);
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/tty/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
struct tty_struct *to = tty->link;
unsigned long flags;

if (tty->stopped)
if (tty->flow.stopped)
return 0;

if (c > 0) {
Expand All @@ -138,7 +138,7 @@ static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)

static int pty_write_room(struct tty_struct *tty)
{
if (tty->stopped)
if (tty->flow.stopped)
return 0;
return tty_buffer_space_avail(tty->link->port);
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/tty/serial/arc_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static unsigned int arc_serial_tx_empty(struct uart_port *port)
/*
* Driver internal routine, used by both tty(serial core) as well as tx-isr
* -Called under spinlock in either cases
* -also tty->stopped has already been checked
* -also tty->flow.stopped has already been checked
* = by uart_start( ) before calling us
* = tx_ist checks that too before calling
*/
Expand Down
2 changes: 1 addition & 1 deletion drivers/tty/serial/dz.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
* rs_stop () and rs_start ()
*
* These routines are called before setting or resetting
* tty->stopped. They enable or disable transmitter interrupts,
* tty->flow.stopped. They enable or disable transmitter interrupts,
* as necessary.
* ------------------------------------------------------------
*/
Expand Down
6 changes: 3 additions & 3 deletions drivers/tty/synclink_gt.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ static int write(struct tty_struct *tty,
if (!info->tx_buf || (count > info->max_frame_size))
return -EIO;

if (!count || tty->stopped || tty->hw_stopped)
if (!count || tty->flow.stopped || tty->hw_stopped)
return 0;

spin_lock_irqsave(&info->lock, flags);
Expand Down Expand Up @@ -889,7 +889,7 @@ static void flush_chars(struct tty_struct *tty)
return;
DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));

if (info->tx_count <= 0 || tty->stopped ||
if (info->tx_count <= 0 || tty->flow.stopped ||
tty->hw_stopped || !info->tx_buf)
return;

Expand Down Expand Up @@ -2241,7 +2241,7 @@ static void isr_txeom(struct slgt_info *info, unsigned short status)
else
#endif
{
if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
if (info->port.tty && (info->port.tty->flow.stopped || info->port.tty->hw_stopped)) {
tx_stop(info);
return;
}
Expand Down
24 changes: 12 additions & 12 deletions drivers/tty/tty_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,14 @@ EXPORT_SYMBOL(tty_hung_up_p);
* but not always.
*
* Locking:
* flow_lock
* flow.lock
*/

void __stop_tty(struct tty_struct *tty)
{
if (tty->stopped)
if (tty->flow.stopped)
return;
tty->stopped = 1;
tty->flow.stopped = true;
if (tty->ops->stop)
tty->ops->stop(tty);
}
Expand All @@ -794,9 +794,9 @@ void stop_tty(struct tty_struct *tty)
{
unsigned long flags;

spin_lock_irqsave(&tty->flow_lock, flags);
spin_lock_irqsave(&tty->flow.lock, flags);
__stop_tty(tty);
spin_unlock_irqrestore(&tty->flow_lock, flags);
spin_unlock_irqrestore(&tty->flow.lock, flags);
}
EXPORT_SYMBOL(stop_tty);

Expand All @@ -809,14 +809,14 @@ EXPORT_SYMBOL(stop_tty);
* start method is invoked and the line discipline woken.
*
* Locking:
* flow_lock
* flow.lock
*/

void __start_tty(struct tty_struct *tty)
{
if (!tty->stopped || tty->flow_stopped)
if (!tty->flow.stopped || tty->flow.tco_stopped)
return;
tty->stopped = 0;
tty->flow.stopped = false;
if (tty->ops->start)
tty->ops->start(tty);
tty_wakeup(tty);
Expand All @@ -826,9 +826,9 @@ void start_tty(struct tty_struct *tty)
{
unsigned long flags;

spin_lock_irqsave(&tty->flow_lock, flags);
spin_lock_irqsave(&tty->flow.lock, flags);
__start_tty(tty);
spin_unlock_irqrestore(&tty->flow_lock, flags);
spin_unlock_irqrestore(&tty->flow.lock, flags);
}
EXPORT_SYMBOL(start_tty);

Expand Down Expand Up @@ -1172,7 +1172,7 @@ ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter)

int tty_send_xchar(struct tty_struct *tty, char ch)
{
int was_stopped = tty->stopped;
bool was_stopped = tty->flow.stopped;

if (tty->ops->send_xchar) {
down_read(&tty->termios_rwsem);
Expand Down Expand Up @@ -3141,7 +3141,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
INIT_WORK(&tty->hangup_work, do_tty_hangup);
mutex_init(&tty->atomic_write_lock);
spin_lock_init(&tty->ctrl_lock);
spin_lock_init(&tty->flow_lock);
spin_lock_init(&tty->flow.lock);
spin_lock_init(&tty->files_lock);
INIT_LIST_HEAD(&tty->tty_files);
INIT_WORK(&tty->SAK_work, do_SAK_work);
Expand Down
16 changes: 8 additions & 8 deletions drivers/tty/tty_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,20 +846,20 @@ int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
return retval;
switch (arg) {
case TCOOFF:
spin_lock_irq(&tty->flow_lock);
if (!tty->flow_stopped) {
tty->flow_stopped = 1;
spin_lock_irq(&tty->flow.lock);
if (!tty->flow.tco_stopped) {
tty->flow.tco_stopped = true;
__stop_tty(tty);
}
spin_unlock_irq(&tty->flow_lock);
spin_unlock_irq(&tty->flow.lock);
break;
case TCOON:
spin_lock_irq(&tty->flow_lock);
if (tty->flow_stopped) {
tty->flow_stopped = 0;
spin_lock_irq(&tty->flow.lock);
if (tty->flow.tco_stopped) {
tty->flow.tco_stopped = false;
__start_tty(tty);
}
spin_unlock_irq(&tty->flow_lock);
spin_unlock_irq(&tty->flow.lock);
break;
case TCIOFF:
if (STOP_CHAR(tty) != __DISABLED_CHAR)
Expand Down
Loading

0 comments on commit 6e94dbc

Please sign in to comment.