[PATCH 3/3] tty: Move task_pgrp() after tty->ctrl.lock for consistency

From: Earl Chew
Date: Thu Aug 31 2023 - 21:51:06 EST


Refactor __tty_check_change_locked() for consistency
with __proc_set_tty() which calls task_pgrp() after
tty->ctrl.lock is acquired.

In addition, spin_lock_irqsave() can block, while
task_pgrp() cannot block. Fetching the process
group immediately before it is used reduces the
window for inconsistency, and improves clarity.

Signed-off-by: Earl Chew <earl.chew@xxxxxxxx>
---
drivers/tty/tty_jobctrl.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/tty/tty_jobctrl.c b/drivers/tty/tty_jobctrl.c
index 462fdf7b2..b4d37cad9 100644
--- a/drivers/tty/tty_jobctrl.c
+++ b/drivers/tty/tty_jobctrl.c
@@ -43,13 +43,13 @@ static int __tty_check_change_locked(struct tty_struct *tty, int sig,
if (current->signal->tty != tty)
return 0;

- rcu_read_lock();
- pgrp = task_pgrp(current);
-
if (!ctrl_lock)
spin_lock_irqsave(&tty->ctrl.lock, flags);
tty_pgrp = tty->ctrl.pgrp;

+ rcu_read_lock();
+ pgrp = task_pgrp(current);
+
if (tty_pgrp && pgrp != tty_pgrp) {
if (is_ignored(sig)) {
if (sig == SIGTTIN)
@@ -62,9 +62,9 @@ static int __tty_check_change_locked(struct tty_struct *tty, int sig,
ret = -ERESTARTSYS;
}
}
+ rcu_read_unlock();
if (!ctrl_lock)
spin_unlock_irqrestore(&tty->ctrl.lock, flags);
- rcu_read_unlock();

if (!tty_pgrp)
tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
--
2.39.1