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

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

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

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
88d65e22 9f8da7b2

+74 -94
+74 -94
drivers/tty/tty_port.c
··· 63 63 64 64 static void tty_port_default_wakeup(struct tty_port *port) 65 65 { 66 - struct tty_struct *tty = tty_port_tty_get(port); 67 - 68 - if (tty) { 69 - tty_wakeup(tty); 70 - tty_kref_put(tty); 71 - } 66 + scoped_guard(tty_port_tty, port) 67 + tty_wakeup(scoped_tty()); 72 68 } 73 69 74 70 const struct tty_port_client_operations tty_port_default_client_ops = { ··· 221 225 int tty_port_alloc_xmit_buf(struct tty_port *port) 222 226 { 223 227 /* We may sleep in get_zeroed_page() */ 224 - mutex_lock(&port->buf_mutex); 225 - if (port->xmit_buf == NULL) { 226 - port->xmit_buf = (u8 *)get_zeroed_page(GFP_KERNEL); 227 - if (port->xmit_buf) 228 - kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE); 229 - } 230 - mutex_unlock(&port->buf_mutex); 228 + guard(mutex)(&port->buf_mutex); 229 + 230 + if (port->xmit_buf) 231 + return 0; 232 + 233 + port->xmit_buf = (u8 *)get_zeroed_page(GFP_KERNEL); 231 234 if (port->xmit_buf == NULL) 232 235 return -ENOMEM; 236 + 237 + kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE); 238 + 233 239 return 0; 234 240 } 235 241 EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 236 242 237 243 void tty_port_free_xmit_buf(struct tty_port *port) 238 244 { 239 - mutex_lock(&port->buf_mutex); 245 + guard(mutex)(&port->buf_mutex); 240 246 free_page((unsigned long)port->xmit_buf); 241 247 port->xmit_buf = NULL; 242 248 INIT_KFIFO(port->xmit_fifo); 243 - mutex_unlock(&port->buf_mutex); 244 249 } 245 250 EXPORT_SYMBOL(tty_port_free_xmit_buf); 246 251 ··· 298 301 */ 299 302 struct tty_struct *tty_port_tty_get(struct tty_port *port) 300 303 { 301 - unsigned long flags; 302 - struct tty_struct *tty; 303 - 304 - spin_lock_irqsave(&port->lock, flags); 305 - tty = tty_kref_get(port->tty); 306 - spin_unlock_irqrestore(&port->lock, flags); 307 - return tty; 304 + guard(spinlock_irqsave)(&port->lock); 305 + return tty_kref_get(port->tty); 308 306 } 309 307 EXPORT_SYMBOL(tty_port_tty_get); 310 308 ··· 313 321 */ 314 322 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 315 323 { 316 - unsigned long flags; 317 - 318 - spin_lock_irqsave(&port->lock, flags); 324 + guard(spinlock_irqsave)(&port->lock); 319 325 tty_kref_put(port->tty); 320 326 port->tty = tty_kref_get(tty); 321 - spin_unlock_irqrestore(&port->lock, flags); 322 327 } 323 328 EXPORT_SYMBOL(tty_port_tty_set); 324 329 ··· 331 342 */ 332 343 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) 333 344 { 334 - mutex_lock(&port->mutex); 345 + guard(mutex)(&port->mutex); 346 + 335 347 if (port->console) 336 - goto out; 348 + return; 337 349 338 - if (tty_port_initialized(port)) { 339 - tty_port_set_initialized(port, false); 340 - /* 341 - * Drop DTR/RTS if HUPCL is set. This causes any attached 342 - * modem to hang up the line. 343 - */ 344 - if (tty && C_HUPCL(tty)) 345 - tty_port_lower_dtr_rts(port); 350 + if (!tty_port_initialized(port)) 351 + return; 346 352 347 - if (port->ops->shutdown) 348 - port->ops->shutdown(port); 349 - } 350 - out: 351 - mutex_unlock(&port->mutex); 353 + tty_port_set_initialized(port, false); 354 + /* 355 + * Drop DTR/RTS if HUPCL is set. This causes any attached 356 + * modem to hang up the line. 357 + */ 358 + if (tty && C_HUPCL(tty)) 359 + tty_port_lower_dtr_rts(port); 360 + 361 + if (port->ops->shutdown) 362 + port->ops->shutdown(port); 352 363 } 353 364 354 365 /** ··· 363 374 void tty_port_hangup(struct tty_port *port) 364 375 { 365 376 struct tty_struct *tty; 366 - unsigned long flags; 367 377 368 - spin_lock_irqsave(&port->lock, flags); 369 - port->count = 0; 370 - tty = port->tty; 371 - if (tty) 372 - set_bit(TTY_IO_ERROR, &tty->flags); 373 - port->tty = NULL; 374 - spin_unlock_irqrestore(&port->lock, flags); 378 + scoped_guard(spinlock_irqsave, &port->lock) { 379 + port->count = 0; 380 + tty = port->tty; 381 + if (tty) 382 + set_bit(TTY_IO_ERROR, &tty->flags); 383 + port->tty = NULL; 384 + } 385 + 375 386 tty_port_set_active(port, false); 376 387 tty_port_shutdown(port, tty); 377 388 tty_kref_put(tty); ··· 382 393 383 394 void __tty_port_tty_hangup(struct tty_port *port, bool check_clocal, bool async) 384 395 { 385 - struct tty_struct *tty = tty_port_tty_get(port); 396 + scoped_guard(tty_port_tty, port) { 397 + struct tty_struct *tty = scoped_tty(); 386 398 387 - if (tty && (!check_clocal || !C_CLOCAL(tty))) { 388 - if (async) 389 - tty_hangup(tty); 390 - else 391 - tty_vhangup(tty); 399 + if (!check_clocal || !C_CLOCAL(tty)) { 400 + if (async) 401 + tty_hangup(tty); 402 + else 403 + tty_vhangup(tty); 404 + } 392 405 } 393 - tty_kref_put(tty); 394 406 } 395 407 EXPORT_SYMBOL_GPL(__tty_port_tty_hangup); 396 408 ··· 480 490 struct tty_struct *tty, struct file *filp) 481 491 { 482 492 int do_clocal = 0, retval; 483 - unsigned long flags; 484 493 DEFINE_WAIT(wait); 485 494 486 495 /* if non-blocking mode is set we can pass directly to open unless ··· 508 519 retval = 0; 509 520 510 521 /* The port lock protects the port counts */ 511 - spin_lock_irqsave(&port->lock, flags); 512 - port->count--; 513 - port->blocked_open++; 514 - spin_unlock_irqrestore(&port->lock, flags); 522 + scoped_guard(spinlock_irqsave, &port->lock) { 523 + port->count--; 524 + port->blocked_open++; 525 + } 515 526 516 527 while (1) { 517 528 /* Indicate we are open */ ··· 550 561 /* Update counts. A parallel hangup will have set count to zero and 551 562 * we must not mess that up further. 552 563 */ 553 - spin_lock_irqsave(&port->lock, flags); 554 - if (!tty_hung_up_p(filp)) 555 - port->count++; 556 - port->blocked_open--; 557 - spin_unlock_irqrestore(&port->lock, flags); 564 + scoped_guard(spinlock_irqsave, &port->lock) { 565 + if (!tty_hung_up_p(filp)) 566 + port->count++; 567 + port->blocked_open--; 568 + } 558 569 if (retval == 0) 559 570 tty_port_set_active(port, true); 560 571 return retval; ··· 593 604 int tty_port_close_start(struct tty_port *port, 594 605 struct tty_struct *tty, struct file *filp) 595 606 { 596 - unsigned long flags; 597 - 598 607 if (tty_hung_up_p(filp)) 599 608 return 0; 600 609 601 - spin_lock_irqsave(&port->lock, flags); 602 - if (tty->count == 1 && port->count != 1) { 603 - tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, 604 - port->count); 605 - port->count = 1; 606 - } 607 - if (--port->count < 0) { 608 - tty_warn(tty, "%s: bad port count (%d)\n", __func__, 609 - port->count); 610 - port->count = 0; 611 - } 610 + scoped_guard(spinlock_irqsave, &port->lock) { 611 + if (tty->count == 1 && port->count != 1) { 612 + tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, 613 + port->count); 614 + port->count = 1; 615 + } 616 + if (--port->count < 0) { 617 + tty_warn(tty, "%s: bad port count (%d)\n", __func__, 618 + port->count); 619 + port->count = 0; 620 + } 612 621 613 - if (port->count) { 614 - spin_unlock_irqrestore(&port->lock, flags); 615 - return 0; 622 + if (port->count) 623 + return 0; 616 624 } 617 - spin_unlock_irqrestore(&port->lock, flags); 618 625 619 626 tty->closing = 1; 620 627 ··· 729 744 int tty_port_open(struct tty_port *port, struct tty_struct *tty, 730 745 struct file *filp) 731 746 { 732 - spin_lock_irq(&port->lock); 733 - ++port->count; 734 - spin_unlock_irq(&port->lock); 747 + scoped_guard(spinlock_irq, &port->lock) 748 + ++port->count; 735 749 tty_port_tty_set(port, tty); 736 750 737 751 /* ··· 739 755 * port mutex. 740 756 */ 741 757 742 - mutex_lock(&port->mutex); 743 - 744 - if (!tty_port_initialized(port)) { 758 + scoped_guard(mutex, &port->mutex) { 759 + if (tty_port_initialized(port)) 760 + break; 745 761 clear_bit(TTY_IO_ERROR, &tty->flags); 746 762 if (port->ops->activate) { 747 763 int retval = port->ops->activate(port, tty); 748 - 749 - if (retval) { 750 - mutex_unlock(&port->mutex); 764 + if (retval) 751 765 return retval; 752 - } 753 766 } 754 767 tty_port_set_initialized(port, true); 755 768 } 756 - mutex_unlock(&port->mutex); 757 769 return tty_port_block_til_ready(port, tty, filp); 758 770 } 759 771 EXPORT_SYMBOL(tty_port_open);