OSDN Git Service

cgroup, memcg: allocate cgroup ID from 1
authorTejun Heo <tj@kernel.org>
Sun, 4 May 2014 19:09:13 +0000 (15:09 -0400)
committerTejun Heo <tj@kernel.org>
Sun, 4 May 2014 19:09:13 +0000 (15:09 -0400)
Currently, cgroup->id is allocated from 0, which is always assigned to
the root cgroup; unfortunately, memcg wants to use ID 0 to indicate
invalid IDs and ends up incrementing all IDs by one.

It's reasonable to reserve 0 for special purposes.  This patch updates
cgroup core so that ID 0 is not used and the root cgroups get ID 1.
The ID incrementing is removed form memcg.

Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.cz>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Li Zefan <lizefan@huawei.com>
include/linux/cgroup.h
kernel/cgroup.c
mm/memcontrol.c

index c6c703f..793f70a 100644 (file)
@@ -144,8 +144,8 @@ struct cgroup {
        /*
         * idr allocated in-hierarchy ID.
         *
-        * The ID of the root cgroup is always 0, and a new cgroup
-        * will be assigned with a smallest available ID.
+        * ID 0 is not used, the ID of the root cgroup is always 1, and a
+        * new cgroup will be assigned with a smallest available ID.
         *
         * Allocating/Removing ID must be protected by cgroup_mutex.
         */
index 21667f3..3fa0463 100644 (file)
@@ -1531,7 +1531,7 @@ static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
        lockdep_assert_held(&cgroup_tree_mutex);
        lockdep_assert_held(&cgroup_mutex);
 
-       ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
+       ret = idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
        if (ret < 0)
                goto out;
        root_cgrp->id = ret;
@@ -4225,7 +4225,7 @@ static long cgroup_create(struct cgroup *parent, const char *name,
         * Temporarily set the pointer to NULL, so idr_find() won't return
         * a half-baked cgroup.
         */
-       cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
+       cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
        if (cgrp->id < 0) {
                err = -ENOMEM;
                goto err_unlock;
index 29501f0..1d0b297 100644 (file)
@@ -527,18 +527,14 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
 
 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
 {
-       /*
-        * The ID of the root cgroup is 0, but memcg treat 0 as an
-        * invalid ID, so we return (cgroup_id + 1).
-        */
-       return memcg->css.cgroup->id + 1;
+       return memcg->css.cgroup->id;
 }
 
 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
 {
        struct cgroup_subsys_state *css;
 
-       css = css_from_id(id - 1, &memory_cgrp_subsys);
+       css = css_from_id(id, &memory_cgrp_subsys);
        return mem_cgroup_from_css(css);
 }