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 <linux/seq_file.h>
15 #include <linux/kdev_t.h>
16 #include "blk-cgroup.h"
18 extern void cfq_unlink_blkio_group(void *, struct blkio_group *);
20 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
22 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
24 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
25 struct blkio_cgroup, css);
28 void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
29 unsigned long time, unsigned long sectors)
32 blkg->sectors += sectors;
35 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
36 struct blkio_group *blkg, void *key, dev_t dev)
40 spin_lock_irqsave(&blkcg->lock, flags);
41 rcu_assign_pointer(blkg->key, key);
42 blkg->blkcg_id = css_id(&blkcg->css);
43 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
44 spin_unlock_irqrestore(&blkcg->lock, flags);
45 #ifdef CONFIG_DEBUG_BLK_CGROUP
46 /* Need to take css reference ? */
47 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
52 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
54 hlist_del_init_rcu(&blkg->blkcg_node);
59 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
60 * indicating that blk_group was unhashed by the time we got to it.
62 int blkiocg_del_blkio_group(struct blkio_group *blkg)
64 struct blkio_cgroup *blkcg;
66 struct cgroup_subsys_state *css;
70 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
74 blkcg = container_of(css, struct blkio_cgroup, css);
75 spin_lock_irqsave(&blkcg->lock, flags);
76 if (!hlist_unhashed(&blkg->blkcg_node)) {
77 __blkiocg_del_blkio_group(blkg);
80 spin_unlock_irqrestore(&blkcg->lock, flags);
86 /* called under rcu_read_lock(). */
87 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
89 struct blkio_group *blkg;
93 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
102 #define SHOW_FUNCTION(__VAR) \
103 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
104 struct cftype *cftype) \
106 struct blkio_cgroup *blkcg; \
108 blkcg = cgroup_to_blkio_cgroup(cgroup); \
109 return (u64)blkcg->__VAR; \
112 SHOW_FUNCTION(weight);
116 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
118 struct blkio_cgroup *blkcg;
120 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
123 blkcg = cgroup_to_blkio_cgroup(cgroup);
124 blkcg->weight = (unsigned int)val;
128 #define SHOW_FUNCTION_PER_GROUP(__VAR) \
129 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
130 struct cftype *cftype, struct seq_file *m) \
132 struct blkio_cgroup *blkcg; \
133 struct blkio_group *blkg; \
134 struct hlist_node *n; \
136 if (!cgroup_lock_live_group(cgroup)) \
139 blkcg = cgroup_to_blkio_cgroup(cgroup); \
141 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
143 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
144 MINOR(blkg->dev), blkg->__VAR); \
151 SHOW_FUNCTION_PER_GROUP(time);
152 SHOW_FUNCTION_PER_GROUP(sectors);
153 #ifdef CONFIG_DEBUG_BLK_CGROUP
154 SHOW_FUNCTION_PER_GROUP(dequeue);
156 #undef SHOW_FUNCTION_PER_GROUP
158 #ifdef CONFIG_DEBUG_BLK_CGROUP
159 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
160 unsigned long dequeue)
162 blkg->dequeue += dequeue;
166 struct cftype blkio_files[] = {
169 .read_u64 = blkiocg_weight_read,
170 .write_u64 = blkiocg_weight_write,
174 .read_seq_string = blkiocg_time_read,
178 .read_seq_string = blkiocg_sectors_read,
180 #ifdef CONFIG_DEBUG_BLK_CGROUP
183 .read_seq_string = blkiocg_dequeue_read,
188 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
190 return cgroup_add_files(cgroup, subsys, blkio_files,
191 ARRAY_SIZE(blkio_files));
194 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
196 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
198 struct blkio_group *blkg;
203 spin_lock_irqsave(&blkcg->lock, flags);
205 if (hlist_empty(&blkcg->blkg_list)) {
206 spin_unlock_irqrestore(&blkcg->lock, flags);
210 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
212 key = rcu_dereference(blkg->key);
213 __blkiocg_del_blkio_group(blkg);
215 spin_unlock_irqrestore(&blkcg->lock, flags);
218 * This blkio_group is being unlinked as associated cgroup is going
219 * away. Let all the IO controlling policies know about this event.
221 * Currently this is static call to one io controlling policy. Once
222 * we have more policies in place, we need some dynamic registration
223 * of callback function.
225 cfq_unlink_blkio_group(key, blkg);
228 free_css_id(&blkio_subsys, &blkcg->css);
233 static struct cgroup_subsys_state *
234 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
236 struct blkio_cgroup *blkcg, *parent_blkcg;
238 if (!cgroup->parent) {
239 blkcg = &blkio_root_cgroup;
243 /* Currently we do not support hierarchy deeper than two level (0,1) */
244 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
245 if (css_depth(&parent_blkcg->css) > 0)
246 return ERR_PTR(-EINVAL);
248 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
250 return ERR_PTR(-ENOMEM);
252 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
254 spin_lock_init(&blkcg->lock);
255 INIT_HLIST_HEAD(&blkcg->blkg_list);
261 * We cannot support shared io contexts, as we have no mean to support
262 * two tasks with the same ioc in two different groups without major rework
263 * of the main cic data structures. For now we allow a task to change
264 * its cgroup only if it's the only owner of its ioc.
266 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
267 struct cgroup *cgroup, struct task_struct *tsk,
270 struct io_context *ioc;
273 /* task_lock() is needed to avoid races with exit_io_context() */
275 ioc = tsk->io_context;
276 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
283 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
284 struct cgroup *prev, struct task_struct *tsk,
287 struct io_context *ioc;
290 ioc = tsk->io_context;
292 ioc->cgroup_changed = 1;
296 struct cgroup_subsys blkio_subsys = {
298 .create = blkiocg_create,
299 .can_attach = blkiocg_can_attach,
300 .attach = blkiocg_attach,
301 .destroy = blkiocg_destroy,
302 .populate = blkiocg_populate,
303 .subsys_id = blkio_subsys_id,