Skip to content

Commit 977e759

Browse files
Jiri Slaby (SUSE)gregkh
authored andcommitted
tty: pty: use guard()s
Use guards in the pty code. This improves readability, makes error handling easier, and marks locked portions of code explicit. All that while being sure the lock is unlocked. pty_set_pktmode() is handled specially -- the conditions are inverted and return called if conditions unmet. This avoid double nested 'if's. The variable is renamed to want_pktmode so it is not confused with the current state of pktmode. Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org> Link: https://patch.msgid.link/20251119100140.830761-2-jirislaby@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6c84a61 commit 977e759

1 file changed

Lines changed: 45 additions & 58 deletions

File tree

drivers/tty/pty.c

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
5757
set_bit(TTY_IO_ERROR, &tty->flags);
5858
wake_up_interruptible(&tty->read_wait);
5959
wake_up_interruptible(&tty->write_wait);
60-
spin_lock_irq(&tty->ctrl.lock);
61-
tty->ctrl.packet = false;
62-
spin_unlock_irq(&tty->ctrl.lock);
60+
scoped_guard(spinlock_irq, &tty->ctrl.lock)
61+
tty->ctrl.packet = false;
6362
/* Review - krefs on tty_link ?? */
6463
if (!tty->link)
6564
return;
@@ -70,10 +69,9 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
7069
set_bit(TTY_OTHER_CLOSED, &tty->flags);
7170
#ifdef CONFIG_UNIX98_PTYS
7271
if (tty->driver == ptm_driver) {
73-
mutex_lock(&devpts_mutex);
72+
guard(mutex)(&devpts_mutex);
7473
if (tty->link->driver_data)
7574
devpts_pty_kill(tty->link->driver_data);
76-
mutex_unlock(&devpts_mutex);
7775
}
7876
#endif
7977
tty_vhangup(tty->link);
@@ -157,21 +155,23 @@ static int pty_get_lock(struct tty_struct *tty, int __user *arg)
157155
/* Set the packet mode on a pty */
158156
static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
159157
{
160-
int pktmode;
158+
int want_pktmode;
161159

162-
if (get_user(pktmode, arg))
160+
if (get_user(want_pktmode, arg))
163161
return -EFAULT;
164162

165-
spin_lock_irq(&tty->ctrl.lock);
166-
if (pktmode) {
167-
if (!tty->ctrl.packet) {
168-
tty->link->ctrl.pktstatus = 0;
169-
smp_mb();
170-
tty->ctrl.packet = true;
171-
}
172-
} else
163+
guard(spinlock_irq)(&tty->ctrl.lock);
164+
if (!want_pktmode) {
173165
tty->ctrl.packet = false;
174-
spin_unlock_irq(&tty->ctrl.lock);
166+
return 0;
167+
}
168+
169+
if (tty->ctrl.packet)
170+
return 0;
171+
172+
tty->link->ctrl.pktstatus = 0;
173+
smp_mb();
174+
tty->ctrl.packet = true;
175175

176176
return 0;
177177
}
@@ -210,10 +210,9 @@ static void pty_flush_buffer(struct tty_struct *tty)
210210

211211
tty_buffer_flush(to, NULL);
212212
if (to->ctrl.packet) {
213-
spin_lock_irq(&tty->ctrl.lock);
213+
guard(spinlock_irq)(&tty->ctrl.lock);
214214
tty->ctrl.pktstatus |= TIOCPKT_FLUSHWRITE;
215215
wake_up_interruptible(&to->read_wait);
216-
spin_unlock_irq(&tty->ctrl.lock);
217216
}
218217
}
219218

@@ -252,17 +251,17 @@ static void pty_set_termios(struct tty_struct *tty,
252251
STOP_CHAR(tty) == '\023' &&
253252
START_CHAR(tty) == '\021');
254253
if ((old_flow != new_flow) || extproc) {
255-
spin_lock_irq(&tty->ctrl.lock);
256-
if (old_flow != new_flow) {
257-
tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
258-
if (new_flow)
259-
tty->ctrl.pktstatus |= TIOCPKT_DOSTOP;
260-
else
261-
tty->ctrl.pktstatus |= TIOCPKT_NOSTOP;
254+
scoped_guard(spinlock_irq, &tty->ctrl.lock) {
255+
if (old_flow != new_flow) {
256+
tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
257+
if (new_flow)
258+
tty->ctrl.pktstatus |= TIOCPKT_DOSTOP;
259+
else
260+
tty->ctrl.pktstatus |= TIOCPKT_NOSTOP;
261+
}
262+
if (extproc)
263+
tty->ctrl.pktstatus |= TIOCPKT_IOCTL;
262264
}
263-
if (extproc)
264-
tty->ctrl.pktstatus |= TIOCPKT_IOCTL;
265-
spin_unlock_irq(&tty->ctrl.lock);
266265
wake_up_interruptible(&tty->link->read_wait);
267266
}
268267
}
@@ -286,9 +285,9 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
286285
struct tty_struct *pty = tty->link;
287286

