Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tty: n_tty: use guard()s

Use guards in the n_tty code. This improves readability, makes error
handling easier, and marks locked portions of code explicit. All that
while being sure the lock is unlocked.

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Link: https://patch.msgid.link/20251119100140.830761-3-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
2fc541e5 977e7590

+47 -60
+47 -60
drivers/tty/n_tty.c
··· 324 324 325 325 static void n_tty_packet_mode_flush(struct tty_struct *tty) 326 326 { 327 - unsigned long flags; 328 - 329 327 if (tty->link->ctrl.packet) { 330 - spin_lock_irqsave(&tty->ctrl.lock, flags); 331 - tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD; 332 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 328 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) 329 + tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD; 333 330 wake_up_interruptible(&tty->link->read_wait); 334 331 } 335 332 } ··· 346 349 */ 347 350 static void n_tty_flush_buffer(struct tty_struct *tty) 348 351 { 349 - down_write(&tty->termios_rwsem); 352 + guard(rwsem_write)(&tty->termios_rwsem); 350 353 reset_buffer_flags(tty->disc_data); 351 354 n_tty_kick_worker(tty); 352 355 353 356 if (tty->link) 354 357 n_tty_packet_mode_flush(tty); 355 - up_write(&tty->termios_rwsem); 356 358 } 357 359 358 360 /** ··· 733 737 size_t nr, old, echoed; 734 738 size_t head; 735 739 736 - mutex_lock(&ldata->output_lock); 737 - head = ldata->echo_head; 738 - ldata->echo_mark = head; 739 - old = ldata->echo_commit - ldata->echo_tail; 740 + scoped_guard(mutex, &ldata->output_lock) { 741 + head = ldata->echo_head; 742 + ldata->echo_mark = head; 743 + old = ldata->echo_commit - ldata->echo_tail; 740 744 741 - /* Process committed echoes if the accumulated # of bytes 742 - * is over the threshold (and try again each time another 743 - * block is accumulated) */ 744 - nr = head - ldata->echo_tail; 745 - if (nr < ECHO_COMMIT_WATERMARK || 746 - (nr % ECHO_BLOCK > old % ECHO_BLOCK)) { 747 - mutex_unlock(&ldata->output_lock); 748 - return; 745 + /* 746 + * Process committed echoes if the accumulated # of bytes is over the threshold 747 + * (and try again each time another block is accumulated) 748 + */ 749 + nr = head - ldata->echo_tail; 750 + if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK)) 751 + return; 752 + 753 + ldata->echo_commit = head; 754 + echoed = __process_echoes(tty); 749 755 } 750 - 751 - ldata->echo_commit = head; 752 - echoed = __process_echoes(tty); 753 - mutex_unlock(&ldata->output_lock); 754 756 755 757 if (echoed && tty->ops->flush_chars) 756 758 tty->ops->flush_chars(tty); ··· 762 768 if (ldata->echo_mark == ldata->echo_tail) 763 769 return; 764 770 765 - mutex_lock(&ldata->output_lock); 766 - ldata->echo_commit = ldata->echo_mark; 767 - echoed = __process_echoes(tty); 768 - mutex_unlock(&ldata->output_lock); 771 + scoped_guard(mutex, &ldata->output_lock) { 772 + ldata->echo_commit = ldata->echo_mark; 773 + echoed = __process_echoes(tty); 774 + } 769 775 770 776 if (echoed && tty->ops->flush_chars) 771 777 tty->ops->flush_chars(tty); ··· 780 786 ldata->echo_commit == ldata->echo_head) 781 787 return; 782 788 783 - mutex_lock(&ldata->output_lock); 789 + guard(mutex)(&ldata->output_lock); 784 790 ldata->echo_commit = ldata->echo_head; 785 791 __process_echoes(tty); 786 - mutex_unlock(&ldata->output_lock); 787 792 } 788 793 789 794 /** ··· 1071 1078 if (L_NOFLSH(tty)) { 1072 1079 /* signal only */ 1073 1080 __isig(sig, tty); 1081 + return; 1082 + } 1074 1083 1075 - } else { /* signal and flush */ 1076 - up_read(&tty->termios_rwsem); 1077 - down_write(&tty->termios_rwsem); 1078 - 1084 + /* signal and flush */ 1085 + up_read(&tty->termios_rwsem); 1086 + scoped_guard(rwsem_write, &tty->termios_rwsem) { 1079 1087 __isig(sig, tty); 1080 1088 1081 1089 /* clear echo buffer */ 1082 - mutex_lock(&ldata->output_lock); 1083 - ldata->echo_head = ldata->echo_tail = 0; 1084 - ldata->echo_mark = ldata->echo_commit = 0; 1085 - mutex_unlock(&ldata->output_lock); 1090 + scoped_guard(mutex, &ldata->output_lock) { 1091 + ldata->echo_head = ldata->echo_tail = 0; 1092 + ldata->echo_mark = ldata->echo_commit = 0; 1093 + } 1086 1094 1087 1095 /* clear output buffer */ 1088 1096 tty_driver_flush_buffer(tty); ··· 1094 1100 /* notify pty master of flush */ 1095 1101 if (tty->link) 1096 1102 n_tty_packet_mode_flush(tty); 1097 - 1098 - up_write(&tty->termios_rwsem); 1099 - down_read(&tty->termios_rwsem); 1100 1103 } 1104 + down_read(&tty->termios_rwsem); 1101 1105 } 1102 1106 1103 1107 /** ··· 1675 1683 size_t n, rcvd = 0; 1676 1684 int room, overflow; 1677 1685 1678 - down_read(&tty->termios_rwsem); 1686 + guard(rwsem_read)(&tty->termios_rwsem); 1679 1687 1680 1688 do { 1681 1689 /* ··· 1743 1751 if (!chars_in_buffer(tty)) 1744 1752 n_tty_kick_worker(tty); 1745 1753 } 1746 - 1747 - up_read(&tty->termios_rwsem); 1748 1754 1749 1755 return rcvd; 1750 1756 } ··· 1869 1879 if (tty->link) 1870 1880 n_tty_packet_mode_flush(tty); 1871 1881 1872 - down_write(&tty->termios_rwsem); 1882 + guard(rwsem_write)(&tty->termios_rwsem); 1873 1883 vfree(ldata); 1874 1884 tty->disc_data = NULL; 1875 - up_write(&tty->termios_rwsem); 1876 1885 } 1877 1886 1878 1887 /** ··· 2236 2247 u8 cs; 2237 2248 if (kb != kbuf) 2238 2249 break; 2239 - spin_lock_irq(&tty->link->ctrl.lock); 2240 - cs = tty->link->ctrl.pktstatus; 2241 - tty->link->ctrl.pktstatus = 0; 2242 - spin_unlock_irq(&tty->link->ctrl.lock); 2250 + scoped_guard(spinlock_irq, &tty->link->ctrl.lock) { 2251 + cs = tty->link->ctrl.pktstatus; 2252 + tty->link->ctrl.pktstatus = 0; 2253 + } 2243 2254 *kb++ = cs; 2244 2255 nr--; 2245 2256 break; ··· 2346 2357 return retval; 2347 2358 } 2348 2359 2349 - down_read(&tty->termios_rwsem); 2360 + guard(rwsem_read)(&tty->termios_rwsem); 2350 2361 2351 2362 /* Write out any echoed characters that are still pending */ 2352 2363 process_echoes(tty); ··· 2384 2395 struct n_tty_data *ldata = tty->disc_data; 2385 2396 2386 2397 while (nr > 0) { 2387 - mutex_lock(&ldata->output_lock); 2388 - num = tty->ops->write(tty, b, nr); 2389 - mutex_unlock(&ldata->output_lock); 2398 + scoped_guard(mutex, &ldata->output_lock) 2399 + num = tty->ops->write(tty, b, nr); 2390 2400 if (num < 0) { 2391 2401 retval = num; 2392 2402 goto break_out; ··· 2412 2424 remove_wait_queue(&tty->write_wait, &wait); 2413 2425 if (nr && tty->fasync) 2414 2426 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 2415 - up_read(&tty->termios_rwsem); 2427 + 2416 2428 return (b - buf) ? b - buf : retval; 2417 2429 } 2418 2430 ··· 2486 2498 case TIOCOUTQ: 2487 2499 return put_user(tty_chars_in_buffer(tty), (int __user *) arg); 2488 2500 case TIOCINQ: 2489 - down_write(&tty->termios_rwsem); 2490 - if (L_ICANON(tty) && !L_EXTPROC(tty)) 2491 - num = inq_canon(ldata); 2492 - else 2493 - num = read_cnt(ldata); 2494 - up_write(&tty->termios_rwsem); 2501 + scoped_guard(rwsem_write, &tty->termios_rwsem) 2502 + if (L_ICANON(tty) && !L_EXTPROC(tty)) 2503 + num = inq_canon(ldata); 2504 + else 2505 + num = read_cnt(ldata); 2495 2506 return put_user(num, (unsigned int __user *) arg); 2496 2507 default: 2497 2508 return n_tty_ioctl_helper(tty, cmd, arg);