Skip to contentSkip to conversation

cgroups: fix pid namespace bug

PATCH In review Li Zefan 14 participants 38 messages Jul 2, 2009 – Jul 4, 2009 14 messages not in the archive cgroup

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 1/1 cgroups: fix pid namespace bug

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
 	 */
 	struct list_head release_list;
 
-	/* pids_mutex protects the fields below */
+	/* pids_mutex protects pids_list and cached pid arrays. */
 	struct rw_semaphore pids_mutex;
-	/* Array of process ids in the cgroup */
-	pid_t *tasks_pids;
-	/* How many files are using the current tasks_pids array */
-	int pids_use_count;
-	/* Length of the current tasks_pids array */
-	int pids_length;
+
+	/* Linked list of struct cgroup_pids */
+	struct list_head pids_list;
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
kernel/cgroup.c +70 -24

191 lines not on this page · raw message

Signature · 1 line
1.5.4.rc3
#1 lore raw

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

Paul

Quoted 247 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +26 -5
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};
+
  static int cmppid(const void *a, const void *b)
  {
        return *(pid_t *)a - *(pid_t *)b;
  }
 
-
  /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
         * after a seek to the start). Use a binary-search to find the
         * next pid to display, if any
         */
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp;
        int index = 0, pid = *pos;
        int *iter;
 
        down_read(&cgrp->pids_mutex);
        if (pid) {
-               int end = cgrp->pids_length;
+               int end = cp->pids_length;
 
                while (index < end) {
                        int mid = (index + end) / 2;
-                       if (cgrp->tasks_pids[mid] == pid) {
+                       if (cp->tasks_pids[mid] == pid) {
                                index = mid;
                                break;
-                       } else if (cgrp->tasks_pids[mid] <= pid)
+                       } else if (cp->tasks_pids[mid] <= pid)
                                index = mid + 1;

                       else
                               end = mid;
               }
       }
       /* If we're off the end of the array, we're done */
-       if (index >= cgrp->pids_length)
+       if (index >= cp->pids_length)
               return NULL;
       /* Update the abstract position to be the actual pid that we found */
-       iter = cgrp->tasks_pids + index;
+       iter = cp->tasks_pids + index;
       *pos = *iter;
       return iter;
 }

 static void cgroup_tasks_stop(struct seq_file *s, void *v) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp; up_read(&cgrp->pids_mutex);
 }

 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
       int *p = v;
-       int *end = cgrp->tasks_pids + cgrp->pids_length;
+       int *end = cp->tasks_pids + cp->pids_length;

       /*
        * Advance to the next pid in the array. If this goes off the

kernel/cgroup.c +38 -15
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
        .show = cgroup_tasks_show,
  };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
  {
+       struct cgroup *cgrp = cp->cgrp;
+
        down_write(&cgrp->pids_mutex);
-       BUG_ON(!cgrp->pids_use_count);
-       if (!--cgrp->pids_use_count) {
-               kfree(cgrp->tasks_pids);
-               cgrp->tasks_pids = NULL;
-               cgrp->pids_length = 0;
+       BUG_ON(!cp->pids_use_count);
+       if (!--cp->pids_use_count) {
+               list_del(&cp->list);
+               kfree(cp->tasks_pids);
+               kfree(cp);
        }
        up_write(&cgrp->pids_mutex);
  }
 
  static int cgroup_tasks_release(struct inode *inode, struct file *file)
  {
-       struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct seq_file *seq;
+       struct cgroup_pids *cp;
 
        if (!(file->f_mode & FMODE_READ))
                return 0;
 
-       release_cgroup_pid_array(cgrp);
+       seq = file->private_data;
+       cp = seq->private;
+
+       release_cgroup_pid_array(cp);
        return seq_release(inode, file);
  }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;
+       list_add(&cp->list, &cgrp->pids_list);
+found:
+       kfree(cp->tasks_pids);
+       cp->tasks_pids = pidarray;
+       cp->pids_length = npids;
+       cp->pids_use_count++;
        up_write(&cgrp->pids_mutex);
 
        file->f_op = &cgroup_tasks_operations;
 
        retval = seq_open(file, &cgroup_tasks_seq_operations);
        if (retval) {
-               release_cgroup_pid_array(cgrp);
+               release_cgroup_pid_array(cp);
                return retval;
        }
-       ((struct seq_file *)file->private_data)->private = cgrp;
+       ((struct seq_file *)file->private_data)->private = cp;
        return 0;
  }
 
--

1.5.4.rc3

author
#2 lore raw
Quoted 9 lines · Paul Menage

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Quoted 4 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

It's fine for me. I waited for your fix for a week or so but didn't see it, so I thought you were stuck in other business and I did this fix.

A message that is not in the archive (sent off-list or not yet received): 6599ad830907011836x5eccc83eyc896a67295a6486d-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#4 lore raw
Quoted 9 lines · Paul Menage

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Quoted 4 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

It's fine for me. I waited for your fix for a week or so but didn't see it, so I thought you were stuck in other business and I did this fix.

#5 lore raw
In reply to a message not in the archive
Quoted 9 lines · On Wed, 1 Jul 2009 18:36:36 -0700 Paul Menage <[email protected]> wrote:

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Wow, I welcome "procs" file :)

Off-topic:
Is there relationship betweem mm->owner and tgid ? If no, is it difficult to synchronize tgid and mm->owner ?

THanks,
-Kame

Quoted 251 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

Paul

Quoted 244 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +26 -5
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};
+
  static int cmppid(const void *a, const void *b)
  {
        return *(pid_t *)a - *(pid_t *)b;
  }
 
-
  /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
         * after a seek to the start). Use a binary-search to find the
         * next pid to display, if any
         */
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp;
        int index = 0, pid = *pos;
        int *iter;
 
        down_read(&cgrp->pids_mutex);
        if (pid) {
-               int end = cgrp->pids_length;
+               int end = cp->pids_length;
 
                while (index < end) {
                        int mid = (index + end) / 2;
-                       if (cgrp->tasks_pids[mid] == pid) {
+                       if (cp->tasks_pids[mid] == pid) {
                                index = mid;
                                break;
-                       } else if (cgrp->tasks_pids[mid] <= pid)
+                       } else if (cp->tasks_pids[mid] <= pid)
                                index = mid + 1;

                       else
                               end = mid;
               }
       }
       /* If we're off the end of the array, we're done */
-       if (index >= cgrp->pids_length)
+       if (index >= cp->pids_length)
               return NULL;
       /* Update the abstract position to be the actual pid that we found */
-       iter = cgrp->tasks_pids + index;
+       iter = cp->tasks_pids + index;
       *pos = *iter;
       return iter;
 }

 static void cgroup_tasks_stop(struct seq_file *s, void *v) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp; up_read(&cgrp->pids_mutex);
 }

 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
       int *p = v;