288287
/* For a PTY we need to lock the tty side */
289-
mutex_lock(&tty->winsize_mutex);
288+
guard(mutex)(&tty->winsize_mutex);
290289
if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
291-
goto done;
290+
return 0;
292291

293292
/* Signal the foreground process group of both ptys */
294293
pgrp = tty_get_pgrp(tty);
@@ -304,8 +303,7 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
304303

305304
tty->winsize = *ws;
306305
pty->winsize = *ws; /* Never used so will go away soon */
307-
done:
308-
mutex_unlock(&tty->winsize_mutex);
306+
309307
return 0;
310308
}
311309

@@ -321,28 +319,26 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
321319
*/
322320
static void pty_start(struct tty_struct *tty)
323321
{
324-
unsigned long flags;
322+
if (!tty->link || !tty->link->ctrl.packet)
323+
return;
325324

326-
if (tty->link && tty->link->ctrl.packet) {
327-
spin_lock_irqsave(&tty->ctrl.lock, flags);
325+
scoped_guard(spinlock_irqsave, &tty->ctrl.lock) {
328326
tty->ctrl.pktstatus &= ~TIOCPKT_STOP;
329327
tty->ctrl.pktstatus |= TIOCPKT_START;
330-
spin_unlock_irqrestore(&tty->ctrl.lock, flags);
331-
wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
332328
}
329+
wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
333330
}
334331

335332
static void pty_stop(struct tty_struct *tty)
336333
{
337-
unsigned long flags;
334+
if (!tty->link || !tty->link->ctrl.packet)
335+
return;
338336

339-
if (tty->link && tty->link->ctrl.packet) {
340-
spin_lock_irqsave(&tty->ctrl.lock, flags);
337+
scoped_guard(spinlock_irqsave, &tty->ctrl.lock) {
341338
tty->ctrl.pktstatus &= ~TIOCPKT_START;
342339
tty->ctrl.pktstatus |= TIOCPKT_STOP;
343-
spin_unlock_irqrestore(&tty->ctrl.lock, flags);
344-
wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
345340
}
341+
wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
346342
}
347343

348344
/**
@@ -705,15 +701,9 @@ static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
705701
static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
706702
struct file *file, int idx)
707703
{
708-
struct tty_struct *tty;
709-
710-
mutex_lock(&devpts_mutex);
711-
tty = devpts_get_priv(file->f_path.dentry);
712-
mutex_unlock(&devpts_mutex);
704+
guard(mutex)(&devpts_mutex);
713705
/* Master must be open before slave */
714-
if (!tty)
715-
return ERR_PTR(-EIO);
716-
return tty;
706+
return devpts_get_priv(file->f_path.dentry) ? : ERR_PTR(-EIO);
717707
}
718708

719709
static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
@@ -811,20 +801,17 @@ static int ptmx_open(struct inode *inode, struct file *filp)
811801
}
812802

813803
/* find a device that is not in use. */
814-
mutex_lock(&devpts_mutex);
815-
index = devpts_new_index(fsi);
816-
mutex_unlock(&devpts_mutex);
804+
scoped_guard(mutex, &devpts_mutex)
805+
index = devpts_new_index(fsi);
817806

818807
retval = index;
819808
if (index < 0)
820809
goto out_put_fsi;
821810

822811

823-
mutex_lock(&tty_mutex);
824-
tty = tty_init_dev(ptm_driver, index);
825-
/* The tty returned here is locked so we can safely
826-
drop the mutex */
827-
mutex_unlock(&tty_mutex);
812+
/* The tty returned here is locked so we can safely drop the mutex */
813+
scoped_guard(mutex, &tty_mutex)
814+
tty = tty_init_dev(ptm_driver, index);
828815

829816
retval = PTR_ERR(tty);
830817
if (IS_ERR(tty))

0 commit comments

Comments
 (0)