linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cyrill Gorcunov <gorcunov@openvz.org>
To: linux-kernel@vger.kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Pavel Emelyanov <xemul@parallels.com>,
	Serge Hallyn <serge.hallyn@canonical.com>,
	KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>,
	Kees Cook <keescook@chromium.org>, Tejun Heo <tj@kernel.org>,
	Andrew Vagin <avagin@openvz.org>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Andi Kleen <andi@firstfloor.org>,
	Cyrill Gorcunov <gorcunov@openvz.org>
Subject: [patch cr 1/4] fs, proc: Introduce /proc/<pid>/task/<tid>/children entry v9
Date: Mon, 30 Jan 2012 18:09:06 +0400	[thread overview]
Message-ID: <20120130141852.234112739@openvz.org> (raw)
In-Reply-To: 20120130140905.441199885@openvz.org

[-- Attachment #1: fs-proc-tid-children-13 --]
[-- Type: text/plain, Size: 7055 bytes --]

When we do checkpoint of a task we need to know the list of children
the task, has but there is no easy and fast way to generate reverse
parent->children chain from arbitrary <pid> (while a parent pid is
provided in "PPid" field of /proc/<pid>/status).

So instead of walking over all pids in the system (creating one big process
tree in memory, just to figure out which children a task has) -- we add
explicit /proc/<pid>/task/<tid>/children entry, because the kernel already has
this kind of information but it is not yet exported.

This is a first level children, not the whole process tree.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Pavel Emelyanov <xemul@parallels.com>
Cc: Serge Hallyn <serge.hallyn@canonical.com>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
---
 Documentation/filesystems/proc.txt |   18 +++++
 fs/proc/array.c                    |  123 +++++++++++++++++++++++++++++++++++++
 fs/proc/base.c                     |    3 
 fs/proc/internal.h                 |    1 
 4 files changed, 145 insertions(+)

Index: linux-2.6.git/Documentation/filesystems/proc.txt
===================================================================
--- linux-2.6.git.orig/Documentation/filesystems/proc.txt
+++ linux-2.6.git/Documentation/filesystems/proc.txt
@@ -40,6 +40,7 @@ Table of Contents
   3.4	/proc/<pid>/coredump_filter - Core dump filtering settings
   3.5	/proc/<pid>/mountinfo - Information about mounts
   3.6	/proc/<pid>/comm  & /proc/<pid>/task/<tid>/comm
+  3.7   /proc/<pid>/task/<tid>/children - Information about task children
 
   4	Configuring procfs
   4.1	Mount options
@@ -1549,6 +1550,23 @@ then the kernel's TASK_COMM_LEN (current
 comm value.
 
 
+3.7	/proc/<pid>/task/<tid>/children - Information about task children
+-------------------------------------------------------------------------
+This file provides a fast way to retrieve first level children pids
+of a task pointed by <pid>/<tid> pair. The format is a space separated
+stream of pids.
+
+Note the "first level" here -- if a child has own children they will
+not be listed here, one needs to read /proc/<children-pid>/task/<tid>/children
+to obtain the descendants.
+
+Since this interface is intended to be fast and cheap it doesn't
+guarantee to provide precise results and some children might be
+skipped, especially if they've exited right after we printed their
+pids, so one need to either stop or freeze processes being inspected
+if precise results are needed.
+
+
 ------------------------------------------------------------------------------
 Configuring procfs
 ------------------------------------------------------------------------------
Index: linux-2.6.git/fs/proc/array.c
===================================================================
--- linux-2.6.git.orig/fs/proc/array.c
+++ linux-2.6.git/fs/proc/array.c
@@ -547,3 +547,126 @@ int proc_pid_statm(struct seq_file *m, s
 
 	return 0;
 }
+
+#ifdef CONFIG_CHECKPOINT_RESTORE
+static struct pid *
+get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t 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;
+
+	/*
+	 * 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 &&
+		    !(list_empty(&task->sibling))) {
+			if (list_is_last(&task->sibling, &start->children))
+				goto out;
+			task = list_first_entry(&task->sibling,
+						struct task_struct, sibling);
+			pid = get_pid(task_pid(task));
+			goto out;
+		}
+	}
+
+	/*
+	 * Slow search case.
+	 *
+	 * We might miss some children here if children
+	 * are exited while we were not holding the lock,
+	 * but it was never promised to be accurate that
+	 * much.
+	 *
+	 * "Just suppose that the parent sleeps, but N children
+	 *  exit after we printed their tids. Now the slow paths
+	 *  skips N extra children, we miss N tasks." (c)
+	 *
+	 * 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;
+		}
+	}
+
+out:
+	read_unlock(&tasklist_lock);
+	return pid;
+}
+
+static int children_seq_show(struct seq_file *seq, void *v)
+{
+	struct inode *inode = seq->private;
+	pid_t pid;
+
+	pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
+	return seq_printf(seq, "%d ", pid);
+}
+
+static void *children_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	return get_children_pid(seq->private, NULL, *pos);
+}
+
+static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+	struct pid *pid;
+
+	pid = get_children_pid(seq->private, v, *pos + 1);
+	put_pid(v);
+
+	++*pos;
+	return pid;
+}
+
+static void children_seq_stop(struct seq_file *seq, void *v)
+{
+	put_pid(v);
+}
+
+static const struct seq_operations children_seq_ops = {
+	.start	= children_seq_start,
+	.next	= children_seq_next,
+	.stop	= children_seq_stop,
+	.show	= children_seq_show,
+};
+
+static int children_seq_open(struct inode *inode, struct file *file)
+{
+	struct seq_file *m;
+	int ret;
+
+	ret = seq_open(file, &children_seq_ops);
+	if (ret)
+		return ret;
+
+	m = file->private_data;
+	m->private = inode;
+
+	return ret;
+}
+
+int children_seq_release(struct inode *inode, struct file *file)
+{
+	seq_release(inode, file);
+	return 0;
+}
+
+const struct file_operations proc_tid_children_operations = {
+	.open    = children_seq_open,
+	.read    = seq_read,
+	.llseek  = seq_lseek,
+	.release = children_seq_release,
+};
+#endif
Index: linux-2.6.git/fs/proc/base.c
===================================================================
--- linux-2.6.git.orig/fs/proc/base.c
+++ linux-2.6.git/fs/proc/base.c
@@ -3384,6 +3384,9 @@ static const struct pid_entry tid_base_s
 	ONE("stat",      S_IRUGO, proc_tid_stat),
 	ONE("statm",     S_IRUGO, proc_pid_statm),
 	REG("maps",      S_IRUGO, proc_maps_operations),
+#ifdef CONFIG_CHECKPOINT_RESTORE
+	REG("children",  S_IRUGO, proc_tid_children_operations),
+#endif
 #ifdef CONFIG_NUMA
 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
 #endif
Index: linux-2.6.git/fs/proc/internal.h
===================================================================
--- linux-2.6.git.orig/fs/proc/internal.h
+++ linux-2.6.git/fs/proc/internal.h
@@ -53,6 +53,7 @@ extern int proc_pid_statm(struct seq_fil
 				struct pid *pid, struct task_struct *task);
 extern loff_t mem_lseek(struct file *file, loff_t offset, int orig);
 
+extern const struct file_operations proc_tid_children_operations;
 extern const struct file_operations proc_maps_operations;
 extern const struct file_operations proc_numa_maps_operations;
 extern const struct file_operations proc_smaps_operations;


  reply	other threads:[~2012-01-30 14:19 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-30 14:09 [patch cr 0/4] [patch cr 0/@total@] Cyrill Gorcunov
2012-01-30 14:09 ` Cyrill Gorcunov [this message]
2012-01-30 14:09 ` [patch cr 2/4] [RFC] syscalls, x86: Add __NR_kcmp syscall v7 Cyrill Gorcunov
2012-01-30 19:58   ` Jonathan Corbet
2012-01-30 21:07     ` Cyrill Gorcunov
2012-01-30 21:11     ` H. Peter Anvin
2012-02-02 23:26   ` Andrew Morton
2012-02-03  2:27     ` H. Peter Anvin
2012-02-03  7:09       ` Cyrill Gorcunov
2012-02-03  7:46   ` Ingo Molnar
2012-02-03  8:35     ` Cyrill Gorcunov
2012-02-03  9:09       ` Ingo Molnar
2012-02-03  9:22         ` Andrew Morton
2012-02-03  9:28           ` Cyrill Gorcunov
2012-02-03 17:32             ` H. Peter Anvin
2012-02-03 17:35               ` H. Peter Anvin
2012-02-03 17:42                 ` Cyrill Gorcunov
2012-02-03  9:52           ` Ingo Molnar
2012-02-03 10:07             ` [PATCH] SubmittingPatches: Increase the line length limit from 80 to 100 colums Ingo Molnar
2012-02-03 10:17               ` Pekka Enberg
2012-02-03 10:23                 ` Cyrill Gorcunov
2012-02-03 10:40               ` Alexey Dobriyan
2012-02-03 16:13               ` Tejun Heo
2012-02-03 16:39                 ` hpanvin@gmail.com
2012-02-03 17:56               ` Andi Kleen
2012-02-03 20:57               ` Andrew Morton
2012-02-03 21:00                 ` H. Peter Anvin
2012-02-03 21:06                 ` H. Peter Anvin
2012-02-04 13:08                 ` Ingo Molnar
2012-02-03 21:27               ` Linus Torvalds
2012-02-03 23:20                 ` [PATCH] checkpatch: Warn on code with 6+ tab indentation Joe Perches
2012-02-04  1:27                   ` Linus Torvalds
2012-02-04  1:33                     ` Joe Perches
2012-02-04  3:09                       ` Linus Torvalds
2012-02-04  3:21                         ` Joe Perches
2012-02-04  3:35                           ` Linus Torvalds
2012-02-04  3:58                             ` Joe Perches
2012-02-04  1:37                     ` Andrew Morton
2012-02-04  2:40                   ` Eric W. Biederman
2012-02-04  2:46                     ` Joe Perches
2012-02-04  4:45                   ` Tony Luck
2012-02-04  4:53                     ` Joe Perches
2012-02-04 13:03                   ` [PATCH, v2] checkpatch: Warn on code with 6+ tab indentation, remove 80col warning Ingo Molnar
2012-02-04 16:22                     ` Joe Perches
2012-02-04 18:02                       ` Ingo Molnar
2012-02-04 18:48                         ` Joe Perches
2012-02-04 18:54                           ` Pekka Enberg
2012-02-04 19:27                             ` Joe Perches
2012-02-04 19:32                               ` Pekka Enberg
2012-02-05 11:38                               ` Ingo Molnar
2012-02-05 16:21                                 ` Joe Perches
2012-02-05 18:13                                   ` Ingo Molnar
2012-02-05 19:01                                     ` [PATCH] checkpatch: Add line-length options, set default to 100 Joe Perches
2012-02-06 12:36                                       ` Dan Carpenter
2012-02-04  1:24                 ` [PATCH] SubmittingPatches: Increase the line length limit from 80 to 100 colums Randy Dunlap
2012-02-09 21:55               ` Jan Engelhardt
2012-02-09 22:09                 ` Joe Perches
2012-02-09 22:30                 ` Mark Brown
2012-01-30 14:09 ` [patch cr 3/4] c/r: procfs: add arg_start/end, env_start/end and exit_code members to /proc/$pid/stat Cyrill Gorcunov
2012-02-02 23:26   ` Andrew Morton
2012-02-03  7:11     ` Cyrill Gorcunov
2012-01-30 14:09 ` [patch cr 4/4] c/r: prctl: Extend PR_SET_MM to set up more mm_struct entries Cyrill Gorcunov
2012-02-02 23:27   ` Andrew Morton
2012-02-03  7:18     ` Cyrill Gorcunov
2012-02-02 23:26 ` [patch cr 0/4] [patch cr 0/@total@] Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120130141852.234112739@openvz.org \
    --to=gorcunov@openvz.org \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=avagin@openvz.org \
    --cc=ebiederm@xmission.com \
    --cc=kamezawa.hiroyu@jp.fujitsu.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=serge.hallyn@canonical.com \
    --cc=tj@kernel.org \
    --cc=xemul@parallels.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).