提交 096b7fe0 编写于 作者: L Li Zefan 提交者: Linus Torvalds

cgroups: fix pid namespace bug

The bug was introduced by commit cc31edce
("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.
Reported-by: NPaul Menage <menage@google.com>
Idea-by: NPaul Menage <menage@google.com>
Signed-off-by: NLi Zefan <lizf@cn.fujitsu.com>
Reviewed-by: NSerge Hallyn <serue@us.ibm.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 b317c833
...@@ -179,14 +179,11 @@ struct cgroup { ...@@ -179,14 +179,11 @@ struct cgroup {
*/ */
struct list_head release_list; 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; struct rw_semaphore pids_mutex;
/* Array of process ids in the cgroup */
pid_t *tasks_pids; /* Linked list of struct cgroup_pids */
/* How many files are using the current tasks_pids array */ struct list_head pids_list;
int pids_use_count;
/* Length of the current tasks_pids array */
int pids_length;
/* For RCU-protected deletion */ /* For RCU-protected deletion */
struct rcu_head rcu_head; struct rcu_head rcu_head;
......
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include <linux/hash.h> #include <linux/hash.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/smp_lock.h> #include <linux/smp_lock.h>
#include <linux/pid_namespace.h>
#include <asm/atomic.h> #include <asm/atomic.h>
...@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp) ...@@ -960,6 +961,7 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
INIT_LIST_HEAD(&cgrp->children); INIT_LIST_HEAD(&cgrp->children);
INIT_LIST_HEAD(&cgrp->css_sets); INIT_LIST_HEAD(&cgrp->css_sets);
INIT_LIST_HEAD(&cgrp->release_list); INIT_LIST_HEAD(&cgrp->release_list);
INIT_LIST_HEAD(&cgrp->pids_list);
init_rwsem(&cgrp->pids_mutex); init_rwsem(&cgrp->pids_mutex);
} }
static void init_cgroup_root(struct cgroupfs_root *root) static void init_cgroup_root(struct cgroupfs_root *root)
...@@ -2201,12 +2203,30 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -2201,12 +2203,30 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
return ret; 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 *ns;
/* Array of process ids in the cgroup */
pid_t *tasks_pids;
/* How many files are using the this tasks_pids array */
int use_count;
/* Length of the current tasks_pids array */
int length;
};
static int cmppid(const void *a, const void *b) static int cmppid(const void *a, const void *b)
{ {
return *(pid_t *)a - *(pid_t *)b; return *(pid_t *)a - *(pid_t *)b;
} }
/* /*
* seq_file methods for the "tasks" file. The seq_file position is the * 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 * 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) ...@@ -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 * after a seek to the start). Use a binary-search to find the
* next pid to display, if any * 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 index = 0, pid = *pos;
int *iter; int *iter;
down_read(&cgrp->pids_mutex); down_read(&cgrp->pids_mutex);
if (pid) { if (pid) {
int end = cgrp->pids_length; int end = cp->length;
while (index < end) { while (index < end) {
int mid = (index + end) / 2; int mid = (index + end) / 2;
if (cgrp->tasks_pids[mid] == pid) { if (cp->tasks_pids[mid] == pid) {
index = mid; index = mid;
break; break;
} else if (cgrp->tasks_pids[mid] <= pid) } else if (cp->tasks_pids[mid] <= pid)
index = mid + 1; index = mid + 1;
else else
end = mid; end = mid;
} }
} }
/* If we're off the end of the array, we're done */ /* If we're off the end of the array, we're done */
if (index >= cgrp->pids_length) if (index >= cp->length)
return NULL; return NULL;
/* Update the abstract position to be the actual pid that we found */ /* Update the abstract position to be the actual pid that we found */
iter = cgrp->tasks_pids + index; iter = cp->tasks_pids + index;
*pos = *iter; *pos = *iter;
return iter; return iter;
} }
static void cgroup_tasks_stop(struct seq_file *s, void *v) 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); up_read(&cgrp->pids_mutex);
} }
static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) 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 *p = v;
int *end = cgrp->tasks_pids + cgrp->pids_length; int *end = cp->tasks_pids + cp->length;
/* /*
* Advance to the next pid in the array. If this goes off the * Advance to the next pid in the array. If this goes off the
...@@ -2286,26 +2308,33 @@ static struct seq_operations cgroup_tasks_seq_operations = { ...@@ -2286,26 +2308,33 @@ static struct seq_operations cgroup_tasks_seq_operations = {
.show = cgroup_tasks_show, .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); down_write(&cgrp->pids_mutex);
BUG_ON(!cgrp->pids_use_count); BUG_ON(!cp->use_count);
if (!--cgrp->pids_use_count) { if (!--cp->use_count) {
kfree(cgrp->tasks_pids); list_del(&cp->list);
cgrp->tasks_pids = NULL; put_pid_ns(cp->ns);
cgrp->pids_length = 0; kfree(cp->tasks_pids);
kfree(cp);
} }
up_write(&cgrp->pids_mutex); up_write(&cgrp->pids_mutex);
} }
static int cgroup_tasks_release(struct inode *inode, struct file *file) 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)) if (!(file->f_mode & FMODE_READ))
return 0; return 0;
release_cgroup_pid_array(cgrp); seq = file->private_data;
cp = seq->private;
release_cgroup_pid_array(cp);
return seq_release(inode, file); return seq_release(inode, file);
} }
...@@ -2324,6 +2353,8 @@ static struct file_operations cgroup_tasks_operations = { ...@@ -2324,6 +2353,8 @@ static struct file_operations cgroup_tasks_operations = {
static int cgroup_tasks_open(struct inode *unused, struct file *file) static int cgroup_tasks_open(struct inode *unused, struct file *file)
{ {
struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
struct pid_namespace *ns = current->nsproxy->pid_ns;
struct cgroup_pids *cp;
pid_t *pidarray; pid_t *pidarray;
int npids; int npids;
int retval; int retval;
...@@ -2350,20 +2381,37 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file) ...@@ -2350,20 +2381,37 @@ static int cgroup_tasks_open(struct inode *unused, struct file *file)
* array if necessary * array if necessary
*/ */
down_write(&cgrp->pids_mutex); down_write(&cgrp->pids_mutex);
kfree(cgrp->tasks_pids);
cgrp->tasks_pids = pidarray; list_for_each_entry(cp, &cgrp->pids_list, list) {
cgrp->pids_length = npids; if (ns == cp->ns)
cgrp->pids_use_count++; goto found;
}
cp = kzalloc(sizeof(*cp), GFP_KERNEL);
if (!cp) {
up_write(&cgrp->pids_mutex);
kfree(pidarray);
return -ENOMEM;
}
cp->cgrp = cgrp;
cp->ns = ns;
get_pid_ns(ns);
list_add(&cp->list, &cgrp->pids_list);
found:
kfree(cp->tasks_pids);
cp->tasks_pids = pidarray;
cp->length = npids;
cp->use_count++;
up_write(&cgrp->pids_mutex); up_write(&cgrp->pids_mutex);
file->f_op = &cgroup_tasks_operations; file->f_op = &cgroup_tasks_operations;
retval = seq_open(file, &cgroup_tasks_seq_operations); retval = seq_open(file, &cgroup_tasks_seq_operations);
if (retval) { if (retval) {
release_cgroup_pid_array(cgrp); release_cgroup_pid_array(cp);
return retval; return retval;
} }
((struct seq_file *)file->private_data)->private = cgrp; ((struct seq_file *)file->private_data)->private = cp;
return 0; return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册