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

From: Earl Chew
Date: Thu Sep 28 2023 - 09:07:22 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 da028aadf..e82ffd875 100644
--- a/drivers/tty/tty_jobctrl.c
+++ b/drivers/tty/tty_jobctrl.c
@@ -44,13 +44,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)
@@ -63,9 +63,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