[RHEL7,COMMIT] ms/prctl: introduce the walk_process_tree() helper

Submitted by Konstantin Khorenko on Sept. 30, 2019, 12:47 p.m.

Details

Message ID 201909301247.x8UCl1bM006857@finist-ce7.sw.ru
State New
Series "Series without cover letter"
Headers show

Commit Message

Konstantin Khorenko Sept. 30, 2019, 12:47 p.m.
The commit is pushed to "branch-rh7-3.10.0-957.27.2.vz7.107.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-957.27.2.vz7.107.10
------>
commit b6bd189be69dc047f69f2f1a50576b48f82486f9
Author: Oleg Nesterov <oleg@redhat.com>
Date:   Mon Sep 30 15:47:01 2019 +0300

    ms/prctl: introduce the walk_process_tree() helper
    
    Add the new helper to walk the process tree, the next patch adds a user.
    Note that it visits the group leaders only, proc_visitor can do
    for_each_thread itself or we can trivially extend walk_process_tree() to
    do this.
    
    Signed-off-by: Oleg Nesterov <oleg@redhat.com>
    Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
    
    Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
    
    https://jira.sw.ru/browse/PSBM-96263
    
    (cherry picked from commit 0f1b92cbdd0309afae0af1963e8cccddb3d2eaff)
    Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 include/linux/sched.h |  3 +++
 kernel/fork.c         | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

Patch hide | download patch | download mbox

diff --git a/include/linux/sched.h b/include/linux/sched.h
index a196da0ef849..72ea5a5d82e8 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2778,6 +2778,9 @@  extern bool current_is_single_threaded(void);
 #define for_each_process_thread(p, t)	\
 	for_each_process(p) for_each_thread(p, t)
 
+typedef int (*proc_visitor)(struct task_struct *p, void *data);
+void walk_process_tree(struct task_struct *top, proc_visitor, void *);
+
 static inline int get_nr_threads(struct task_struct *tsk)
 {
 	return tsk->signal->nr_threads;
diff --git a/kernel/fork.c b/kernel/fork.c
index 544b504c75c2..9c1c675f1f74 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1934,6 +1934,38 @@  SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
 }
 #endif
 
+void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data)
+{
+	struct task_struct *leader, *parent, *child;
+	int res;
+
+	tasklist_read_lock();
+	leader = top = top->group_leader;
+down:
+	for_each_thread(leader, parent) {
+		list_for_each_entry(child, &parent->children, sibling) {
+			res = visitor(child, data);
+			if (res) {
+				if (res < 0)
+					goto out;
+				leader = child;
+				goto down;
+			}
+up:
+			;
+		}
+	}
+
+	if (leader != top) {
+		child = leader;
+		parent = child->real_parent;
+		leader = parent->group_leader;
+		goto up;
+	}
+out:
+	qread_unlock(&tasklist_lock);
+}
+
 #ifndef ARCH_MIN_MMSTRUCT_ALIGN
 #define ARCH_MIN_MMSTRUCT_ALIGN 0
 #endif