-       int *end = cgrp->tasks_pids + cgrp->pids_length;
+       int *end = cp->tasks_pids + cp->pids_length;

       /*
        * Advance to the next pid in the array. If this goes off the

kernel/cgroup.c +38 -14
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
        .show = cgroup_tasks_show,
  };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
  {
+       struct cgroup *cgrp = cp->cgrp;
+
        down_write(&cgrp->pids_mutex);
-       BUG_ON(!cgrp->pids_use_count);
-       if (!--cgrp->pids_use_count) {
-               kfree(cgrp->tasks_pids);
-               cgrp->tasks_pids = NULL;
-               cgrp->pids_length = 0;
+       BUG_ON(!cp->pids_use_count);
+       if (!--cp->pids_use_count) {
+               list_del(&cp->list);
+               kfree(cp->tasks_pids);
+               kfree(cp);
        }
        up_write(&cgrp->pids_mutex);
  }
 
  static int cgroup_tasks_release(struct inode *inode, struct file *file)
  {
-       struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct seq_file *seq;
+       struct cgroup_pids *cp;
 
        if (!(file->f_mode & FMODE_READ))
                return 0;
 
-       release_cgroup_pid_array(cgrp);
+       seq = file->private_data;
+       cp = seq->private;
+
+       release_cgroup_pid_array(cp);
        return seq_release(inode, file);
  }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;
+       list_add(&cp->list, &cgrp->pids_list);
+found:
+       kfree(cp->tasks_pids);
+       cp->tasks_pids = pidarray;
+       cp->pids_length = npids;
+       cp->pids_use_count++;
        up_write(&cgrp->pids_mutex);
 
        file->f_op = &cgroup_tasks_operations;
 
        retval = seq_open(file, &cgroup_tasks_seq_operations);
        if (retval) {
-               release_cgroup_pid_array(cgrp);
+               release_cgroup_pid_array(cp);
                return retval;
        }
-       ((struct seq_file *)file->private_data)->private = cgrp;
+       ((struct seq_file *)file->private_data)->private = cp;
        return 0;
  }
 
Quoted 8 lines
Quoted 3 lines

--
1.5.4.rc3

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

#6 lore raw
In reply to a message not in the archive
Quoted 9 lines · Paul Menage

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Quoted 4 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

#7 lore raw
In reply to Paul Menage
Quoted 9 lines · On Wed, 1 Jul 2009 18:36:36 -0700 Paul Menage <[email protected]> wrote:

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Wow, I welcome "procs" file :)

Off-topic:
Is there relationship betweem mm->owner and tgid ? If no, is it difficult to synchronize tgid and mm->owner ?

THanks,
-Kame

Quoted 259 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

Paul

Quoted 245 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +26 -5
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};
+
  static int cmppid(const void *a, const void *b)
  {
        return *(pid_t *)a - *(pid_t *)b;
  }
 
-
  /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
         * after a seek to the start). Use a binary-search to find the
         * next pid to display, if any
         */
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp;
        int index = 0, pid = *pos;
        int *iter;
 
        down_read(&cgrp->pids_mutex);
        if (pid) {
-               int end = cgrp->pids_length;
+               int end = cp->pids_length;
 
                while (index < end) {
                        int mid = (index + end) / 2;
-                       if (cgrp->tasks_pids[mid] == pid) {
+                       if (cp->tasks_pids[mid] == pid) {
                                index = mid;
                                break;
-                       } else if (cgrp->tasks_pids[mid] <= pid)
+                       } else if (cp->tasks_pids[mid] <= pid)
                                index = mid + 1;

                       else
                               end = mid;
               }
       }
       /* If we're off the end of the array, we're done */
-       if (index >= cgrp->pids_length)
+       if (index >= cp->pids_length)
               return NULL;
       /* Update the abstract position to be the actual pid that we found */
-       iter = cgrp->tasks_pids + index;
+       iter = cp->tasks_pids + index;
       *pos = *iter;
       return iter;
 }

 static void cgroup_tasks_stop(struct seq_file *s, void *v) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp; up_read(&cgrp->pids_mutex);
 }

 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
       int *p = v;
-       int *end = cgrp->tasks_pids + cgrp->pids_length;
+       int *end = cp->tasks_pids + cp->pids_length;

       /*
        * Advance to the next pid in the array. If this goes off the

kernel/cgroup.c +38 -15
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
        .show = cgroup_tasks_show,
  };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
  {
+       struct cgroup *cgrp = cp->cgrp;
+
        down_write(&cgrp->pids_mutex);
-       BUG_ON(!cgrp->pids_use_count);
-       if (!--cgrp->pids_use_count) {
-               kfree(cgrp->tasks_pids);
-               cgrp->tasks_pids = NULL;
-               cgrp->pids_length = 0;
+       BUG_ON(!cp->pids_use_count);
+       if (!--cp->pids_use_count) {
+               list_del(&cp->list);
+               kfree(cp->tasks_pids);
+               kfree(cp);
        }
        up_write(&cgrp->pids_mutex);
  }
 
  static int cgroup_tasks_release(struct inode *inode, struct file *file)
  {
-       struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct seq_file *seq;
+       struct cgroup_pids *cp;
 
        if (!(file->f_mode & FMODE_READ))
                return 0;
 
-       release_cgroup_pid_array(cgrp);
+       seq = file->private_data;
+       cp = seq->private;
+
+       release_cgroup_pid_array(cp);
        return seq_release(inode, file);
  }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;
+       list_add(&cp->list, &cgrp->pids_list);
+found:
+       kfree(cp->tasks_pids);
+       cp->tasks_pids = pidarray;
+       cp->pids_length = npids;
+       cp->pids_use_count++;
        up_write(&cgrp->pids_mutex);
 
        file->f_op = &cgroup_tasks_operations;
 
        retval = seq_open(file, &cgroup_tasks_seq_operations);
        if (retval) {
-               release_cgroup_pid_array(cgrp);
+               release_cgroup_pid_array(cp);
                return retval;
        }
-       ((struct seq_file *)file->private_data)->private = cgrp;
+       ((struct seq_file *)file->private_data)->private = cp;
        return 0;
  }
 
--
Quoted 2 lines

1.5.4.rc3

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20090702105707.8b2135d9.kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org

#9 lore raw
Quoted 12 lines · KAMEZAWA Hiroyuki
Quoted 9 lines · On Wed, 1 Jul 2009 18:36:36 -0700 Paul Menage…

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Quoted 2 lines

Wow, I welcome "procs" file :)

Yes, we discussed this in the Resouce Management topic of the containers mini-summit. I hope we are talking of the same thing.

Quoted 4 lines

Off-topic:
Is there relationship betweem mm->owner and tgid ? If no, is it difficult to synchronize tgid and mm->owner ?

At the beginning tgid is mm->owner.

Quoted 261 lines
Quoted 260 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

Paul

Quoted 245 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +26 -5
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};
+
  static int cmppid(const void *a, const void *b)
  {
        return *(pid_t *)a - *(pid_t *)b;
  }
 
-
  /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
         * after a seek to the start). Use a binary-search to find the
         * next pid to display, if any
         */
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp;
        int index = 0, pid = *pos;
        int *iter;
 
        down_read(&cgrp->pids_mutex);
        if (pid) {
-               int end = cgrp->pids_length;
+               int end = cp->pids_length;
 
                while (index < end) {
                        int mid = (index + end) / 2;
-                       if (cgrp->tasks_pids[mid] == pid) {
+                       if (cp->tasks_pids[mid] == pid) {
                                index = mid;
                                break;
-                       } else if (cgrp->tasks_pids[mid] <= pid)
+                       } else if (cp->tasks_pids[mid] <= pid)
                                index = mid + 1;

                       else
                               end = mid;
               }
       }
       /* If we're off the end of the array, we're done */
