Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
Hey, Li.

On Mon, Apr 15, 2013 at 11:49:57AM +0800, Li Zefan wrote:
> But if id is invalid, idr_find() will return NULL. As long as root is not 
> NULL,
> task_cgroup_from_root() will always return a valid cgroup.

I'm hopping across multiple devel branches quickly today and somewhat
tipsy.  Sorry about the blabber.  :)

-- 
tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Li Zefan
On 2013/4/15 11:46, Tejun Heo wrote:
> On Mon, Apr 15, 2013 at 11:43:15AM +0800, Li Zefan wrote:
>>> +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
>>> hierarchy_id,
>>> +   char *buf, size_t buflen)
>>> +{
>>> +   struct cgroupfs_root *root;
>>> +   struct cgroup *cgrp = NULL;
>>> +   int ret = -ENOENT;
>>> +
>>> +   mutex_lock(_mutex);
>>> +
>>> +   root = idr_find(_hierarchy_idr, hierarchy_id);
>>> +   if (root) {
>>> +   cgrp = task_cgroup_from_root(task, root);
>>
>> task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) 
>> in it.
> 
> @hierarchy_id may come from userland, so we probably should update
> task_cgroup_from_root() to return NULL if the id is invalid.  Will add
> a patch.
> 

But if id is invalid, idr_find() will return NULL. As long as root is not NULL,
task_cgroup_from_root() will always return a valid cgroup.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
On Sun, Apr 14, 2013 at 08:46:13PM -0700, Tejun Heo wrote:
> On Mon, Apr 15, 2013 at 11:43:15AM +0800, Li Zefan wrote:
> > > +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
> > > hierarchy_id,
> > > + char *buf, size_t buflen)
> > > +{
> > > + struct cgroupfs_root *root;
> > > + struct cgroup *cgrp = NULL;
> > > + int ret = -ENOENT;
> > > +
> > > + mutex_lock(_mutex);
> > > +
> > > + root = idr_find(_hierarchy_idr, hierarchy_id);
> > > + if (root) {
> > > + cgrp = task_cgroup_from_root(task, root);
> > 
> > task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) 
> > in it.
> 
> @hierarchy_id may come from userland, so we probably should update
> task_cgroup_from_root() to return NULL if the id is invalid.  Will add
> a patch.

For some reason, I thought you were talking about hierarchy lookup.
Yeap, task_cgroup_from_root() can't be NULL for a valid hierarchy.
Will drop the if.

-- 
tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
On Mon, Apr 15, 2013 at 11:43:15AM +0800, Li Zefan wrote:
> > +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
> > hierarchy_id,
> > +   char *buf, size_t buflen)
> > +{
> > +   struct cgroupfs_root *root;
> > +   struct cgroup *cgrp = NULL;
> > +   int ret = -ENOENT;
> > +
> > +   mutex_lock(_mutex);
> > +
> > +   root = idr_find(_hierarchy_idr, hierarchy_id);
> > +   if (root) {
> > +   cgrp = task_cgroup_from_root(task, root);
> 
> task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) in 
> it.

@hierarchy_id may come from userland, so we probably should update
task_cgroup_from_root() to return NULL if the id is invalid.  Will add
a patch.

Thanks.

-- 
tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Li Zefan
> +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
> hierarchy_id,
> + char *buf, size_t buflen)
> +{
> + struct cgroupfs_root *root;
> + struct cgroup *cgrp = NULL;
> + int ret = -ENOENT;
> +
> + mutex_lock(_mutex);
> +
> + root = idr_find(_hierarchy_idr, hierarchy_id);
> + if (root) {
> + cgrp = task_cgroup_from_root(task, root);

task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) in 
it.

> + if (cgrp)
> + ret = cgroup_path(cgrp, buf, buflen);
> + }
> +
> + mutex_unlock(_mutex);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Greg KH
On Sun, Apr 14, 2013 at 11:36:59AM -0700, Tejun Heo wrote:
> kdbus folks want a sane way to determine the cgroup path that a given
> task belongs to on a given hierarchy, which is a reasonble thing to
> expect from cgroup core.
> 
> Implement task_cgroup_path_from_hierarchy().
> 
> Signed-off-by: Tejun Heo 
> Cc: Kay Sievers 
> Cc: Greg Kroah-Hartman 
> Cc: Lennart Poettering 
> Cc: Daniel Mack 

Thanks so much for doing this.

