]> bbs.cooldavid.org Git - net-next-2.6.git/blame - block/blk-cgroup.c
blkio: Add io controller stats like
[net-next-2.6.git] / block / blk-cgroup.c
CommitLineData
31e4c28d
VG
1/*
2 * Common Block IO controller cgroup interface
3 *
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
12 */
13#include <linux/ioprio.h>
22084190
VG
14#include <linux/seq_file.h>
15#include <linux/kdev_t.h>
9d6a986c 16#include <linux/module.h>
accee785 17#include <linux/err.h>
31e4c28d 18#include "blk-cgroup.h"
3e252066
VG
19
20static DEFINE_SPINLOCK(blkio_list_lock);
21static LIST_HEAD(blkio_list);
b1c35769 22
31e4c28d 23struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
9d6a986c
VG
24EXPORT_SYMBOL_GPL(blkio_root_cgroup);
25
67523c48
BB
26static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
27 struct cgroup *);
28static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
29 struct task_struct *, bool);
30static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
31 struct cgroup *, struct task_struct *, bool);
32static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
33static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
34
35struct cgroup_subsys blkio_subsys = {
36 .name = "blkio",
37 .create = blkiocg_create,
38 .can_attach = blkiocg_can_attach,
39 .attach = blkiocg_attach,
40 .destroy = blkiocg_destroy,
41 .populate = blkiocg_populate,
42#ifdef CONFIG_BLK_CGROUP
43 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
44 .subsys_id = blkio_subsys_id,
45#endif
46 .use_id = 1,
47 .module = THIS_MODULE,
48};
49EXPORT_SYMBOL_GPL(blkio_subsys);
50
31e4c28d
VG
51struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
52{
53 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
54 struct blkio_cgroup, css);
55}
9d6a986c 56EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
31e4c28d 57
303a3acb 58void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
22084190 59{
303a3acb
DS
60 unsigned long flags;
61
62 spin_lock_irqsave(&blkg->stats_lock, flags);
63 blkg->stats.time += time;
64 spin_unlock_irqrestore(&blkg->stats_lock, flags);
22084190 65}
303a3acb 66EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
22084190 67
31e4c28d 68void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
22084190 69 struct blkio_group *blkg, void *key, dev_t dev)
31e4c28d
VG
70{
71 unsigned long flags;
72
73 spin_lock_irqsave(&blkcg->lock, flags);
74 rcu_assign_pointer(blkg->key, key);
b1c35769 75 blkg->blkcg_id = css_id(&blkcg->css);
31e4c28d
VG
76 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
77 spin_unlock_irqrestore(&blkcg->lock, flags);
2868ef7b
VG
78#ifdef CONFIG_DEBUG_BLK_CGROUP
79 /* Need to take css reference ? */
80 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
81#endif
22084190 82 blkg->dev = dev;
31e4c28d 83}
9d6a986c 84EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
31e4c28d 85
b1c35769
VG
86static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
87{
88 hlist_del_init_rcu(&blkg->blkcg_node);
89 blkg->blkcg_id = 0;
90}
91
92/*
93 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
94 * indicating that blk_group was unhashed by the time we got to it.
95 */
31e4c28d
VG
96int blkiocg_del_blkio_group(struct blkio_group *blkg)
97{
b1c35769
VG
98 struct blkio_cgroup *blkcg;
99 unsigned long flags;
100 struct cgroup_subsys_state *css;
101 int ret = 1;
102
103 rcu_read_lock();
104 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
105 if (!css)
106 goto out;
107
108 blkcg = container_of(css, struct blkio_cgroup, css);
109 spin_lock_irqsave(&blkcg->lock, flags);
110 if (!hlist_unhashed(&blkg->blkcg_node)) {
111 __blkiocg_del_blkio_group(blkg);
112 ret = 0;
113 }
114 spin_unlock_irqrestore(&blkcg->lock, flags);
115out:
116 rcu_read_unlock();
117 return ret;
31e4c28d 118}
9d6a986c 119EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
31e4c28d
VG
120
121/* called under rcu_read_lock(). */
122struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
123{
124 struct blkio_group *blkg;
125 struct hlist_node *n;
126 void *__key;
127
128 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
129 __key = blkg->key;
130 if (__key == key)
131 return blkg;
132 }
133
134 return NULL;
135}
9d6a986c 136EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
31e4c28d
VG
137
138#define SHOW_FUNCTION(__VAR) \
139static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
140 struct cftype *cftype) \
141{ \
142 struct blkio_cgroup *blkcg; \
143 \
144 blkcg = cgroup_to_blkio_cgroup(cgroup); \
145 return (u64)blkcg->__VAR; \
146}
147
148SHOW_FUNCTION(weight);
149#undef SHOW_FUNCTION
150
151static int
152blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
153{
154 struct blkio_cgroup *blkcg;
f8d461d6
VG
155 struct blkio_group *blkg;
156 struct hlist_node *n;
3e252066 157 struct blkio_policy_type *blkiop;
31e4c28d
VG
158
159 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
160 return -EINVAL;
161
162 blkcg = cgroup_to_blkio_cgroup(cgroup);
bcf4dd43 163 spin_lock(&blkio_list_lock);
f8d461d6 164 spin_lock_irq(&blkcg->lock);
31e4c28d 165 blkcg->weight = (unsigned int)val;
3e252066 166 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
3e252066
VG
167 list_for_each_entry(blkiop, &blkio_list, list)
168 blkiop->ops.blkio_update_group_weight_fn(blkg,
169 blkcg->weight);
3e252066 170 }
f8d461d6 171 spin_unlock_irq(&blkcg->lock);
bcf4dd43 172 spin_unlock(&blkio_list_lock);
31e4c28d
VG
173 return 0;
174}
175
303a3acb
DS
176static int
177blkiocg_reset_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
178{
179 struct blkio_cgroup *blkcg;
180 struct blkio_group *blkg;
181 struct hlist_node *n;
182 struct blkio_group_stats *stats;
183
184 blkcg = cgroup_to_blkio_cgroup(cgroup);
185 spin_lock_irq(&blkcg->lock);
186 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
187 spin_lock(&blkg->stats_lock);
188 stats = &blkg->stats;
189 memset(stats, 0, sizeof(struct blkio_group_stats));
190 spin_unlock(&blkg->stats_lock);
191 }
192 spin_unlock_irq(&blkcg->lock);
193 return 0;
194}
195
196void get_key_name(int type, char *disk_id, char *str, int chars_left)
197{
198 strlcpy(str, disk_id, chars_left);
199 chars_left -= strlen(str);
200 if (chars_left <= 0) {
201 printk(KERN_WARNING
202 "Possibly incorrect cgroup stat display format");
203 return;
204 }
205 switch (type) {
206 case IO_READ:
207 strlcat(str, " Read", chars_left);
208 break;
209 case IO_WRITE:
210 strlcat(str, " Write", chars_left);
211 break;
212 case IO_SYNC:
213 strlcat(str, " Sync", chars_left);
214 break;
215 case IO_ASYNC:
216 strlcat(str, " Async", chars_left);
217 break;
218 case IO_TYPE_MAX:
219 strlcat(str, " Total", chars_left);
220 break;
221 default:
222 strlcat(str, " Invalid", chars_left);
223 }
224}
225
226typedef uint64_t (get_var) (struct blkio_group *, int);
227
228#define MAX_KEY_LEN 100
229uint64_t get_typed_stat(struct blkio_group *blkg, struct cgroup_map_cb *cb,
230 get_var *getvar, char *disk_id)
231{
232 uint64_t disk_total;
233 char key_str[MAX_KEY_LEN];
234 int type;
235
236 for (type = 0; type < IO_TYPE_MAX; type++) {
237 get_key_name(type, disk_id, key_str, MAX_KEY_LEN);
238 cb->fill(cb, key_str, getvar(blkg, type));
239 }
240 disk_total = getvar(blkg, IO_READ) + getvar(blkg, IO_WRITE);
241 get_key_name(IO_TYPE_MAX, disk_id, key_str, MAX_KEY_LEN);
242 cb->fill(cb, key_str, disk_total);
243 return disk_total;
244}
245
246uint64_t get_stat(struct blkio_group *blkg, struct cgroup_map_cb *cb,
247 get_var *getvar, char *disk_id)
248{
249 uint64_t var = getvar(blkg, 0);
250 cb->fill(cb, disk_id, var);
251 return var;
252}
253
254#define GET_STAT_INDEXED(__VAR) \
255uint64_t get_##__VAR##_stat(struct blkio_group *blkg, int type) \
256{ \
257 return blkg->stats.__VAR[type]; \
258} \
259
260GET_STAT_INDEXED(io_service_bytes);
261GET_STAT_INDEXED(io_serviced);
262GET_STAT_INDEXED(io_service_time);
263GET_STAT_INDEXED(io_wait_time);
264#undef GET_STAT_INDEXED
265
266#define GET_STAT(__VAR, __CONV) \
267uint64_t get_##__VAR##_stat(struct blkio_group *blkg, int dummy) \
268{ \
269 uint64_t data = blkg->stats.__VAR; \
270 if (__CONV) \
271 data = (uint64_t)jiffies_to_msecs(data) * NSEC_PER_MSEC;\
272 return data; \
273}
274
275GET_STAT(time, 1);
276GET_STAT(sectors, 0);
277#ifdef CONFIG_DEBUG_BLK_CGROUP
278GET_STAT(dequeue, 0);
279#endif
280#undef GET_STAT
281
282#define SHOW_FUNCTION_PER_GROUP(__VAR, get_stats, getvar, show_total) \
22084190 283static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
303a3acb 284 struct cftype *cftype, struct cgroup_map_cb *cb) \
22084190
VG
285{ \
286 struct blkio_cgroup *blkcg; \
287 struct blkio_group *blkg; \
288 struct hlist_node *n; \
303a3acb
DS
289 uint64_t cgroup_total = 0; \
290 char disk_id[10]; \
22084190
VG
291 \
292 if (!cgroup_lock_live_group(cgroup)) \
293 return -ENODEV; \
294 \
295 blkcg = cgroup_to_blkio_cgroup(cgroup); \
296 rcu_read_lock(); \
297 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
303a3acb
DS
298 if (blkg->dev) { \
299 spin_lock_irq(&blkg->stats_lock); \
300 snprintf(disk_id, 10, "%u:%u", MAJOR(blkg->dev),\
301 MINOR(blkg->dev)); \
302 cgroup_total += get_stats(blkg, cb, getvar, \
303 disk_id); \
304 spin_unlock_irq(&blkg->stats_lock); \
305 } \
22084190 306 } \
303a3acb
DS
307 if (show_total) \
308 cb->fill(cb, "Total", cgroup_total); \
22084190
VG
309 rcu_read_unlock(); \
310 cgroup_unlock(); \
311 return 0; \
312}
313
303a3acb
DS
314SHOW_FUNCTION_PER_GROUP(time, get_stat, get_time_stat, 0);
315SHOW_FUNCTION_PER_GROUP(sectors, get_stat, get_sectors_stat, 0);
316SHOW_FUNCTION_PER_GROUP(io_service_bytes, get_typed_stat,
317 get_io_service_bytes_stat, 1);
318SHOW_FUNCTION_PER_GROUP(io_serviced, get_typed_stat, get_io_serviced_stat, 1);
319SHOW_FUNCTION_PER_GROUP(io_service_time, get_typed_stat,
320 get_io_service_time_stat, 1);
321SHOW_FUNCTION_PER_GROUP(io_wait_time, get_typed_stat, get_io_wait_time_stat, 1);
22084190 322#ifdef CONFIG_DEBUG_BLK_CGROUP
303a3acb 323SHOW_FUNCTION_PER_GROUP(dequeue, get_stat, get_dequeue_stat, 0);
22084190
VG
324#endif
325#undef SHOW_FUNCTION_PER_GROUP
326
327#ifdef CONFIG_DEBUG_BLK_CGROUP
328void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
329 unsigned long dequeue)
330{
303a3acb 331 blkg->stats.dequeue += dequeue;
22084190 332}
9d6a986c 333EXPORT_SYMBOL_GPL(blkiocg_update_blkio_group_dequeue_stats);
22084190
VG
334#endif
335
31e4c28d
VG
336struct cftype blkio_files[] = {
337 {
338 .name = "weight",
339 .read_u64 = blkiocg_weight_read,
340 .write_u64 = blkiocg_weight_write,
341 },
22084190
VG
342 {
343 .name = "time",
303a3acb
DS
344 .read_map = blkiocg_time_read,
345 .write_u64 = blkiocg_reset_write,
22084190
VG
346 },
347 {
348 .name = "sectors",
303a3acb
DS
349 .read_map = blkiocg_sectors_read,
350 .write_u64 = blkiocg_reset_write,
351 },
352 {
353 .name = "io_service_bytes",
354 .read_map = blkiocg_io_service_bytes_read,
355 .write_u64 = blkiocg_reset_write,
356 },
357 {
358 .name = "io_serviced",
359 .read_map = blkiocg_io_serviced_read,
360 .write_u64 = blkiocg_reset_write,
361 },
362 {
363 .name = "io_service_time",
364 .read_map = blkiocg_io_service_time_read,
365 .write_u64 = blkiocg_reset_write,
366 },
367 {
368 .name = "io_wait_time",
369 .read_map = blkiocg_io_wait_time_read,
370 .write_u64 = blkiocg_reset_write,
22084190
VG
371 },
372#ifdef CONFIG_DEBUG_BLK_CGROUP
373 {
374 .name = "dequeue",
303a3acb 375 .read_map = blkiocg_dequeue_read,
22084190
VG
376 },
377#endif
31e4c28d
VG
378};
379
380static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
381{
382 return cgroup_add_files(cgroup, subsys, blkio_files,
383 ARRAY_SIZE(blkio_files));
384}
385
386static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
387{
388 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
b1c35769
VG
389 unsigned long flags;
390 struct blkio_group *blkg;
391 void *key;
3e252066 392 struct blkio_policy_type *blkiop;
b1c35769
VG
393
394 rcu_read_lock();
395remove_entry:
396 spin_lock_irqsave(&blkcg->lock, flags);
397
398 if (hlist_empty(&blkcg->blkg_list)) {
399 spin_unlock_irqrestore(&blkcg->lock, flags);
400 goto done;
401 }
402
403 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
404 blkcg_node);
405 key = rcu_dereference(blkg->key);
406 __blkiocg_del_blkio_group(blkg);
31e4c28d 407
b1c35769
VG
408 spin_unlock_irqrestore(&blkcg->lock, flags);
409
410 /*
411 * This blkio_group is being unlinked as associated cgroup is going
412 * away. Let all the IO controlling policies know about this event.
413 *
414 * Currently this is static call to one io controlling policy. Once
415 * we have more policies in place, we need some dynamic registration
416 * of callback function.
417 */
3e252066
VG
418 spin_lock(&blkio_list_lock);
419 list_for_each_entry(blkiop, &blkio_list, list)
420 blkiop->ops.blkio_unlink_group_fn(key, blkg);
421 spin_unlock(&blkio_list_lock);
b1c35769
VG
422 goto remove_entry;
423done:
31e4c28d 424 free_css_id(&blkio_subsys, &blkcg->css);
b1c35769 425 rcu_read_unlock();
67523c48
BB
426 if (blkcg != &blkio_root_cgroup)
427 kfree(blkcg);
31e4c28d
VG
428}
429
430static struct cgroup_subsys_state *
431blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
432{
433 struct blkio_cgroup *blkcg, *parent_blkcg;
434
435 if (!cgroup->parent) {
436 blkcg = &blkio_root_cgroup;
437 goto done;
438 }
439
440 /* Currently we do not support hierarchy deeper than two level (0,1) */
441 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
442 if (css_depth(&parent_blkcg->css) > 0)
443 return ERR_PTR(-EINVAL);
444
445 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
446 if (!blkcg)
447 return ERR_PTR(-ENOMEM);
448
449 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
450done:
451 spin_lock_init(&blkcg->lock);
452 INIT_HLIST_HEAD(&blkcg->blkg_list);
453
454 return &blkcg->css;
455}
456
457/*
458 * We cannot support shared io contexts, as we have no mean to support
459 * two tasks with the same ioc in two different groups without major rework
460 * of the main cic data structures. For now we allow a task to change
461 * its cgroup only if it's the only owner of its ioc.
462 */
463static int blkiocg_can_attach(struct cgroup_subsys *subsys,
464 struct cgroup *cgroup, struct task_struct *tsk,
465 bool threadgroup)
466{
467 struct io_context *ioc;
468 int ret = 0;
469
470 /* task_lock() is needed to avoid races with exit_io_context() */
471 task_lock(tsk);
472 ioc = tsk->io_context;
473 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
474 ret = -EINVAL;
475 task_unlock(tsk);
476
477 return ret;
478}
479
480static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
481 struct cgroup *prev, struct task_struct *tsk,
482 bool threadgroup)
483{
484 struct io_context *ioc;
485
486 task_lock(tsk);
487 ioc = tsk->io_context;
488 if (ioc)
489 ioc->cgroup_changed = 1;
490 task_unlock(tsk);
491}
492
3e252066
VG
493void blkio_policy_register(struct blkio_policy_type *blkiop)
494{
495 spin_lock(&blkio_list_lock);
496 list_add_tail(&blkiop->list, &blkio_list);
497 spin_unlock(&blkio_list_lock);
498}
499EXPORT_SYMBOL_GPL(blkio_policy_register);
500
501void blkio_policy_unregister(struct blkio_policy_type *blkiop)
502{
503 spin_lock(&blkio_list_lock);
504 list_del_init(&blkiop->list);
505 spin_unlock(&blkio_list_lock);
506}
507EXPORT_SYMBOL_GPL(blkio_policy_unregister);
67523c48
BB
508
509static int __init init_cgroup_blkio(void)
510{
511 return cgroup_load_subsys(&blkio_subsys);
512}
513
514static void __exit exit_cgroup_blkio(void)
515{
516 cgroup_unload_subsys(&blkio_subsys);
517}
518
519module_init(init_cgroup_blkio);
520module_exit(exit_cgroup_blkio);
521MODULE_LICENSE("GPL");