]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/commitdiff
cgroup: add css_parent()
authorTejun Heo <tj@kernel.org>
Fri, 9 Aug 2013 00:11:23 +0000 (20:11 -0400)
committerTejun Heo <tj@kernel.org>
Fri, 9 Aug 2013 00:11:23 +0000 (20:11 -0400)
Currently, controllers have to explicitly follow the cgroup hierarchy
to find the parent of a given css.  cgroup is moving towards using
cgroup_subsys_state as the main controller interface construct, so
let's provide a way to climb the hierarchy using just csses.

This patch implements css_parent() which, given a css, returns its
parent.  The function is guarnateed to valid non-NULL parent css as
long as the target css is not at the top of the hierarchy.

freezer, cpuset, cpu, cpuacct, hugetlb, memory, net_cls and devices
are converted to use css_parent() instead of accessing cgroup->parent
directly.

* __parent_ca() is dropped from cpuacct and its usage is replaced with
  parent_ca().  The only difference between the two was NULL test on
  cgroup->parent which is now embedded in css_parent() making the
  distinction moot.  Note that eventually a css->parent field will be
  added to css and the NULL check in css_parent() will go away.

This patch shouldn't cause any behavior differences.

Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Li Zefan <lizefan@huawei.com>
block/blk-cgroup.h
include/linux/cgroup.h
kernel/cgroup_freezer.c
kernel/cpuset.c
kernel/sched/core.c
kernel/sched/cpuacct.c
mm/hugetlb_cgroup.c
mm/memcontrol.c
net/sched/cls_cgroup.c
security/device_cgroup.c

index 8e5863e900bf1a2cc2f4c18dca0b2e2703f74e42..b6802c46d68fdcf17ca679806ebae0e282bee2b3 100644 (file)
@@ -209,9 +209,7 @@ static inline struct blkcg *bio_blkcg(struct bio *bio)
  */
 static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
 {
-       struct cgroup *pcg = blkcg->css.cgroup->parent;
-
-       return pcg ? cgroup_to_blkcg(pcg) : NULL;
+       return css_to_blkcg(css_parent(&blkcg->css));
 }
 
 /**
index 821678aae4db607aa0bcc539107bbf6c441597a7..18112a3bb12bc2a80a4c48d979774ad28ef23166 100644 (file)
@@ -646,6 +646,21 @@ struct cgroup_subsys {
 #undef IS_SUBSYS_ENABLED
 #undef SUBSYS
 
+/**
+ * css_parent - find the parent css
+ * @css: the target cgroup_subsys_state
+ *
+ * Return the parent css of @css.  This function is guaranteed to return
+ * non-NULL parent as long as @css isn't the root.
+ */
+static inline
+struct cgroup_subsys_state *css_parent(struct cgroup_subsys_state *css)
+{
+       struct cgroup *parent_cgrp = css->cgroup->parent;
+
+       return parent_cgrp ? parent_cgrp->subsys[css->ss->subsys_id] : NULL;
+}
+
 /**
  * cgroup_css - obtain a cgroup's css for the specified subsystem
  * @cgrp: the cgroup of interest
index 1db686e47a22a24eafe322e4249bbd983aeef76a..657a73cd44c4003de917e951069617a699b8e732 100644 (file)
@@ -62,11 +62,7 @@ static inline struct freezer *task_freezer(struct task_struct *task)
 
 static struct freezer *parent_freezer(struct freezer *freezer)
 {
-       struct cgroup *pcg = freezer->css.cgroup->parent;
-
-       if (pcg)
-               return cgroup_freezer(pcg);
-       return NULL;
+       return css_freezer(css_parent(&freezer->css));
 }
 
 bool cgroup_freezing(struct task_struct *task)
@@ -234,7 +230,7 @@ static void freezer_fork(struct task_struct *task)
         * The root cgroup is non-freezable, so we can skip the
         * following check.
         */
-       if (!freezer->css.cgroup->parent)
+       if (!parent_freezer(freezer))
                goto out;
 
        spin_lock_irq(&freezer->lock);
index 6e9cbdde25bd61e994ca395dd0f131c857a7aeaf..259a4af37e69258ebdaadd145af24f16fbd4cd19 100644 (file)
@@ -133,11 +133,7 @@ static inline struct cpuset *task_cs(struct task_struct *task)
 
 static inline struct cpuset *parent_cs(struct cpuset *cs)
 {
-       struct cgroup *pcgrp = cs->css.cgroup->parent;
-
-       if (pcgrp)
-               return cgroup_cs(pcgrp);
-       return NULL;
+       return css_cs(css_parent(&cs->css));
 }
 
 #ifdef CONFIG_NUMA