Acked-by: Greg Kroah-Hartman 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
kdbus folks want a sane way to determine the cgroup path that a given
task belongs to on a given hierarchy, which is a reasonble thing to
expect from cgroup core.

Implement task_cgroup_path_from_hierarchy().

Signed-off-by: Tejun Heo 
Cc: Kay Sievers 
Cc: Greg Kroah-Hartman 
Cc: Lennart Poettering 
Cc: Daniel Mack 
---
 include/linux/cgroup.h |  2 ++
 kernel/cgroup.c| 33 +
 2 files changed, 35 insertions(+)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 17ed818..ee83af2 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -443,6 +443,8 @@ int cgroup_is_removed(const struct cgroup *cgrp);
 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
 
 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
+int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
+   char *buf, size_t buflen);
 
 int cgroup_task_count(const struct cgroup *cgrp);
 
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 75d85e8..5184fcd 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -1842,6 +1842,39 @@ out:
 }
 EXPORT_SYMBOL_GPL(cgroup_path);
 
+/**
+ * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
+ * @task: target task
+ * @hierarchy_id: the hierarchy to look up @task's cgroup from
+ * @buf: the buffer to write the path into
+ * @buflen: the length of the buffer
+ *
+ * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
+ * copy its path into @buf.  This function grabs cgroup_mutex and shouldn't
+ * be used inside locks used by cgroup controller callbacks.
+ */
+int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
+   char *buf, size_t buflen)
+{
+   struct cgroupfs_root *root;
+   struct cgroup *cgrp = NULL;
+   int ret = -ENOENT;
+
+   mutex_lock(_mutex);
+
+   root = idr_find(_hierarchy_idr, hierarchy_id);
+   if (root) {
+   cgrp = task_cgroup_from_root(task, root);
+   if (cgrp)
+   ret = cgroup_path(cgrp, buf, buflen);
+   }
+
+   mutex_unlock(_mutex);
+
+   return ret;
+}
+EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
+
 /*
  * Control Group taskset
  */
-- 
1.8.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Li Zefan
 +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
 hierarchy_id,
 + char *buf, size_t buflen)
 +{
 + struct cgroupfs_root *root;
 + struct cgroup *cgrp = NULL;
 + int ret = -ENOENT;
 +
 + mutex_lock(cgroup_mutex);
 +
 + root = idr_find(cgroup_hierarchy_idr, hierarchy_id);
 + if (root) {
 + cgrp = task_cgroup_from_root(task, root);

task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) in 
it.

 + if (cgrp)
 + ret = cgroup_path(cgrp, buf, buflen);
 + }
 +
 + mutex_unlock(cgroup_mutex);
 +
 + return ret;
 +}
 +EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
