Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755274AbcDKXgL (ORCPT ); Mon, 11 Apr 2016 19:36:11 -0400 Received: from mail-pf0-f169.google.com ([209.85.192.169]:36641 "EHLO mail-pf0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754068AbcDKXgH (ORCPT ); Mon, 11 Apr 2016 19:36:07 -0400 From: Andrey Vagin To: linux-kernel@vger.kernel.org Cc: Andrey Vagin , Oleg Nesterov , Andrew Morton , Cyrill Gorcunov , Pavel Emelyanov , Roger Luethi , Arnd Bergmann , Arnaldo Carvalho de Melo , David Ahern , Andy Lutomirski , Pavel Odintsov Subject: [PATCH 01/15] proc: pick out a function to iterate task children Date: Mon, 11 Apr 2016 16:35:41 -0700 Message-Id: <1460417755-18201-2-git-send-email-avagin@openvz.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1460417755-18201-1-git-send-email-avagin@openvz.org> References: <1460417755-18201-1-git-send-email-avagin@openvz.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2942 Lines: 103 This function will be used in task_diag. Signed-off-by: Andrey Vagin --- fs/proc/array.c | 53 +++++++++++++++++++++++++++++++++-------------------- fs/proc/internal.h | 3 +++ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/fs/proc/array.c b/fs/proc/array.c index b6c00ce..3eceab1 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -593,31 +593,25 @@ int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns, } #ifdef CONFIG_PROC_CHILDREN -static struct pid * -get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos) +struct task_struct * +task_next_child(struct task_struct *parent, struct task_struct *prev, unsigned int pos) { - struct task_struct *start, *task; - struct pid *pid = NULL; - - read_lock(&tasklist_lock); - - start = pid_task(proc_pid(inode), PIDTYPE_PID); - if (!start) - goto out; + struct task_struct *task; /* * Lets try to continue searching first, this gives * us significant speedup on children-rich processes. */ - if (pid_prev) { - task = pid_task(pid_prev, PIDTYPE_PID); - if (task && task->real_parent == start && + if (prev) { + task = prev; + if (task && task->real_parent == parent && !(list_empty(&task->sibling))) { - if (list_is_last(&task->sibling, &start->children)) + if (list_is_last(&task->sibling, &parent->children)) { + task = NULL; goto out; + } task = list_first_entry(&task->sibling, struct task_struct, sibling); - pid = get_pid(task_pid(task)); goto out; } } @@ -637,12 +631,31 @@ get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos) * So one need to stop or freeze the leader and all * its children to get a precise result. */ - list_for_each_entry(task, &start->children, sibling) { - if (pos-- == 0) { - pid = get_pid(task_pid(task)); - break; - } + list_for_each_entry(task, &parent->children, sibling) { + if (pos-- == 0) + goto out; } + task = NULL; +out: + return task; +} + +static struct pid * +get_children_pid(struct inode *inode, struct pid *prev_pid, loff_t pos) +{ + struct task_struct *start, *task, *prev; + struct pid *pid = NULL; + + read_lock(&tasklist_lock); + start = pid_task(proc_pid(inode), PIDTYPE_PID); + if (!start) + goto out; + + prev = prev_pid ? pid_task(prev_pid, PIDTYPE_PID) : NULL; + + task = task_next_child(start, prev, pos); + if (task) + pid = get_pid(task_pid(task)); out: read_unlock(&tasklist_lock); diff --git a/fs/proc/internal.h b/fs/proc/internal.h index aa27810..969e05b 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -303,3 +303,6 @@ extern unsigned long task_statm(struct mm_struct *, unsigned long *, unsigned long *, unsigned long *, unsigned long *); extern void task_mem(struct seq_file *, struct mm_struct *); + +struct task_struct * +task_next_child(struct task_struct *parent, struct task_struct *prev, unsigned int pos); -- 2.5.5