Skip to contentSkip to conversation

cgroup: implement task_cgroup_path_from_hierarchy()

PATCH Applied Tejun Heo 5 participants 21 messages Apr 14, 2013 – May 14, 2013 4 messages not in the archive cgroup

lore
Share & tools
lore

Tejun Heo

author maintainer
#0 lore raw

PATCH 1/1 cgroup: implement task_cgroup_path_from_hierarchy()

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.

This patchset make hierarchy_id allocation use idr instead of ida and implement task_cgroup_path_from_hierarchy(). In the process, the yucky ida cyclic allocation is replaced with idr_alloc_cyclic().

0001-cgroup-refactor-hierarchy_id-handling.patch 0002-cgroup-drop-hierarchy_id_lock.patch 0003-cgroup-make-hierarchy_id-use-cyclic-idr.patch 0004-cgroup-implement-task_cgroup_path_from_hierarchy.patch

0001-0002 prepare for conversion to idr, which 0003 does.

0004 implements the new function.

This patchset is on top of next-20130412 as idr_alloc_cyclic() patch is currently in -mm. Given that this isn't an urgent thing and the merge window is just around the corner, it'd be probably best to route these through cgroup/for-3.11 once v3.10-rc1 drops.

These patches are also available in the following git branch.

git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git review-task_cgroup_path_from_hierarchy

And it actually reduces LOC. Woot Woot.

 include/linux/cgroup.h |    2
 kernel/cgroup.c        |  128 +++++++++++++++++++++++++++++++++----------------
 2 files changed, 89 insertions(+), 41 deletions(-)

--
tejun

author maintainer
#1 lore raw

PATCH 1/4 cgroup: refactor hierarchy_id handling

We're planning to converting hierarchy_ida to an idr and use it to look up hierarchy from its id. As we want the mapping to happen atomically with cgroupfs_root registration, this patch refactors hierarchy_id init / exit so that ida operations happen inside cgroup_[root_]mutex.

* s/init_root_id()/cgroup_init_root_id()/ and make it return 0 or -errno like a normal function.

* Move hierarchy_id initialization from cgroup_root_from_opts() into cgroup_mount() block where the root is confirmed to be used and being registered while holding both mutexes.

* Split cgroup_drop_id() into cgroup_exit_root_id() and cgroup_free_root(), so that ID release can happen before dropping the mutexes in cgroup_kill_sb(). The latter expects hierarchy_id to be exited before being invoked.

---
 kernel/cgroup.c | 56 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 21 deletions(-)
kernel/cgroup.c +35 -21
index a790409..823cb56 100644
@@ -1452,13 +1452,13 @@ static void init_cgroup_root(struct cgroupfs_root *root)
 	list_add_tail(&cgrp->allcg_node, &root->allcg_list);
 }
 
-static bool init_root_id(struct cgroupfs_root *root)
+static int cgroup_init_root_id(struct cgroupfs_root *root)
 {
-	int ret = 0;
+	int ret;
 
 	do {
 		if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
-			return false;
+			return -ENOMEM;
 		spin_lock(&hierarchy_id_lock);
 		/* Try to allocate the next unused ID */
 		ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
@@ -1474,7 +1474,18 @@ static bool init_root_id(struct cgroupfs_root *root)
 		}
 		spin_unlock(&hierarchy_id_lock);
 	} while (ret);
-	return true;
+	return 0;
+}
+
+static void cgroup_exit_root_id(struct cgroupfs_root *root)
+{
+	if (root->hierarchy_id) {
+		spin_lock(&hierarchy_id_lock);
+		ida_remove(&hierarchy_ida, root->hierarchy_id);
+		spin_unlock(&hierarchy_id_lock);
+
+		root->hierarchy_id = 0;
+	}
 }
 
 static int cgroup_test_super(struct super_block *sb, void *data)
@@ -1508,10 +1519,6 @@ static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
 	if (!root)
 		return ERR_PTR(-ENOMEM);
 
-	if (!init_root_id(root)) {
-		kfree(root);
-		return ERR_PTR(-ENOMEM);
-	}
 	init_cgroup_root(root);
 
 	root->subsys_mask = opts->subsys_mask;
@@ -1526,17 +1533,15 @@ static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
 	return root;
 }
 
