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: pty: use guard()s

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

pty_set_pktmode() is handled specially -- the conditions are inverted
and return called if conditions unmet. This avoid double nested 'if's.
The variable is renamed to want_pktmode so it is not confused with the
current state of pktmode.

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

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
977e7590 6c84a61a

+45 -58
+45 -58
drivers/tty/pty.c
··· 57 57 set_bit(TTY_IO_ERROR, &tty->flags); 58 58 wake_up_interruptible(&tty->read_wait); 59 59 wake_up_interruptible(&tty->write_wait); 60 - spin_lock_irq(&tty->ctrl.lock); 61 - tty->ctrl.packet = false; 62 - spin_unlock_irq(&tty->ctrl.lock); 60 + scoped_guard(spinlock_irq, &tty->ctrl.lock) 61 + tty->ctrl.packet = false; 63 62 /* Review - krefs on tty_link ?? */ 64 63 if (!tty->link) 65 64 return; ··· 69 70 set_bit(TTY_OTHER_CLOSED, &tty->flags); 70 71 #ifdef CONFIG_UNIX98_PTYS 71 72 if (tty->driver == ptm_driver) { 72 - mutex_lock(&devpts_mutex); 73 + guard(mutex)(&devpts_mutex); 73 74 if (tty->link->driver_data) 74 75 devpts_pty_kill(tty->link->driver_data); 75 - mutex_unlock(&devpts_mutex); 76 76 } 77 77 #endif 78 78 tty_vhangup(tty->link); ··· 155 157 /* Set the packet mode on a pty */ 156 158 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg) 157 159 { 158 - int pktmode; 160 + int want_pktmode; 159 161 160 - if (get_user(pktmode, arg)) 162 + if (get_user(want_pktmode, arg)) 161 163 return -EFAULT; 162 164 163 - spin_lock_irq(&tty->ctrl.lock); 164 - if (pktmode) { 165 - if (!tty->ctrl.packet) { 166 - tty->link->ctrl.pktstatus = 0; 167 - smp_mb(); 168 - tty->ctrl.packet = true; 169 - } 170 - } else 165 + guard(spinlock_irq)(&tty->ctrl.lock); 166 + if (!want_pktmode) { 171 167 tty->ctrl.packet = false; 172 - spin_unlock_irq(&tty->ctrl.lock); 168 + return 0; 169 + } 170 + 171 + if (tty->ctrl.packet) 172 + return 0; 173 + 174 + tty->link->ctrl.pktstatus = 0; 175 + smp_mb(); 176 + tty->ctrl.packet = true; 173 177 174 178 return 0; 175 179 } ··· 210 210 211 211 tty_buffer_flush(to, NULL); 212 212 if (to->ctrl.packet) { 213 - spin_lock_irq(&tty->ctrl.lock); 213 + guard(spinlock_irq)(&tty->ctrl.lock); 214 214 tty->ctrl.pktstatus |= TIOCPKT_FLUSHWRITE; 215 215 wake_up_interruptible(&to->read_wait); 216 - spin_unlock_irq(&tty->ctrl.lock); 217 216 } 218 217 } 219 218 ··· 251 252 STOP_CHAR(tty) == '\023' && 252 253 START_CHAR(tty) == '\021'); 253 254 if ((old_flow != new_flow) || extproc) { 254 - spin_lock_irq(&tty->ctrl.lock); 255 - if (old_flow != new_flow) { 256 - tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP); 257 - if (new_flow) 258 - tty->ctrl.pktstatus |= TIOCPKT_DOSTOP; 259 - else 260 - tty->ctrl.pktstatus |= TIOCPKT_NOSTOP; 255 + scoped_guard(spinlock_irq, &tty->ctrl.lock) { 256 + if (old_flow != new_flow) { 257 + tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP); 258 + if (new_flow) 259 + tty->ctrl.pktstatus |= TIOCPKT_DOSTOP; 260 + else 261 + tty->ctrl.pktstatus |= TIOCPKT_NOSTOP; 262 + } 263 + if (extproc) 264 + tty->ctrl.pktstatus |= TIOCPKT_IOCTL; 261 265 } 262 - if (extproc) 263 - tty->ctrl.pktstatus |= TIOCPKT_IOCTL; 264 - spin_unlock_irq(&tty->ctrl.lock); 265 266 wake_up_interruptible(&tty->link->read_wait); 266 267 } 267 268 } ··· 285 286 struct tty_struct *pty = tty->link; 286 287 287 288 /* For a PTY we need to lock the tty side */ 288 - mutex_lock(&tty->winsize_mutex); 289 + guard(mutex)(&tty->winsize_mutex); 289 290 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 290 - goto done; 291 + return 0; 291 292 292 293 /* Signal the foreground process group of both ptys */ 293 294 pgrp = tty_get_pgrp(tty); ··· 303 304 304 305 tty->winsize = *ws; 305 306 pty->winsize = *ws; /* Never used so will go away soon */ 306 - done: 307 - mutex_unlock(&tty->winsize_mutex); 307 + 308 308 return 0; 309 309 } 310 310 ··· 319 321 */ 320 322 static void pty_start(struct tty_struct *tty) 321 323 { 322 - unsigned long flags; 324 + if (!tty->link || !tty->link->ctrl.packet) 325 + return; 323 326 324 - if (tty->link && tty->link->ctrl.packet) { 325 - spin_lock_irqsave(&tty->ctrl.lock, flags); 327 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) { 326 328 tty->ctrl.pktstatus &= ~TIOCPKT_STOP; 327 329 tty->ctrl.pktstatus |= TIOCPKT_START; 328 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 329 - wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 330 330 } 331 + wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 331 332 } 332 333 333 334 static void pty_stop(struct tty_struct *tty) 334 335 { 335 - unsigned long flags; 336 + if (!tty->link || !tty->link->ctrl.packet) 337 + return; 336 338 337 - if (tty->link && tty->link->ctrl.packet) { 338 - spin_lock_irqsave(&tty->ctrl.lock, flags); 339 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) { 339 340 tty->ctrl.pktstatus &= ~TIOCPKT_START; 340 341 tty->ctrl.pktstatus |= TIOCPKT_STOP; 341 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 342 - wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 343 342 } 343 + wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 344 344 } 345 345 346 346 /** ··· 701 705 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, 702 706 struct file *file, int idx) 703 707 { 704 - struct tty_struct *tty; 705 - 706 - mutex_lock(&devpts_mutex); 707 - tty = devpts_get_priv(file->f_path.dentry); 708 - mutex_unlock(&devpts_mutex); 708 + guard(mutex)(&devpts_mutex); 709 709 /* Master must be open before slave */ 710 - if (!tty) 711 - return ERR_PTR(-EIO); 712 - return tty; 710 + return devpts_get_priv(file->f_path.dentry) ? : ERR_PTR(-EIO); 713 711 } 714 712 715 713 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) ··· 801 811 } 802 812 803 813 /* find a device that is not in use. */ 804 - mutex_lock(&devpts_mutex); 805 - index = devpts_new_index(fsi); 806 - mutex_unlock(&devpts_mutex); 814 + scoped_guard(mutex, &devpts_mutex) 815 + index = devpts_new_index(fsi); 807 816 808 817 retval = index; 809 818 if (index < 0) 810 819 goto out_put_fsi; 811 820 812 821 813 - mutex_lock(&tty_mutex); 814 - tty = tty_init_dev(ptm_driver, index); 815 - /* The tty returned here is locked so we can safely 816 - drop the mutex */ 817 - mutex_unlock(&tty_mutex); 822 + /* The tty returned here is locked so we can safely drop the mutex */ 823 + scoped_guard(mutex, &tty_mutex) 824 + tty = tty_init_dev(ptm_driver, index); 818 825 819 826 retval = PTR_ERR(tty); 820 827 if (IS_ERR(tty))