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: use lock guard()s in tty_io

guard()s and scoped_guard()s express more clearly what is protected by
locks. And also makes the code cleaner as it can return immediately in
case of short returns.

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20250425111315.1036184-3-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
f49573f2 3eabc1a3

+40 -56
+40 -56
drivers/tty/tty_io.c
··· 276 276 struct list_head *p; 277 277 int count = 0, kopen_count = 0; 278 278 279 - spin_lock(&tty->files_lock); 280 - list_for_each(p, &tty->tty_files) { 281 - count++; 282 - } 283 - spin_unlock(&tty->files_lock); 279 + scoped_guard(spinlock, &tty->files_lock) 280 + list_for_each(p, &tty->tty_files) 281 + count++; 282 + 284 283 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 285 284 tty->driver->subtype == PTY_TYPE_SLAVE && 286 285 tty->link && tty->link->count) ··· 377 378 */ 378 379 struct tty_driver *tty_find_polling_driver(char *name, int *line) 379 380 { 380 - struct tty_driver *p, *res = NULL; 381 + struct tty_driver *p; 381 382 int tty_line = 0; 382 383 int len; 383 384 char *str, *stp; ··· 391 392 len = str - name; 392 393 tty_line = simple_strtoul(str, &str, 10); 393 394 394 - mutex_lock(&tty_mutex); 395 + guard(mutex)(&tty_mutex); 396 + 395 397 /* Search through the tty devices to look for a match */ 396 398 list_for_each_entry(p, &tty_drivers, tty_drivers) { 397 399 if (!len || strncmp(name, p->name, len) != 0) ··· 405 405 406 406 if (tty_line >= 0 && tty_line < p->num && p->ops && 407 407 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { 408 - res = tty_driver_kref_get(p); 409 408 *line = tty_line; 410 - break; 409 + return tty_driver_kref_get(p); 411 410 } 412 411 } 413 - mutex_unlock(&tty_mutex); 414 412 415 - return res; 413 + return NULL; 416 414 } 417 415 EXPORT_SYMBOL_GPL(tty_find_polling_driver); 418 416 #endif ··· 529 531 */ 530 532 static struct file *tty_release_redirect(struct tty_struct *tty) 531 533 { 532 - struct file *f = NULL; 534 + guard(spinlock)(&redirect_lock); 533 535 534 - spin_lock(&redirect_lock); 535 536 if (redirect && file_tty(redirect) == tty) { 536 - f = redirect; 537 + struct file *f = redirect; 537 538 redirect = NULL; 539 + return f; 538 540 } 539 - spin_unlock(&redirect_lock); 540 541 541 - return f; 542 + return NULL; 542 543 } 543 544 544 545 /** ··· 762 765 */ 763 766 void stop_tty(struct tty_struct *tty) 764 767 { 765 - unsigned long flags; 766 - 767 - spin_lock_irqsave(&tty->flow.lock, flags); 768 + guard(spinlock_irqsave)(&tty->flow.lock); 768 769 __stop_tty(tty); 769 - spin_unlock_irqrestore(&tty->flow.lock, flags); 770 770 } 771 771 EXPORT_SYMBOL(stop_tty); 772 772 ··· 790 796 */ 791 797 void start_tty(struct tty_struct *tty) 792 798 { 793 - unsigned long flags; 794 - 795 - spin_lock_irqsave(&tty->flow.lock, flags); 799 + guard(spinlock_irqsave)(&tty->flow.lock); 796 800 __start_tty(tty); 797 - spin_unlock_irqrestore(&tty->flow.lock, flags); 798 801 } 799 802 EXPORT_SYMBOL(start_tty); 800 803 ··· 800 809 time64_t sec = ktime_get_real_seconds(); 801 810 struct tty_file_private *priv; 802 811 803 - spin_lock(&tty->files_lock); 812 + guard(spinlock)(&tty->files_lock); 813 + 804 814 list_for_each_entry(priv, &tty->tty_files, list) { 805 815 struct inode *inode = file_inode(priv->file); 806 816 struct timespec64 time = mtime ? inode_get_mtime(inode) : inode_get_atime(inode); ··· 819 827 inode_set_atime(inode, sec, 0); 820 828 } 821 829 } 822 - spin_unlock(&tty->files_lock); 823 830 } 824 831 825 832 /* ··· 2305 2314 */ 2306 2315 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) 2307 2316 { 2308 - int err; 2317 + guard(mutex)(&tty->winsize_mutex); 2309 2318 2310 - mutex_lock(&tty->winsize_mutex); 2311 - err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2312 - mutex_unlock(&tty->winsize_mutex); 2319 + if (copy_to_user(arg, &tty->winsize, sizeof(*arg))) 2320 + return -EFAULT; 2313 2321 2314 - return err ? -EFAULT : 0; 2322 + return 0; 2315 2323 } 2316 2324 2317 2325 /** ··· 2325 2335 { 2326 2336 struct pid *pgrp; 2327 2337 2328 - /* Lock the tty */ 2329 - mutex_lock(&tty->winsize_mutex); 2338 + guard(mutex)(&tty->winsize_mutex); 2339 + 2330 2340 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 2331 - goto done; 2341 + return 0; 2332 2342 2333 2343 /* Signal the foreground process group */ 2334 2344 pgrp = tty_get_pgrp(tty); ··· 2337 2347 put_pid(pgrp); 2338 2348 2339 2349 tty->winsize = *ws; 2340 - done: 2341 - mutex_unlock(&tty->winsize_mutex); 2350 + 2342 2351 return 0; 2343 2352 } 2344 2353 EXPORT_SYMBOL(tty_do_resize); ··· 2398 2409 return -EBADF; 2399 2410 if (!(file->f_mode & FMODE_CAN_WRITE)) 2400 2411 return -EINVAL; 2401 - spin_lock(&redirect_lock); 2402 - if (redirect) { 2403 - spin_unlock(&redirect_lock); 2412 + 2413 + guard(spinlock)(&redirect_lock); 2414 + 2415 + if (redirect) 2404 2416 return -EBUSY; 2405 - } 2417 + 2406 2418 redirect = get_file(file); 2407 - spin_unlock(&redirect_lock); 2419 + 2408 2420 return 0; 2409 2421 } 2410 2422 ··· 3018 3028 struct task_struct *g, *p; 3019 3029 struct pid *session; 3020 3030 int i; 3021 - unsigned long flags; 3022 3031 3023 - spin_lock_irqsave(&tty->ctrl.lock, flags); 3024 - session = get_pid(tty->ctrl.session); 3025 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 3032 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) 3033 + session = get_pid(tty->ctrl.session); 3026 3034 3027 3035 tty_ldisc_flush(tty); 3028 3036 ··· 3043 3055 PIDTYPE_SID); 3044 3056 continue; 3045 3057 } 3046 - task_lock(p); 3058 + guard(task_lock)(p); 3047 3059 i = iterate_fd(p->files, 0, this_tty, tty); 3048 3060 if (i != 0) { 3049 3061 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", ··· 3051 3063 group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, 3052 3064 PIDTYPE_SID); 3053 3065 } 3054 - task_unlock(p); 3055 3066 } 3056 3067 read_unlock(&tasklist_lock); 3057 3068 put_pid(session); ··· 3452 3465 goto err_unreg_char; 3453 3466 } 3454 3467 3455 - mutex_lock(&tty_mutex); 3456 - list_add(&driver->tty_drivers, &tty_drivers); 3457 - mutex_unlock(&tty_mutex); 3468 + scoped_guard(mutex, &tty_mutex) 3469 + list_add(&driver->tty_drivers, &tty_drivers); 3458 3470 3459 3471 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { 3460 3472 for (i = 0; i < driver->num; i++) { ··· 3472 3486 for (i--; i >= 0; i--) 3473 3487 tty_unregister_device(driver, i); 3474 3488 3475 - mutex_lock(&tty_mutex); 3476 - list_del(&driver->tty_drivers); 3477 - mutex_unlock(&tty_mutex); 3489 + scoped_guard(mutex, &tty_mutex) 3490 + list_del(&driver->tty_drivers); 3478 3491 3479 3492 err_unreg_char: 3480 3493 unregister_chrdev_region(dev, driver->num); ··· 3492 3507 { 3493 3508 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 3494 3509 driver->num); 3495 - mutex_lock(&tty_mutex); 3496 - list_del(&driver->tty_drivers); 3497 - mutex_unlock(&tty_mutex); 3510 + scoped_guard(mutex, &tty_mutex) 3511 + list_del(&driver->tty_drivers); 3498 3512 } 3499 3513 EXPORT_SYMBOL(tty_unregister_driver); 3500 3514