Skip to contentSkip to conversation

pid: improved namespaced iteration over processes list

PATCH In review Gowrishankar M 4 participants 5 messages Dec 15, 2008 2 messages not in the archive Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pid: improved namespaced iteration over processes list

Below patch addresses a common solution for any place where a process should be checked if it is associated to caller namespace. At present, we use 'task_pid_vnr(t) > 0' to further proceed with task 't' in current namespace.

To avoid applying this check in every code related to PID namespace, this patch reworks on iterative macros;for_each_process and do_each_thread.

This patch can also reduce latency time on process list lookup inside the container, as we walk along pidmap, instead of every process in system.

---
 include/linux/sched.h |    8 +++++---
 kernel/pid.c          |   17 +++++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/sched.h +5 -3
index 2e46189..8d3b520 100644
@@ -1917,17 +1917,19 @@ static inline unsigned long wait_task_inactive(struct task_struct *p,
 }
 #endif
 
-#define next_task(p)	list_entry(rcu_dereference((p)->tasks.next), struct task_struct, tasks)
+#include <linux/nsproxy.h>
+#define next_task(p)	pid_task(find_ge_tgid(task_pid_vnr(p) + 1, p->nsproxy->pid_ns), PIDTYPE_PID)
+#define ns_init_task	(current->nsproxy->pid_ns == &init_pid_ns ? next_task((&init_task)) : find_task_by_vpid(1))
 
 #define for_each_process(p) \
-	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
+	for (p = ns_init_task ; p != NULL ; p = next_task(p))
 
 /*
  * Careful: do_each_thread/while_each_thread is a double loop so
  *          'break' will not work as expected - use goto instead.
  */
 #define do_each_thread(g, t) \
-	for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
+	for (g = t = ns_init_task ; g  != NULL ; (g = t = next_task(g))) do
 
 #define while_each_thread(g, t) \
 	while ((t = next_thread(t)) != g)
kernel/pid.c +17 -0
index 064e76a..3273a96 100644
@@ -493,6 +493,23 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
 	return pid;
 }
 
+struct pid *find_ge_tgid(int nr,  struct pid_namespace *ns)
+{
+	struct pid* pid;
+	struct task_struct* task;
+
+retry:
+	pid = find_ge_pid(nr, ns);
+	if (pid) {
+		task = pid_task(pid, PIDTYPE_PID);
+		if (!task || !has_group_leader_pid(task)) {
+			nr += 1;
+			goto retry;
+		}
+	}
+	return pid;
+}
+
 /*
  * The pid hash table is scaled according to the amount of memory in the
  * machine.  From a minimum of 16 slots up to 4096 slots at one gigabyte or
Signature · 1 line
1.5.5.1
#2 lore raw
In reply to a message not in the archive
Quoted 22 lines · Gowrishankar M

Below patch addresses a common solution for any place where a process should be checked if it is associated to caller namespace. At present, we use 'task_pid_vnr(t) > 0' to further proceed with task 't' in current namespace.

To avoid applying this check in every code related to PID namespace, this patch reworks on iterative macros;for_each_process and do_each_thread.

This patch can also reduce latency time on process list lookup inside the container, as we walk along pidmap, instead of every process in system.

---
 include/linux/sched.h |    8 +++++---
 kernel/pid.c          |   17 +++++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/sched.h +0 -0
index 2e46189..8d3b520 100644

include/linux/sched.h static inline unsigned long wait_task_inactive(struct task_struct *p, · line 1922

Quoted 7 lines
include/linux/sched.h +3 -1
@@ -1917,17 +1917,19 @@ static inline unsigned long wait_task_inactive(struct task_struct *p,
 }
 #endif
 
-#define next_task(p)	list_entry(rcu_dereference((p)->tasks.next), struct task_struct, tasks)
+#include <linux/nsproxy.h>
+#define next_task(p)	pid_task(find_ge_tgid(task_pid_vnr(p) + 1, p->nsproxy->pid_ns), PIDTYPE_PID)
+#define ns_init_task	(current->nsproxy->pid_ns == &init_pid_ns ? next_task((&init_task)) : find_task_by_vpid(1))

Can you turn these into static inlines so that they're a bit more readable?

include/linux/sched.h static inline unsigned long wait_task_inactive(struct task_struct *p, · line 1932

Quoted 11 lines
include/linux/sched.h +2 -2
 #define for_each_process(p) \
-	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
+	for (p = ns_init_task ; p != NULL ; p = next_task(p))
 
 /*
  * Careful: do_each_thread/while_each_thread is a double loop so
  *          'break' will not work as expected - use goto instead.
  */
 #define do_each_thread(g, t) \