-static void cgroup_drop_root(struct cgroupfs_root *root)
+static void cgroup_free_root(struct cgroupfs_root *root)
 {
-	if (!root)
-		return;
+	if (root) {
+		/* hierarhcy ID shoulid already have been released */
+		WARN_ON_ONCE(root->hierarchy_id);
 
-	BUG_ON(!root->hierarchy_id);
-	spin_lock(&hierarchy_id_lock);
-	ida_remove(&hierarchy_ida, root->hierarchy_id);
-	spin_unlock(&hierarchy_id_lock);
-	ida_destroy(&root->cgroup_ida);
-	kfree(root);
+		ida_destroy(&root->cgroup_ida);
+		kfree(root);
+	}
 }
 
 static int cgroup_set_super(struct super_block *sb, void *data)
@@ -1623,7 +1628,7 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
 	sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
 	if (IS_ERR(sb)) {
 		ret = PTR_ERR(sb);
-		cgroup_drop_root(opts.new_root);
+		cgroup_free_root(opts.new_root);
 		goto drop_modules;
 	}
 
@@ -1667,6 +1672,10 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
 		if (ret)
 			goto unlock_drop;
 
+		ret = cgroup_init_root_id(root);
+		if (ret)
+			goto unlock_drop;
+
 		ret = rebind_subsystems(root, root->subsys_mask);
 		if (ret == -EBUSY) {
 			free_cg_links(&tmp_cg_links);
@@ -1710,7 +1719,7 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
 		 * We re-used an existing hierarchy - the new root (if
 		 * any) is not needed
 		 */
-		cgroup_drop_root(opts.new_root);
+		cgroup_free_root(opts.new_root);
 		/* no subsys rebinding, so refcounts don't change */
 		drop_parsed_module_refcounts(opts.subsys_mask);
 	}
@@ -1720,6 +1729,7 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
 	return dget(sb->s_root);
 
  unlock_drop:
+	cgroup_exit_root_id(root);
 	mutex_unlock(&cgroup_root_mutex);
 	mutex_unlock(&cgroup_mutex);
 	mutex_unlock(&inode->i_mutex);
@@ -1772,13 +1782,15 @@ static void cgroup_kill_sb(struct super_block *sb) {
 		root_count--;
 	}
 
+	cgroup_exit_root_id(root);
+
 	mutex_unlock(&cgroup_root_mutex);
 	mutex_unlock(&cgroup_mutex);
 
 	simple_xattrs_free(&cgrp->xattrs);
 
 	kill_litter_super(sb);
