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/blkdev.h>
19 #include <linux/slab.h>
20 #include "blk-cgroup.h"
21 #include <linux/genhd.h>
23 #define MAX_KEY_LEN 100
25 static DEFINE_SPINLOCK(blkio_list_lock);
26 static LIST_HEAD(blkio_list);
28 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
29 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
31 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
33 static int blkiocg_can_attach_task(struct cgroup *, struct task_struct *);
34 static void blkiocg_attach_task(struct cgroup *, struct task_struct *);
35 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
36 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
38 /* for encoding cft->private value on file */
39 #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
40 /* What policy owns the file, proportional or throttle */
41 #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
42 #define BLKIOFILE_ATTR(val) ((val) & 0xffff)
44 struct cgroup_subsys blkio_subsys = {
46 .create = blkiocg_create,
47 .can_attach_task = blkiocg_can_attach_task,
48 .attach_task = blkiocg_attach_task,
49 .destroy = blkiocg_destroy,
50 .populate = blkiocg_populate,
51 #ifdef CONFIG_BLK_CGROUP
52 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
53 .subsys_id = blkio_subsys_id,
56 .module = THIS_MODULE,
58 EXPORT_SYMBOL_GPL(blkio_subsys);
60 static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
61 struct blkio_policy_node *pn)
63 list_add(&pn->node, &blkcg->policy_list);
66 static inline bool cftype_blkg_same_policy(struct cftype *cft,
67 struct blkio_group *blkg)
69 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
71 if (blkg->plid == plid)
77 /* Determines if policy node matches cgroup file being accessed */
78 static inline bool pn_matches_cftype(struct cftype *cft,
79 struct blkio_policy_node *pn)
81 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
82 int fileid = BLKIOFILE_ATTR(cft->private);
84 return (plid == pn->plid && fileid == pn->fileid);
87 /* Must be called with blkcg->lock held */
88 static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
93 /* Must be called with blkcg->lock held */
94 static struct blkio_policy_node *
95 blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
96 enum blkio_policy_id plid, int fileid)
98 struct blkio_policy_node *pn;
100 list_for_each_entry(pn, &blkcg->policy_list, node) {
101 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
108 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
110 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
111 struct blkio_cgroup, css);
113 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
115 struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
117 return container_of(task_subsys_state(tsk, blkio_subsys_id),
118 struct blkio_cgroup, css);
120 EXPORT_SYMBOL_GPL(task_blkio_cgroup);
123 blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
125 struct blkio_policy_type *blkiop;
127 list_for_each_entry(blkiop, &blkio_list, list) {
128 /* If this policy does not own the blkg, do not send updates */
129 if (blkiop->plid != blkg->plid)
131 if (blkiop->ops.blkio_update_group_weight_fn)
132 blkiop->ops.blkio_update_group_weight_fn(blkg->key,
137 static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
140 struct blkio_policy_type *blkiop;
142 list_for_each_entry(blkiop, &blkio_list, list) {
144 /* If this policy does not own the blkg, do not send updates */
145 if (blkiop->plid != blkg->plid)
148 if (fileid == BLKIO_THROTL_read_bps_device
149 && blkiop->ops.blkio_update_group_read_bps_fn)
150 blkiop->ops.blkio_update_group_read_bps_fn(blkg->key,
153 if (fileid == BLKIO_THROTL_write_bps_device
154 && blkiop->ops.blkio_update_group_write_bps_fn)
155 blkiop->ops.blkio_update_group_write_bps_fn(blkg->key,
160 static inline void blkio_update_group_iops(struct blkio_group *blkg,
161 unsigned int iops, int fileid)
163 struct blkio_policy_type *blkiop;
165 list_for_each_entry(blkiop, &blkio_list, list) {
167 /* If this policy does not own the blkg, do not send updates */
168 if (blkiop->plid != blkg->plid)
171 if (fileid == BLKIO_THROTL_read_iops_device
172 && blkiop->ops.blkio_update_group_read_iops_fn)
173 blkiop->ops.blkio_update_group_read_iops_fn(blkg->key,
176 if (fileid == BLKIO_THROTL_write_iops_device
177 && blkiop->ops.blkio_update_group_write_iops_fn)
178 blkiop->ops.blkio_update_group_write_iops_fn(blkg->key,
184 * Add to the appropriate stat variable depending on the request type.
185 * This should be called with the blkg->stats_lock held.
187 static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
191 stat[BLKIO_STAT_WRITE] += add;
193 stat[BLKIO_STAT_READ] += add;
195 stat[BLKIO_STAT_SYNC] += add;
197 stat[BLKIO_STAT_ASYNC] += add;
201 * Decrements the appropriate stat variable if non-zero depending on the
202 * request type. Panics on value being zero.
203 * This should be called with the blkg->stats_lock held.
205 static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
208 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
209 stat[BLKIO_STAT_WRITE]--;
211 BUG_ON(stat[BLKIO_STAT_READ] == 0);
212 stat[BLKIO_STAT_READ]--;
215 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
216 stat[BLKIO_STAT_SYNC]--;
218 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
219 stat[BLKIO_STAT_ASYNC]--;
223 #ifdef CONFIG_DEBUG_BLK_CGROUP
224 /* This should be called with the blkg->stats_lock held. */
225 static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
226 struct blkio_group *curr_blkg)
228 if (blkio_blkg_waiting(&blkg->stats))
230 if (blkg == curr_blkg)
232 blkg->stats.start_group_wait_time = sched_clock();
233 blkio_mark_blkg_waiting(&blkg->stats);
236 /* This should be called with the blkg->stats_lock held. */
237 static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
239 unsigned long long now;
241 if (!blkio_blkg_waiting(stats))
245 if (time_after64(now, stats->start_group_wait_time))
246 stats->group_wait_time += now - stats->start_group_wait_time;
247 blkio_clear_blkg_waiting(stats);
250 /* This should be called with the blkg->stats_lock held. */
251 static void blkio_end_empty_time(struct blkio_group_stats *stats)
253 unsigned long long now;
255 if (!blkio_blkg_empty(stats))
259 if (time_after64(now, stats->start_empty_time))
260 stats->empty_time += now - stats->start_empty_time;
261 blkio_clear_blkg_empty(stats);
264 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
268 spin_lock_irqsave(&blkg->stats_lock, flags);
269 BUG_ON(blkio_blkg_idling(&blkg->stats));
270 blkg->stats.start_idle_time = sched_clock();
271 blkio_mark_blkg_idling(&blkg->stats);
272 spin_unlock_irqrestore(&blkg->stats_lock, flags);
274 EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
276 void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
279 unsigned long long now;
280 struct blkio_group_stats *stats;
282 spin_lock_irqsave(&blkg->stats_lock, flags);
283 stats = &blkg->stats;
284 if (blkio_blkg_idling(stats)) {
286 if (time_after64(now, stats->start_idle_time))
287 stats->idle_time += now - stats->start_idle_time;
288 blkio_clear_blkg_idling(stats);
290 spin_unlock_irqrestore(&blkg->stats_lock, flags);
292 EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
294 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
297 struct blkio_group_stats *stats;
299 spin_lock_irqsave(&blkg->stats_lock, flags);
300 stats = &blkg->stats;
301 stats->avg_queue_size_sum +=
302 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
303 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
304 stats->avg_queue_size_samples++;
305 blkio_update_group_wait_time(stats);
306 spin_unlock_irqrestore(&blkg->stats_lock, flags);
308 EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
310 void blkiocg_set_start_empty_time(struct blkio_group *blkg)
313 struct blkio_group_stats *stats;
315 spin_lock_irqsave(&blkg->stats_lock, flags);
316 stats = &blkg->stats;
318 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
319 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
320 spin_unlock_irqrestore(&blkg->stats_lock, flags);
325 * group is already marked empty. This can happen if cfqq got new
326 * request in parent group and moved to this group while being added
327 * to service tree. Just ignore the event and move on.
329 if(blkio_blkg_empty(stats)) {
330 spin_unlock_irqrestore(&blkg->stats_lock, flags);
334 stats->start_empty_time = sched_clock();
335 blkio_mark_blkg_empty(stats);
336 spin_unlock_irqrestore(&blkg->stats_lock, flags);
338 EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
340 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
341 unsigned long dequeue)
343 blkg->stats.dequeue += dequeue;
345 EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
347 static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
348 struct blkio_group *curr_blkg) {}
349 static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
352 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
353 struct blkio_group *curr_blkg, bool direction,
358 spin_lock_irqsave(&blkg->stats_lock, flags);
359 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
361 blkio_end_empty_time(&blkg->stats);
362 blkio_set_start_group_wait_time(blkg, curr_blkg);
363 spin_unlock_irqrestore(&blkg->stats_lock, flags);
365 EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
367 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
368 bool direction, bool sync)
372 spin_lock_irqsave(&blkg->stats_lock, flags);
373 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
375 spin_unlock_irqrestore(&blkg->stats_lock, flags);
377 EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
379 void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
380 unsigned long unaccounted_time)
384 spin_lock_irqsave(&blkg->stats_lock, flags);
385 blkg->stats.time += time;
386 #ifdef CONFIG_DEBUG_BLK_CGROUP
387 blkg->stats.unaccounted_time += unaccounted_time;
389 spin_unlock_irqrestore(&blkg->stats_lock, flags);
391 EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
394 * should be called under rcu read lock or queue lock to make sure blkg pointer
397 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
398 uint64_t bytes, bool direction, bool sync)
400 struct blkio_group_stats_cpu *stats_cpu;
404 * Disabling interrupts to provide mutual exclusion between two
405 * writes on same cpu. It probably is not needed for 64bit. Not
406 * optimizing that case yet.
408 local_irq_save(flags);
410 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
412 u64_stats_update_begin(&stats_cpu->syncp);
413 stats_cpu->sectors += bytes >> 9;
414 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
416 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
417 bytes, direction, sync);
418 u64_stats_update_end(&stats_cpu->syncp);
419 local_irq_restore(flags);
421 EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
423 void blkiocg_update_completion_stats(struct blkio_group *blkg,
424 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
426 struct blkio_group_stats *stats;
428 unsigned long long now = sched_clock();
430 spin_lock_irqsave(&blkg->stats_lock, flags);
431 stats = &blkg->stats;
432 if (time_after64(now, io_start_time))
433 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
434 now - io_start_time, direction, sync);
435 if (time_after64(io_start_time, start_time))
436 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
437 io_start_time - start_time, direction, sync);
438 spin_unlock_irqrestore(&blkg->stats_lock, flags);
440 EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
442 /* Merged stats are per cpu. */
443 void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
446 struct blkio_group_stats_cpu *stats_cpu;
450 * Disabling interrupts to provide mutual exclusion between two
451 * writes on same cpu. It probably is not needed for 64bit. Not
452 * optimizing that case yet.
454 local_irq_save(flags);
456 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
458 u64_stats_update_begin(&stats_cpu->syncp);
459 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
461 u64_stats_update_end(&stats_cpu->syncp);
462 local_irq_restore(flags);
464 EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
467 * This function allocates the per cpu stats for blkio_group. Should be called
468 * from sleepable context as alloc_per_cpu() requires that.
470 int blkio_alloc_blkg_stats(struct blkio_group *blkg)
472 /* Allocate memory for per cpu stats */
473 blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
474 if (!blkg->stats_cpu)
478 EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
480 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
481 struct blkio_group *blkg, void *key, dev_t dev,
482 enum blkio_policy_id plid)
486 spin_lock_irqsave(&blkcg->lock, flags);
487 spin_lock_init(&blkg->stats_lock);
488 rcu_assign_pointer(blkg->key, key);
489 blkg->blkcg_id = css_id(&blkcg->css);
490 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
492 spin_unlock_irqrestore(&blkcg->lock, flags);
493 /* Need to take css reference ? */
494 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
497 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
499 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
501 hlist_del_init_rcu(&blkg->blkcg_node);
506 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
507 * indicating that blk_group was unhashed by the time we got to it.
509 int blkiocg_del_blkio_group(struct blkio_group *blkg)
511 struct blkio_cgroup *blkcg;
513 struct cgroup_subsys_state *css;
517 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
519 blkcg = container_of(css, struct blkio_cgroup, css);
520 spin_lock_irqsave(&blkcg->lock, flags);
521 if (!hlist_unhashed(&blkg->blkcg_node)) {
522 __blkiocg_del_blkio_group(blkg);
525 spin_unlock_irqrestore(&blkcg->lock, flags);
531 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
533 /* called under rcu_read_lock(). */
534 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
536 struct blkio_group *blkg;
537 struct hlist_node *n;
540 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
548 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
550 static void blkio_reset_stats_cpu(struct blkio_group *blkg)
552 struct blkio_group_stats_cpu *stats_cpu;
555 * Note: On 64 bit arch this should not be an issue. This has the
556 * possibility of returning some inconsistent value on 32bit arch
557 * as 64bit update on 32bit is non atomic. Taking care of this
558 * corner case makes code very complicated, like sending IPIs to
559 * cpus, taking care of stats of offline cpus etc.
561 * reset stats is anyway more of a debug feature and this sounds a
562 * corner case. So I am not complicating the code yet until and
563 * unless this becomes a real issue.
565 for_each_possible_cpu(i) {
566 stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
567 stats_cpu->sectors = 0;
568 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
569 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
570 stats_cpu->stat_arr_cpu[j][k] = 0;
575 blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
577 struct blkio_cgroup *blkcg;
578 struct blkio_group *blkg;
579 struct blkio_group_stats *stats;
580 struct hlist_node *n;
581 uint64_t queued[BLKIO_STAT_TOTAL];
583 #ifdef CONFIG_DEBUG_BLK_CGROUP
584 bool idling, waiting, empty;
585 unsigned long long now = sched_clock();
588 blkcg = cgroup_to_blkio_cgroup(cgroup);
589 spin_lock_irq(&blkcg->lock);
590 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
591 spin_lock(&blkg->stats_lock);
592 stats = &blkg->stats;
593 #ifdef CONFIG_DEBUG_BLK_CGROUP
594 idling = blkio_blkg_idling(stats);
595 waiting = blkio_blkg_waiting(stats);
596 empty = blkio_blkg_empty(stats);
598 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
599 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
600 memset(stats, 0, sizeof(struct blkio_group_stats));
601 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
602 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
603 #ifdef CONFIG_DEBUG_BLK_CGROUP
605 blkio_mark_blkg_idling(stats);
606 stats->start_idle_time = now;
609 blkio_mark_blkg_waiting(stats);
610 stats->start_group_wait_time = now;
613 blkio_mark_blkg_empty(stats);
614 stats->start_empty_time = now;
617 spin_unlock(&blkg->stats_lock);
619 /* Reset Per cpu stats which don't take blkg->stats_lock */
620 blkio_reset_stats_cpu(blkg);
623 spin_unlock_irq(&blkcg->lock);
627 static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
628 int chars_left, bool diskname_only)
630 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
631 chars_left -= strlen(str);
632 if (chars_left <= 0) {
634 "Possibly incorrect cgroup stat display format");
640 case BLKIO_STAT_READ:
641 strlcat(str, " Read", chars_left);
643 case BLKIO_STAT_WRITE:
644 strlcat(str, " Write", chars_left);
646 case BLKIO_STAT_SYNC:
647 strlcat(str, " Sync", chars_left);
649 case BLKIO_STAT_ASYNC:
650 strlcat(str, " Async", chars_left);
652 case BLKIO_STAT_TOTAL:
653 strlcat(str, " Total", chars_left);
656 strlcat(str, " Invalid", chars_left);
660 static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
661 struct cgroup_map_cb *cb, dev_t dev)
663 blkio_get_key_name(0, dev, str, chars_left, true);
664 cb->fill(cb, str, val);
669 static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
670 enum stat_type_cpu type, enum stat_sub_type sub_type)
673 struct blkio_group_stats_cpu *stats_cpu;
676 for_each_possible_cpu(cpu) {
678 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
681 start = u64_stats_fetch_begin(&stats_cpu->syncp);
682 if (type == BLKIO_STAT_CPU_SECTORS)
683 tval = stats_cpu->sectors;
685 tval = stats_cpu->stat_arr_cpu[type][sub_type];
686 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
694 static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
695 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
697 uint64_t disk_total, val;
698 char key_str[MAX_KEY_LEN];
699 enum stat_sub_type sub_type;
701 if (type == BLKIO_STAT_CPU_SECTORS) {
702 val = blkio_read_stat_cpu(blkg, type, 0);
703 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
706 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
708 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
709 val = blkio_read_stat_cpu(blkg, type, sub_type);
710 cb->fill(cb, key_str, val);
713 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
714 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
716 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
717 cb->fill(cb, key_str, disk_total);
721 /* This should be called with blkg->stats_lock held */
722 static uint64_t blkio_get_stat(struct blkio_group *blkg,
723 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
726 char key_str[MAX_KEY_LEN];
727 enum stat_sub_type sub_type;
729 if (type == BLKIO_STAT_TIME)
730 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
731 blkg->stats.time, cb, dev);
732 #ifdef CONFIG_DEBUG_BLK_CGROUP
733 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
734 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
735 blkg->stats.unaccounted_time, cb, dev);
736 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
737 uint64_t sum = blkg->stats.avg_queue_size_sum;
738 uint64_t samples = blkg->stats.avg_queue_size_samples;
740 do_div(sum, samples);
743 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
745 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
746 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
747 blkg->stats.group_wait_time, cb, dev);
748 if (type == BLKIO_STAT_IDLE_TIME)
749 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
750 blkg->stats.idle_time, cb, dev);
751 if (type == BLKIO_STAT_EMPTY_TIME)
752 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
753 blkg->stats.empty_time, cb, dev);
754 if (type == BLKIO_STAT_DEQUEUE)
755 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
756 blkg->stats.dequeue, cb, dev);
759 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
761 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
762 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
764 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
765 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
766 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
767 cb->fill(cb, key_str, disk_total);
771 static int blkio_policy_parse_and_set(char *buf,
772 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
774 struct gendisk *disk = NULL;
775 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
776 unsigned long major, minor;
777 int i = 0, ret = -EINVAL;
782 memset(s, 0, sizeof(s));
784 while ((p = strsep(&buf, " ")) != NULL) {
790 /* Prevent from inputing too many things */
798 p = strsep(&s[0], ":");
808 if (strict_strtoul(major_s, 10, &major))
811 if (strict_strtoul(minor_s, 10, &minor))
814 dev = MKDEV(major, minor);
816 if (strict_strtoull(s[1], 10, &temp))
819 /* For rule removal, do not check for device presence. */
821 disk = get_gendisk(dev, &part);
831 case BLKIO_POLICY_PROP:
832 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
833 temp > BLKIO_WEIGHT_MAX)
837 newpn->fileid = fileid;
838 newpn->val.weight = temp;
840 case BLKIO_POLICY_THROTL:
842 case BLKIO_THROTL_read_bps_device:
843 case BLKIO_THROTL_write_bps_device:
845 newpn->fileid = fileid;
846 newpn->val.bps = temp;
848 case BLKIO_THROTL_read_iops_device:
849 case BLKIO_THROTL_write_iops_device:
850 if (temp > THROTL_IOPS_MAX)
854 newpn->fileid = fileid;
855 newpn->val.iops = (unsigned int)temp;
868 unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
871 struct blkio_policy_node *pn;
873 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
874 BLKIO_PROP_weight_device);
876 return pn->val.weight;
878 return blkcg->weight;
880 EXPORT_SYMBOL_GPL(blkcg_get_weight);
882 uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
884 struct blkio_policy_node *pn;
886 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
887 BLKIO_THROTL_read_bps_device);
894 uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
896 struct blkio_policy_node *pn;
897 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
898 BLKIO_THROTL_write_bps_device);
905 unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
907 struct blkio_policy_node *pn;
909 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
910 BLKIO_THROTL_read_iops_device);
917 unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
919 struct blkio_policy_node *pn;
920 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
921 BLKIO_THROTL_write_iops_device);
928 /* Checks whether user asked for deleting a policy rule */
929 static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
932 case BLKIO_POLICY_PROP:
933 if (pn->val.weight == 0)
936 case BLKIO_POLICY_THROTL:
938 case BLKIO_THROTL_read_bps_device:
939 case BLKIO_THROTL_write_bps_device:
940 if (pn->val.bps == 0)
943 case BLKIO_THROTL_read_iops_device:
944 case BLKIO_THROTL_write_iops_device:
945 if (pn->val.iops == 0)
956 static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
957 struct blkio_policy_node *newpn)
959 switch(oldpn->plid) {
960 case BLKIO_POLICY_PROP:
961 oldpn->val.weight = newpn->val.weight;
963 case BLKIO_POLICY_THROTL:
964 switch(newpn->fileid) {
965 case BLKIO_THROTL_read_bps_device:
966 case BLKIO_THROTL_write_bps_device:
967 oldpn->val.bps = newpn->val.bps;
969 case BLKIO_THROTL_read_iops_device:
970 case BLKIO_THROTL_write_iops_device:
971 oldpn->val.iops = newpn->val.iops;
980 * Some rules/values in blkg have changed. Propagate those to respective
983 static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
984 struct blkio_group *blkg, struct blkio_policy_node *pn)
986 unsigned int weight, iops;
990 case BLKIO_POLICY_PROP:
991 weight = pn->val.weight ? pn->val.weight :
993 blkio_update_group_weight(blkg, weight);
995 case BLKIO_POLICY_THROTL:
997 case BLKIO_THROTL_read_bps_device:
998 case BLKIO_THROTL_write_bps_device:
999 bps = pn->val.bps ? pn->val.bps : (-1);
1000 blkio_update_group_bps(blkg, bps, pn->fileid);
1002 case BLKIO_THROTL_read_iops_device:
1003 case BLKIO_THROTL_write_iops_device:
1004 iops = pn->val.iops ? pn->val.iops : (-1);
1005 blkio_update_group_iops(blkg, iops, pn->fileid);
1015 * A policy node rule has been updated. Propagate this update to all the
1016 * block groups which might be affected by this update.
1018 static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
1019 struct blkio_policy_node *pn)
1021 struct blkio_group *blkg;
1022 struct hlist_node *n;
1024 spin_lock(&blkio_list_lock);
1025 spin_lock_irq(&blkcg->lock);
1027 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1028 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
1030 blkio_update_blkg_policy(blkcg, blkg, pn);
1033 spin_unlock_irq(&blkcg->lock);
1034 spin_unlock(&blkio_list_lock);
1037 static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1042 struct blkio_policy_node *newpn, *pn;
1043 struct blkio_cgroup *blkcg;
1045 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1046 int fileid = BLKIOFILE_ATTR(cft->private);
1048 buf = kstrdup(buffer, GFP_KERNEL);
1052 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1058 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
1062 blkcg = cgroup_to_blkio_cgroup(cgrp);
1064 spin_lock_irq(&blkcg->lock);
1066 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
1068 if (!blkio_delete_rule_command(newpn)) {
1069 blkio_policy_insert_node(blkcg, newpn);
1072 spin_unlock_irq(&blkcg->lock);
1073 goto update_io_group;
1076 if (blkio_delete_rule_command(newpn)) {
1077 blkio_policy_delete_node(pn);
1078 spin_unlock_irq(&blkcg->lock);
1079 goto update_io_group;
1081 spin_unlock_irq(&blkcg->lock);
1083 blkio_update_policy_rule(pn, newpn);
1086 blkio_update_policy_node_blkg(blkcg, newpn);
1097 blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
1100 case BLKIO_POLICY_PROP:
1101 if (pn->fileid == BLKIO_PROP_weight_device)
1102 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1103 MINOR(pn->dev), pn->val.weight);
1105 case BLKIO_POLICY_THROTL:
1106 switch(pn->fileid) {
1107 case BLKIO_THROTL_read_bps_device:
1108 case BLKIO_THROTL_write_bps_device:
1109 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1110 MINOR(pn->dev), pn->val.bps);
1112 case BLKIO_THROTL_read_iops_device:
1113 case BLKIO_THROTL_write_iops_device:
1114 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1115 MINOR(pn->dev), pn->val.iops);
1124 /* cgroup files which read their data from policy nodes end up here */
1125 static void blkio_read_policy_node_files(struct cftype *cft,
1126 struct blkio_cgroup *blkcg, struct seq_file *m)
1128 struct blkio_policy_node *pn;
1130 if (!list_empty(&blkcg->policy_list)) {
1131 spin_lock_irq(&blkcg->lock);
1132 list_for_each_entry(pn, &blkcg->policy_list, node) {
1133 if (!pn_matches_cftype(cft, pn))
1135 blkio_print_policy_node(m, pn);
1137 spin_unlock_irq(&blkcg->lock);
1141 static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1144 struct blkio_cgroup *blkcg;
1145 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1146 int name = BLKIOFILE_ATTR(cft->private);
1148 blkcg = cgroup_to_blkio_cgroup(cgrp);
1151 case BLKIO_POLICY_PROP:
1153 case BLKIO_PROP_weight_device:
1154 blkio_read_policy_node_files(cft, blkcg, m);
1160 case BLKIO_POLICY_THROTL:
1162 case BLKIO_THROTL_read_bps_device:
1163 case BLKIO_THROTL_write_bps_device:
1164 case BLKIO_THROTL_read_iops_device:
1165 case BLKIO_THROTL_write_iops_device:
1166 blkio_read_policy_node_files(cft, blkcg, m);
1179 static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1180 struct cftype *cft, struct cgroup_map_cb *cb,
1181 enum stat_type type, bool show_total, bool pcpu)
1183 struct blkio_group *blkg;
1184 struct hlist_node *n;
1185 uint64_t cgroup_total = 0;
1188 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1190 if (!cftype_blkg_same_policy(cft, blkg))
1193 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1196 spin_lock_irq(&blkg->stats_lock);
1197 cgroup_total += blkio_get_stat(blkg, cb,
1199 spin_unlock_irq(&blkg->stats_lock);
1204 cb->fill(cb, "Total", cgroup_total);
1209 /* All map kind of cgroup file get serviced by this function */
1210 static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1211 struct cgroup_map_cb *cb)
1213 struct blkio_cgroup *blkcg;
1214 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1215 int name = BLKIOFILE_ATTR(cft->private);
1217 blkcg = cgroup_to_blkio_cgroup(cgrp);
1220 case BLKIO_POLICY_PROP:
1222 case BLKIO_PROP_time:
1223 return blkio_read_blkg_stats(blkcg, cft, cb,
1224 BLKIO_STAT_TIME, 0, 0);
1225 case BLKIO_PROP_sectors:
1226 return blkio_read_blkg_stats(blkcg, cft, cb,
1227 BLKIO_STAT_CPU_SECTORS, 0, 1);
1228 case BLKIO_PROP_io_service_bytes:
1229 return blkio_read_blkg_stats(blkcg, cft, cb,
1230 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1231 case BLKIO_PROP_io_serviced:
1232 return blkio_read_blkg_stats(blkcg, cft, cb,
1233 BLKIO_STAT_CPU_SERVICED, 1, 1);
1234 case BLKIO_PROP_io_service_time:
1235 return blkio_read_blkg_stats(blkcg, cft, cb,
1236 BLKIO_STAT_SERVICE_TIME, 1, 0);
1237 case BLKIO_PROP_io_wait_time:
1238 return blkio_read_blkg_stats(blkcg, cft, cb,
1239 BLKIO_STAT_WAIT_TIME, 1, 0);
1240 case BLKIO_PROP_io_merged:
1241 return blkio_read_blkg_stats(blkcg, cft, cb,
1242 BLKIO_STAT_CPU_MERGED, 1, 1);
1243 case BLKIO_PROP_io_queued:
1244 return blkio_read_blkg_stats(blkcg, cft, cb,
1245 BLKIO_STAT_QUEUED, 1, 0);
1246 #ifdef CONFIG_DEBUG_BLK_CGROUP
1247 case BLKIO_PROP_unaccounted_time:
1248 return blkio_read_blkg_stats(blkcg, cft, cb,
1249 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
1250 case BLKIO_PROP_dequeue:
1251 return blkio_read_blkg_stats(blkcg, cft, cb,
1252 BLKIO_STAT_DEQUEUE, 0, 0);
1253 case BLKIO_PROP_avg_queue_size:
1254 return blkio_read_blkg_stats(blkcg, cft, cb,
1255 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
1256 case BLKIO_PROP_group_wait_time:
1257 return blkio_read_blkg_stats(blkcg, cft, cb,
1258 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
1259 case BLKIO_PROP_idle_time:
1260 return blkio_read_blkg_stats(blkcg, cft, cb,
1261 BLKIO_STAT_IDLE_TIME, 0, 0);
1262 case BLKIO_PROP_empty_time:
1263 return blkio_read_blkg_stats(blkcg, cft, cb,
1264 BLKIO_STAT_EMPTY_TIME, 0, 0);
1270 case BLKIO_POLICY_THROTL:
1272 case BLKIO_THROTL_io_service_bytes:
1273 return blkio_read_blkg_stats(blkcg, cft, cb,
1274 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1275 case BLKIO_THROTL_io_serviced:
1276 return blkio_read_blkg_stats(blkcg, cft, cb,
1277 BLKIO_STAT_CPU_SERVICED, 1, 1);
1289 static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1291 struct blkio_group *blkg;
1292 struct hlist_node *n;
1293 struct blkio_policy_node *pn;
1295 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1298 spin_lock(&blkio_list_lock);
1299 spin_lock_irq(&blkcg->lock);
1300 blkcg->weight = (unsigned int)val;
1302 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1303 pn = blkio_policy_search_node(blkcg, blkg->dev,
1304 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1308 blkio_update_group_weight(blkg, blkcg->weight);
1310 spin_unlock_irq(&blkcg->lock);
1311 spin_unlock(&blkio_list_lock);
1315 static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1316 struct blkio_cgroup *blkcg;
1317 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1318 int name = BLKIOFILE_ATTR(cft->private);
1320 blkcg = cgroup_to_blkio_cgroup(cgrp);
1323 case BLKIO_POLICY_PROP:
1325 case BLKIO_PROP_weight:
1326 return (u64)blkcg->weight;
1336 blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1338 struct blkio_cgroup *blkcg;
1339 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1340 int name = BLKIOFILE_ATTR(cft->private);
1342 blkcg = cgroup_to_blkio_cgroup(cgrp);
1345 case BLKIO_POLICY_PROP:
1347 case BLKIO_PROP_weight:
1348 return blkio_weight_write(blkcg, val);
1358 struct cftype blkio_files[] = {
1360 .name = "weight_device",
1361 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1362 BLKIO_PROP_weight_device),
1363 .read_seq_string = blkiocg_file_read,
1364 .write_string = blkiocg_file_write,
1365 .max_write_len = 256,
1369 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1371 .read_u64 = blkiocg_file_read_u64,
1372 .write_u64 = blkiocg_file_write_u64,
1376 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1378 .read_map = blkiocg_file_read_map,
1382 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1383 BLKIO_PROP_sectors),
1384 .read_map = blkiocg_file_read_map,
1387 .name = "io_service_bytes",
1388 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1389 BLKIO_PROP_io_service_bytes),
1390 .read_map = blkiocg_file_read_map,
1393 .name = "io_serviced",
1394 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1395 BLKIO_PROP_io_serviced),
1396 .read_map = blkiocg_file_read_map,
1399 .name = "io_service_time",
1400 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1401 BLKIO_PROP_io_service_time),
1402 .read_map = blkiocg_file_read_map,
1405 .name = "io_wait_time",
1406 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1407 BLKIO_PROP_io_wait_time),
1408 .read_map = blkiocg_file_read_map,
1411 .name = "io_merged",
1412 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1413 BLKIO_PROP_io_merged),
1414 .read_map = blkiocg_file_read_map,
1417 .name = "io_queued",
1418 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1419 BLKIO_PROP_io_queued),
1420 .read_map = blkiocg_file_read_map,
1423 .name = "reset_stats",
1424 .write_u64 = blkiocg_reset_stats,
1426 #ifdef CONFIG_BLK_DEV_THROTTLING
1428 .name = "throttle.read_bps_device",
1429 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1430 BLKIO_THROTL_read_bps_device),
1431 .read_seq_string = blkiocg_file_read,
1432 .write_string = blkiocg_file_write,
1433 .max_write_len = 256,
1437 .name = "throttle.write_bps_device",
1438 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1439 BLKIO_THROTL_write_bps_device),
1440 .read_seq_string = blkiocg_file_read,
1441 .write_string = blkiocg_file_write,
1442 .max_write_len = 256,
1446 .name = "throttle.read_iops_device",
1447 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1448 BLKIO_THROTL_read_iops_device),
1449 .read_seq_string = blkiocg_file_read,
1450 .write_string = blkiocg_file_write,
1451 .max_write_len = 256,
1455 .name = "throttle.write_iops_device",
1456 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1457 BLKIO_THROTL_write_iops_device),
1458 .read_seq_string = blkiocg_file_read,
1459 .write_string = blkiocg_file_write,
1460 .max_write_len = 256,
1463 .name = "throttle.io_service_bytes",
1464 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1465 BLKIO_THROTL_io_service_bytes),
1466 .read_map = blkiocg_file_read_map,
1469 .name = "throttle.io_serviced",
1470 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1471 BLKIO_THROTL_io_serviced),
1472 .read_map = blkiocg_file_read_map,
1474 #endif /* CONFIG_BLK_DEV_THROTTLING */
1476 #ifdef CONFIG_DEBUG_BLK_CGROUP
1478 .name = "avg_queue_size",
1479 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1480 BLKIO_PROP_avg_queue_size),
1481 .read_map = blkiocg_file_read_map,
1484 .name = "group_wait_time",
1485 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1486 BLKIO_PROP_group_wait_time),
1487 .read_map = blkiocg_file_read_map,
1490 .name = "idle_time",
1491 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1492 BLKIO_PROP_idle_time),
1493 .read_map = blkiocg_file_read_map,
1496 .name = "empty_time",
1497 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1498 BLKIO_PROP_empty_time),
1499 .read_map = blkiocg_file_read_map,
1503 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1504 BLKIO_PROP_dequeue),
1505 .read_map = blkiocg_file_read_map,
1508 .name = "unaccounted_time",
1509 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1510 BLKIO_PROP_unaccounted_time),
1511 .read_map = blkiocg_file_read_map,
1516 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1518 return cgroup_add_files(cgroup, subsys, blkio_files,
1519 ARRAY_SIZE(blkio_files));
1522 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1524 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1525 unsigned long flags;
1526 struct blkio_group *blkg;
1528 struct blkio_policy_type *blkiop;
1529 struct blkio_policy_node *pn, *pntmp;
1533 spin_lock_irqsave(&blkcg->lock, flags);
1535 if (hlist_empty(&blkcg->blkg_list)) {
1536 spin_unlock_irqrestore(&blkcg->lock, flags);
1540 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1542 key = rcu_dereference(blkg->key);
1543 __blkiocg_del_blkio_group(blkg);
1545 spin_unlock_irqrestore(&blkcg->lock, flags);
1548 * This blkio_group is being unlinked as associated cgroup is
1549 * going away. Let all the IO controlling policies know about
1552 spin_lock(&blkio_list_lock);
1553 list_for_each_entry(blkiop, &blkio_list, list) {
1554 if (blkiop->plid != blkg->plid)
1556 blkiop->ops.blkio_unlink_group_fn(key, blkg);
1558 spin_unlock(&blkio_list_lock);
1561 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1562 blkio_policy_delete_node(pn);
1566 free_css_id(&blkio_subsys, &blkcg->css);
1568 if (blkcg != &blkio_root_cgroup)
1572 static struct cgroup_subsys_state *
1573 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1575 struct blkio_cgroup *blkcg;
1576 struct cgroup *parent = cgroup->parent;
1579 blkcg = &blkio_root_cgroup;
1583 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1585 return ERR_PTR(-ENOMEM);
1587 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1589 spin_lock_init(&blkcg->lock);
1590 INIT_HLIST_HEAD(&blkcg->blkg_list);
1592 INIT_LIST_HEAD(&blkcg->policy_list);
1597 * We cannot support shared io contexts, as we have no mean to support
1598 * two tasks with the same ioc in two different groups without major rework
1599 * of the main cic data structures. For now we allow a task to change
1600 * its cgroup only if it's the only owner of its ioc.
1602 static int blkiocg_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1604 struct io_context *ioc;
1607 /* task_lock() is needed to avoid races with exit_io_context() */
1609 ioc = tsk->io_context;
1610 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1617 static void blkiocg_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1619 struct io_context *ioc;
1622 ioc = tsk->io_context;
1624 ioc->cgroup_changed = 1;
1628 void blkio_policy_register(struct blkio_policy_type *blkiop)
1630 spin_lock(&blkio_list_lock);
1631 list_add_tail(&blkiop->list, &blkio_list);
1632 spin_unlock(&blkio_list_lock);
1634 EXPORT_SYMBOL_GPL(blkio_policy_register);
1636 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1638 spin_lock(&blkio_list_lock);
1639 list_del_init(&blkiop->list);
1640 spin_unlock(&blkio_list_lock);
1642 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
1644 static int __init init_cgroup_blkio(void)
1646 return cgroup_load_subsys(&blkio_subsys);
1649 static void __exit exit_cgroup_blkio(void)
1651 cgroup_unload_subsys(&blkio_subsys);
1654 module_init(init_cgroup_blkio);
1655 module_exit(exit_cgroup_blkio);
1656 MODULE_LICENSE("GPL");