index 5bccb0277129a728978a6e1ebc630cda7366ac0c..7a10742b389a61511e8c09854fff5b4a30a54a21 100644 (file)
@@ -7114,13 +7114,10 @@ static struct cgroup_subsys_state *cpu_cgroup_css_alloc(struct cgroup *cgrp)
 static int cpu_cgroup_css_online(struct cgroup *cgrp)
 {
        struct task_group *tg = cgroup_tg(cgrp);
-       struct task_group *parent;
+       struct task_group *parent = css_tg(css_parent(&tg->css));
 
-       if (!cgrp->parent)
-               return 0;
-
-       parent = cgroup_tg(cgrp->parent);
-       sched_online_group(tg, parent);
+       if (parent)
+               sched_online_group(tg, parent);
        return 0;
 }
 
index 8ccfa10cc89ff4af0f9b4692b6f6d19129f1d4ec..f6926a149a71d40ed9d4f061f01c04766de3e5f9 100644 (file)
@@ -50,16 +50,9 @@ static inline struct cpuacct *task_ca(struct task_struct *tsk)
        return css_ca(task_css(tsk, cpuacct_subsys_id));
 }
 
-static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
-{
-       return cgroup_ca(ca->css.cgroup->parent);
-}
-
 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 {
-       if (!ca->css.cgroup->parent)
-               return NULL;
-       return cgroup_ca(ca->css.cgroup->parent);
+       return css_ca(css_parent(&ca->css));
 }
 
 static DEFINE_PER_CPU(u64, root_cpuacct_cpuusage);
@@ -284,7 +277,7 @@ void cpuacct_account_field(struct task_struct *p, int index, u64 val)
        while (ca != &root_cpuacct) {
                kcpustat = this_cpu_ptr(ca->cpustat);
                kcpustat->cpustat[index] += val;
-               ca = __parent_ca(ca);
+               ca = parent_ca(ca);
        }
        rcu_read_unlock();
 }
index 95585a0b9c8da8b1582d2341720a944b3b47a0b7..57ecb5d2513fe53f062c58712ee1cc6103114bab 100644 (file)
@@ -59,11 +59,7 @@ static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
 static inline struct hugetlb_cgroup *
 parent_hugetlb_cgroup(struct hugetlb_cgroup *h_cg)
 {
-       struct cgroup *parent = h_cg->css.cgroup->parent;
-
-       if (!parent)
-               return NULL;
-       return hugetlb_cgroup_from_cgroup(parent);
+       return hugetlb_cgroup_from_css(css_parent(&h_cg->css));
 }
 
 static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)
