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

tty/vt: use guard()s

Having all the new guards, use them in the vt code. This improves
readability, makes error handling easier, and marks locked portions of
code explicit.

A local free_page_ptr __free guard is introduced for
__get_free_page/free_page (with proper casts). This could be made public
in include/. But I am not sure if there are more possible users, so
keeping completely private here.

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

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
e730c373 2fe16088

+170 -244
+13 -19
drivers/tty/vt/consolemap.c
··· 361 361 inbuf[i] = UNI_DIRECT_BASE | ch; 362 362 } 363 363 364 - console_lock(); 364 + guard(console_lock)(); 365 365 memcpy(translations[USER_MAP], inbuf, sizeof(inbuf)); 366 366 update_user_maps(); 367 - console_unlock(); 367 + 368 368 return 0; 369 369 } 370 370 ··· 374 374 unsigned short *p = translations[USER_MAP]; 375 375 unsigned char outbuf[E_TABSZ]; 376 376 377 - console_lock(); 378 - for (i = 0; i < ARRAY_SIZE(outbuf); i++) 379 - { 380 - ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]); 381 - outbuf[i] = (ch & ~0xff) ? 0 : ch; 382 - } 383 - console_unlock(); 377 + scoped_guard(console_lock) 378 + for (i = 0; i < ARRAY_SIZE(outbuf); i++) { 379 + ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]); 380 + outbuf[i] = (ch & ~0xff) ? 0 : ch; 381 + } 384 382 385 383 return copy_to_user(arg, outbuf, sizeof(outbuf)) ? -EFAULT : 0; 386 384 } ··· 390 392 if (copy_from_user(inbuf, arg, sizeof(inbuf))) 391 393 return -EFAULT; 392 394 393 - console_lock(); 395 + guard(console_lock)(); 394 396 memcpy(translations[USER_MAP], inbuf, sizeof(inbuf)); 395 397 update_user_maps(); 396 - console_unlock(); 398 + 397 399 return 0; 398 400 } 399 401 ··· 401 403 { 402 404 unsigned short outbuf[E_TABSZ]; 403 405 404 - console_lock(); 405 - memcpy(outbuf, translations[USER_MAP], sizeof(outbuf)); 406 - console_unlock(); 406 + scoped_guard(console_lock) 407 + memcpy(outbuf, translations[USER_MAP], sizeof(outbuf)); 407 408 408 409 return copy_to_user(arg, outbuf, sizeof(outbuf)) ? -EFAULT : 0; 409 410 } ··· 568 571 569 572 int con_clear_unimap(struct vc_data *vc) 570 573 { 571 - int ret; 572 - console_lock(); 573 - ret = con_do_clear_unimap(vc); 574 - console_unlock(); 575 - return ret; 574 + guard(console_lock)(); 575 + return con_do_clear_unimap(vc); 576 576 } 577 577 578 578 static struct uni_pagedict *con_unshare_unimap(struct vc_data *vc,
+6 -14
drivers/tty/vt/selection.c
··· 127 127 if (copy_from_user(tmplut, lut, sizeof(inwordLut))) 128 128 return -EFAULT; 129 129 130 - console_lock(); 130 + guard(console_lock)(); 131 131 memcpy(inwordLut, tmplut, sizeof(inwordLut)); 132 - console_unlock(); 133 132 134 133 return 0; 135 134 } ··· 374 375 375 376 int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty) 376 377 { 377 - int ret; 378 - 379 - mutex_lock(&vc_sel.lock); 380 - console_lock(); 381 - ret = vc_selection(vc_cons[fg_console].d, v, tty); 382 - console_unlock(); 383 - mutex_unlock(&vc_sel.lock); 384 - 385 - return ret; 378 + guard(mutex)(&vc_sel.lock); 379 + guard(console_lock)(); 380 + return vc_selection(vc_cons[fg_console].d, v, tty); 386 381 } 387 382 EXPORT_SYMBOL_GPL(set_selection_kernel); 388 383 ··· 402 409 const char *bps = bp ? bracketed_paste_start : NULL; 403 410 const char *bpe = bp ? bracketed_paste_end : NULL; 404 411 405 - console_lock(); 406 - poke_blanked_console(); 407 - console_unlock(); 412 + scoped_guard(console_lock) 413 + poke_blanked_console(); 408 414 409 415 ld = tty_ldisc_ref_wait(tty); 410 416 if (!ld)
+29 -45
drivers/tty/vt/vc_screen.c
··· 53 53 #define HEADER_SIZE 4u 54 54 #define CON_BUF_SIZE (IS_ENABLED(CONFIG_BASE_SMALL) ? 256 : PAGE_SIZE) 55 55 56 + DEFINE_FREE(free_page_ptr, void *, if (_T) free_page((unsigned long)_T)); 57 + 56 58 /* 57 59 * Our minor space: 58 60 * ··· 73 71 #define console(inode) (iminor(inode) & 63) 74 72 #define use_unicode(inode) (iminor(inode) & 64) 75 73 #define use_attributes(inode) (iminor(inode) & 128) 76 - 77 74 78 75 struct vcs_poll_data { 79 76 struct notifier_block notifier; ··· 232 231 struct vc_data *vc; 233 232 int size; 234 233 235 - console_lock(); 236 - vc = vcs_vc(inode, NULL); 237 - if (!vc) { 238 - console_unlock(); 239 - return -ENXIO; 240 - } 234 + scoped_guard(console_lock) { 235 + vc = vcs_vc(inode, NULL); 236 + if (!vc) 237 + return -ENXIO; 241 238 242 - size = vcs_size(vc, use_attributes(inode), use_unicode(inode)); 243 - console_unlock(); 239 + size = vcs_size(vc, use_attributes(inode), use_unicode(inode)); 240 + } 244 241 if (size < 0) 245 242 return size; 246 243 return fixed_size_llseek(file, offset, orig, size); ··· 368 369 struct vcs_poll_data *poll; 369 370 unsigned int read; 370 371 ssize_t ret; 371 - char *con_buf; 372 372 loff_t pos; 373 373 bool viewed, attr, uni_mode; 374 374 375 - con_buf = (char *) __get_free_page(GFP_KERNEL); 375 + char *con_buf __free(free_page_ptr) = (char *)__get_free_page(GFP_KERNEL); 376 376 if (!con_buf) 377 377 return -ENOMEM; 378 378 ··· 380 382 /* Select the proper current console and verify 381 383 * sanity of the situation under the console lock. 382 384 */ 383 - console_lock(); 385 + guard(console_lock)(); 384 386 385 387 uni_mode = use_unicode(inode); 386 388 attr = use_attributes(inode); 387 389 388 - ret = -EINVAL; 389 390 if (pos < 0) 390 - goto unlock_out; 391 + return -EINVAL; 391 392 /* we enforce 32-bit alignment for pos and count in unicode mode */ 392 393 if (uni_mode && (pos | count) & 3) 393 - goto unlock_out; 394 + return -EINVAL; 394 395 395 396 poll = file->private_data; 396 397 if (count && poll) ··· 465 468 } 466 469 *ppos += read; 467 470 if (read) 468 - ret = read; 469 - unlock_out: 470 - console_unlock(); 471 - free_page((unsigned long) con_buf); 471 + return read; 472 + 472 473 return ret; 473 474 } 474 475 ··· 586 591 { 587 592 struct inode *inode = file_inode(file); 588 593 struct vc_data *vc; 589 - char *con_buf; 590 594 u16 *org0, *org; 591 595 unsigned int written; 592 596 int size; ··· 596 602 if (use_unicode(inode)) 597 603 return -EOPNOTSUPP; 598 604 599 - con_buf = (char *) __get_free_page(GFP_KERNEL); 605 + char *con_buf __free(free_page_ptr) = (char *)__get_free_page(GFP_KERNEL); 600 606 if (!con_buf) 601 607 return -ENOMEM; 602 608 ··· 605 611 /* Select the proper current console and verify 606 612 * sanity of the situation under the console lock. 607 613 */ 608 - console_lock(); 614 + guard(console_lock)(); 609 615 610 616 attr = use_attributes(inode); 611 - ret = -ENXIO; 612 617 vc = vcs_vc(inode, &viewed); 613 618 if (!vc) 614 - goto unlock_out; 619 + return -ENXIO; 615 620 616 621 size = vcs_size(vc, attr, false); 617 - if (size < 0) { 618 - ret = size; 619 - goto unlock_out; 620 - } 621 - ret = -EINVAL; 622 + if (size < 0) 623 + return size; 622 624 if (pos < 0 || pos > size) 623 - goto unlock_out; 625 + return -EINVAL; 624 626 if (count > size - pos) 625 627 count = size - pos; 626 628 written = 0; ··· 641 651 */ 642 652 if (written) 643 653 break; 644 - ret = -EFAULT; 645 - goto unlock_out; 654 + return -EFAULT; 646 655 } 647 656 } 648 657 ··· 653 664 if (!vc) { 654 665 if (written) 655 666 break; 656 - ret = -ENXIO; 657 - goto unlock_out; 667 + return -ENXIO; 658 668 } 659 669 size = vcs_size(vc, attr, false); 660 670 if (size < 0) { 661 671 if (written) 662 672 break; 663 - ret = size; 664 - goto unlock_out; 673 + return size; 665 674 } 666 675 if (pos >= size) 667 676 break; ··· 689 702 if (written) 690 703 vcs_scr_updated(vc); 691 704 692 - unlock_out: 693 - console_unlock(); 694 - free_page((unsigned long) con_buf); 695 705 return ret; 696 706 } 697 707 ··· 738 754 unsigned int currcons = console(inode); 739 755 bool attr = use_attributes(inode); 740 756 bool uni_mode = use_unicode(inode); 741 - int ret = 0; 742 757 743 758 /* we currently don't support attributes in unicode mode */ 744 759 if (attr && uni_mode) 745 760 return -EOPNOTSUPP; 746 761 747 - console_lock(); 748 - if(currcons && !vc_cons_allocated(currcons-1)) 749 - ret = -ENXIO; 750 - console_unlock(); 751 - return ret; 762 + guard(console_lock)(); 763 + 764 + if (currcons && !vc_cons_allocated(currcons - 1)) 765 + return -ENXIO; 766 + 767 + return 0; 752 768 } 753 769 754 770 static int vcs_release(struct inode *inode, struct file *file)
+38 -60
drivers/tty/vt/vt.c
··· 1317 1317 static int vt_resize(struct tty_struct *tty, struct winsize *ws) 1318 1318 { 1319 1319 struct vc_data *vc = tty->driver_data; 1320 - int ret; 1321 1320 1322 - console_lock(); 1323 - ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row, false); 1324 - console_unlock(); 1325 - return ret; 1321 + guard(console_lock)(); 1322 + return vc_do_resize(tty, vc, ws->ws_col, ws->ws_row, false); 1326 1323 } 1327 1324 1328 1325 struct vc_data *vc_deallocate(unsigned int currcons) ··· 3132 3135 if (in_interrupt()) 3133 3136 return count; 3134 3137 3135 - console_lock(); 3138 + guard(console_lock)(); 3136 3139 currcons = vc->vc_num; 3137 3140 if (!vc_cons_allocated(currcons)) { 3138 3141 /* could this happen? */ 3139 3142 pr_warn_once("con_write: tty %d not allocated\n", currcons+1); 3140 - console_unlock(); 3141 3143 return 0; 3142 3144 } 3143 3145 ··· 3180 3184 con_flush(vc, &draw); 3181 3185 console_conditional_schedule(); 3182 3186 notify_update(vc); 3183 - console_unlock(); 3187 + 3184 3188 return n; 3185 3189 } 3186 3190 ··· 3195 3199 */ 3196 3200 static void console_callback(struct work_struct *ignored) 3197 3201 { 3198 - console_lock(); 3202 + guard(console_lock)(); 3199 3203 3200 3204 if (want_console >= 0) { 3201 3205 if (want_console != fg_console && ··· 3224 3228 blank_timer_expired = 0; 3225 3229 } 3226 3230 notify_update(vc_cons[fg_console].d); 3227 - 3228 - console_unlock(); 3229 3231 } 3230 3232 3231 3233 int set_console(int nr) ··· 3427 3433 return -EPERM; 3428 3434 return paste_selection(tty); 3429 3435 case TIOCL_UNBLANKSCREEN: 3430 - console_lock(); 3431 - unblank_screen(); 3432 - console_unlock(); 3436 + scoped_guard(console_lock) 3437 + unblank_screen(); 3433 3438 break; 3434 3439 case TIOCL_SELLOADLUT: 3435 3440 if (!capable(CAP_SYS_ADMIN)) ··· 3444 3451 data = vt_get_shift_state(); 3445 3452 return put_user(data, p); 3446 3453 case TIOCL_GETMOUSEREPORTING: 3447 - console_lock(); /* May be overkill */ 3448 - data = mouse_reporting(); 3449 - console_unlock(); 3454 + scoped_guard(console_lock) /* May be overkill */ 3455 + data = mouse_reporting(); 3450 3456 return put_user(data, p); 3451 3457 case TIOCL_SETVESABLANK: 3452 3458 return set_vesa_blanking(param); ··· 3476 3484 * Needs the console lock here. Note that lots of other calls 3477 3485 * need fixing before the lock is actually useful! 3478 3486 */ 3479 - console_lock(); 3480 - scrollfront(vc_cons[fg_console].d, lines); 3481 - console_unlock(); 3487 + scoped_guard(console_lock) 3488 + scrollfront(vc_cons[fg_console].d, lines); 3482 3489 break; 3483 3490 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ 3484 - console_lock(); 3485 - ignore_poke = 1; 3486 - do_blank_screen(0); 3487 - console_unlock(); 3491 + scoped_guard(console_lock) { 3492 + ignore_poke = 1; 3493 + do_blank_screen(0); 3494 + } 3488 3495 break; 3489 3496 case TIOCL_BLANKEDSCREEN: 3490 3497 return console_blanked; ··· 3573 3582 if (in_interrupt()) /* from flush_to_ldisc */ 3574 3583 return; 3575 3584 3576 - console_lock(); 3585 + guard(console_lock)(); 3577 3586 set_cursor(vc); 3578 - console_unlock(); 3579 3587 } 3580 3588 3581 3589 /* ··· 3586 3596 struct vc_data *vc; 3587 3597 int ret; 3588 3598 3589 - console_lock(); 3599 + guard(console_lock)(); 3590 3600 ret = vc_allocate(currcons); 3591 3601 if (ret) 3592 - goto unlock; 3602 + return ret; 3593 3603 3594 3604 vc = vc_cons[currcons].d; 3595 3605 3596 3606 /* Still being freed */ 3597 - if (vc->port.tty) { 3598 - ret = -ERESTARTSYS; 3599 - goto unlock; 3600 - } 3607 + if (vc->port.tty) 3608 + return -ERESTARTSYS; 3601 3609 3602 3610 ret = tty_port_install(&vc->port, driver, tty); 3603 3611 if (ret) 3604 - goto unlock; 3612 + return ret; 3605 3613 3606 3614 tty->driver_data = vc; 3607 3615 vc->port.tty = tty; ··· 3613 3625 tty->termios.c_iflag |= IUTF8; 3614 3626 else 3615 3627 tty->termios.c_iflag &= ~IUTF8; 3616 - unlock: 3617 - console_unlock(); 3618 - return ret; 3628 + 3629 + return 0; 3619 3630 } 3620 3631 3621 3632 static int con_open(struct tty_struct *tty, struct file *filp) ··· 3633 3646 { 3634 3647 struct vc_data *vc = tty->driver_data; 3635 3648 BUG_ON(vc == NULL); 3636 - console_lock(); 3649 + 3650 + guard(console_lock)(); 3637 3651 vc->port.tty = NULL; 3638 - console_unlock(); 3639 3652 } 3640 3653 3641 3654 static void con_cleanup(struct tty_struct *tty) ··· 4124 4137 struct con_driver *con = dev_get_drvdata(dev); 4125 4138 int bind = simple_strtoul(buf, NULL, 0); 4126 4139 4127 - console_lock(); 4140 + guard(console_lock)(); 4128 4141 4129 4142 if (bind) 4130 4143 vt_bind(con); 4131 4144 else 4132 4145 vt_unbind(con); 4133 - 4134 - console_unlock(); 4135 4146 4136 4147 return count; 4137 4148 } ··· 4140 4155 struct con_driver *con = dev_get_drvdata(dev); 4141 4156 int bind; 4142 4157 4143 - console_lock(); 4144 - bind = con_is_bound(con->con); 4145 - console_unlock(); 4158 + scoped_guard(console_lock) 4159 + bind = con_is_bound(con->con); 4146 4160 4147 4161 return sysfs_emit(buf, "%i\n", bind); 4148 4162 } ··· 4413 4429 { 4414 4430 int i; 4415 4431 4416 - console_lock(); 4432 + guard(console_lock)(); 4417 4433 4418 4434 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4419 4435 struct con_driver *con_driver = &registered_con_driver[i]; ··· 4438 4454 con_driver->first = 0; 4439 4455 con_driver->last = 0; 4440 4456 } 4441 - 4442 - console_unlock(); 4443 4457 } 4444 4458 4445 4459 /* ··· 4473 4491 */ 4474 4492 void give_up_console(const struct consw *csw) 4475 4493 { 4476 - console_lock(); 4494 + guard(console_lock)(); 4477 4495 do_unregister_con_driver(csw); 4478 - console_unlock(); 4479 4496 } 4480 4497 EXPORT_SYMBOL(give_up_console); 4481 4498 ··· 4522 4541 if (get_user(mode, mode_user)) 4523 4542 return -EFAULT; 4524 4543 4525 - console_lock(); 4544 + guard(console_lock)(); 4526 4545 vesa_blank_mode = (mode <= VESA_BLANK_MAX) ? mode : VESA_NO_BLANKING; 4527 - console_unlock(); 4528 4546 4529 4547 return 0; 4530 4548 } ··· 4709 4729 if (copy_from_user(colormap, arg, sizeof(colormap))) 4710 4730 return -EFAULT; 4711 4731 4712 - console_lock(); 4732 + guard(console_lock)(); 4713 4733 for (i = k = 0; i < 16; i++) { 4714 4734 default_red[i] = colormap[k++]; 4715 4735 default_grn[i] = colormap[k++]; ··· 4725 4745 } 4726 4746 set_palette(vc_cons[i].d); 4727 4747 } 4728 - console_unlock(); 4729 4748 4730 4749 return 0; 4731 4750 } ··· 4734 4755 int i, k; 4735 4756 unsigned char colormap[3*16]; 4736 4757 4737 - console_lock(); 4738 - for (i = k = 0; i < 16; i++) { 4739 - colormap[k++] = default_red[i]; 4740 - colormap[k++] = default_grn[i]; 4741 - colormap[k++] = default_blu[i]; 4742 - } 4743 - console_unlock(); 4758 + scoped_guard(console_lock) 4759 + for (i = k = 0; i < 16; i++) { 4760 + colormap[k++] = default_red[i]; 4761 + colormap[k++] = default_grn[i]; 4762 + colormap[k++] = default_blu[i]; 4763 + } 4744 4764 4745 4765 if (copy_to_user(arg, colormap, sizeof(colormap))) 4746 4766 return -EFAULT;
+84 -106
drivers/tty/vt/vt_ioctl.c
··· 373 373 break; 374 374 } 375 375 376 - case KDSETMODE: 376 + case KDSETMODE: { 377 377 if (!perm) 378 378 return -EPERM; 379 379 380 - console_lock(); 381 - ret = vt_kdsetmode(vc, arg); 382 - console_unlock(); 383 - return ret; 384 - 380 + guard(console_lock)(); 381 + return vt_kdsetmode(vc, arg); 382 + } 385 383 case KDGETMODE: 386 384 return put_user(vc->vc_mode, (int __user *)arg); 387 385 ··· 599 601 600 602 vsa.console--; 601 603 vsa.console = array_index_nospec(vsa.console, MAX_NR_CONSOLES); 602 - console_lock(); 603 - ret = vc_allocate(vsa.console); 604 - if (ret) { 605 - console_unlock(); 606 - return ret; 607 - } 604 + scoped_guard(console_lock) { 605 + ret = vc_allocate(vsa.console); 606 + if (ret) 607 + return ret; 608 608 609 - /* 610 - * This is safe providing we don't drop the console sem between 611 - * vc_allocate and finishing referencing nvc. 612 - */ 613 - nvc = vc_cons[vsa.console].d; 614 - nvc->vt_mode = vsa.mode; 615 - nvc->vt_mode.frsig = 0; 616 - put_pid(nvc->vt_pid); 617 - nvc->vt_pid = get_pid(task_pid(current)); 618 - console_unlock(); 609 + /* 610 + * This is safe providing we don't drop the console sem between 611 + * vc_allocate and finishing referencing nvc. 612 + */ 613 + nvc = vc_cons[vsa.console].d; 614 + nvc->vt_mode = vsa.mode; 615 + nvc->vt_mode.frsig = 0; 616 + put_pid(nvc->vt_pid); 617 + nvc->vt_pid = get_pid(task_pid(current)); 618 + } 619 619 620 620 /* Commence switch and lock */ 621 621 /* Review set_console locks */ ··· 626 630 static int vt_disallocate(unsigned int vc_num) 627 631 { 628 632 struct vc_data *vc = NULL; 629 - int ret = 0; 630 633 631 - console_lock(); 632 - if (vt_busy(vc_num)) 633 - ret = -EBUSY; 634 - else if (vc_num) 635 - vc = vc_deallocate(vc_num); 636 - console_unlock(); 634 + scoped_guard(console_lock) { 635 + if (vt_busy(vc_num)) 636 + return -EBUSY; 637 + if (vc_num) 638 + vc = vc_deallocate(vc_num); 639 + } 637 640 638 641 if (vc && vc_num >= MIN_NR_CONSOLES) 639 642 tty_port_put(&vc->port); 640 643 641 - return ret; 644 + return 0; 642 645 } 643 646 644 647 /* deallocate all unused consoles, but leave 0 */ ··· 646 651 struct vc_data *vc[MAX_NR_CONSOLES]; 647 652 int i; 648 653 649 - console_lock(); 650 - for (i = 1; i < MAX_NR_CONSOLES; i++) 651 - if (!vt_busy(i)) 652 - vc[i] = vc_deallocate(i); 653 - else 654 - vc[i] = NULL; 655 - console_unlock(); 654 + scoped_guard(console_lock) 655 + for (i = 1; i < MAX_NR_CONSOLES; i++) 656 + if (!vt_busy(i)) 657 + vc[i] = vc_deallocate(i); 658 + else 659 + vc[i] = NULL; 656 660 657 661 for (i = 1; i < MAX_NR_CONSOLES; i++) { 658 662 if (vc[i] && i >= MIN_NR_CONSOLES) ··· 697 703 698 704 if (!vc_cons[i].d) 699 705 continue; 700 - console_lock(); 706 + guard(console_lock)(); 701 707 vcp = vc_cons[i].d; 702 708 if (vcp) { 703 709 int ret; ··· 712 718 if (ret) { 713 719 vcp->vc_scan_lines = save_scan_lines; 714 720 vcp->vc_cell_height = save_cell_height; 715 - console_unlock(); 716 721 return ret; 717 722 } 718 723 } 719 - console_unlock(); 720 724 } 721 725 722 726 return 0; ··· 762 770 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) 763 771 return -EINVAL; 764 772 765 - console_lock(); 773 + guard(console_lock)(); 766 774 vc->vt_mode = tmp; 767 775 /* the frsig is ignored, so we set it to 0 */ 768 776 vc->vt_mode.frsig = 0; ··· 770 778 vc->vt_pid = get_pid(task_pid(current)); 771 779 /* no switch is required -- saw@shade.msu.ru */ 772 780 vc->vt_newvt = -1; 773 - console_unlock(); 774 781 break; 775 782 } 776 783 ··· 778 787 struct vt_mode tmp; 779 788 int rc; 780 789 781 - console_lock(); 782 - memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); 783 - console_unlock(); 790 + scoped_guard(console_lock) 791 + memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); 784 792 785 793 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode)); 786 794 if (rc) ··· 801 811 return -EFAULT; 802 812 803 813 state = 1; /* /dev/tty0 is always open */ 804 - console_lock(); /* required by vt_in_use() */ 805 - for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; 806 - ++i, mask <<= 1) 807 - if (vt_in_use(i)) 808 - state |= mask; 809 - console_unlock(); 814 + scoped_guard(console_lock) /* required by vt_in_use() */ 815 + for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1) 816 + if (vt_in_use(i)) 817 + state |= mask; 810 818 return put_user(state, &vtstat->v_state); 811 819 } 812 820 ··· 812 824 * Returns the first available (non-opened) console. 813 825 */ 814 826 case VT_OPENQRY: 815 - console_lock(); /* required by vt_in_use() */ 816 - for (i = 0; i < MAX_NR_CONSOLES; ++i) 817 - if (!vt_in_use(i)) 818 - break; 819 - console_unlock(); 827 + scoped_guard(console_lock) /* required by vt_in_use() */ 828 + for (i = 0; i < MAX_NR_CONSOLES; ++i) 829 + if (!vt_in_use(i)) 830 + break; 820 831 i = i < MAX_NR_CONSOLES ? (i+1) : -1; 821 832 return put_user(i, (int __user *)arg); 822 833 ··· 832 845 833 846 arg--; 834 847 arg = array_index_nospec(arg, MAX_NR_CONSOLES); 835 - console_lock(); 836 - ret = vc_allocate(arg); 837 - console_unlock(); 838 - if (ret) 839 - return ret; 848 + scoped_guard(console_lock) { 849 + ret = vc_allocate(arg); 850 + if (ret) 851 + return ret; 852 + } 840 853 set_console(arg); 841 854 break; 842 855 ··· 867 880 * 2: completed switch-to OK 868 881 */ 869 882 case VT_RELDISP: 883 + { 870 884 if (!perm) 871 885 return -EPERM; 872 886 873 - console_lock(); 874 - ret = vt_reldisp(vc, arg); 875 - console_unlock(); 876 - 877 - return ret; 878 - 887 + guard(console_lock)(); 888 + return vt_reldisp(vc, arg); 889 + } 879 890 880 891 /* 881 892 * Disallocate memory associated to VT (but leave VT1) ··· 902 917 get_user(cc, &vtsizes->v_cols)) 903 918 return -EFAULT; 904 919 905 - console_lock(); 920 + guard(console_lock)(); 906 921 for (i = 0; i < MAX_NR_CONSOLES; i++) { 907 922 vc = vc_cons[i].d; 908 923 ··· 911 926 __vc_resize(vc_cons[i].d, cc, ll, true); 912 927 } 913 928 } 914 - console_unlock(); 915 929 break; 916 930 } 917 931 ··· 980 996 struct vc_data *vc; 981 997 struct tty_struct *tty; 982 998 983 - console_lock(); 999 + guard(console_lock)(); 984 1000 vc = vc_con->d; 985 - if (vc) { 986 - /* FIXME: review tty ref counting */ 987 - tty = vc->port.tty; 988 - /* 989 - * SAK should also work in all raw modes and reset 990 - * them properly. 991 - */ 992 - if (tty) 993 - __do_SAK(tty); 994 - reset_vc(vc); 995 - } 996 - console_unlock(); 1001 + if (!vc) 1002 + return; 1003 + 1004 + /* FIXME: review tty ref counting */ 1005 + tty = vc->port.tty; 1006 + /* SAK should also work in all raw modes and reset them properly. */ 1007 + if (tty) 1008 + __do_SAK(tty); 1009 + reset_vc(vc); 997 1010 } 998 1011 999 1012 #ifdef CONFIG_COMPAT ··· 1268 1287 { 1269 1288 int prev; 1270 1289 1271 - console_lock(); 1272 - /* Graphics mode - up to X */ 1273 - if (disable_vt_switch) { 1274 - console_unlock(); 1275 - return 0; 1276 - } 1277 - prev = fg_console; 1290 + scoped_guard(console_lock) { 1291 + /* Graphics mode - up to X */ 1292 + if (disable_vt_switch) 1293 + return 0; 1278 1294 1279 - if (alloc && vc_allocate(vt)) { 1280 - /* we can't have a free VC for now. Too bad, 1281 - * we don't want to mess the screen for now. */ 1282 - console_unlock(); 1283 - return -ENOSPC; 1284 - } 1295 + prev = fg_console; 1285 1296 1286 - if (set_console(vt)) { 1287 - /* 1288 - * We're unable to switch to the SUSPEND_CONSOLE. 1289 - * Let the calling function know so it can decide 1290 - * what to do. 1291 - */ 1292 - console_unlock(); 1293 - return -EIO; 1297 + if (alloc && vc_allocate(vt)) { 1298 + /* 1299 + * We can't have a free VC for now. Too bad, we don't want to mess the 1300 + * screen for now. 1301 + */ 1302 + return -ENOSPC; 1303 + } 1304 + 1305 + if (set_console(vt)) { 1306 + /* 1307 + * We're unable to switch to the SUSPEND_CONSOLE. Let the calling function 1308 + * know so it can decide what to do. 1309 + */ 1310 + return -EIO; 1311 + } 1294 1312 } 1295 - console_unlock(); 1296 1313 if (vt_waitactive(vt + 1)) { 1297 1314 pr_debug("Suspend: Can't switch VCs."); 1298 1315 return -EINTR; ··· 1307 1328 */ 1308 1329 void pm_set_vt_switch(int do_switch) 1309 1330 { 1310 - console_lock(); 1331 + guard(console_lock)(); 1311 1332 disable_vt_switch = !do_switch; 1312 - console_unlock(); 1313 1333 } 1314 1334 EXPORT_SYMBOL(pm_set_vt_switch);