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 <linux/module.h>
17 #include "blk-cgroup.h"
19 static DEFINE_SPINLOCK(blkio_list_lock);
20 static LIST_HEAD(blkio_list);
22 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
23 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
25 bool blkiocg_css_tryget(struct blkio_cgroup *blkcg)
27 if (!css_tryget(&blkcg->css))
31 EXPORT_SYMBOL_GPL(blkiocg_css_tryget);
33 void blkiocg_css_put(struct blkio_cgroup *blkcg)
37 EXPORT_SYMBOL_GPL(blkiocg_css_put);
39 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
41 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
42 struct blkio_cgroup, css);
44 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
46 void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
47 unsigned long time, unsigned long sectors)
50 blkg->sectors += sectors;
52 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
54 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
55 struct blkio_group *blkg, void *key, dev_t dev)
59 spin_lock_irqsave(&blkcg->lock, flags);
60 rcu_assign_pointer(blkg->key, key);
61 blkg->blkcg_id = css_id(&blkcg->css);
62 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
63 spin_unlock_irqrestore(&blkcg->lock, flags);
64 #ifdef CONFIG_DEBUG_BLK_CGROUP
65 /* Need to take css reference ? */
66 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
70 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
72 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
74 hlist_del_init_rcu(&blkg->blkcg_node);
79 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
80 * indicating that blk_group was unhashed by the time we got to it.
82 int blkiocg_del_blkio_group(struct blkio_group *blkg)
84 struct blkio_cgroup *blkcg;
86 struct cgroup_subsys_state *css;
90 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
94 blkcg = container_of(css, struct blkio_cgroup, css);
95 spin_lock_irqsave(&blkcg->lock, flags);
96 if (!hlist_unhashed(&blkg->blkcg_node)) {
97 __blkiocg_del_blkio_group(blkg);
100 spin_unlock_irqrestore(&blkcg->lock, flags);
105 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
107 /* called under rcu_read_lock(). */
108 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
110 struct blkio_group *blkg;
111 struct hlist_node *n;
114 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
122 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
124 #define SHOW_FUNCTION(__VAR) \
125 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
126 struct cftype *cftype) \
128 struct blkio_cgroup *blkcg; \
130 blkcg = cgroup_to_blkio_cgroup(cgroup); \
131 return (u64)blkcg->__VAR; \
134 SHOW_FUNCTION(weight);
138 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
140 struct blkio_cgroup *blkcg;
141 struct blkio_group *blkg;
142 struct hlist_node *n;
143 struct blkio_policy_type *blkiop;
145 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
148 blkcg = cgroup_to_blkio_cgroup(cgroup);
149 spin_lock_irq(&blkcg->lock);
150 blkcg->weight = (unsigned int)val;
151 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
152 spin_lock(&blkio_list_lock);
153 list_for_each_entry(blkiop, &blkio_list, list)
154 blkiop->ops.blkio_update_group_weight_fn(blkg,
156 spin_unlock(&blkio_list_lock);
158 spin_unlock_irq(&blkcg->lock);
162 #define SHOW_FUNCTION_PER_GROUP(__VAR) \
163 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
164 struct cftype *cftype, struct seq_file *m) \
166 struct blkio_cgroup *blkcg; \
167 struct blkio_group *blkg; \
168 struct hlist_node *n; \
170 if (!cgroup_lock_live_group(cgroup)) \
173 blkcg = cgroup_to_blkio_cgroup(cgroup); \
175 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
177 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
178 MINOR(blkg->dev), blkg->__VAR); \
185 SHOW_FUNCTION_PER_GROUP(time);
186 SHOW_FUNCTION_PER_GROUP(sectors);
187 #ifdef CONFIG_DEBUG_BLK_CGROUP
188 SHOW_FUNCTION_PER_GROUP(dequeue);
190 #undef SHOW_FUNCTION_PER_GROUP
192 #ifdef CONFIG_DEBUG_BLK_CGROUP
193 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
194 unsigned long dequeue)
196 blkg->dequeue += dequeue;
198 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
201 struct cftype blkio_files[] = {
204 .read_u64 = blkiocg_weight_read,
205 .write_u64 = blkiocg_weight_write,
209 .read_seq_string = blkiocg_time_read,
213 .read_seq_string = blkiocg_sectors_read,
215 #ifdef CONFIG_DEBUG_BLK_CGROUP
218 .read_seq_string = blkiocg_dequeue_read,
223 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
225 return cgroup_add_files(cgroup, subsys, blkio_files,
226 ARRAY_SIZE(blkio_files));
229 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
231 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
233 struct blkio_group *blkg;
235 struct blkio_policy_type *blkiop;
239 spin_lock_irqsave(&blkcg->lock, flags);
241 if (hlist_empty(&blkcg->blkg_list)) {
242 spin_unlock_irqrestore(&blkcg->lock, flags);
246 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
248 key = rcu_dereference(blkg->key);
249 __blkiocg_del_blkio_group(blkg);
251 spin_unlock_irqrestore(&blkcg->lock, flags);
254 * This blkio_group is being unlinked as associated cgroup is going
255 * away. Let all the IO controlling policies know about this event.
257 * Currently this is static call to one io controlling policy. Once
258 * we have more policies in place, we need some dynamic registration
259 * of callback function.
261 spin_lock(&blkio_list_lock);
262 list_for_each_entry(blkiop, &blkio_list, list)
263 blkiop->ops.blkio_unlink_group_fn(key, blkg);
264 spin_unlock(&blkio_list_lock);
267 free_css_id(&blkio_subsys, &blkcg->css);
272 static struct cgroup_subsys_state *
273 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
275 struct blkio_cgroup *blkcg, *parent_blkcg;
277 if (!cgroup->parent) {
278 blkcg = &blkio_root_cgroup;
282 /* Currently we do not support hierarchy deeper than two level (0,1) */
283 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
284 if (css_depth(&parent_blkcg->css) > 0)
285 return ERR_PTR(-EINVAL);
287 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
289 return ERR_PTR(-ENOMEM);
291 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
293 spin_lock_init(&blkcg->lock);
294 INIT_HLIST_HEAD(&blkcg->blkg_list);
300 * We cannot support shared io contexts, as we have no mean to support
301 * two tasks with the same ioc in two different groups without major rework
302 * of the main cic data structures. For now we allow a task to change
303 * its cgroup only if it's the only owner of its ioc.
305 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
306 struct cgroup *cgroup, struct task_struct *tsk,
309 struct io_context *ioc;
312 /* task_lock() is needed to avoid races with exit_io_context() */
314 ioc = tsk->io_context;
315 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
322 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
323 struct cgroup *prev, struct task_struct *tsk,
326 struct io_context *ioc;
329 ioc = tsk->io_context;
331 ioc->cgroup_changed = 1;
335 struct cgroup_subsys blkio_subsys = {
337 .create = blkiocg_create,
338 .can_attach = blkiocg_can_attach,
339 .attach = blkiocg_attach,
340 .destroy = blkiocg_destroy,
341 .populate = blkiocg_populate,
342 .subsys_id = blkio_subsys_id,
346 void blkio_policy_register(struct blkio_policy_type *blkiop)
348 spin_lock(&blkio_list_lock);
349 list_add_tail(&blkiop->list, &blkio_list);
350 spin_unlock(&blkio_list_lock);
352 EXPORT_SYMBOL_GPL(blkio_policy_register);
354 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
356 spin_lock(&blkio_list_lock);
357 list_del_init(&blkiop->list);
358 spin_unlock(&blkio_list_lock);
360 EXPORT_SYMBOL_GPL(blkio_policy_unregister);