-	for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
+	for (g = t = ns_init_task ; g  != NULL ; (g = t = next_task(g))) do

I have to wonder whether we should be changing this globally or adding a new do_each_ns_thread() or something. Are you worried this will cause some collateral damage?

Quoted 6 lines
include/linux/sched.h +6 -0
+struct pid *find_ge_tgid(int nr,  struct pid_namespace *ns)
+{
+	struct pid* pid;
+	struct task_struct* task;
+
+retry:

include/linux/sched.h static inline unsigned long wait_task_inactive(struct task_struct *p,

Quoted 10 lines
include/linux/sched.h +10 -0
+	pid = find_ge_pid(nr, ns);
+	if (pid) {
+		task = pid_task(pid, PIDTYPE_PID);
+		if (!task || !has_group_leader_pid(task)) {
+			nr += 1;
+			goto retry;
+		}
+	}
+	return pid;
+}

I might have written that loop a bit differently. Does this work? Is it any more clear?

	while (pid = find_ge_pid(nr, ns) {
		task = pid_task(pid, PIDTYPE_PID);
		if (task && has_group_leader_pid(task))
			break;
		nr++;
	}

-- Dave

#3 lore raw

Dave Hansen [[email protected]] wrote:

| On Mon, 2008-12-15 at 22:19 +0530, Gowrishankar M wrote:
| > Below patch addresses a common solution for any place where a process
| > should be checked if it is associated to caller namespace. At present,
| > we use 'task_pid_vnr(t) > 0' to further proceed with task 't' in current
| > namespace.
| > 
| > To avoid applying this check in every code related to PID namespace,
| > this patch reworks on iterative macros;for_each_process and do_each_thread.
| > 
| > This patch can also reduce latency time on process list lookup inside the
| > container, as we walk along pidmap, instead of every process in system.

The obvious trade-off is with systems that don't use containers which are porbably the majority at present. For them next_task() now becomes more expensive (instead of a simply going to next item on list, they have lookup in the pidmap, a lookup in pid hash table followed by mapping the pid back to task). I think there was a discussion once on this and the conclusion was things like "kill sig -1" are inherently expensive.

Do you need these to be optimized for containers for some other reason ?

Sukadev

#6 lore raw
In reply to a message not in the archive
Quoted 7 lines · Gowrishankar M

Below patch addresses a common solution for any place where a process should be checked if it is associated to caller namespace. At present, we use 'task_pid_vnr(t) > 0' to further proceed with task 't' in current namespace.

To avoid applying this check in every code related to PID namespace, this patch reworks on iterative macros;for_each_process and do_each_thread.

Which is just wrong. Most of the time when we call for_each_process and do_each_thread we are iterating through them for kernel internal purposes not because of a user space request.

Quoted 2 lines

This patch can also reduce latency time on process list lookup inside the container, as we walk along pidmap, instead of every process in system.

I support walking pidmap, in those cases where it makes sense. kill -1 in particular.

But I don't think there are any significant unconverted instances of that problem.

So specific helpers to do the job is fine (if the problem is more general than kill -1) but changing the generic helpers looks like a good way to introduce lots of subtle bugs into the kernel. So different names please.

Eric

#5 lore raw
In reply to a message not in the archive
Quoted 6 lines · Sukadev Bhattiprolu

The obvious trade-off is with systems that don't use containers which are porbably the majority at present. For them next_task() now becomes more expensive (instead of a simply going to next item on list, they have lookup in the pidmap, a lookup in pid hash table followed by mapping the pid back to task). I think there was a discussion once on this and the conclusion was things like "kill sig -1" are inherently expensive.

Cost wise it would be worth measuring. I have a report that when that change was made to /proc readdir in /proc sped up.

The problem that I see is that changing generic methods is not generally applicable.

Quoted 1 line

Do you need these to be optimized for containers for some other reason ?

A good question.

Eric