-       if (index >= cgrp->pids_length)
+       if (index >= cp->pids_length)
               return NULL;
       /* Update the abstract position to be the actual pid that we found */
-       iter = cgrp->tasks_pids + index;
+       iter = cp->tasks_pids + index;
       *pos = *iter;
       return iter;
 }

 static void cgroup_tasks_stop(struct seq_file *s, void *v) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp; up_read(&cgrp->pids_mutex);
 }

 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
       int *p = v;
-       int *end = cgrp->tasks_pids + cgrp->pids_length;
+       int *end = cp->tasks_pids + cp->pids_length;

       /*
        * Advance to the next pid in the array. If this goes off the

kernel/cgroup.c +38 -15
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
        .show = cgroup_tasks_show,
  };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
  {
+       struct cgroup *cgrp = cp->cgrp;
+
        down_write(&cgrp->pids_mutex);
-       BUG_ON(!cgrp->pids_use_count);
-       if (!--cgrp->pids_use_count) {
-               kfree(cgrp->tasks_pids);
-               cgrp->tasks_pids = NULL;
-               cgrp->pids_length = 0;
+       BUG_ON(!cp->pids_use_count);
+       if (!--cp->pids_use_count) {
+               list_del(&cp->list);
+               kfree(cp->tasks_pids);
+               kfree(cp);
        }
        up_write(&cgrp->pids_mutex);
  }
 
  static int cgroup_tasks_release(struct inode *inode, struct file *file)
  {
-       struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct seq_file *seq;
+       struct cgroup_pids *cp;
 
        if (!(file->f_mode & FMODE_READ))
                return 0;
 
-       release_cgroup_pid_array(cgrp);
+       seq = file->private_data;
+       cp = seq->private;
+
+       release_cgroup_pid_array(cp);
        return seq_release(inode, file);
  }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;
+       list_add(&cp->list, &cgrp->pids_list);
+found:
+       kfree(cp->tasks_pids);
+       cp->tasks_pids = pidarray;
+       cp->pids_length = npids;
+       cp->pids_use_count++;
        up_write(&cgrp->pids_mutex);
 
        file->f_op = &cgroup_tasks_operations;
 
        retval = seq_open(file, &cgroup_tasks_seq_operations);
        if (retval) {
-               release_cgroup_pid_array(cgrp);
+               release_cgroup_pid_array(cp);
                return retval;
        }
-       ((struct seq_file *)file->private_data)->private = cgrp;
+       ((struct seq_file *)file->private_data)->private = cp;
        return 0;
  }
 
--
Quoted 2 lines

1.5.4.rc3

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

Signature · 1 line
	Balbir
#10 lore raw
In reply to KAMEZAWA Hiroyuki
Quoted 12 lines · KAMEZAWA Hiroyuki
Quoted 9 lines · On Wed, 1 Jul 2009 18:36:36 -0700 Paul Menage <[email protected]> wrote:

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Quoted 2 lines

Wow, I welcome "procs" file :)

Yes, we discussed this in the Resouce Management topic of the containers mini-summit. I hope we are talking of the same thing.

Quoted 4 lines

Off-topic:
Is there relationship betweem mm->owner and tgid ? If no, is it difficult to synchronize tgid and mm->owner ?

At the beginning tgid is mm->owner.

Quoted 261 lines
Quoted 260 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

Paul

Quoted 245 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +26 -5
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};
+
  static int cmppid(const void *a, const void *b)
  {
        return *(pid_t *)a - *(pid_t *)b;
  }
 
-
  /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
         * after a seek to the start). Use a binary-search to find the
         * next pid to display, if any
         */
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp;
        int index = 0, pid = *pos;
        int *iter;
 
        down_read(&cgrp->pids_mutex);
        if (pid) {
-               int end = cgrp->pids_length;
+               int end = cp->pids_length;
 
                while (index < end) {
                        int mid = (index + end) / 2;
-                       if (cgrp->tasks_pids[mid] == pid) {
+                       if (cp->tasks_pids[mid] == pid) {
                                index = mid;
                                break;
-                       } else if (cgrp->tasks_pids[mid] <= pid)
+                       } else if (cp->tasks_pids[mid] <= pid)
                                index = mid + 1;

                       else
                               end = mid;
               }
       }
       /* If we're off the end of the array, we're done */
-       if (index >= cgrp->pids_length)
+       if (index >= cp->pids_length)
               return NULL;
       /* Update the abstract position to be the actual pid that we found */
-       iter = cgrp->tasks_pids + index;
+       iter = cp->tasks_pids + index;
       *pos = *iter;
       return iter;
 }

 static void cgroup_tasks_stop(struct seq_file *s, void *v) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
