ceph: make mds requests killable, not interruptible
[linux-2.6.git] / block / blk-cgroup.c
blob2cc682b860ead9d15350620c595bfbb30f4a533d
1 /*
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 <linux/err.h>
18 #include <linux/slab.h>
19 #include "blk-cgroup.h"
21 static DEFINE_SPINLOCK(blkio_list_lock);
22 static LIST_HEAD(blkio_list);
24 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
25 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
27 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
28 struct cgroup *);
29 static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
30 struct task_struct *, bool);
31 static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
32 struct cgroup *, struct task_struct *, bool);
33 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
34 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
36 struct cgroup_subsys blkio_subsys = {
37 .name = "blkio",
38 .create = blkiocg_create,
39 .can_attach = blkiocg_can_attach,
40 .attach = blkiocg_attach,
41 .destroy = blkiocg_destroy,
42 .populate = blkiocg_populate,
43 #ifdef CONFIG_BLK_CGROUP
44 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
45 .subsys_id = blkio_subsys_id,
46 #endif
47 .use_id = 1,
48 .module = THIS_MODULE,
50 EXPORT_SYMBOL_GPL(blkio_subsys);
52 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
54 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
55 struct blkio_cgroup, css);
57 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
59 void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
60 unsigned long time, unsigned long sectors)
62 blkg->time += time;
63 blkg->sectors += sectors;
65 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_stats);
67 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
68 struct blkio_group *blkg, void *key, dev_t dev)
70 unsigned long flags;
72 spin_lock_irqsave(&blkcg->lock, flags);
73 rcu_assign_pointer(blkg->key, key);
74 blkg->blkcg_id = css_id(&blkcg->css);
75 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
76 spin_unlock_irqrestore(&blkcg->lock, flags);
77 #ifdef CONFIG_DEBUG_BLK_CGROUP
78 /* Need to take css reference ? */
79 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
80 #endif
81 blkg->dev = dev;
83 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
85 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
87 hlist_del_init_rcu(&blkg->blkcg_node);
88 blkg->blkcg_id = 0;
92 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
93 * indicating that blk_group was unhashed by the time we got to it.
95 int blkiocg_del_blkio_group(struct blkio_group *blkg)
97 struct blkio_cgroup *blkcg;
98 unsigned long flags;
99 struct cgroup_subsys_state *css;
100 int ret = 1;
102 rcu_read_lock();
103 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
104 if (!css)
105 goto out;
107 blkcg = container_of(css, struct blkio_cgroup, css);
108 spin_lock_irqsave(&blkcg->lock, flags);
109 if (!hlist_unhashed(&blkg->blkcg_node)) {
110 __blkiocg_del_blkio_group(blkg);
111 ret = 0;
113 spin_unlock_irqrestore(&blkcg->lock, flags);
114 out:
115 rcu_read_unlock();
116 return ret;
118 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
120 /* called under rcu_read_lock(). */
121 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
123 struct blkio_group *blkg;
124 struct hlist_node *n;
125 void *__key;
127 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
128 __key = blkg->key;
129 if (__key == key)
130 return blkg;
133 return NULL;
135 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
137 #define SHOW_FUNCTION(__VAR) \
138 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
139 struct cftype *cftype) \
141 struct blkio_cgroup *blkcg; \
143 blkcg = cgroup_to_blkio_cgroup(cgroup); \
144 return (u64)blkcg->__VAR; \
147 SHOW_FUNCTION(weight);
148 #undef SHOW_FUNCTION
150 static int
151 blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
153 struct blkio_cgroup *blkcg;
154 struct blkio_group *blkg;
155 struct hlist_node *n;
156 struct blkio_policy_type *blkiop;
158 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
159 return -EINVAL;
161 blkcg = cgroup_to_blkio_cgroup(cgroup);
162 spin_lock(&blkio_list_lock);
163 spin_lock_irq(&blkcg->lock);
164 blkcg->weight = (unsigned int)val;
165 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
166 list_for_each_entry(blkiop, &blkio_list, list)
167 blkiop->ops.blkio_update_group_weight_fn(blkg,
168 blkcg->weight);
170 spin_unlock_irq(&blkcg->lock);
171 spin_unlock(&blkio_list_lock);
172 return 0;
175 #define SHOW_FUNCTION_PER_GROUP(__VAR) \
176 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
177 struct cftype *cftype, struct seq_file *m) \
179 struct blkio_cgroup *blkcg; \
180 struct blkio_group *blkg; \
181 struct hlist_node *n; \
183 if (!cgroup_lock_live_group(cgroup)) \
184 return -ENODEV; \
186 blkcg = cgroup_to_blkio_cgroup(cgroup); \
187 rcu_read_lock(); \
188 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
189 if (blkg->dev) \
190 seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
191 MINOR(blkg->dev), blkg->__VAR); \
193 rcu_read_unlock(); \
194 cgroup_unlock(); \
195 return 0; \
198 SHOW_FUNCTION_PER_GROUP(time);
199 SHOW_FUNCTION_PER_GROUP(sectors);
200 #ifdef CONFIG_DEBUG_BLK_CGROUP
201 SHOW_FUNCTION_PER_GROUP(dequeue);
202 #endif
203 #undef SHOW_FUNCTION_PER_GROUP
205 #ifdef CONFIG_DEBUG_BLK_CGROUP
206 void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
207 unsigned long dequeue)
209 blkg->dequeue += dequeue;
211 EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
212 #endif
214 struct cftype blkio_files[] = {
216 .name = "weight",
217 .read_u64 = blkiocg_weight_read,
218 .write_u64 = blkiocg_weight_write,
221 .name = "time",
222 .read_seq_string = blkiocg_time_read,
225 .name = "sectors",
226 .read_seq_string = blkiocg_sectors_read,
228 #ifdef CONFIG_DEBUG_BLK_CGROUP
230 .name = "dequeue",
231 .read_seq_string = blkiocg_dequeue_read,
233 #endif
236 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
238 return cgroup_add_files(cgroup, subsys, blkio_files,
239 ARRAY_SIZE(blkio_files));
242 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
244 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
245 unsigned long flags;
246 struct blkio_group *blkg;
247 void *key;
248 struct blkio_policy_type *blkiop;
250 rcu_read_lock();
251 remove_entry:
252 spin_lock_irqsave(&blkcg->lock, flags);
254 if (hlist_empty(&blkcg->blkg_list)) {
255 spin_unlock_irqrestore(&blkcg->lock, flags);
256 goto done;
259 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
260 blkcg_node);
261 key = rcu_dereference(blkg->key);
262 __blkiocg_del_blkio_group(blkg);
264 spin_unlock_irqrestore(&blkcg->lock, flags);
267 * This blkio_group is being unlinked as associated cgroup is going
268 * away. Let all the IO controlling policies know about this event.
270 * Currently this is static call to one io controlling policy. Once
271 * we have more policies in place, we need some dynamic registration
272 * of callback function.
274 spin_lock(&blkio_list_lock);
275 list_for_each_entry(blkiop, &blkio_list, list)
276 blkiop->ops.blkio_unlink_group_fn(key, blkg);
277 spin_unlock(&blkio_list_lock);
278 goto remove_entry;
279 done:
280 free_css_id(&blkio_subsys, &blkcg->css);
281 rcu_read_unlock();
282 if (blkcg != &blkio_root_cgroup)
283 kfree(blkcg);
286 static struct cgroup_subsys_state *
287 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
289 struct blkio_cgroup *blkcg;
290 struct cgroup *parent = cgroup->parent;
292 if (!parent) {
293 blkcg = &blkio_root_cgroup;
294 goto done;
297 /* Currently we do not support hierarchy deeper than two level (0,1) */
298 if (parent != cgroup->top_cgroup)
299 return ERR_PTR(-EINVAL);
301 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
302 if (!blkcg)
303 return ERR_PTR(-ENOMEM);
305 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
306 done:
307 spin_lock_init(&blkcg->lock);
308 INIT_HLIST_HEAD(&blkcg->blkg_list);
310 return &blkcg->css;
314 * We cannot support shared io contexts, as we have no mean to support
315 * two tasks with the same ioc in two different groups without major rework
316 * of the main cic data structures. For now we allow a task to change
317 * its cgroup only if it's the only owner of its ioc.
319 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
320 struct cgroup *cgroup, struct task_struct *tsk,
321 bool threadgroup)
323 struct io_context *ioc;
324 int ret = 0;
326 /* task_lock() is needed to avoid races with exit_io_context() */
327 task_lock(tsk);
328 ioc = tsk->io_context;
329 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
330 ret = -EINVAL;
331 task_unlock(tsk);
333 return ret;
336 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
337 struct cgroup *prev, struct task_struct *tsk,
338 bool threadgroup)
340 struct io_context *ioc;
342 task_lock(tsk);
343 ioc = tsk->io_context;
344 if (ioc)
345 ioc->cgroup_changed = 1;
346 task_unlock(tsk);
349 void blkio_policy_register(struct blkio_policy_type *blkiop)
351 spin_lock(&blkio_list_lock);
352 list_add_tail(&blkiop->list, &blkio_list);
353 spin_unlock(&blkio_list_lock);
355 EXPORT_SYMBOL_GPL(blkio_policy_register);
357 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
359 spin_lock(&blkio_list_lock);
360 list_del_init(&blkiop->list);
361 spin_unlock(&blkio_list_lock);
363 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
365 static int __init init_cgroup_blkio(void)
367 return cgroup_load_subsys(&blkio_subsys);
370 static void __exit exit_cgroup_blkio(void)
372 cgroup_unload_subsys(&blkio_subsys);
375 module_init(init_cgroup_blkio);
376 module_exit(exit_cgroup_blkio);
377 MODULE_LICENSE("GPL");