Skip to content

Commit eb95419

Browse files
committed
cgroup: pass around cgroup_subsys_state instead of cgroup in subsystem methods
cgroup is currently in the process of transitioning to using struct cgroup_subsys_state * as the primary handle instead of struct cgroup * in subsystem implementations for the following reasons. * With unified hierarchy, subsystems will be dynamically bound and unbound from cgroups and thus css's (cgroup_subsys_state) may be created and destroyed dynamically over the lifetime of a cgroup, which is different from the current state where all css's are allocated and destroyed together with the associated cgroup. This in turn means that cgroup_css() should be synchronized and may return NULL, making it more cumbersome to use. * Differing levels of per-subsystem granularity in the unified hierarchy means that the task and descendant iterators should behave differently depending on the specific subsystem the iteration is being performed for. * In majority of the cases, subsystems only care about its part in the cgroup hierarchy - ie. the hierarchy of css's. Subsystem methods often obtain the matching css pointer from the cgroup and don't bother with the cgroup pointer itself. Passing around css fits much better. This patch converts all cgroup_subsys methods to take @css instead of @CGROUP. The conversions are mostly straight-forward. A few noteworthy changes are * ->css_alloc() now takes css of the parent cgroup rather than the pointer to the new cgroup as the css for the new cgroup doesn't exist yet. Knowing the parent css is enough for all the existing subsystems. * In kernel/cgroup.c::offline_css(), unnecessary open coded css dereference is replaced with local variable access. This patch shouldn't cause any behavior differences. v2: Unnecessary explicit cgrp->subsys[] deref in css_online() replaced with local variable @css as suggested by Li Zefan. Rebased on top of new for-3.12 which includes for-3.11-fixes so that ->css_free() invocation added by da0a12c ("cgroup: fix a leak when percpu_ref_init() fails") is converted too. Suggested by Li Zefan. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Li Zefan <lizefan@huawei.com> Acked-by: Michal Hocko <mhocko@suse.cz> Acked-by: Vivek Goyal <vgoyal@redhat.com> Acked-by: Aristeu Rozanski <aris@redhat.com> Acked-by: Daniel Wagner <daniel.wagner@bmw-carit.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Balbir Singh <bsingharora@gmail.com> Cc: Matt Helsley <matthltc@us.ibm.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Steven Rostedt <rostedt@goodmis.org>
1 parent 6387698 commit eb95419

13 files changed