+       struct cgroup *cgrp = cp->cgrp; up_read(&cgrp->pids_mutex);
 }

 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) {
-       struct cgroup *cgrp = s->private;
+       struct cgroup_pids *cp = s->private;
       int *p = v;
-       int *end = cgrp->tasks_pids + cgrp->pids_length;
+       int *end = cp->tasks_pids + cp->pids_length;

       /*
        * Advance to the next pid in the array. If this goes off the

kernel/cgroup.c +38 -15
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
        .show = cgroup_tasks_show,
  };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
  {
+       struct cgroup *cgrp = cp->cgrp;
+
        down_write(&cgrp->pids_mutex);
-       BUG_ON(!cgrp->pids_use_count);
-       if (!--cgrp->pids_use_count) {
-               kfree(cgrp->tasks_pids);
-               cgrp->tasks_pids = NULL;
-               cgrp->pids_length = 0;
+       BUG_ON(!cp->pids_use_count);
+       if (!--cp->pids_use_count) {
+               list_del(&cp->list);
+               kfree(cp->tasks_pids);
+               kfree(cp);
        }
        up_write(&cgrp->pids_mutex);
  }
 
  static int cgroup_tasks_release(struct inode *inode, struct file *file)
  {
-       struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct seq_file *seq;
+       struct cgroup_pids *cp;
 
        if (!(file->f_mode & FMODE_READ))
                return 0;
 
-       release_cgroup_pid_array(cgrp);
+       seq = file->private_data;
+       cp = seq->private;
+
+       release_cgroup_pid_array(cp);
        return seq_release(inode, file);
  }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;
+       list_add(&cp->list, &cgrp->pids_list);
+found:
+       kfree(cp->tasks_pids);
+       cp->tasks_pids = pidarray;
+       cp->pids_length = npids;
+       cp->pids_use_count++;
        up_write(&cgrp->pids_mutex);
 
        file->f_op = &cgroup_tasks_operations;
 
        retval = seq_open(file, &cgroup_tasks_seq_operations);
        if (retval) {
-               release_cgroup_pid_array(cgrp);
+               release_cgroup_pid_array(cp);
                return retval;
        }
-       ((struct seq_file *)file->private_data)->private = cgrp;
+       ((struct seq_file *)file->private_data)->private = cp;
        return 0;
  }
 
--
Quoted 2 lines

1.5.4.rc3

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

Signature · 1 line
	Balbir
author
#11 lore raw
In reply to Paul Menage
Quoted 9 lines · Paul Menage

Thanks Li - but as I said to Serge in the email when I brought this up originally, I already had a patch in mind for this; I've had an intern (Ben) at Google working on it. His patch (pretty much ready, and being sent out tomorrow I hope) is pretty similar to yours, but his is on top of another patch that provides a (currently read-only) "cgroup.procs" file in each cgroup dir that lists the unique tgids in the cgroup. So the key in the list of pid arrays is actually a pid_ns and a file type (indicating procs or tasks) rather than just a pid_ns.

Quoted 4 lines

So I think it makes more sense to not use this patch, but to use the pair of patches that Ben's written, since they provide more overal functionality.

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw
Quoted 4 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

Paul

#14 lore raw
In reply to Li Zefan
Quoted 4 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

Paul

author
#15 lore raw
Quoted 9 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

If so, it's ok for me. Unless someone else has objections. Serge?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#17 lore raw
Quoted 9 lines · Li Zefan
Quoted 8 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Quoted 1 line

If so, it's ok for me. Unless someone else has objections. Serge?

Well, on the one hand it's not a horrible bug in that at least it won't crash the kernel. But what bugs me is that there is no workaround for userspace, no way for an admin to know that if he does for t in cat /cgroup/victim/tasks; do kill $t; done he won't kill his mysql server.

I think that's a bad enough risk to make it worth trying to push Li's patch. Surely changing Ben's procs file should be pretty trivial to rebase?

thanks,
-serge

#18 lore raw
In reply to Li Zefan
Quoted 9 lines · Li Zefan
Quoted 8 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Quoted 1 line

If so, it's ok for me. Unless someone else has objections. Serge?

Well, on the one hand it's not a horrible bug in that at least it won't crash the kernel. But what bugs me is that there is no workaround for userspace, no way for an admin to know that if he does for t in cat /cgroup/victim/tasks; do kill $t; done he won't kill his mysql server.

I think that's a bad enough risk to make it worth trying to push Li's patch. Surely changing Ben's procs file should be pretty trivial to rebase?

thanks,
-serge

A message that is not in the archive (sent off-list or not yet received): 20090702132659.GA8051-r/[email protected]

#20 lore raw
Quoted 7 lines · Serge E. Hallyn
Quoted 6 lines · Li Zefan
Quoted 5 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months back). Pid namespaces are useful for process migration (which is still under development) or virtual servers (for which user namespaces are pretty much essential). So I'm not sure quite what you'd use pid namespaces for yet.

Paul

#21 lore raw
In reply to Serge E. Hallyn
Quoted 7 lines · Serge E. Hallyn
Quoted 6 lines · Li Zefan
Quoted 5 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months back). Pid namespaces are useful for process migration (which is still under development) or virtual servers (for which user namespaces are pretty much essential). So I'm not sure quite what you'd use pid namespaces for yet.

Paul

A message that is not in the archive (sent off-list or not yet received): 6599ad830907020843l7ce75abfq3e78b8f67a407ab9-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#23 lore raw
Quoted 12 lines · Paul Menage
Quoted 11 lines · Serge E. Hallyn
Quoted 9 lines · Li Zefan
Quoted 8 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

Quoted 5 lines
Quoted 2 lines

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months

incomplete (due to signaling issues which have mostly been resolved) but stable and usable.

user namespace are a completely different story :)

Quoted 4 lines

back). Pid namespaces are useful for process migration (which is still under development) or virtual servers (for which user namespaces are pretty much essential). So I'm not sure quite what you'd use pid namespaces for yet.

You don't need user namespaces to use pid namespaces for virtual servers (depending on your use).

Now the fact remains this is a hard to trigger bug which doesn't corrupt the kernel, and - to take back what I said earlier - userspace can work around it by simply freezing the cgroup before reading its tasks file.

So I guess I can go either way... If Li's patch were more complicated I'd definately be for waiting. But I do object to the general process of making a fix of a pretty bad bag depend on an unrelated new feature!

-serge

#24 lore raw
In reply to a message not in the archive
Quoted 15 lines · Paul Menage
Quoted 7 lines · Serge E. Hallyn
Quoted 6 lines · Li Zefan
Quoted 5 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Quoted 6 lines

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months back). Pid namespaces are useful for process migration (which is still under development) or virtual servers (for which user namespaces are pretty much essential). So I'm not sure quite what you'd use pid namespaces for yet.

I have pid namespaces in pretty heavy use already.

Inescapable process groups are quite handy.

Eric

#25 lore raw
In reply to Paul Menage
Quoted 12 lines · Paul Menage
Quoted 11 lines · Serge E. Hallyn
Quoted 9 lines · Li Zefan
Quoted 8 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

Quoted 5 lines
Quoted 2 lines

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months

incomplete (due to signaling issues which have mostly been resolved) but stable and usable.

user namespace are a completely different story :)

Quoted 4 lines

back). Pid namespaces are useful for process migration (which is still under development) or virtual servers (for which user namespaces are pretty much essential). So I'm not sure quite what you'd use pid namespaces for yet.

You don't need user namespaces to use pid namespaces for virtual servers (depending on your use).

Now the fact remains this is a hard to trigger bug which doesn't corrupt the kernel, and - to take back what I said earlier - userspace can work around it by simply freezing the cgroup before reading its tasks file.

So I guess I can go either way... If Li's patch were more complicated I'd definately be for waiting. But I do object to the general process of making a fix of a pretty bad bag depend on an unrelated new feature!

-serge

A message that is not in the archive (sent off-list or not yet received): 20090702161548.GA13383-r/[email protected]

#27 lore raw
Quoted 8 lines · Serge E. Hallyn
Quoted 3 lines

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months

incomplete (due to signaling issues which have mostly been resolved) but stable and usable.

user namespace are a completely different story :)

Sorry, that wasn't clear - the "very incomplete" referred to user namespaces.

Paul

#28 lore raw
In reply to Serge E. Hallyn
Quoted 8 lines · Serge E. Hallyn
Quoted 3 lines

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months

incomplete (due to signaling issues which have mostly been resolved) but stable and usable.

user namespace are a completely different story :)

Sorry, that wasn't clear - the "very incomplete" referred to user namespaces.

Paul

A message that is not in the archive (sent off-list or not yet received): 6599ad830907020927x44a88a6dgb229fb2ad6ef5481-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Reviewed-by Serge Hallyn
#30 lore raw
Quoted 5 lines · On Thu, 2 Jul 2009 09:27:20 -0700 Paul Menage…
Quoted 4 lines · Serge E. Hallyn
Quoted 3 lines

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months

Quoted 7 lines
Quoted 4 lines

incomplete (due to signaling issues which have mostly been resolved) but stable and usable.

user namespace are a completely different story :)

Sorry, that wasn't clear - the "very incomplete" referred to user namespaces.

I do think we should lean toward fixing it. After all, it used to work OK and now it doesn't.

It is a three-minute matter of patch-wrangling to take the fix out again within a more comprehensive 2.6.32 patch series, so that's not an issue.

So are there any strong (enough) objections to putting this into 2.6.31?

From: Li Zefan <[email protected]>

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---

 include/linux/cgroup.h |   11 +---
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++----------
 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h renamed from include/linux/cgroup.h~cgroups-fix-pid-namespace-bug +4 -7
diff -puN include/linux/cgroup.h~cgroups-fix-pid-namespace-bug include/linux/cgroup.h
@@ -179,14 +179,11 @@ struct cgroup {
 	 */
 	struct list_head release_list;
 
-	/* pids_mutex protects the fields below */
+	/* pids_mutex protects pids_list and cached pid arrays. */
 	struct rw_semaphore pids_mutex;
-	/* Array of process ids in the cgroup */
-	pid_t *tasks_pids;
-	/* How many files are using the current tasks_pids array */
-	int pids_use_count;
-	/* Length of the current tasks_pids array */
-	int pids_length;
+
+	/* Linked list of struct cgroup_pids */
+	struct list_head pids_list;
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
kernel/cgroup.c renamed from kernel/cgroup.c~cgroups-fix-pid-namespace-bug +70 -24

191 lines not on this page · raw message

_

Reviewed-by Serge Hallyn
#31 lore raw
In reply to Paul Menage
Quoted 5 lines · On Thu, 2 Jul 2009 09:27:20 -0700 Paul Menage <[email protected]> wrote:
Quoted 4 lines · Serge E. Hallyn
Quoted 3 lines

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months

Quoted 7 lines
Quoted 4 lines

incomplete (due to signaling issues which have mostly been resolved) but stable and usable.

user namespace are a completely different story :)