On Mon, Apr 15, 2013 at 11:43:15AM +0800, Li Zefan wrote:
  +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
  hierarchy_id,
  +   char *buf, size_t buflen)
  +{
  +   struct cgroupfs_root *root;
  +   struct cgroup *cgrp = NULL;
  +   int ret = -ENOENT;
  +
  +   mutex_lock(cgroup_mutex);
  +
  +   root = idr_find(cgroup_hierarchy_idr, hierarchy_id);
  +   if (root) {
  +   cgrp = task_cgroup_from_root(task, root);
 
 task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) in 
 it.

@hierarchy_id may come from userland, so we probably should update
task_cgroup_from_root() to return NULL if the id is invalid.  Will add
a patch.

Thanks.

-- 
tejun
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
On Sun, Apr 14, 2013 at 08:46:13PM -0700, Tejun Heo wrote:
 On Mon, Apr 15, 2013 at 11:43:15AM +0800, Li Zefan wrote:
   +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
   hierarchy_id,
   + char *buf, size_t buflen)
   +{
   + struct cgroupfs_root *root;
   + struct cgroup *cgrp = NULL;
   + int ret = -ENOENT;
   +
   + mutex_lock(cgroup_mutex);
   +
   + root = idr_find(cgroup_hierarchy_idr, hierarchy_id);
   + if (root) {
   + cgrp = task_cgroup_from_root(task, root);
  
  task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) 
  in it.
 
 @hierarchy_id may come from userland, so we probably should update
 task_cgroup_from_root() to return NULL if the id is invalid.  Will add
 a patch.

For some reason, I thought you were talking about hierarchy lookup.
Yeap, task_cgroup_from_root() can't be NULL for a valid hierarchy.
Will drop the if.

-- 
tejun
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Li Zefan
On 2013/4/15 11:46, Tejun Heo wrote:
 On Mon, Apr 15, 2013 at 11:43:15AM +0800, Li Zefan wrote:
 +int task_cgroup_path_from_hierarchy(struct task_struct *task, int 
 hierarchy_id,
 +   char *buf, size_t buflen)
 +{
 +   struct cgroupfs_root *root;
 +   struct cgroup *cgrp = NULL;
 +   int ret = -ENOENT;
 +
 +   mutex_lock(cgroup_mutex);
 +
 +   root = idr_find(cgroup_hierarchy_idr, hierarchy_id);
 +   if (root) {
 +   cgrp = task_cgroup_from_root(task, root);

 task_cgroup_from_root() will never return NULL, and there's a BUG_ON(!res) 
 in it.
 
 @hierarchy_id may come from userland, so we probably should update
 task_cgroup_from_root() to return NULL if the id is invalid.  Will add
 a patch.
 

But if id is invalid, idr_find() will return NULL. As long as root is not NULL,
task_cgroup_from_root() will always return a valid cgroup.

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
Hey, Li.

On Mon, Apr 15, 2013 at 11:49:57AM +0800, Li Zefan wrote:
 But if id is invalid, idr_find() will return NULL. As long as root is not 
 NULL,
 task_cgroup_from_root() will always return a valid cgroup.

I'm hopping across multiple devel branches quickly today and somewhat
tipsy.  Sorry about the blabber.  :)

-- 
tejun
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Tejun Heo
kdbus folks want a sane way to determine the cgroup path that a given
task belongs to on a given hierarchy, which is a reasonble thing to
expect from cgroup core.

Implement task_cgroup_path_from_hierarchy().

Signed-off-by: Tejun Heo t...@kernel.org
Cc: Kay Sievers k...@vrfy.org
Cc: Greg Kroah-Hartman g...@kroah.com
Cc: Lennart Poettering lenn...@poettering.net
Cc: Daniel Mack dan...@zonque.org
---
 include/linux/cgroup.h |  2 ++
 kernel/cgroup.c| 33 +
 2 files changed, 35 insertions(+)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 17ed818..ee83af2 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -443,6 +443,8 @@ int cgroup_is_removed(const struct cgroup *cgrp);
 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
 
 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
+int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
+   char *buf, size_t buflen);
 
 int cgroup_task_count(const struct cgroup *cgrp);
 
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 75d85e8..5184fcd 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -1842,6 +1842,39 @@ out:
 }
 EXPORT_SYMBOL_GPL(cgroup_path);
 
+/**
+ * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
+ * @task: target task
+ * @hierarchy_id: the hierarchy to look up @task's cgroup from
+ * @buf: the buffer to write the path into
+ * @buflen: the length of the buffer
+ *
+ * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
+ * copy its path into @buf.  This function grabs cgroup_mutex and shouldn't
+ * be used inside locks used by cgroup controller callbacks.
+ */
+int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
+   char *buf, size_t buflen)
+{
+   struct cgroupfs_root *root;
+   struct cgroup *cgrp = NULL;
+   int ret = -ENOENT;
+
+   mutex_lock(cgroup_mutex);
+
+   root = idr_find(cgroup_hierarchy_idr, hierarchy_id);
+   if (root) {
+   cgrp = task_cgroup_from_root(task, root);
+   if (cgrp)
+   ret = cgroup_path(cgrp, buf, buflen);
+   }
+
+   mutex_unlock(cgroup_mutex);
+
+   return ret;
+}
+EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
+
 /*
  * Control Group taskset
  */
-- 
1.8.1.4

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 4/4] cgroup: implement task_cgroup_path_from_hierarchy()

2013-04-14 Thread Greg KH
On Sun, Apr 14, 2013 at 11:36:59AM -0700, Tejun Heo wrote:
 kdbus folks want a sane way to determine the cgroup path that a given
 task belongs to on a given hierarchy, which is a reasonble thing to
 expect from cgroup core.
 
 Implement task_cgroup_path_from_hierarchy().
 
 Signed-off-by: Tejun Heo t...@kernel.org
 Cc: Kay Sievers k...@vrfy.org
 Cc: Greg Kroah-Hartman g...@kroah.com
 Cc: Lennart Poettering lenn...@poettering.net
 Cc: Daniel Mack dan...@zonque.org

Thanks so much for doing this.

Acked-by: Greg Kroah-Hartman gre...@linuxfoundation.org
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/