Lines changed: 197 additions & 171 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -765,18 +765,18 @@ struct cftype blkcg_files[] = {
765765

766766
/**
767767
* blkcg_css_offline - cgroup css_offline callback
768-
* @cgroup: cgroup of interest
768+
* @css: css of interest
769769
*
770-
* This function is called when @cgroup is about to go away and responsible
771-
* for shooting down all blkgs associated with @cgroup. blkgs should be
770+
* This function is called when @css is about to go away and responsible
771+
* for shooting down all blkgs associated with @css. blkgs should be
772772
* removed while holding both q and blkcg locks. As blkcg lock is nested
773773
* inside q lock, this function performs reverse double lock dancing.
774774
*
775775
* This is the blkcg counterpart of ioc_release_fn().
776776
*/
777-
static void blkcg_css_offline(struct cgroup *cgroup)
777+
static void blkcg_css_offline(struct cgroup_subsys_state *css)
778778
{
779-
struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
779+
struct blkcg *blkcg = css_to_blkcg(css);
780780

781781
spin_lock_irq(&blkcg->lock);
782782

@@ -798,21 +798,21 @@ static void blkcg_css_offline(struct cgroup *cgroup)
798798
spin_unlock_irq(&blkcg->lock);
799799
}
800800

801-
static void blkcg_css_free(struct cgroup *cgroup)
801+
static void blkcg_css_free(struct cgroup_subsys_state *css)
802802
{
803-
struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
803+
struct blkcg *blkcg = css_to_blkcg(css);
804804

805805
if (blkcg != &blkcg_root)
806806
kfree(blkcg);
807807
}
808808

809-
static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
809+
static struct cgroup_subsys_state *
810+
blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
810811
{
811812
static atomic64_t id_seq = ATOMIC64_INIT(0);
812813
struct blkcg *blkcg;
813-
struct cgroup *parent = cgroup->parent;
814814

815-
if (!parent) {
815+
if (!parent_css) {
816816
blkcg = &blkcg_root;
817817
goto done;
818818
}
@@ -883,14 +883,15 @@ void blkcg_exit_queue(struct request_queue *q)
883883
* of the main cic data structures. For now we allow a task to change
884884
* its cgroup only if it's the only owner of its ioc.
885885
*/
886-
static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
886+
static int blkcg_can_attach(struct cgroup_subsys_state *css,
887+
struct cgroup_taskset *tset)
887888
{
888889
struct task_struct *task;
889890
struct io_context *ioc;
890891
int ret = 0;
891892

892893
/* task_lock() is needed to avoid races with exit_io_context() */
893-
cgroup_taskset_for_each(task, cgrp, tset) {
894+
cgroup_taskset_for_each(task, css->cgroup, tset) {
894895
task_lock(task);
895896
ioc = task->io_context;
896897
if (ioc && atomic_read(&ioc->nr_tasks) > 1)

‎include/linux/cgroup.h‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,18 +579,22 @@ int cgroup_taskset_size(struct cgroup_taskset *tset);
579579
*/
580580

581581
struct cgroup_subsys {
582-
struct cgroup_subsys_state *(*css_alloc)(struct cgroup *cgrp);
583-
int (*css_online)(struct cgroup *cgrp);
584-
void (*css_offline)(struct cgroup *cgrp);
585-
void (*css_free)(struct cgroup *cgrp);
586-
587-
int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
588-
void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
589-
void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
582+
struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
583+
int (*css_online)(struct cgroup_subsys_state *css);
584+
void (*css_offline)(struct cgroup_subsys_state *css);
585+
void (*css_free)(struct cgroup_subsys_state *css);
586+
587+
int (*can_attach)(struct cgroup_subsys_state *css,
588+
struct cgroup_taskset *tset);
589+
void (*cancel_attach)(struct cgroup_subsys_state *css,
590+
struct cgroup_taskset *tset);
591+
void (*attach)(struct cgroup_subsys_state *css,
592+
struct cgroup_taskset *tset);
590593
void (*fork)(struct task_struct *task);
591-
void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
594+
void (*exit)(struct cgroup_subsys_state *css,
595+
struct cgroup_subsys_state *old_css,
592596
struct task_struct *task);
593-
void (*bind)(struct cgroup *root);
597+
void (*bind)(struct cgroup_subsys_state *root_css);
594598

595599
int subsys_id;
596600
int disabled;

‎kernel/cgroup.c‎

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,11 @@ static void cgroup_free_fn(struct work_struct *work)
853853
/*
854854
* Release the subsystem state objects.
855855
*/
856-
for_each_root_subsys(cgrp->root, ss)
857-
ss->css_free(cgrp);
856+
for_each_root_subsys(cgrp->root, ss) {
857+
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
858+
859+
ss->css_free(css);
860+
}
858861

859862
cgrp->root->number_of_cgroups--;
860863
mutex_unlock(&cgroup_mutex);
@@ -1056,7 +1059,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
10561059
list_move(&ss->sibling, &root->subsys_list);
10571060
ss->root = root;
10581061
if (ss->bind)
1059-
ss->bind(cgrp);
1062+
ss->bind(cgrp->subsys[i]);
10601063

10611064
/* refcount was already taken, and we're keeping it */
10621065
root->subsys_mask |= bit;
@@ -1066,7 +1069,7 @@ static int rebind_subsystems(struct cgroupfs_root *root,
10661069
BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
10671070

10681071
if (ss->bind)
1069-
ss->bind(cgroup_dummy_top);
1072+
ss->bind(cgroup_dummy_top->subsys[i]);
10701073
cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
10711074
cgrp->subsys[i] = NULL;
10721075
cgroup_subsys[i]->root = &cgroup_dummy_root;
@@ -2049,8 +2052,10 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
20492052
* step 1: check that we can legitimately attach to the cgroup.
20502053
*/
20512054
for_each_root_subsys(root, ss) {
2055+
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2056+
20522057
if (ss->can_attach) {
2053-
retval = ss->can_attach(cgrp, &tset);
2058+
retval = ss->can_attach(css, &tset);
20542059
if (retval) {
20552060
failed_ss = ss;
20562061
goto out_cancel_attach;
@@ -2089,8 +2094,10 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
20892094
* step 4: do subsystem attach callbacks.
20902095
*/
20912096
for_each_root_subsys(root, ss) {
2097+
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2098+
20922099
if (ss->attach)
2093-
ss->attach(cgrp, &tset);
2100+
ss->attach(css, &tset);
20942101
}
20952102

20962103
/*
@@ -2109,10 +2116,12 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
21092116
out_cancel_attach:
21102117
if (retval) {
21112118
for_each_root_subsys(root, ss) {
2119+
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2120+
21122121
if (ss == failed_ss)
21132122
break;
21142123
if (ss->cancel_attach)
2115-
ss->cancel_attach(cgrp, &tset);
2124+
ss->cancel_attach(css, &tset);
21162125
}
21172126
}
21182127
out_free_group_list:
@@ -4206,14 +4215,15 @@ static void init_cgroup_css(struct cgroup_subsys_state *css,
42064215
/* invoke ->css_online() on a new CSS and mark it online if successful */
42074216
static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
42084217
{
4218+
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
42094219
int ret = 0;
42104220

42114221
lockdep_assert_held(&cgroup_mutex);
42124222

42134223
if (ss->css_online)
4214-
ret = ss->css_online(cgrp);
4224+
ret = ss->css_online(css);
42154225
if (!ret)
4216-
cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4226+
css->flags |= CSS_ONLINE;
42174227
return ret;
42184228
}
42194229

@@ -4228,9 +4238,9 @@ static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
42284238
return;
42294239

42304240
if (ss->css_offline)
4231-
ss->css_offline(cgrp);
4241+
ss->css_offline(css);
42324242

4233-
cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4243+
css->flags &= ~CSS_ONLINE;
42344244
}
42354245

42364246
/*
@@ -4305,15 +4315,15 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
43054315
for_each_root_subsys(root, ss) {
43064316
struct cgroup_subsys_state *css;
43074317

4308-
css = ss->css_alloc(cgrp);
4318+
css = ss->css_alloc(parent->subsys[ss->subsys_id]);
43094319
if (IS_ERR(css)) {
43104320
err = PTR_ERR(css);
43114321
goto err_free_all;
43124322
}
43134323

43144324
err = percpu_ref_init(&css->refcnt, css_release);
43154325
if (err) {
4316-
ss->css_free(cgrp);
4326+
ss->css_free(css);
43174327
goto err_free_all;
43184328
}
43194329

@@ -4386,7 +4396,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
43864396

43874397
if (css) {
43884398
percpu_ref_cancel_init(&css->refcnt);
4389-
ss->css_free(cgrp);
4399+
ss->css_free(css);
43904400
}
43914401
}
43924402
mutex_unlock(&cgroup_mutex);
@@ -4641,7 +4651,7 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
46414651
/* Create the top cgroup state for this subsystem */
46424652
list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
46434653
ss->root = &cgroup_dummy_root;
4644-
css = ss->css_alloc(cgroup_dummy_top);
4654+
css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
46454655
/* We don't handle early failures gracefully */
46464656
BUG_ON(IS_ERR(css));
46474657
init_cgroup_css(css, ss, cgroup_dummy_top);
@@ -4720,7 +4730,7 @@ int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
47204730
* struct, so this can happen first (i.e. before the dummy root
47214731
* attachment).
47224732
*/
4723-
css = ss->css_alloc(cgroup_dummy_top);
4733+
css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
47244734
if (IS_ERR(css)) {
47254735
/* failure case - need to deassign the cgroup_subsys[] slot. */
47264736
cgroup_subsys[ss->subsys_id] = NULL;
@@ -4836,7 +4846,7 @@ void cgroup_unload_subsys(struct cgroup_subsys *ss)
48364846
* the cgrp->subsys pointer to find their state. note that this
48374847
* also takes care of freeing the css_id.
48384848
*/
4839-
ss->css_free(cgroup_dummy_top);
4849+
ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]);
48404850
cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
48414851

48424852
mutex_unlock(&cgroup_mutex);
@@ -5192,10 +5202,10 @@ void cgroup_exit(struct task_struct *tsk, int run_callbacks)
51925202
*/
51935203
for_each_builtin_subsys(ss, i) {
51945204
if (ss->exit) {
5195-
struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
5196-
struct cgroup *cgrp = task_cgroup(tsk, i);
5205+
struct cgroup_subsys_state *old_css = cset->subsys[i];
5206+
struct cgroup_subsys_state *css = task_css(tsk, i);
51975207

5198-
ss->exit(cgrp, old_cgrp, tsk);
5208+
ss->exit(css, old_css, tsk);
51995209
}
52005210
}
52015211
}
@@ -5529,7 +5539,8 @@ struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
55295539
}
55305540

55315541
#ifdef CONFIG_CGROUP_DEBUG
5532-
static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
5542+
static struct cgroup_subsys_state *
5543+
debug_css_alloc(struct cgroup_subsys_state *parent_css)
55335544
{
55345545
struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
55355546

@@ -5539,9 +5550,9 @@ static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
55395550
return css;
55405551
}
55415552

5542-
static void debug_css_free(struct cgroup *cgrp)
5553+
static void debug_css_free(struct cgroup_subsys_state *css)
55435554
{
5544-
kfree(cgrp->subsys[debug_subsys_id]);
5555+
kfree(css);
55455556
}
55465557

55475558
static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)

‎kernel/cgroup_freezer.c‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static const char *freezer_state_strs(unsigned int state)
9191

9292
struct cgroup_subsys freezer_subsys;
9393

94-
static struct cgroup_subsys_state *freezer_css_alloc(struct cgroup *cgroup)
94+
static struct cgroup_subsys_state *
95+
freezer_css_alloc(struct cgroup_subsys_state *parent_css)
9596
{
9697
struct freezer *freezer;
9798

@@ -104,16 +105,16 @@ static struct cgroup_subsys_state *freezer_css_alloc(struct cgroup *cgroup)
104105
}
105106

106107
/**
107-
* freezer_css_online - commit creation of a freezer cgroup
108-
* @cgroup: cgroup being created
108+
* freezer_css_online - commit creation of a freezer css
109+
* @css: css being created
109110
*
110-
* We're committing to creation of @cgroup. Mark it online and inherit
111+
* We're committing to creation of @css. Mark it online and inherit
111112
* parent's freezing state while holding both parent's and our
112113
* freezer->lock.
113114
*/
114-
static int freezer_css_online(struct cgroup *cgroup)
115+
static int freezer_css_online(struct cgroup_subsys_state *css)
115116
{
116-
struct freezer *freezer = cgroup_freezer(cgroup);
117+
struct freezer *freezer = css_freezer(css);
117118
struct freezer *parent = parent_freezer(freezer);
118119

119120
/*
@@ -140,15 +141,15 @@ static int freezer_css_online(struct cgroup *cgroup)
140141
}
141142

142143
/**
143-
* freezer_css_offline - initiate destruction of @cgroup
144-
* @cgroup: cgroup being destroyed
144+
* freezer_css_offline - initiate destruction of a freezer css
145+
* @css: css being destroyed
145146
*
146-
* @cgroup is going away. Mark it dead and decrement system_freezing_count
147-
* if it was holding one.
147+
* @css is going away. Mark it dead and decrement system_freezing_count if
148+
* it was holding one.
148149
*/
149-
static void freezer_css_offline(struct cgroup *cgroup)
150+
static void freezer_css_offline(struct cgroup_subsys_state *css)
150151
{
151-
struct freezer *freezer = cgroup_freezer(cgroup);
152+
struct freezer *freezer = css_freezer(css);
152153

153154
spin_lock_irq(&freezer->lock);
154155

@@ -160,9 +161,9 @@ static void freezer_css_offline(struct cgroup *cgroup)
160161
spin_unlock_irq(&freezer->lock);
161162
}
162163

163-
static void freezer_css_free(struct cgroup *cgroup)
164+
static void freezer_css_free(struct cgroup_subsys_state *css)
164165
{
165-
kfree(cgroup_freezer(cgroup));
166+
kfree(css_freezer(css));
166167
}
167168

168169
/*
@@ -174,25 +175,26 @@ static void freezer_css_free(struct cgroup *cgroup)
174175
* @freezer->lock. freezer_attach() makes the new tasks conform to the
175176
* current state and all following state changes can see the new tasks.
176177
*/
177-
static void freezer_attach(struct cgroup *new_cgrp, struct cgroup_taskset *tset)
178+
static void freezer_attach(struct cgroup_subsys_state *new_css,
179+
struct cgroup_taskset *tset)
178180
{
179-
struct freezer *freezer = cgroup_freezer(new_cgrp);
181+
struct freezer *freezer = css_freezer(new_css);
180182
struct task_struct *task;
181183
bool clear_frozen = false;
182184

183185
spin_lock_irq(&freezer->lock);
184186

185187
/*
186-
* Make the new tasks conform to the current state of @new_cgrp.
188+
* Make the new tasks conform to the current state of @new_css.
187189
* For simplicity, when migrating any task to a FROZEN cgroup, we
188190
* revert it to FREEZING and let update_if_frozen() determine the
189191
* correct state later.
190192
*
191-
* Tasks in @tset are on @new_cgrp but may not conform to its
193+
* Tasks in @tset are on @new_css but may not conform to its
192194
* current state before executing the following - !frozen tasks may
193195
* be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
194196
*/
195-
cgroup_taskset_for_each(task, new_cgrp, tset) {
197+
cgroup_taskset_for_each(task, new_css->cgroup, tset) {
196198
if (!(freezer->state & CGROUP_FREEZING)) {
197199
__thaw_task(task);
198200
} else {

0 commit comments

Comments
 (0)