-	cgroup_drop_root(root);
+	cgroup_free_root(root);
 }
 
 static struct file_system_type cgroup_fs_type = {
@@ -4642,7 +4654,9 @@ int __init cgroup_init(void)
 	/* Add init_css_set to the hash table */
 	key = css_set_hash(init_css_set.subsys);
 	hash_add(css_set_table, &init_css_set.hlist, key);
-	BUG_ON(!init_root_id(&rootnode));
+
+	/* allocate id for the dummy hierarchy */
+	BUG_ON(cgroup_init_root_id(&rootnode));
 
 	cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
 	if (!cgroup_kobj) {
Signature · 1 line
1.8.1.4
author maintainer
#2 lore raw

PATCH 2/4 cgroup: drop hierarchy_id_lock

In reply to Tejun Heo

Now that hierarchy_id alloc / free are protected by the cgroup mutexes, there's no need for this separate lock. Drop it.

---
 kernel/cgroup.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
kernel/cgroup.c +17 -6
index 823cb56..e15bdb7 100644
@@ -237,9 +237,13 @@ struct cgroup_event {
 static LIST_HEAD(roots);
 static int root_count;
 
+/*
+ * Hierarchy ID allocation and mapping.  It follows the same exclusion
+ * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
+ * writes, either for reads.
+ */
 static DEFINE_IDA(hierarchy_ida);
 static int next_hierarchy_id;
-static DEFINE_SPINLOCK(hierarchy_id_lock);
 
 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
 #define dummytop (&rootnode.top_cgroup)
@@ -1456,10 +1460,12 @@ static int cgroup_init_root_id(struct cgroupfs_root *root)
 {
 	int ret;
 
+	lockdep_assert_held(&cgroup_mutex);
+	lockdep_assert_held(&cgroup_root_mutex);
+
 	do {
 		if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
 			return -ENOMEM;
-		spin_lock(&hierarchy_id_lock);
 		/* Try to allocate the next unused ID */
 		ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
 					&root->hierarchy_id);
@@ -1472,18 +1478,17 @@ static int cgroup_init_root_id(struct cgroupfs_root *root)
 			/* Can only get here if the 31-bit IDR is full ... */
 			BUG_ON(ret);
 		}
-		spin_unlock(&hierarchy_id_lock);
 	} while (ret);
 	return 0;
 }
 
 static void cgroup_exit_root_id(struct cgroupfs_root *root)
 {
+	lockdep_assert_held(&cgroup_mutex);
+	lockdep_assert_held(&cgroup_root_mutex);
+
 	if (root->hierarchy_id) {
-		spin_lock(&hierarchy_id_lock);
 		ida_remove(&hierarchy_ida, root->hierarchy_id);
-		spin_unlock(&hierarchy_id_lock);
-
 		root->hierarchy_id = 0;
 	}
 }
@@ -4656,8 +4661,14 @@ int __init cgroup_init(void)
 	hash_add(css_set_table, &init_css_set.hlist, key);
 
 	/* allocate id for the dummy hierarchy */
+	mutex_lock(&cgroup_mutex);
+	mutex_lock(&cgroup_root_mutex);
+
 	BUG_ON(cgroup_init_root_id(&rootnode));
 
+	mutex_unlock(&cgroup_root_mutex);
+	mutex_unlock(&cgroup_mutex);
+
 	cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
 	if (!cgroup_kobj) {
 		err = -ENOMEM;
Signature · 1 line
1.8.1.4
author maintainer
#3 lore raw

PATCH 3/4 cgroup: make hierarchy_id use cyclic idr

In reply to Tejun Heo

We want to be able to lookup a hierarchy from its id and cyclic allocation is a whole lot simpler with idr. Convert to idr and use idr_alloc_cyclc().

---
 kernel/cgroup.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)
kernel/cgroup.c +8 -20
index e15bdb7..75d85e8 100644
@@ -242,8 +242,7 @@ static int root_count;
  * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
  * writes, either for reads.
  */
-static DEFINE_IDA(hierarchy_ida);
-static int next_hierarchy_id;
+static DEFINE_IDR(cgroup_hierarchy_idr);
 
 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
 #define dummytop (&rootnode.top_cgroup)
@@ -1458,27 +1457,16 @@ static void init_cgroup_root(struct cgroupfs_root *root)
 
 static int cgroup_init_root_id(struct cgroupfs_root *root)
 {
-	int ret;
+	int id;
 
 	lockdep_assert_held(&cgroup_mutex);
 	lockdep_assert_held(&cgroup_root_mutex);
 
-	do {
-		if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
-			return -ENOMEM;
-		/* Try to allocate the next unused ID */
-		ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
-					&root->hierarchy_id);
-		if (ret == -ENOSPC)
-			/* Try again starting from 0 */
-			ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
-		if (!ret) {
-			next_hierarchy_id = root->hierarchy_id + 1;
-		} else if (ret != -EAGAIN) {
-			/* Can only get here if the 31-bit IDR is full ... */
-			BUG_ON(ret);
-		}
-	} while (ret);
+	id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
+	if (id < 0)
+		return id;
+
+	root->hierarchy_id = id;
 	return 0;
 }
 
@@ -1488,7 +1476,7 @@ static void cgroup_exit_root_id(struct cgroupfs_root *root)
 	lockdep_assert_held(&cgroup_root_mutex);
 
 	if (root->hierarchy_id) {
-		ida_remove(&hierarchy_ida, root->hierarchy_id);
+		idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
 		root->hierarchy_id = 0;
 	}
 }
Signature · 1 line
1.8.1.4
author maintainer
#4 lore raw

PATCH 4/4 cgroup: implement task_cgroup_path_from_hierarchy()

In reply to 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().

Cc Kay Sievers <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Lennart Poettering <[email protected]>
Cc Daniel Mack <[email protected]>
---
 include/linux/cgroup.h |  2 ++
 kernel/cgroup.c        | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/cgroup.h +2 -0
index 17ed818..ee83af2 100644
@@ -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);
 
kernel/cgroup.c +33 -0
index 75d85e8..5184fcd 100644
@@ -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
  */
Signature · 1 line
1.8.1.4
Acked-by on v1 4/4
#5 lore raw
Quoted 11 lines · 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().

Cc Kay Sievers <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Lennart Poettering <[email protected]>
Cc Daniel Mack <[email protected]>

Thanks so much for doing this.

#6 lore raw
In reply to Tejun Heo

kernel/cgroup.c out: · line 1867

Quoted 12 lines
kernel/cgroup.c +12 -0
+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.

Quoted 9 lines
kernel/cgroup.c +9 -0
+		if (cgrp)
+			ret = cgroup_path(cgrp, buf, buflen);
+	}
+
+	mutex_unlock(&cgroup_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
author maintainer
#7 lore raw
Quoted 6 lines · Li Zefan
Quoted 6 lines
kernel/cgroup.c +6 -0
+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;
Quoted 8 lines

kernel/cgroup.c out: · line 1867

Quoted 6 lines
kernel/cgroup.c +6 -0
+
+	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.

Signature · 1 line
tejun
author maintainer
#8 lore raw
Quoted 16 lines · Tejun Heo
Quoted 6 lines · Li Zefan
Quoted 6 lines
kernel/cgroup.c +6 -0
+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;
Quoted 8 lines

kernel/cgroup.c out: · line 1867

Quoted 6 lines
kernel/cgroup.c +6 -0
+
+	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.

Quoted 3 lines

@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.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): 20130415034613.GK3050-Gd/HAXX7CRxy/[email protected]

#10 lore raw
Quoted 16 lines · Tejun Heo
Quoted 6 lines · Li Zefan
Quoted 6 lines
kernel/cgroup.c +6 -0
+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;
Quoted 8 lines

kernel/cgroup.c out: · line 1867

Quoted 6 lines
kernel/cgroup.c +6 -0
+
+	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.

Quoted 3 lines

@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.

Signature · 1 line
tejun
#11 lore raw
In reply to Tejun Heo
Quoted 16 lines · Tejun Heo
Quoted 6 lines · Li Zefan
Quoted 6 lines
kernel/cgroup.c +6 -0
+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;
Quoted 8 lines

kernel/cgroup.c out: · line 1867

Quoted 6 lines
kernel/cgroup.c +6 -0
+
+	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.

Quoted 4 lines

@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.

author maintainer
#12 lore raw

Hey, Li.

Quoted 2 lines · Li Zefan

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. :)

Signature · 1 line
tejun

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

#14 lore raw

Hey, Li.

Quoted 2 lines · Li Zefan

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. :)

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): 1365964619-14762-5-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org

#16 lore raw

kernel/cgroup.c out: · line 1867

Quoted 12 lines
kernel/cgroup.c +12 -0
+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.

Quoted 9 lines
kernel/cgroup.c +9 -0
+		if (cgrp)
+			ret = cgroup_path(cgrp, buf, buflen);
+	}
+
+	mutex_unlock(&cgroup_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
#17 lore raw

PATCH 4/4 cgroup: implement task_cgroup_path_from_hierarchy()

In reply to a message not in the archive

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().

v2: Dropped unnecessary NULL check on the return value of
task_cgroup_from_root() as suggested by Li Zefan.

---
 include/linux/cgroup.h |    2 ++
 kernel/cgroup.c        |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
include/linux/cgroup.h +2 -0
@@ -443,6 +443,8 @@ int cgroup_is_removed(const struct cgrou
 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);
 
kernel/cgroup.c +32 -0
@@ -1842,6 +1842,38 @@ 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);
+		ret = cgroup_path(cgrp, buf, buflen);
+	}
+
+	mutex_unlock(&cgroup_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
+
 /*
  * Control Group taskset
  */
author maintainer
#18 lore raw

PATCH 4/4 cgroup: implement task_cgroup_path_from_hierarchy()

In reply to 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().

v2: Dropped unnecessary NULL check on the return value of
task_cgroup_from_root() as suggested by Li Zefan.

---
 include/linux/cgroup.h |    2 ++
 kernel/cgroup.c        |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
include/linux/cgroup.h +2 -0
@@ -443,6 +443,8 @@ int cgroup_is_removed(const struct cgrou
 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);
 
kernel/cgroup.c +32 -0
@@ -1842,6 +1842,38 @@ 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);
+		ret = cgroup_path(cgrp, buf, buflen);
+	}
+
+	mutex_unlock(&cgroup_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
+
 /*
  * Control Group taskset
  */

A message that is not in the archive (sent off-list or not yet received): 1365964619-14762-1-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org

#20 lore raw

PATCH 4/4 cgroup: implement task_cgroup_path_from_hierarchy()

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().

Cc Kay Sievers <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Lennart Poettering <[email protected]>
Cc Daniel Mack <[email protected]>
---
 include/linux/cgroup.h |  2 ++
 kernel/cgroup.c        | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/cgroup.h +2 -0
index 17ed818..ee83af2 100644
@@ -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);
 
kernel/cgroup.c +33 -0
index 75d85e8..5184fcd 100644
@@ -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
  */
Signature · 1 line
1.8.1.4
#21 lore raw
In reply to a message not in the archive
Quoted 24 lines · 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.

This patchset make hierarchy_id allocation use idr instead of ida and implement task_cgroup_path_from_hierarchy(). In the process, the yucky ida cyclic allocation is replaced with idr_alloc_cyclic().

0001-cgroup-refactor-hierarchy_id-handling.patch 0002-cgroup-drop-hierarchy_id_lock.patch 0003-cgroup-make-hierarchy_id-use-cyclic-idr.patch 0004-cgroup-implement-task_cgroup_path_from_hierarchy.patch

0001-0002 prepare for conversion to idr, which 0003 does.

0004 implements the new function.

This patchset is on top of next-20130412 as idr_alloc_cyclic() patch is currently in -mm. Given that this isn't an urgent thing and the merge window is just around the corner, it'd be probably best to route these through cgroup/for-3.11 once v3.10-rc1 drops.

These patches are also available in the following git branch.

Quoted 7 lines

git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git review-task_cgroup_path_from_hierarchy

And it actually reduces LOC. Woot Woot.

 include/linux/cgroup.h |    2
 kernel/cgroup.c        |  128 +++++++++++++++++++++++++++++++++----------------
 2 files changed, 89 insertions(+), 41 deletions(-)

Heh, I must have been tripping or something. 89 > 41. It's still a lot cleaner tho. :)

Signature · 1 line
tejun
author maintainer
#22 lore raw
In reply to Tejun Heo
Quoted 24 lines · 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.

This patchset make hierarchy_id allocation use idr instead of ida and implement task_cgroup_path_from_hierarchy(). In the process, the yucky ida cyclic allocation is replaced with idr_alloc_cyclic().

0001-cgroup-refactor-hierarchy_id-handling.patch 0002-cgroup-drop-hierarchy_id_lock.patch 0003-cgroup-make-hierarchy_id-use-cyclic-idr.patch 0004-cgroup-implement-task_cgroup_path_from_hierarchy.patch

0001-0002 prepare for conversion to idr, which 0003 does.

0004 implements the new function.

This patchset is on top of next-20130412 as idr_alloc_cyclic() patch is currently in -mm. Given that this isn't an urgent thing and the merge window is just around the corner, it'd be probably best to route these through cgroup/for-3.11 once v3.10-rc1 drops.

These patches are also available in the following git branch.

Quoted 7 lines

git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git review-task_cgroup_path_from_hierarchy

And it actually reduces LOC. Woot Woot.

 include/linux/cgroup.h |    2
 kernel/cgroup.c        |  128 +++++++++++++++++++++++++++++++++----------------
 2 files changed, 89 insertions(+), 41 deletions(-)

Heh, I must have been tripping or something. 89 > 41. It's still a lot cleaner tho. :)

Signature · 1 line
tejun
Acked-by on v1 1/1
#23 lore raw
In reply to Tejun Heo
Quoted 8 lines · 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.

This patchset make hierarchy_id allocation use idr instead of ida and implement task_cgroup_path_from_hierarchy(). In the process, the yucky ida cyclic allocation is replaced with idr_alloc_cyclic().

Quoted 9 lines

0001-cgroup-refactor-hierarchy_id-handling.patch 0002-cgroup-drop-hierarchy_id_lock.patch 0003-cgroup-make-hierarchy_id-use-cyclic-idr.patch 0004-cgroup-implement-task_cgroup_path_from_hierarchy.patch

0001-0002 prepare for conversion to idr, which 0003 does.

0004 implements the new function.

author maintainer Applied
#24 lore raw
In reply to Tejun Heo
Quoted 3 lines · 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.

Applied to cgroup/for-3.11.

Thanks.

Signature · 1 line
tejun