Sorry, that wasn't clear - the "very incomplete" referred to user namespaces.

I do think we should lean toward fixing it. After all, it used to work OK and now it doesn't.

It is a three-minute matter of patch-wrangling to take the fix out again within a more comprehensive 2.6.32 patch series, so that's not an issue.

So are there any strong (enough) objections to putting this into 2.6.31?

From: Li Zefan <[email protected]>

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---

 include/linux/cgroup.h |   11 +---
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++----------
 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h renamed from include/linux/cgroup.h~cgroups-fix-pid-namespace-bug +4 -7
diff -puN include/linux/cgroup.h~cgroups-fix-pid-namespace-bug include/linux/cgroup.h
@@ -179,14 +179,11 @@ struct cgroup {
 	 */
 	struct list_head release_list;
 
-	/* pids_mutex protects the fields below */
+	/* pids_mutex protects pids_list and cached pid arrays. */
 	struct rw_semaphore pids_mutex;
-	/* Array of process ids in the cgroup */
-	pid_t *tasks_pids;
-	/* How many files are using the current tasks_pids array */
-	int pids_use_count;
-	/* Length of the current tasks_pids array */
-	int pids_length;
+
+	/* Linked list of struct cgroup_pids */
+	struct list_head pids_list;
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
kernel/cgroup.c renamed from kernel/cgroup.c~cgroups-fix-pid-namespace-bug +70 -24

191 lines not on this page · raw message

_

A message that is not in the archive (sent off-list or not yet received): 20090702162019.f34f12d6.akpm-de/[email protected]

#33 lore raw
Quoted 10 lines · Andrew Morton

I do think we should lean toward fixing it.  After all, it used to work OK and now it doesn't.

It is a three-minute matter of patch-wrangling to take the fix out again within a more comprehensive 2.6.32 patch series, so that's not an issue.

So are there any strong (enough) objections to putting this into 2.6.31?

I guess no strong objection on the principle, but the patch as it stands is missing a couple of pid_ns refcount operations in order to be a complete fix (IMO).

Paul

#34 lore raw
In reply to Andrew Morton
Quoted 10 lines · Andrew Morton

I do think we should lean toward fixing it.  After all, it used to work OK and now it doesn't.

It is a three-minute matter of patch-wrangling to take the fix out again within a more comprehensive 2.6.32 patch series, so that's not an issue.

So are there any strong (enough) objections to putting this into 2.6.31?

I guess no strong objection on the principle, but the patch as it stands is missing a couple of pid_ns refcount operations in order to be a complete fix (IMO).

Paul

#35 lore raw
In reply to Paul Menage
Quoted 15 lines · Paul Menage
Quoted 7 lines · Serge E. Hallyn
Quoted 6 lines · Li Zefan
Quoted 5 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

I don't know where the bar is for 'production environments', but I'd have to claim that pid namespaces are there...

Quoted 6 lines

Well, pid namespaces are marked as experimental, as are user namespaces (and were described as "very incomplete" a few months back). Pid namespaces are useful for process migration (which is still under development) or virtual servers (for which user namespaces are pretty much essential). So I'm not sure quite what you'd use pid namespaces for yet.

I have pid namespaces in pretty heavy use already.

Inescapable process groups are quite handy.

Eric

A message that is not in the archive (sent off-list or not yet received): 6599ad830907011920r44df4022p53808b574da4a886-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#37 lore raw
Quoted 9 lines · Paul Menage
Quoted 3 lines · Li Zefan

But I guess we are going to fix the bug for 2.6.31? So is it ok to merge a new feature 'cgroup.procs' together into 2.6.31?

Does this bug really need to be fixed for 2.6.31? I didn't think that the namespace support in mainline was robust enough yet for people to use them for virtual servers in production environments.

If so, it's ok for me. Unless someone else has objections. Serge?

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#39 lore raw
Quoted 8 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

Quoted 6 lines

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

Quoted 232 lines
---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
 	 */
 	struct list_head release_list;
 
-	/* pids_mutex protects the fields below */
+	/* pids_mutex protects pids_list and cached pid arrays. */
 	struct rw_semaphore pids_mutex;
-	/* Array of process ids in the cgroup */
-	pid_t *tasks_pids;
-	/* How many files are using the current tasks_pids array */
-	int pids_use_count;
-	/* Length of the current tasks_pids array */
-	int pids_length;
+
+	/* Linked list of struct cgroup_pids */
+	struct list_head pids_list;
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
kernel/cgroup.c +70 -24
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
 #include <linux/hash.h>
 #include <linux/namei.h>
 #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
 #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
 	INIT_LIST_HEAD(&cgrp->children);
 	INIT_LIST_HEAD(&cgrp->css_sets);
 	INIT_LIST_HEAD(&cgrp->release_list);
+	INIT_LIST_HEAD(&cgrp->pids_list);
 	init_rwsem(&cgrp->pids_mutex);
 }
 static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
 	return ret;
 }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+	/* The node in cgrp->pids_list */
