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

Use guards in the n_hdlc 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-5-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
8c03bfcf 3ae99599

+26 -41
+26 -41
drivers/tty/n_hdlc.c
··· 263 263 */ 264 264 static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) 265 265 { 266 - unsigned long flags; 267 266 struct n_hdlc_buf *tbuf; 268 267 ssize_t actual; 269 268 270 269 check_again: 271 - 272 - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); 273 - if (n_hdlc->tbusy) { 274 - n_hdlc->woke_up = true; 275 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 276 - return; 270 + scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) { 271 + if (n_hdlc->tbusy) { 272 + n_hdlc->woke_up = true; 273 + return; 274 + } 275 + n_hdlc->tbusy = true; 276 + n_hdlc->woke_up = false; 277 277 } 278 - n_hdlc->tbusy = true; 279 - n_hdlc->woke_up = false; 280 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 281 278 282 279 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); 283 280 while (tbuf) { ··· 321 324 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 322 325 323 326 /* Clear the re-entry flag */ 324 - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); 325 - n_hdlc->tbusy = false; 326 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 327 + scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) 328 + n_hdlc->tbusy = false; 327 329 328 330 if (n_hdlc->woke_up) 329 331 goto check_again; ··· 581 585 { 582 586 struct n_hdlc *n_hdlc = tty->disc_data; 583 587 int count; 584 - unsigned long flags; 585 588 struct n_hdlc_buf *buf = NULL; 586 589 587 590 pr_debug("%s() called %d\n", __func__, cmd); ··· 589 594 case FIONREAD: 590 595 /* report count of read data available */ 591 596 /* in next available frame (if any) */ 592 - spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags); 593 - buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, 594 - struct n_hdlc_buf, list_item); 595 - if (buf) 596 - count = buf->count; 597 - else 598 - count = 0; 599 - spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags); 597 + scoped_guard(spinlock_irqsave, &n_hdlc->rx_buf_list.spinlock) { 598 + buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, 599 + struct n_hdlc_buf, list_item); 600 + if (buf) 601 + count = buf->count; 602 + else 603 + count = 0; 604 + } 600 605 return put_user(count, (int __user *)arg); 601 606 602 607 case TIOCOUTQ: 603 608 /* get the pending tx byte count in the driver */ 604 609 count = tty_chars_in_buffer(tty); 605 610 /* add size of next output frame in queue */ 606 - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); 607 - buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, 608 - struct n_hdlc_buf, list_item); 609 - if (buf) 610 - count += buf->count; 611 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 611 + scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) { 612 + buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, 613 + struct n_hdlc_buf, list_item); 614 + if (buf) 615 + count += buf->count; 616 + } 612 617 return put_user(count, (int __user *)arg); 613 618 614 619 case TCFLSH: ··· 715 720 static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list, 716 721 struct n_hdlc_buf *buf) 717 722 { 718 - unsigned long flags; 719 - 720 - spin_lock_irqsave(&buf_list->spinlock, flags); 723 + guard(spinlock_irqsave)(&buf_list->spinlock); 721 724 722 725 list_add(&buf->list_item, &buf_list->list); 723 726 buf_list->count++; 724 - 725 - spin_unlock_irqrestore(&buf_list->spinlock, flags); 726 727 } 727 728 728 729 /** ··· 729 738 static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list, 730 739 struct n_hdlc_buf *buf) 731 740 { 732 - unsigned long flags; 733 - 734 - spin_lock_irqsave(&buf_list->spinlock, flags); 741 + guard(spinlock_irqsave)(&buf_list->spinlock); 735 742 736 743 list_add_tail(&buf->list_item, &buf_list->list); 737 744 buf_list->count++; 738 - 739 - spin_unlock_irqrestore(&buf_list->spinlock, flags); 740 745 } /* end of n_hdlc_buf_put() */ 741 746 742 747 /** ··· 745 758 */ 746 759 static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list) 747 760 { 748 - unsigned long flags; 749 761 struct n_hdlc_buf *buf; 750 762 751 - spin_lock_irqsave(&buf_list->spinlock, flags); 763 + guard(spinlock_irqsave)(&buf_list->spinlock); 752 764 753 765 buf = list_first_entry_or_null(&buf_list->list, 754 766 struct n_hdlc_buf, list_item); ··· 756 770 buf_list->count--; 757 771 } 758 772 759 - spin_unlock_irqrestore(&buf_list->spinlock, flags); 760 773 return buf; 761 774 } /* end of n_hdlc_buf_get() */ 762 775