2 * Common Block IO controller cgroup interface
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
13 #include <linux/ioprio.h>
14 #include "blk-cgroup.h"
16 struct blkio_cgroup blkio_root_cgroup
= { .weight
= 2*BLKIO_WEIGHT_DEFAULT
};
18 struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
)
20 return container_of(cgroup_subsys_state(cgroup
, blkio_subsys_id
),
21 struct blkio_cgroup
, css
);
24 void blkiocg_add_blkio_group(struct blkio_cgroup
*blkcg
,
25 struct blkio_group
*blkg
, void *key
)
29 spin_lock_irqsave(&blkcg
->lock
, flags
);
30 rcu_assign_pointer(blkg
->key
, key
);
31 hlist_add_head_rcu(&blkg
->blkcg_node
, &blkcg
->blkg_list
);
32 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
35 int blkiocg_del_blkio_group(struct blkio_group
*blkg
)
37 /* Implemented later */
41 /* called under rcu_read_lock(). */
42 struct blkio_group
*blkiocg_lookup_group(struct blkio_cgroup
*blkcg
, void *key
)
44 struct blkio_group
*blkg
;
48 hlist_for_each_entry_rcu(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
57 #define SHOW_FUNCTION(__VAR) \
58 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
59 struct cftype *cftype) \
61 struct blkio_cgroup *blkcg; \
63 blkcg = cgroup_to_blkio_cgroup(cgroup); \
64 return (u64)blkcg->__VAR; \
67 SHOW_FUNCTION(weight
);
71 blkiocg_weight_write(struct cgroup
*cgroup
, struct cftype
*cftype
, u64 val
)
73 struct blkio_cgroup
*blkcg
;
75 if (val
< BLKIO_WEIGHT_MIN
|| val
> BLKIO_WEIGHT_MAX
)
78 blkcg
= cgroup_to_blkio_cgroup(cgroup
);
79 blkcg
->weight
= (unsigned int)val
;
83 struct cftype blkio_files
[] = {
86 .read_u64
= blkiocg_weight_read
,
87 .write_u64
= blkiocg_weight_write
,
91 static int blkiocg_populate(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
93 return cgroup_add_files(cgroup
, subsys
, blkio_files
,
94 ARRAY_SIZE(blkio_files
));
97 static void blkiocg_destroy(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
99 struct blkio_cgroup
*blkcg
= cgroup_to_blkio_cgroup(cgroup
);
101 free_css_id(&blkio_subsys
, &blkcg
->css
);
105 static struct cgroup_subsys_state
*
106 blkiocg_create(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
108 struct blkio_cgroup
*blkcg
, *parent_blkcg
;
110 if (!cgroup
->parent
) {
111 blkcg
= &blkio_root_cgroup
;
115 /* Currently we do not support hierarchy deeper than two level (0,1) */
116 parent_blkcg
= cgroup_to_blkio_cgroup(cgroup
->parent
);
117 if (css_depth(&parent_blkcg
->css
) > 0)
118 return ERR_PTR(-EINVAL
);
120 blkcg
= kzalloc(sizeof(*blkcg
), GFP_KERNEL
);
122 return ERR_PTR(-ENOMEM
);
124 blkcg
->weight
= BLKIO_WEIGHT_DEFAULT
;
126 spin_lock_init(&blkcg
->lock
);
127 INIT_HLIST_HEAD(&blkcg
->blkg_list
);
133 * We cannot support shared io contexts, as we have no mean to support
134 * two tasks with the same ioc in two different groups without major rework
135 * of the main cic data structures. For now we allow a task to change
136 * its cgroup only if it's the only owner of its ioc.
138 static int blkiocg_can_attach(struct cgroup_subsys
*subsys
,
139 struct cgroup
*cgroup
, struct task_struct
*tsk
,
142 struct io_context
*ioc
;
145 /* task_lock() is needed to avoid races with exit_io_context() */
147 ioc
= tsk
->io_context
;
148 if (ioc
&& atomic_read(&ioc
->nr_tasks
) > 1)
155 static void blkiocg_attach(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
,
156 struct cgroup
*prev
, struct task_struct
*tsk
,
159 struct io_context
*ioc
;
162 ioc
= tsk
->io_context
;
164 ioc
->cgroup_changed
= 1;
168 struct cgroup_subsys blkio_subsys
= {
170 .create
= blkiocg_create
,
171 .can_attach
= blkiocg_can_attach
,
172 .attach
= blkiocg_attach
,
173 .destroy
= blkiocg_destroy
,
174 .populate
= blkiocg_populate
,
175 .subsys_id
= blkio_subsys_id
,