+	struct list_head list;
+	/* The cgroup those pids belong to */
+	struct cgroup *cgrp;
+	/* The namepsace those pids belong to */
+	struct pid_namespace *pid_ns;
+	/* Array of process ids in the cgroup */
+	pid_t *tasks_pids;
+	/* How many files are using the this tasks_pids array */
+	int pids_use_count;
+	/* Length of the current tasks_pids array */
+	int pids_length;
+};
+
 static int cmppid(const void *a, const void *b)
 {
 	return *(pid_t *)a - *(pid_t *)b;
 }
 
-
 /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
 	 * after a seek to the start). Use a binary-search to find the
 	 * next pid to display, if any
 	 */
-	struct cgroup *cgrp = s->private;
+	struct cgroup_pids *cp = s->private;
+	struct cgroup *cgrp = cp->cgrp;
 	int index = 0, pid = *pos;
 	int *iter;
 
 	down_read(&cgrp->pids_mutex);
 	if (pid) {
-		int end = cgrp->pids_length;
+		int end = cp->pids_length;
 
 		while (index < end) {
 			int mid = (index + end) / 2;
-			if (cgrp->tasks_pids[mid] == pid) {
+			if (cp->tasks_pids[mid] == pid) {
 				index = mid;
 				break;
-			} else if (cgrp->tasks_pids[mid] <= pid)
+			} else if (cp->tasks_pids[mid] <= pid)
 				index = mid + 1;
 			else
 				end = mid;
 		}
 	}
 	/* If we're off the end of the array, we're done */
-	if (index >= cgrp->pids_length)
+	if (index >= cp->pids_length)
 		return NULL;
 	/* Update the abstract position to be the actual pid that we found */
-	iter = cgrp->tasks_pids + index;
+	iter = cp->tasks_pids + index;
 	*pos = *iter;
 	return iter;
 }
 
 static void cgroup_tasks_stop(struct seq_file *s, void *v)
 {
-	struct cgroup *cgrp = s->private;
+	struct cgroup_pids *cp = s->private;
+	struct cgroup *cgrp = cp->cgrp;
 	up_read(&cgrp->pids_mutex);
 }
 
 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
 {
-	struct cgroup *cgrp = s->private;
+	struct cgroup_pids *cp = s->private;
 	int *p = v;
-	int *end = cgrp->tasks_pids + cgrp->pids_length;
+	int *end = cp->tasks_pids + cp->pids_length;
 
 	/*
 	 * Advance to the next pid in the array. If this goes off the
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
 	.show = cgroup_tasks_show,
 };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
 {
+	struct cgroup *cgrp = cp->cgrp;
+
 	down_write(&cgrp->pids_mutex);
-	BUG_ON(!cgrp->pids_use_count);
-	if (!--cgrp->pids_use_count) {
-		kfree(cgrp->tasks_pids);
-		cgrp->tasks_pids = NULL;
-		cgrp->pids_length = 0;
+	BUG_ON(!cp->pids_use_count);
+	if (!--cp->pids_use_count) {
+		list_del(&cp->list);
+		kfree(cp->tasks_pids);
+		kfree(cp);
 	}
 	up_write(&cgrp->pids_mutex);
 }
 
 static int cgroup_tasks_release(struct inode *inode, struct file *file)
 {
-	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+	struct seq_file *seq;
+	struct cgroup_pids *cp;
 
 	if (!(file->f_mode & FMODE_READ))
 		return 0;
 
-	release_cgroup_pid_array(cgrp);
+	seq = file->private_data;
+	cp = seq->private;
+
+	release_cgroup_pid_array(cp);
 	return seq_release(inode, file);
 }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
 static int cgroup_tasks_open(struct inode *unused, struct file *file)
 {
 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
+	struct cgroup_pids *cp;
 	pid_t *pidarray;
 	int npids;
 	int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
 	 * array if necessary
 	 */
 	down_write(&cgrp->pids_mutex);
-	kfree(cgrp->tasks_pids);
-	cgrp->tasks_pids = pidarray;
-	cgrp->pids_length = npids;
-	cgrp->pids_use_count++;
+
+	list_for_each_entry(cp, &cgrp->pids_list, list) {
+		if (pid_ns == cp->pid_ns)
+			goto found;
+	}
+
+	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+	if (!cp) {
+		up_write(&cgrp->pids_mutex);
+		kfree(pidarray);
+		return -ENOMEM;
+	}
+	cp->cgrp = cgrp;
+	cp->pid_ns = pid_ns;
+	list_add(&cp->list, &cgrp->pids_list);
+found:
+	kfree(cp->tasks_pids);
+	cp->tasks_pids = pidarray;
+	cp->pids_length = npids;
+	cp->pids_use_count++;
 	up_write(&cgrp->pids_mutex);
 
 	file->f_op = &cgroup_tasks_operations;
 
 	retval = seq_open(file, &cgroup_tasks_seq_operations);
 	if (retval) {
-		release_cgroup_pid_array(cgrp);
+		release_cgroup_pid_array(cp);
 		return retval;
 	}
-	((struct seq_file *)file->private_data)->private = cgrp;
+	((struct seq_file *)file->private_data)->private = cp;
 	return 0;
 }
 
Signature · 1 line
1.5.4.rc3
#40 lore raw
In reply to a message not in the archive
Quoted 74 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +10 -0
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */

kernel/cgroup.c err: · line 2223

Quoted 10 lines
kernel/cgroup.c +10 -0
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};

Maybe lose the unnecessary "pids" suffices and prefixes in this structure?

Quoted 22 lines
kernel/cgroup.c +6 -4
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;

kernel/cgroup.c static int cgroup_tasks_open(struct inode *unused, struct file *file) · line 2396

Quoted 10 lines
kernel/cgroup.c +10 -0
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;

You're storing an uncounted reference to the pid ns here - there's no guarantee that the pid_ns will outlive the open file.

Paul

Reviewed-by on v1 1/1
#41 lore raw
In reply to Li Zefan
Quoted 8 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

Quoted 6 lines

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

Quoted 232 lines
---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
 	 */
 	struct list_head release_list;
 
-	/* pids_mutex protects the fields below */
+	/* pids_mutex protects pids_list and cached pid arrays. */
 	struct rw_semaphore pids_mutex;
-	/* Array of process ids in the cgroup */
-	pid_t *tasks_pids;
-	/* How many files are using the current tasks_pids array */
-	int pids_use_count;
-	/* Length of the current tasks_pids array */
-	int pids_length;
+
+	/* Linked list of struct cgroup_pids */
+	struct list_head pids_list;
 
 	/* For RCU-protected deletion */
 	struct rcu_head rcu_head;
kernel/cgroup.c +70 -24
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
 #include <linux/hash.h>
 #include <linux/namei.h>
 #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
 #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
 	INIT_LIST_HEAD(&cgrp->children);
 	INIT_LIST_HEAD(&cgrp->css_sets);
 	INIT_LIST_HEAD(&cgrp->release_list);
