[Devel] [PATCH RHEL COMMIT] tty: Avoid threads files iterations in __do_SAK()

Konstantin Khorenko khorenko at virtuozzo.com
Mon Oct 4 16:53:41 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after ark-5.14
------>
commit 7eb02409b647db30363a7522ed78b4e94035cbdd
Author: Kirill Tkhai <ktkhai at virtuozzo.com>
Date:   Mon Oct 4 16:53:40 2021 +0300

    tty: Avoid threads files iterations in __do_SAK()
    
    The patch makes __do_SAK() iterate a next thread files
    only in case of the thread's files are different
    to previous. I.e., if all threads points the same
    files_struct, the files will be iterated only once.
    
    Since all threads have the same files_struct is
    the generic case for most Linux systems, this
    improvement should clearly speed up __do_SAK()
    execution.
    
    Also, for_each_process()/for_each_thread() are
    used instead of do_each_thread()/while_each_thread().
    This prepares __do_SAK() to become tasklist_lock
    free, and will be made in next patch.
    
    https://jira.sw.ru/browse/PSBM-80340
    
    Suggested-by: Oleg Nesterov <oleg at redhat.com>
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
    Reviewed-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
    
    (cherry picked from vz7 commit d61ca741c3ae ("tty: Avoid threads files
    iterations in __do_SAK()"))
    Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
    
    Rebase to vz8:
     - Change send_sig to group_send_sig_info to respect ms commit
     a8ebd17160ce ("tty_io: Use group_send_sig_info in __do_SACK to note it is a
     session being killed")
    
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/tty/tty_io.c | 37 +++++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index a6230b25fbe5..535f40164c2b 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3031,7 +3031,8 @@ void __do_SAK(struct tty_struct *tty)
 #ifdef TTY_SOFT_SAK
 	tty_hangup(tty);
 #else
-	struct task_struct *g, *p;
+	struct task_struct *p, *t;
+	struct files_struct *files;
 	struct pid *session;
 	int		i;
 	unsigned long flags;
@@ -3056,22 +3057,34 @@ void __do_SAK(struct tty_struct *tty)
 	} while_each_pid_task(session, PIDTYPE_SID, p);
 
 	/* Now kill any processes that happen to have the tty open */
-	do_each_thread(g, p) {
+	for_each_process(p) {
 		if (p->signal->tty == tty) {
 			tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
 				   task_pid_nr(p), p->comm);
-			group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
-			continue;
+			goto kill;
 		}
-		task_lock(p);
-		i = iterate_fd(p->files, 0, this_tty, tty);
-		if (i != 0) {
-			tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
-				   task_pid_nr(p), p->comm, i - 1);
-			group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
+
+		files = NULL;
+		for_each_thread(p, t) {
+			if (t->files == files) /* racy but we do not care */
+				continue;
+
+			task_lock(t);
+			files = t->files;
+			i = iterate_fd(files, 0, this_tty, tty);
+			task_unlock(t);
+
+			if (i != 0) {
+				dev_notice(tty->dev, "SAK: killed process %d (%s): by fd#%d\n",
+					   task_pid_nr(p), p->comm, i - 1);
+				goto kill;
+			}
 		}
-		task_unlock(p);
-	} while_each_thread(g, p);
+
+		continue;
+kill:
+		group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
+	}
 	read_unlock(&tasklist_lock);
 	put_pid(session);
 #endif


More information about the Devel mailing list