mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrey Vagin <avagin@openvz.org>
To: linux-kernel@vger.kernel.org
Cc: linux-api@vger.kernel.org, Andrey Vagin <avagin@openvz.org>,
	Oleg Nesterov <oleg@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Cyrill Gorcunov <gorcunov@openvz.org>,
	Pavel Emelyanov <xemul@parallels.com>,
	Roger Luethi <rl@hellgate.ch>, Arnd Bergmann <arnd@arndb.de>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	David Ahern <dsahern@gmail.com>,
	Andy Lutomirski <luto@amacapital.net>,
	Pavel Odintsov <pavel.odintsov@gmail.com>
Subject: [PATCH 10/24] task_diag: add ability to dump children (v2)
Date: Mon,  6 Jul 2015 11:47:11 +0300	[thread overview]
Message-ID: <1436172445-6979-11-git-send-email-avagin@openvz.org> (raw)
In-Reply-To: <1436172445-6979-1-git-send-email-avagin@openvz.org>

Now we can dump all task or children of a specified task.
It's an example how this interface can be expanded for different
use-cases.

v2: Fixes from David Ahern
    Add missing break in iter_stop
    Fix 8-byte alignment issues

Cc: David Ahern <dsahern@gmail.com>
Signed-off-by: Andrey Vagin <avagin@openvz.org>
---
 include/uapi/linux/task_diag.h |   1 +
 kernel/taskdiag.c              | 117 +++++++++++++++++++++++++++++++++++------
 2 files changed, 102 insertions(+), 16 deletions(-)

diff --git a/include/uapi/linux/task_diag.h b/include/uapi/linux/task_diag.h
index 0e659d6..af192db 100644
--- a/include/uapi/linux/task_diag.h
+++ b/include/uapi/linux/task_diag.h
@@ -63,6 +63,7 @@ struct task_diag_creds {
 };
 
 #define TASK_DIAG_DUMP_ALL	0
+#define TASK_DIAG_DUMP_CHILDREN	1
 
 struct task_diag_pid {
 	__u64	show_flags;
diff --git a/kernel/taskdiag.c b/kernel/taskdiag.c
index 5123e12..6dd3361 100644
--- a/kernel/taskdiag.c
+++ b/kernel/taskdiag.c
@@ -184,42 +184,127 @@ err:
 	return err;
 }
 