+	INIT_LIST_HEAD(&cgrp->pids_list);
 	init_rwsem(&cgrp->pids_mutex);
 }
 static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
 	return ret;
 }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+	/* The node in cgrp->pids_list */
+	struct list_head list;
+	/* The cgroup those pids belong to */
+	struct cgroup *cgrp;
+	/* The namepsace those pids belong to */
+	struct pid_namespace *pid_ns;
+	/* Array of process ids in the cgroup */
+	pid_t *tasks_pids;
+	/* How many files are using the this tasks_pids array */
+	int pids_use_count;
+	/* Length of the current tasks_pids array */
+	int pids_length;
+};
+
 static int cmppid(const void *a, const void *b)
 {
 	return *(pid_t *)a - *(pid_t *)b;
 }
 
-
 /*
  * seq_file methods for the "tasks" file. The seq_file position is the
  * next pid to display; the seq_file iterator is a pointer to the pid
@@ -2221,45 +2241,47 @@ static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
 	 * after a seek to the start). Use a binary-search to find the
 	 * next pid to display, if any
 	 */
-	struct cgroup *cgrp = s->private;
+	struct cgroup_pids *cp = s->private;
+	struct cgroup *cgrp = cp->cgrp;
 	int index = 0, pid = *pos;
 	int *iter;
 
 	down_read(&cgrp->pids_mutex);
 	if (pid) {
-		int end = cgrp->pids_length;
+		int end = cp->pids_length;
 
 		while (index < end) {
 			int mid = (index + end) / 2;
-			if (cgrp->tasks_pids[mid] == pid) {
+			if (cp->tasks_pids[mid] == pid) {
 				index = mid;
 				break;
-			} else if (cgrp->tasks_pids[mid] <= pid)
+			} else if (cp->tasks_pids[mid] <= pid)
 				index = mid + 1;
 			else
 				end = mid;
 		}
 	}
 	/* If we're off the end of the array, we're done */
-	if (index >= cgrp->pids_length)
+	if (index >= cp->pids_length)
 		return NULL;
 	/* Update the abstract position to be the actual pid that we found */
-	iter = cgrp->tasks_pids + index;
+	iter = cp->tasks_pids + index;
 	*pos = *iter;
 	return iter;
 }
 
 static void cgroup_tasks_stop(struct seq_file *s, void *v)
 {
-	struct cgroup *cgrp = s->private;
+	struct cgroup_pids *cp = s->private;
+	struct cgroup *cgrp = cp->cgrp;
 	up_read(&cgrp->pids_mutex);
 }
 
 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
 {
-	struct cgroup *cgrp = s->private;
+	struct cgroup_pids *cp = s->private;
 	int *p = v;
-	int *end = cgrp->tasks_pids + cgrp->pids_length;
+	int *end = cp->tasks_pids + cp->pids_length;
 
 	/*
 	 * Advance to the next pid in the array. If this goes off the
@@ -2286,26 +2308,32 @@ static struct seq_operations cgroup_tasks_seq_operations = {
 	.show = cgroup_tasks_show,
 };
 
-static void release_cgroup_pid_array(struct cgroup *cgrp)
+static void release_cgroup_pid_array(struct cgroup_pids *cp)
 {
+	struct cgroup *cgrp = cp->cgrp;
+
 	down_write(&cgrp->pids_mutex);
-	BUG_ON(!cgrp->pids_use_count);
-	if (!--cgrp->pids_use_count) {
-		kfree(cgrp->tasks_pids);
-		cgrp->tasks_pids = NULL;
-		cgrp->pids_length = 0;
+	BUG_ON(!cp->pids_use_count);
+	if (!--cp->pids_use_count) {
+		list_del(&cp->list);
+		kfree(cp->tasks_pids);
+		kfree(cp);
 	}
 	up_write(&cgrp->pids_mutex);
 }
 
 static int cgroup_tasks_release(struct inode *inode, struct file *file)
 {
-	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+	struct seq_file *seq;
+	struct cgroup_pids *cp;
 
 	if (!(file->f_mode & FMODE_READ))
 		return 0;
 
-	release_cgroup_pid_array(cgrp);
+	seq = file->private_data;
+	cp = seq->private;
+
+	release_cgroup_pid_array(cp);
 	return seq_release(inode, file);
 }
 
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
 static int cgroup_tasks_open(struct inode *unused, struct file *file)
 {
 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+	struct pid_namespace *pid_ns = task_active_pid_ns(current);
+	struct cgroup_pids *cp;
 	pid_t *pidarray;
 	int npids;
 	int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
 	 * array if necessary
 	 */
 	down_write(&cgrp->pids_mutex);
-	kfree(cgrp->tasks_pids);
-	cgrp->tasks_pids = pidarray;
-	cgrp->pids_length = npids;
-	cgrp->pids_use_count++;
+
+	list_for_each_entry(cp, &cgrp->pids_list, list) {
+		if (pid_ns == cp->pid_ns)
+			goto found;
+	}
+
+	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+	if (!cp) {
+		up_write(&cgrp->pids_mutex);
+		kfree(pidarray);
+		return -ENOMEM;
+	}
+	cp->cgrp = cgrp;
+	cp->pid_ns = pid_ns;
+	list_add(&cp->list, &cgrp->pids_list);
+found:
+	kfree(cp->tasks_pids);
+	cp->tasks_pids = pidarray;
+	cp->pids_length = npids;
+	cp->pids_use_count++;
 	up_write(&cgrp->pids_mutex);
 
 	file->f_op = &cgroup_tasks_operations;
 
 	retval = seq_open(file, &cgroup_tasks_seq_operations);
 	if (retval) {
-		release_cgroup_pid_array(cgrp);
+		release_cgroup_pid_array(cp);
 		return retval;
 	}
-	((struct seq_file *)file->private_data)->private = cgrp;
+	((struct seq_file *)file->private_data)->private = cp;
 	return 0;
 }
 
Signature · 1 line
1.5.4.rc3
#42 lore raw
In reply to Li Zefan
Quoted 74 lines · Li Zefan

The bug was introduced by commit cc31edceee04a7b87f2be48f9489ebb72d264844 ("cgroups: convert tasks file to use a seq_file with shared pid array").

We cache a pid array for all threads that are opening the same "tasks" file, but the pids in the array are always from the namespace of the last process that opened the file, so all other threads will read pids from that namespace instead of their own namespaces.

To fix it, we maintain a list of pid arrays, which is keyed by pid_ns. The list will be of length 1 at most time.

---
 include/linux/cgroup.h |   11 ++----
 kernel/cgroup.c        |   94 +++++++++++++++++++++++++++++++++++------------

 2 files changed, 74 insertions(+), 31 deletions(-)
