Commit a7c6d554 authored by Tejun Heo's avatar Tejun Heo

cgroup: add/update accessors which obtain subsys specific data from css

css (cgroup_subsys_state) is usually embedded in a subsys specific
data structure.  Subsystems either use container_of() directly to cast
from css to such data structure or has an accessor function wrapping
such cast.  As cgroup as whole is moving towards using css as the main
interface handle, add and update such accessors to ease dealing with
css's.

All accessors explicitly handle NULL input and return NULL in those
cases.  While this looks like an extra branch in the code, as all
controllers specific data structures have css as the first field, the
casting doesn't involve any offsetting and the compiler can trivially
optimize out the branch.

* blkio, freezer, cpuset, cpu, cpuacct and net_cls didn't have such
  accessor.  Added.

* memory, hugetlb and devices already had one but didn't explicitly
  handle NULL input.  Updated.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarLi Zefan <lizefan@huawei.com>
parent 72c97e54
...@@ -179,21 +179,25 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, ...@@ -179,21 +179,25 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
void blkg_conf_finish(struct blkg_conf_ctx *ctx); void blkg_conf_finish(struct blkg_conf_ctx *ctx);
static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
{
return css ? container_of(css, struct blkcg, css) : NULL;
}
static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup)
{ {
return container_of(cgroup_css(cgroup, blkio_subsys_id), return css_to_blkcg(cgroup_css(cgroup, blkio_subsys_id));
struct blkcg, css);
} }
static inline struct blkcg *task_blkcg(struct task_struct *tsk) static inline struct blkcg *task_blkcg(struct task_struct *tsk)
{ {
return container_of(task_css(tsk, blkio_subsys_id), struct blkcg, css); return css_to_blkcg(task_css(tsk, blkio_subsys_id));
} }
static inline struct blkcg *bio_blkcg(struct bio *bio) static inline struct blkcg *bio_blkcg(struct bio *bio)
{ {
if (bio && bio->bi_css) if (bio && bio->bi_css)
return container_of(bio->bi_css, struct blkcg, css); return css_to_blkcg(bio->bi_css);
return task_blkcg(current); return task_blkcg(current);
} }
......
...@@ -45,16 +45,19 @@ struct freezer { ...@@ -45,16 +45,19 @@ struct freezer {
spinlock_t lock; spinlock_t lock;
}; };
static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
{
return css ? container_of(css, struct freezer, css) : NULL;
}
static inline struct freezer *cgroup_freezer(struct cgroup *cgroup) static inline struct freezer *cgroup_freezer(struct cgroup *cgroup)
{ {
return container_of(cgroup_css(cgroup, freezer_subsys_id), return css_freezer(cgroup_css(cgroup, freezer_subsys_id));
struct freezer, css);
} }
static inline struct freezer *task_freezer(struct task_struct *task) static inline struct freezer *task_freezer(struct task_struct *task)
{ {
return container_of(task_css(task, freezer_subsys_id), return css_freezer(task_css(task, freezer_subsys_id));
struct freezer, css);
} }
static struct freezer *parent_freezer(struct freezer *freezer) static struct freezer *parent_freezer(struct freezer *freezer)
......
...@@ -114,18 +114,21 @@ struct cpuset { ...@@ -114,18 +114,21 @@ struct cpuset {
int relax_domain_level; int relax_domain_level;
}; };
static inline struct cpuset *css_cs(struct cgroup_subsys_state *css)
{
return css ? container_of(css, struct cpuset, css) : NULL;
}
/* Retrieve the cpuset for a cgroup */ /* Retrieve the cpuset for a cgroup */
static inline struct cpuset *cgroup_cs(struct cgroup *cgrp) static inline struct cpuset *cgroup_cs(struct cgroup *cgrp)
{ {
return container_of(cgroup_css(cgrp, cpuset_subsys_id), return css_cs(cgroup_css(cgrp, cpuset_subsys_id));
struct cpuset, css);
} }
/* Retrieve the cpuset for a task */ /* Retrieve the cpuset for a task */
static inline struct cpuset *task_cs(struct task_struct *task) static inline struct cpuset *task_cs(struct task_struct *task)
{ {
return container_of(task_css(task, cpuset_subsys_id), return css_cs(task_css(task, cpuset_subsys_id));
struct cpuset, css);
} }
static inline struct cpuset *parent_cs(struct cpuset *cs) static inline struct cpuset *parent_cs(struct cpuset *cs)
......
...@@ -7083,11 +7083,15 @@ int sched_rt_handler(struct ctl_table *table, int write, ...@@ -7083,11 +7083,15 @@ int sched_rt_handler(struct ctl_table *table, int write,
#ifdef CONFIG_CGROUP_SCHED #ifdef CONFIG_CGROUP_SCHED
static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
{
return css ? container_of(css, struct task_group, css) : NULL;
}
/* return corresponding task_group object of a cgroup */ /* return corresponding task_group object of a cgroup */
static inline struct task_group *cgroup_tg(struct cgroup *cgrp) static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
{ {
return container_of(cgroup_css(cgrp, cpu_cgroup_subsys_id), return css_tg(cgroup_css(cgrp, cpu_cgroup_subsys_id));
struct task_group, css);
} }
static struct cgroup_subsys_state *cpu_cgroup_css_alloc(struct cgroup *cgrp) static struct cgroup_subsys_state *cpu_cgroup_css_alloc(struct cgroup *cgrp)
......
...@@ -33,18 +33,21 @@ struct cpuacct { ...@@ -33,18 +33,21 @@ struct cpuacct {
struct kernel_cpustat __percpu *cpustat; struct kernel_cpustat __percpu *cpustat;
}; };
static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
{
return css ? container_of(css, struct cpuacct, css) : NULL;
}
/* return cpu accounting group corresponding to this container */ /* return cpu accounting group corresponding to this container */
static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
{ {
return container_of(cgroup_css(cgrp, cpuacct_subsys_id), return css_ca(cgroup_css(cgrp, cpuacct_subsys_id));
struct cpuacct, css);
} }
/* return cpu accounting group to which this task belongs */ /* return cpu accounting group to which this task belongs */
static inline struct cpuacct *task_ca(struct task_struct *tsk) static inline struct cpuacct *task_ca(struct task_struct *tsk)
{ {
return container_of(task_css(tsk, cpuacct_subsys_id), return css_ca(task_css(tsk, cpuacct_subsys_id));
struct cpuacct, css);
} }
static inline struct cpuacct *__parent_ca(struct cpuacct *ca) static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
......
...@@ -36,7 +36,7 @@ static struct hugetlb_cgroup *root_h_cgroup __read_mostly; ...@@ -36,7 +36,7 @@ static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
static inline static inline
struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s) struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
{ {
return container_of(s, struct hugetlb_cgroup, css); return s ? container_of(s, struct hugetlb_cgroup, css) : NULL;
} }
static inline static inline
......
...@@ -486,7 +486,7 @@ static DEFINE_MUTEX(memcg_create_mutex); ...@@ -486,7 +486,7 @@ static DEFINE_MUTEX(memcg_create_mutex);
static inline static inline
struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s) struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
{ {
return container_of(s, struct mem_cgroup, css); return s ? container_of(s, struct mem_cgroup, css) : NULL;
} }
/* Some nice accessors for the vmpressure. */ /* Some nice accessors for the vmpressure. */
......
...@@ -23,16 +23,19 @@ ...@@ -23,16 +23,19 @@
#include <net/sock.h> #include <net/sock.h>
#include <net/cls_cgroup.h> #include <net/cls_cgroup.h>
static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
{
return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
}
static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp) static inline struct cgroup_cls_state *cgrp_cls_state(struct cgroup *cgrp)
{ {
return container_of(cgroup_css(cgrp, net_cls_subsys_id), return css_cls_state(cgroup_css(cgrp, net_cls_subsys_id));
struct cgroup_cls_state, css);
} }
static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p) static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
{ {
return container_of(task_css(p, net_cls_subsys_id), return css_cls_state(task_css(p, net_cls_subsys_id));
struct cgroup_cls_state, css);
} }
static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp) static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
......
...@@ -53,7 +53,7 @@ struct dev_cgroup { ...@@ -53,7 +53,7 @@ struct dev_cgroup {
static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s) static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
{ {
return container_of(s, struct dev_cgroup, css); return s ? container_of(s, struct dev_cgroup, css) : NULL;
} }
static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup) static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment