[RHEL7,COMMIT] tty: Avoid threads files iterations in __do_SAK()

Submitted by Konstantin Khorenko on Jan. 18, 2018, 4:26 p.m.

Details

Message ID 201801181626.w0IGQDcP028467@finist_ce7.work
State New
Series "Series without cover letter"
Headers show

Commit Message

Konstantin Khorenko Jan. 18, 2018, 4:26 p.m.
The commit is pushed to "branch-rh7-3.10.0-693.11.6.vz7.42.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-693.11.6.vz7.42.1
------>
commit d61ca741c3ae5249f07484014ec73289b9266dfa
Author: Kirill Tkhai <ktkhai@virtuozzo.com>
Date:   Thu Jan 18 19:26:12 2018 +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@redhat.com>
    Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
    Reviewed-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 drivers/tty/tty_io.c | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)

Patch hide | download patch | download mbox

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index b46b357a3b76..50b3170e091c 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3006,7 +3006,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;
 
@@ -3029,24 +3030,36 @@  void __do_SAK(struct tty_struct *tty)
 	/* 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) {
 			printk(KERN_NOTICE "SAK: killed process %d"
 			    " (%s): task_session(p)==tty->session\n",
 			    task_pid_nr(p), p->comm);
-			send_sig(SIGKILL, p, 1);
-			continue;
+			goto kill;
 		}
-		task_lock(p);
-		i = iterate_fd(p->files, 0, this_tty, tty);
-		if (i != 0) {
-			printk(KERN_NOTICE "SAK: killed process %d"
-			    " (%s): fd#%d opened to the tty\n",
-				    task_pid_nr(p), p->comm, i - 1);
-			send_sig(SIGKILL, p, 1);
+
+		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) {
+				printk(KERN_NOTICE "SAK: killed process %d"
+					" (%s): fd#%d opened to the tty\n",
+					task_pid_nr(p), p->comm, i - 1);
+				goto kill;
+			}
 		}
-		task_unlock(p);
-	} while_each_thread(g, p);
+
+		continue;
+kill:
+		send_sig(SIGKILL, p, 1);
+	}
 	qread_unlock(&tasklist_lock);
 #endif
 }