include/linux/cgroup.h +4 -7
index 665fa70..20411d2 100644
@@ -179,14 +179,11 @@ struct cgroup {
         */
        struct list_head release_list;
 
-       /* pids_mutex protects the fields below */
+       /* pids_mutex protects pids_list and cached pid arrays. */
        struct rw_semaphore pids_mutex;
-       /* Array of process ids in the cgroup */
-       pid_t *tasks_pids;
-       /* How many files are using the current tasks_pids array */
-       int pids_use_count;
-       /* Length of the current tasks_pids array */
-       int pids_length;
+
+       /* Linked list of struct cgroup_pids */
+       struct list_head pids_list;
 
        /* For RCU-protected deletion */
        struct rcu_head rcu_head;
kernel/cgroup.c +10 -0
index 3737a68..13dddb4 100644
@@ -47,6 +47,7 @@
  #include <linux/hash.h>
  #include <linux/namei.h>
  #include <linux/smp_lock.h>
+#include <linux/pid_namespace.h>
 
  #include <asm/atomic.h>
 
@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        INIT_LIST_HEAD(&cgrp->children);
        INIT_LIST_HEAD(&cgrp->css_sets);
        INIT_LIST_HEAD(&cgrp->release_list);
+       INIT_LIST_HEAD(&cgrp->pids_list);
        init_rwsem(&cgrp->pids_mutex);
  }
  static void init_cgroup_root(struct cgroupfs_root *root)
@@ -2201,12 +2203,30 @@ err:
        return ret;
  }
 
+/*
+ * Cache pids for all threads in the same pid namespace that are
+ * opening the same "tasks" file.
+ */
+struct cgroup_pids {
+       /* The node in cgrp->pids_list */
+       struct list_head list;
+       /* The cgroup those pids belong to */

kernel/cgroup.c err: · line 2223

Quoted 10 lines
kernel/cgroup.c +10 -0
+       struct cgroup *cgrp;
+       /* The namepsace those pids belong to */
+       struct pid_namespace *pid_ns;
+       /* Array of process ids in the cgroup */
+       pid_t *tasks_pids;
+       /* How many files are using the this tasks_pids array */
+       int pids_use_count;
+       /* Length of the current tasks_pids array */
+       int pids_length;
+};

Maybe lose the unnecessary "pids" suffices and prefixes in this structure?

Quoted 22 lines
kernel/cgroup.c +6 -4
@@ -2324,6 +2352,8 @@ static struct file_operations cgroup_tasks_operations = {
  static int cgroup_tasks_open(struct inode *unused, struct file *file)
  {
        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
+       struct pid_namespace *pid_ns = task_active_pid_ns(current);
+       struct cgroup_pids *cp;
        pid_t *pidarray;
        int npids;
        int retval;
@@ -2350,20 +2380,36 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
         * array if necessary
         */
        down_write(&cgrp->pids_mutex);
-       kfree(cgrp->tasks_pids);
-       cgrp->tasks_pids = pidarray;
-       cgrp->pids_length = npids;
-       cgrp->pids_use_count++;
+
+       list_for_each_entry(cp, &cgrp->pids_list, list) {
+               if (pid_ns == cp->pid_ns)
+                       goto found;

kernel/cgroup.c static int cgroup_tasks_open(struct inode *unused, struct file *file) · line 2396

Quoted 10 lines
kernel/cgroup.c +10 -0
+       }
+
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;

You're storing an uncounted reference to the pid ns here - there's no guarantee that the pid_ns will outlive the open file.

Paul

A message that is not in the archive (sent off-list or not yet received): 6599ad830907020926t6305bec9t44a50cc165f6fc28-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#44 lore raw
Quoted 10 lines · Paul Menage
Quoted 8 lines · Li Zefan

+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;

Quoted 2 lines

You're storing an uncounted reference to the pid ns here - there's no guarantee that the pid_ns will outlive the open file.

Yeah I was thinking about that, but

1. the only way it won't outlive the open file is if the task opens the file, hands the open fd over a unix socket, then exits as the last task of its pidns
2. We don't dereference the pid_ns, so there is no actual safety issue. So it would become a problem only if a new pidns gets created at that same address *and* a task in the new pidns opens the same tasks file.

Still, it wouldn't hurt to do get_pid_ns/put_pid_ns at the open and release :)

-serge

#45 lore raw
In reply to Paul Menage
Quoted 10 lines · Paul Menage
Quoted 8 lines · Li Zefan

+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp) {
+               up_write(&cgrp->pids_mutex);
+               kfree(pidarray);
+               return -ENOMEM;
+       }
+       cp->cgrp = cgrp;
+       cp->pid_ns = pid_ns;

Quoted 2 lines

You're storing an uncounted reference to the pid ns here - there's no guarantee that the pid_ns will outlive the open file.

Yeah I was thinking about that, but

1. the only way it won't outlive the open file is if the task opens the file, hands the open fd over a unix socket, then exits as the last task of its pidns
2. We don't dereference the pid_ns, so there is no actual safety issue. So it would become a problem only if a new pidns gets created at that same address *and* a task in the new pidns opens the same tasks file.

Still, it wouldn't hurt to do get_pid_ns/put_pid_ns at the open and release :)

-serge

A message that is not in the archive (sent off-list or not yet received): 20090702163731.GA14267-r/[email protected]

#47 lore raw
Quoted 5 lines · Serge E. Hallyn

       1. the only way it won't outlive the open file is if the task opens the file, hands the open fd over a unix socket, then exits as the last task of its pidns

Right.

Quoted 3 lines

       2. We don't dereference the pid_ns, so there is no actual safety issue.  So it would become a problem only if a new pidns gets created at that same address

Which is fairly likely given that pid_namespace is allocated from a specific cache.

Paul

#48 lore raw
In reply to Serge E. Hallyn
Quoted 5 lines · Serge E. Hallyn

       1. the only way it won't outlive the open file is if the task opens the file, hands the open fd over a unix socket, then exits as the last task of its pidns

Right.

Quoted 3 lines

       2. We don't dereference the pid_ns, so there is no actual safety issue.  So it would become a problem only if a new pidns gets created at that same address

Which is fairly likely given that pid_namespace is allocated from a specific cache.

Paul

A message that is not in the archive (sent off-list or not yet received): 6599ad830907020946r42735c9es642bddf37af755ed-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#50 lore raw
Quoted 9 lines · Paul Menage
Quoted 5 lines · Serge E. Hallyn

       1. the only way it won't outlive the open file is if the task opens the file, hands the open fd over a unix socket, then exits as the last task of its pidns

Right.

Quoted 8 lines
Quoted 3 lines

       2. We don't dereference the pid_ns, so there is no actual safety issue.  So it would become a problem only if a new pidns gets created at that same address

Which is fairly likely given that pid_namespace is allocated from a specific cache.

Paul

The scenario as a whole is still pretty unlikely, but there's just no reason to risk it.

-serge

#51 lore raw
In reply to Paul Menage
Quoted 9 lines · Paul Menage
Quoted 5 lines · Serge E. Hallyn

       1. the only way it won't outlive the open file is if the task opens the file, hands the open fd over a unix socket, then exits as the last task of its pidns

Right.

Quoted 8 lines
Quoted 3 lines

       2. We don't dereference the pid_ns, so there is no actual safety issue.  So it would become a problem only if a new pidns gets created at that same address

Which is fairly likely given that pid_namespace is allocated from a specific cache.

Paul

The scenario as a whole is still pretty unlikely, but there's just no reason to risk it.

-serge