Skip to content

Commit 8c03bfc

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
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>
1 parent 3ae9959 commit 8c03bfc

1 file changed

Lines changed: 26 additions & 41 deletions

File tree

drivers/tty/n_hdlc.c

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,18 @@ static int n_hdlc_tty_open(struct tty_struct *tty)
263263
*/
264264
static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
265265
{
266-
unsigned long flags;
267266
struct n_hdlc_buf *tbuf;
268267
ssize_t actual;
269268

270269
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;
277277
}
278-
n_hdlc->tbusy = true;
279-
n_hdlc->woke_up = false;
280-
spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
281278

282279
tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
283280
while (tbuf) {
@@ -324,9 +321,8 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
324321
clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
325322

326323
/* Clear the re-entry flag */
327-
spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
328-
n_hdlc->tbusy = false;
329-
spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
324+
scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock)
325+
n_hdlc->tbusy = false;
330326

331327
if (n_hdlc->woke_up)
332328
goto check_again;
@@ -585,7 +581,6 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
585581
{
586582
struct n_hdlc *n_hdlc = tty->disc_data;
587583
int count;
588-
unsigned long flags;
589584
struct n_hdlc_buf *buf = NULL;
590585

591586
pr_debug("%s() called %d\n", __func__, cmd);
@@ -594,26 +589,26 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
594589
case FIONREAD:
595590
/* report count of read data available */
596591
/* in next available frame (if any) */
597-
spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags);
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-
spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags);
592+
scoped_guard(spinlock_irqsave, &n_hdlc->rx_buf_list.spinlock) {
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+
}
605600
return put_user(count, (int __user *)arg);
606601

607602
case TIOCOUTQ:
608603
/* get the pending tx byte count in the driver */
609604
count = tty_chars_in_buffer(tty);
610605
/* add size of next output frame in queue */
611-
spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
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-
spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
606+
scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) {
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+
}
617612
return put_user(count, (int __user *)arg);
618613

619614
case TCFLSH:
@@ -720,14 +715,10 @@ static struct n_hdlc *n_hdlc_alloc(void)
720715
static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
721716
struct n_hdlc_buf *buf)
722717
{
723-
unsigned long flags;
724-
725-
spin_lock_irqsave(&buf_list->spinlock, flags);
718+
guard(spinlock_irqsave)(&buf_list->spinlock);
726719

727720
list_add(&buf->list_item, &buf_list->list);
728721
buf_list->count++;
729-
730-
spin_unlock_irqrestore(&buf_list->spinlock, flags);
731722
}
732723

733724
/**
@@ -738,14 +729,10 @@ static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
738729
static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
739730
struct n_hdlc_buf *buf)
740731
{
741-
unsigned long flags;
742-
743-
spin_lock_irqsave(&buf_list->spinlock, flags);
732+
guard(spinlock_irqsave)(&buf_list->spinlock);
744733

745734
list_add_tail(&buf->list_item, &buf_list->list);
746735
buf_list->count++;
747-
748-
spin_unlock_irqrestore(&buf_list->spinlock, flags);
749736
} /* end of n_hdlc_buf_put() */
750737

751738
/**
@@ -758,10 +745,9 @@ static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
758745
*/
759746
static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
760747
{
761-
unsigned long flags;
762748
struct n_hdlc_buf *buf;
763749

764-
spin_lock_irqsave(&buf_list->spinlock, flags);
750+
guard(spinlock_irqsave)(&buf_list->spinlock);
765751

766752
buf = list_first_entry_or_null(&buf_list->list,
767753
struct n_hdlc_buf, list_item);
@@ -770,7 +756,6 @@ static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
770756
buf_list->count--;
771757
}
772758

773-
spin_unlock_irqrestore(&buf_list->spinlock, flags);
774759
return buf;
775760
} /* end of n_hdlc_buf_get() */
776761

0 commit comments

Comments
 (0)