index 11d659e3b08ede8bdf7a8f5b15452e0a013d67d2..69b3e520f9211afdce2cb533a5256d9baf048871 100644 (file)
@@ -1524,10 +1524,8 @@ static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
 
 int mem_cgroup_swappiness(struct mem_cgroup *memcg)
 {
-       struct cgroup *cgrp = memcg->css.cgroup;
-
        /* root ? */
-       if (cgrp->parent == NULL)
+       if (!css_parent(&memcg->css))
                return vm_swappiness;
 
        return memcg->swappiness;
@@ -5026,11 +5024,7 @@ static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
 {
        int retval = 0;
        struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
-       struct cgroup *parent = cont->parent;
-       struct mem_cgroup *parent_memcg = NULL;
-
-       if (parent)
-               parent_memcg = mem_cgroup_from_cont(parent);
+       struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
 
        mutex_lock(&memcg_create_mutex);
 
@@ -5282,18 +5276,15 @@ static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
                unsigned long long *mem_limit, unsigned long long *memsw_limit)
 {
-       struct cgroup *cgroup;
        unsigned long long min_limit, min_memsw_limit, tmp;
 
        min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
        min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
-       cgroup = memcg->css.cgroup;
        if (!memcg->use_hierarchy)
                goto out;
 
-       while (cgroup->parent) {
-               cgroup = cgroup->parent;
-               memcg = mem_cgroup_from_cont(cgroup);
+       while (css_parent(&memcg->css)) {
+               memcg = mem_cgroup_from_css(css_parent(&memcg->css));
                if (!memcg->use_hierarchy)
                        break;
                tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
@@ -5523,16 +5514,11 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
                                       u64 val)
 {
        struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
-       struct mem_cgroup *parent;
-
-       if (val > 100)
-               return -EINVAL;
+       struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
 
-       if (cgrp->parent == NULL)
+       if (val > 100 || !parent)
                return -EINVAL;
 
-       parent = mem_cgroup_from_cont(cgrp->parent);
-
        mutex_lock(&memcg_create_mutex);
 
        /* If under hierarchy, only empty-root can set this value */
@@ -5861,14 +5847,12 @@ static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
        struct cftype *cft, u64 val)
 {
        struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
-       struct mem_cgroup *parent;
+       struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
 
        /* cannot set to root cgroup and only 0 and 1 are allowed */
-       if (!cgrp->parent || !((val == 0) || (val == 1)))
+       if (!parent || !((val == 0) || (val == 1)))
                return -EINVAL;
 
-       parent = mem_cgroup_from_cont(cgrp->parent);
-
        mutex_lock(&memcg_create_mutex);
        /* oom-kill-disable is a flag for subhierarchy. */
        if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
@@ -6266,15 +6250,14 @@ free_out:
 static int
 mem_cgroup_css_online(struct cgroup *cont)
 {
-       struct mem_cgroup *memcg, *parent;
+       struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
+       struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
        int error = 0;
 
-       if (!cont->parent)
+       if (!parent)
                return 0;
 
        mutex_lock(&memcg_create_mutex);
-       memcg = mem_cgroup_from_cont(cont);
-       parent = mem_cgroup_from_cont(cont->parent);
 
        memcg->use_hierarchy = parent->use_hierarchy;
        memcg->oom_kill_disable = parent->oom_kill_disable;
index af412ab2b477777f9348cc51c839681f72a42c24..9e6b75e5efce4261cb46e5fe9160526564f74cd8 100644 (file)
@@ -50,9 +50,11 @@ static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
 
 static int cgrp_css_online(struct cgroup *cgrp)
 {
-       if (cgrp->parent)
-               cgrp_cls_state(cgrp)->classid =
-                       cgrp_cls_state(cgrp->parent)->classid;
+       struct cgroup_cls_state *cs = cgrp_cls_state(cgrp);
+       struct cgroup_cls_state *parent = css_cls_state(css_parent(&cs->css));
+
+       if (parent)
+               cs->classid = parent->classid;
        return 0;
 }
 
index 90953648c643e02b223a42c99fe866722a6b7d42..635a49db005d4a4313f6a52243c8a748dab49c18 100644 (file)
@@ -198,13 +198,11 @@ static inline bool is_devcg_online(const struct dev_cgroup *devcg)
  */
 static int devcgroup_online(struct cgroup *cgroup)
 {
-       struct dev_cgroup *dev_cgroup, *parent_dev_cgroup = NULL;
+       struct dev_cgroup *dev_cgroup = cgroup_to_devcgroup(cgroup);
+       struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css_parent(&dev_cgroup->css));
        int ret = 0;
 
        mutex_lock(&devcgroup_mutex);
-       dev_cgroup = cgroup_to_devcgroup(cgroup);
-       if (cgroup->parent)
-               parent_dev_cgroup = cgroup_to_devcgroup(cgroup->parent);
 
        if (parent_dev_cgroup == NULL)
                dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
@@ -394,12 +392,10 @@ static bool may_access(struct dev_cgroup *dev_cgroup,
 static int parent_has_perm(struct dev_cgroup *childcg,
                                  struct dev_exception_item *ex)
 {
-       struct cgroup *pcg = childcg->css.cgroup->parent;
-       struct dev_cgroup *parent;
+       struct dev_cgroup *parent = css_to_devcgroup(css_parent(&childcg->css));
 
-       if (!pcg)
+       if (!parent)
                return 1;
-       parent = cgroup_to_devcgroup(pcg);
        return may_access(parent, ex, childcg->behavior);
 }
 
@@ -524,15 +520,11 @@ static int devcgroup_update_access(struct dev_cgroup *devcgroup,
        char temp[12];          /* 11 + 1 characters needed for a u32 */
        int count, rc = 0;
        struct dev_exception_item ex;
-       struct cgroup *p = devcgroup->css.cgroup;
-       struct dev_cgroup *parent = NULL;
+       struct dev_cgroup *parent = css_to_devcgroup(css_parent(&devcgroup->css));
 
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
-       if (p->parent)
-               parent = cgroup_to_devcgroup(p->parent);
-
        memset(&ex, 0, sizeof(ex));
        b = buffer;