+struct task_iter {
+	struct task_diag_pid req;
+	struct pid_namespace *ns;
+	struct netlink_callback *cb;
+	struct task_struct *parent;
+
+	union {
+		struct tgid_iter tgid;
+		struct {
+			unsigned int		pos;
+			struct task_struct	*task;
+		};
+	};
+};
+
+static void iter_stop(struct task_iter *iter)
+{
+	struct task_struct *task;
+
+	if (iter->parent)
+		put_task_struct(iter->parent);
+
+	switch (iter->req.dump_strategy) {
+	case TASK_DIAG_DUMP_ALL:
+		task = iter->tgid.task;
+		break;
+	default:
+		task = iter->task;
+	}
+	if (task)
+		put_task_struct(task);
+}
+
+static struct task_struct *iter_start(struct task_iter *iter)
+{
+	if (iter->req.pid > 0) {
+		rcu_read_lock();
+		iter->parent = find_task_by_pid_ns(iter->req.pid, iter->ns);
+		if (iter->parent)
+			get_task_struct(iter->parent);
+		rcu_read_unlock();
+	}
+
+	switch (iter->req.dump_strategy) {
+	case TASK_DIAG_DUMP_CHILDREN:
+
+		if (iter->parent == NULL)
+			return ERR_PTR(-ESRCH);
+
+		iter->pos = iter->cb->args[0];
+		iter->task = task_next_child(iter->parent, NULL, iter->pos);
+		return iter->task;
+
+	case TASK_DIAG_DUMP_ALL:
+		iter->tgid.tgid = iter->cb->args[0];
+		iter->tgid.task = NULL;
+		iter->tgid = next_tgid(iter->ns, iter->tgid);
+		return iter->tgid.task;
+	}
+
+	return ERR_PTR(-EINVAL);
+}
+
+static struct task_struct *iter_next(struct task_iter *iter)
+{
+	switch (iter->req.dump_strategy) {
+	case TASK_DIAG_DUMP_CHILDREN:
+		iter->pos++;
+		iter->task = task_next_child(iter->parent, iter->task, iter->pos);
+		iter->cb->args[0] = iter->pos;
+		return iter->task;
+
+	case TASK_DIAG_DUMP_ALL:
+		iter->tgid.tgid += 1;
+		iter->tgid = next_tgid(iter->ns, iter->tgid);
+		iter->cb->args[0] = iter->tgid.tgid;
+		return iter->tgid.task;
+	}
+
+	return NULL;
+}
+
 int taskdiag_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 {
 	struct pid_namespace *ns = task_active_pid_ns(current);
-	struct tgid_iter iter;
+	struct task_iter iter;
 	struct nlattr *na;
-	struct task_diag_pid req;
+	struct task_struct *task;
 	int rc;
 
-	if (nlmsg_len(cb->nlh) < GENL_HDRLEN + sizeof(req))
+	if (nlmsg_len(cb->nlh) < GENL_HDRLEN + sizeof(iter.req))
 		return -EINVAL;
 
 	na = nlmsg_data(cb->nlh) + GENL_HDRLEN;
 	if (na->nla_type < 0)
 		return -EINVAL;
 
-	memcpy(&req, nla_data(na), sizeof(req));
+	memcpy(&iter.req, nla_data(na), sizeof(iter.req));
 
-	iter.tgid = cb->args[0];
-	iter.task = NULL;
-	for (iter = next_tgid(ns, iter);
-	     iter.task;
-	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
-		if (!ptrace_may_access(iter.task, PTRACE_MODE_READ))
-			continue;
+	iter.ns     = ns;
+	iter.cb     = cb;
+	iter.parent = NULL;
 
-		rc = task_diag_fill(iter.task, skb, req.show_flags,
+	task = iter_start(&iter);
+	if (IS_ERR(task))
+		return PTR_ERR(task);
+
+	for (; task; task = iter_next(&iter)) {
+		if (!ptrace_may_access(task, PTRACE_MODE_READ))
+			continue;
+		rc = task_diag_fill(task, skb, iter.req.show_flags,
 				NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, cb);
 		if (rc < 0) {
-			put_task_struct(iter.task);
-			if (rc != -EMSGSIZE)
+			if (rc != -EMSGSIZE) {
+				iter_stop(&iter);
 				return rc;
+			}
 			break;
 		}
 	}
-
-	cb->args[0] = iter.tgid;
+	iter_stop(&iter);
 
 	return skb->len;
 }
-- 
2.1.0


  parent reply	other threads:[~2015-07-06  8:55 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-06  8:47 [PATCH 0/24] kernel: add a netlink interface to get information about processes (v2) Andrey Vagin
2015-07-06  8:47 ` [PATCH 01/24] kernel: define taststats commands in the one place Andrey Vagin
2015-07-06  8:47 ` [PATCH 02/24] kernel: add a netlink interface to get information about tasks (v2) Andrey Vagin
2015-07-06  8:47 ` [PATCH 03/24] kernel: make taskstats available from all net namespaces Andrey Vagin
2015-07-06  8:47 ` [PATCH 04/24] kernel: move next_tgid from fs/proc Andrey Vagin
2015-07-06  8:47 ` [PATCH 05/24] task_diag: add ability to get information about all tasks Andrey Vagin
2015-07-06  8:47 ` [PATCH 06/24] task_diag: add ability to split per-task data on a few netlink messages Andrey Vagin
2015-07-06  8:47 ` [PATCH 07/24] task_diag: add a new group to get process credentials Andrey Vagin
2015-07-06  8:47 ` [PATCH 08/24] proc: pick out a function to iterate task children Andrey Vagin
2015-07-14 18:02   ` Oleg Nesterov
2015-07-17 15:57     ` Andrew Vagin
2015-07-18 21:22       ` Oleg Nesterov
2015-07-06  8:47 ` [PATCH 09/24] proc: move task_next_child() from fs/proc Andrey Vagin
2015-07-06  8:47 ` Andrey Vagin [this message]
2015-07-06  8:47 ` [PATCH 11/24] task_diag: add a new group to get task statistics Andrey Vagin
2015-07-06  8:47 ` [PATCH 12/24] task_diag: add a new group to get tasks memory mappings (v2) Andrey Vagin
2015-07-14 18:08   ` Oleg Nesterov
2015-07-15  2:02     ` David Ahern
2015-07-06  8:47 ` [PATCH 13/24] task_diag: shows memory consumption for " Andrey Vagin
2015-07-06  8:47 ` [PATCH 14/24] task_diag: add a marcos to enumirate memory mappings Andrey Vagin
2015-07-06  8:47 ` [PATCH 15/24] proc: give task_struct instead of pid into first_tid Andrey Vagin
2015-07-14 18:11   ` Oleg Nesterov
2015-07-06  8:47 ` [PATCH 16/24] proc: move first_tid and next_tid out of proc Andrey Vagin
2015-07-06  8:47 ` [PATCH 17/24] task_diag: add ability to dump theads Andrey Vagin
2015-07-06  8:47 ` [PATCH 18/24] task_diag: add ability to handle one task in a continious mode Andrey Vagin
2015-07-06  8:47 ` [PATCH 19/24] task_diag: Add option to dump all threads for all tasks Andrey Vagin
2015-07-06  8:47 ` [PATCH 20/24] task_diag: Only add VMAs for thread_group leader Andrey Vagin
2015-07-14 17:47   ` Oleg Nesterov
2015-07-15  2:01     ` David Ahern
2015-07-15 13:31       ` Oleg Nesterov
2015-07-06  8:47 ` [PATCH 21/24] task diag: Add support for TGID attribute Andrey Vagin
2015-07-06  8:47 ` [PATCH 22/24] Documentation: add documentation for task_diag Andrey Vagin
2015-07-06  8:47 ` [PATCH 23/24] selftest: check the task_diag functinonality Andrey Vagin
2015-07-06  8:47 ` [PATCH 24/24] task_diag: Enhance fork tool to spawn threads Andrey Vagin
2015-07-06 17:10 ` [PATCH 0/24] kernel: add a netlink interface to get information about processes (v2) Andy Lutomirski
2015-07-07 15:43   ` Andrew Vagin
2015-07-07 15:56     ` Andy Lutomirski
2015-07-07 16:17       ` David Ahern
2015-07-07 16:24         ` Andy Lutomirski
2015-07-07 16:41           ` David Ahern
2015-07-08 16:10       ` Andrew Vagin
2015-07-08 17:39         ` Andy Lutomirski
2015-07-08 22:49           ` Andrey Vagin
2015-07-08 23:48             ` Andy Lutomirski
2015-07-07 16:25     ` Arnaldo Carvalho de Melo
2015-07-07 16:27       ` Andy Lutomirski
2015-07-07 16:56         ` David Ahern
2015-11-24 15:18 ` Andrew Vagin
2015-12-03 23:20   ` Andy Lutomirski
2015-12-03 23:43     ` Arnd Bergmann
2015-12-14  8:05       ` Andrew Vagin
2015-12-14  7:52     ` Andrew Vagin
2015-12-14 22:38       ` Andy Lutomirski
2015-12-15 15:53         ` Andrew Vagin
2015-12-15 16:43           ` Andy Lutomirski

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=1436172445-6979-11-git-send-email-avagin@openvz.org \
    --to=avagin@openvz.org \
    --cc=acme@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=dsahern@gmail.com \
    --cc=gorcunov@openvz.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=oleg@redhat.com \
    --cc=pavel.odintsov@gmail.com \
    --cc=rl@hellgate.ch \
    --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

all inboxes | Powered by JetHome®