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(struct cgroup_subsys *, struct cgroup *,
34 struct task_struct *, bool);
35 static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
36 struct cgroup *, struct task_struct *, bool);
37 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
38 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
40 /* for encoding cft->private value on file */
41 #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
42 /* What policy owns the file, proportional or throttle */
43 #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
44 #define BLKIOFILE_ATTR(val) ((val) & 0xffff)
46 struct cgroup_subsys blkio_subsys = {
48 .create = blkiocg_create,
49 .can_attach = blkiocg_can_attach,
50 .attach = blkiocg_attach,
51 .destroy = blkiocg_destroy,
52 .populate = blkiocg_populate,
53 #ifdef CONFIG_BLK_CGROUP
54 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
55 .subsys_id = blkio_subsys_id,
58 .module = THIS_MODULE,
60 EXPORT_SYMBOL_GPL(blkio_subsys);
62 static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
63 struct blkio_policy_node *pn)
65 list_add(&pn->node, &blkcg->policy_list);
68 static inline bool cftype_blkg_same_policy(struct cftype *cft,
69 struct blkio_group *blkg)
71 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
73 if (blkg->plid == plid)
79 /* Determines if policy node matches cgroup file being accessed */
80 static inline bool pn_matches_cftype(struct cftype *cft,
81 struct blkio_policy_node *pn)
83 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
84 int fileid = BLKIOFILE_ATTR(cft->private);
86 return (plid == pn->plid && fileid == pn->fileid);
89 /* Must be called with blkcg->lock held */
90 static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
95 /* Must be called with blkcg->lock held */
96 static struct blkio_policy_node *
97 blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
98 enum blkio_policy_id plid, int fileid)
100 struct blkio_policy_node *pn;
102 list_for_each_entry(pn, &blkcg->policy_list, node) {
103 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
110 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
112 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
113 struct blkio_cgroup, css);
115 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
117 struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
119 return container_of(task_subsys_state(tsk, blkio_subsys_id),
120 struct blkio_cgroup, css);
122 EXPORT_SYMBOL_GPL(task_blkio_cgroup);
125 blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
127 struct blkio_policy_type *blkiop;
129 list_for_each_entry(blkiop, &blkio_list, list) {
130 /* If this policy does not own the blkg, do not send updates */
131 if (blkiop->plid != blkg->plid)
133 if (blkiop->ops.blkio_update_group_weight_fn)
134 blkiop->ops.blkio_update_group_weight_fn(blkg->key,
139 static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
142 struct blkio_policy_type *blkiop;
144 list_for_each_entry(blkiop, &blkio_list, list) {
146 /* If this policy does not own the blkg, do not send updates */
147 if (blkiop->plid != blkg->plid)
150 if (fileid == BLKIO_THROTL_read_bps_device
151 && blkiop->ops.blkio_update_group_read_bps_fn)
152 blkiop->ops.blkio_update_group_read_bps_fn(blkg->key,
155 if (fileid == BLKIO_THROTL_write_bps_device
156 && blkiop->ops.blkio_update_group_write_bps_fn)
157 blkiop->ops.blkio_update_group_write_bps_fn(blkg->key,
162 static inline void blkio_update_group_iops(struct blkio_group *blkg,
163 unsigned int iops, int fileid)
165 struct blkio_policy_type *blkiop;
167 list_for_each_entry(blkiop, &blkio_list, list) {
169 /* If this policy does not own the blkg, do not send updates */
170 if (blkiop->plid != blkg->plid)
173 if (fileid == BLKIO_THROTL_read_iops_device
174 && blkiop->ops.blkio_update_group_read_iops_fn)
175 blkiop->ops.blkio_update_group_read_iops_fn(blkg->key,
178 if (fileid == BLKIO_THROTL_write_iops_device
179 && blkiop->ops.blkio_update_group_write_iops_fn)
180 blkiop->ops.blkio_update_group_write_iops_fn(blkg->key,
186 * Add to the appropriate stat variable depending on the request type.
187 * This should be called with the blkg->stats_lock held.
189 static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
193 stat[BLKIO_STAT_WRITE] += add;
195 stat[BLKIO_STAT_READ] += add;
197 stat[BLKIO_STAT_SYNC] += add;
199 stat[BLKIO_STAT_ASYNC] += add;
203 * Decrements the appropriate stat variable if non-zero depending on the
204 * request type. Panics on value being zero.
205 * This should be called with the blkg->stats_lock held.
207 static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
210 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
211 stat[BLKIO_STAT_WRITE]--;
213 BUG_ON(stat[BLKIO_STAT_READ] == 0);
214 stat[BLKIO_STAT_READ]--;
217 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
218 stat[BLKIO_STAT_SYNC]--;
220 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
221 stat[BLKIO_STAT_ASYNC]--;
225 #ifdef CONFIG_DEBUG_BLK_CGROUP
226 /* This should be called with the blkg->stats_lock held. */
227 static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
228 struct blkio_group *curr_blkg)
230 if (blkio_blkg_waiting(&blkg->stats))
232 if (blkg == curr_blkg)
234 blkg->stats.start_group_wait_time = sched_clock();
235 blkio_mark_blkg_waiting(&blkg->stats);
238 /* This should be called with the blkg->stats_lock held. */
239 static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
241 unsigned long long now;
243 if (!blkio_blkg_waiting(stats))
247 if (time_after64(now, stats->start_group_wait_time))
248 stats->group_wait_time += now - stats->start_group_wait_time;
249 blkio_clear_blkg_waiting(stats);
252 /* This should be called with the blkg->stats_lock held. */
253 static void blkio_end_empty_time(struct blkio_group_stats *stats)
255 unsigned long long now;
257 if (!blkio_blkg_empty(stats))
261 if (time_after64(now, stats->start_empty_time))
262 stats->empty_time += now - stats->start_empty_time;
263 blkio_clear_blkg_empty(stats);
266 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
270 spin_lock_irqsave(&blkg->stats_lock, flags);
271 BUG_ON(blkio_blkg_idling(&blkg->stats));
272 blkg->stats.start_idle_time = sched_clock();
273 blkio_mark_blkg_idling(&blkg->stats);
274 spin_unlock_irqrestore(&blkg->stats_lock, flags);
276 EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
278 void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
281 unsigned long long now;
282 struct blkio_group_stats *stats;
284 spin_lock_irqsave(&blkg->stats_lock, flags);
285 stats = &blkg->stats;
286 if (blkio_blkg_idling(stats)) {
288 if (time_after64(now, stats->start_idle_time))
289 stats->idle_time += now - stats->start_idle_time;
290 blkio_clear_blkg_idling(stats);
292 spin_unlock_irqrestore(&blkg->stats_lock, flags);
294 EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
296 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
299 struct blkio_group_stats *stats;
301 spin_lock_irqsave(&blkg->stats_lock, flags);
302 stats = &blkg->stats;
303 stats->avg_queue_size_sum +=
304 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
305 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
306 stats->avg_queue_size_samples++;
307 blkio_update_group_wait_time(stats);
308 spin_unlock_irqrestore(&blkg->stats_lock, flags);
310 EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
312 void blkiocg_set_start_empty_time(struct blkio_group *blkg)
315 struct blkio_group_stats *stats;
317 spin_lock_irqsave(&blkg->stats_lock, flags);
318 stats = &blkg->stats;
320 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
321 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
322 spin_unlock_irqrestore(&blkg->stats_lock, flags);
327 * group is already marked empty. This can happen if cfqq got new
328 * request in parent group and moved to this group while being added
329 * to service tree. Just ignore the event and move on.
331 if(blkio_blkg_empty(stats)) {
332 spin_unlock_irqrestore(&blkg->stats_lock, flags);
336 stats->start_empty_time = sched_clock();
337 blkio_mark_blkg_empty(stats);
338 spin_unlock_irqrestore(&blkg->stats_lock, flags);
340 EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
342 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
343 unsigned long dequeue)
345 blkg->stats.dequeue += dequeue;
347 EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
349 static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
350 struct blkio_group *curr_blkg) {}
351 static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
354 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
355 struct blkio_group *curr_blkg, bool direction,
360 spin_lock_irqsave(&blkg->stats_lock, flags);
361 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
363 blkio_end_empty_time(&blkg->stats);
364 blkio_set_start_group_wait_time(blkg, curr_blkg);
365 spin_unlock_irqrestore(&blkg->stats_lock, flags);
367 EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
369 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
370 bool direction, bool sync)
374 spin_lock_irqsave(&blkg->stats_lock, flags);
375 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
377 spin_unlock_irqrestore(&blkg->stats_lock, flags);
379 EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
381 void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
382 unsigned long unaccounted_time)
386 spin_lock_irqsave(&blkg->stats_lock, flags);
387 blkg->stats.time += time;
388 #ifdef CONFIG_DEBUG_BLK_CGROUP
389 blkg->stats.unaccounted_time += unaccounted_time;
391 spin_unlock_irqrestore(&blkg->stats_lock, flags);
393 EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
396 * should be called under rcu read lock or queue lock to make sure blkg pointer
399 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
400 uint64_t bytes, bool direction, bool sync)
402 struct blkio_group_stats_cpu *stats_cpu;
406 * Disabling interrupts to provide mutual exclusion between two
407 * writes on same cpu. It probably is not needed for 64bit. Not
408 * optimizing that case yet.
410 local_irq_save(flags);
412 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
414 u64_stats_update_begin(&stats_cpu->syncp);
415 stats_cpu->sectors += bytes >> 9;
416 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
418 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
419 bytes, direction, sync);
420 u64_stats_update_end(&stats_cpu->syncp);
421 local_irq_restore(flags);
423 EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
425 void blkiocg_update_completion_stats(struct blkio_group *blkg,
426 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
428 struct blkio_group_stats *stats;
430 unsigned long long now = sched_clock();
432 spin_lock_irqsave(&blkg->stats_lock, flags);
433 stats = &blkg->stats;
434 if (time_after64(now, io_start_time))
435 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
436 now - io_start_time, direction, sync);
437 if (time_after64(io_start_time, start_time))
438 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
439 io_start_time - start_time, direction, sync);
440 spin_unlock_irqrestore(&blkg->stats_lock, flags);
442 EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
444 void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
449 spin_lock_irqsave(&blkg->stats_lock, flags);
450 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
452 spin_unlock_irqrestore(&blkg->stats_lock, flags);
454 EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
457 * This function allocates the per cpu stats for blkio_group. Should be called
458 * from sleepable context as alloc_per_cpu() requires that.
460 int blkio_alloc_blkg_stats(struct blkio_group *blkg)
462 /* Allocate memory for per cpu stats */
463 blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
464 if (!blkg->stats_cpu)
468 EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
470 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
471 struct blkio_group *blkg, void *key, dev_t dev,
472 enum blkio_policy_id plid)
476 spin_lock_irqsave(&blkcg->lock, flags);
477 spin_lock_init(&blkg->stats_lock);
478 rcu_assign_pointer(blkg->key, key);
479 blkg->blkcg_id = css_id(&blkcg->css);
480 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
482 spin_unlock_irqrestore(&blkcg->lock, flags);
483 /* Need to take css reference ? */
484 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
487 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
489 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
491 hlist_del_init_rcu(&blkg->blkcg_node);
496 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
497 * indicating that blk_group was unhashed by the time we got to it.
499 int blkiocg_del_blkio_group(struct blkio_group *blkg)
501 struct blkio_cgroup *blkcg;
503 struct cgroup_subsys_state *css;
507 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
509 blkcg = container_of(css, struct blkio_cgroup, css);
510 spin_lock_irqsave(&blkcg->lock, flags);
511 if (!hlist_unhashed(&blkg->blkcg_node)) {
512 __blkiocg_del_blkio_group(blkg);
515 spin_unlock_irqrestore(&blkcg->lock, flags);
521 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
523 /* called under rcu_read_lock(). */
524 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
526 struct blkio_group *blkg;
527 struct hlist_node *n;
530 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
538 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
541 blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
543 struct blkio_cgroup *blkcg;
544 struct blkio_group *blkg;
545 struct blkio_group_stats *stats;
546 struct hlist_node *n;
547 uint64_t queued[BLKIO_STAT_TOTAL];
549 #ifdef CONFIG_DEBUG_BLK_CGROUP
550 bool idling, waiting, empty;
551 unsigned long long now = sched_clock();
554 blkcg = cgroup_to_blkio_cgroup(cgroup);
555 spin_lock_irq(&blkcg->lock);
556 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
557 spin_lock(&blkg->stats_lock);
558 stats = &blkg->stats;
559 #ifdef CONFIG_DEBUG_BLK_CGROUP
560 idling = blkio_blkg_idling(stats);
561 waiting = blkio_blkg_waiting(stats);
562 empty = blkio_blkg_empty(stats);
564 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
565 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
566 memset(stats, 0, sizeof(struct blkio_group_stats));
567 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
568 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
569 #ifdef CONFIG_DEBUG_BLK_CGROUP
571 blkio_mark_blkg_idling(stats);
572 stats->start_idle_time = now;
575 blkio_mark_blkg_waiting(stats);
576 stats->start_group_wait_time = now;
579 blkio_mark_blkg_empty(stats);
580 stats->start_empty_time = now;
583 spin_unlock(&blkg->stats_lock);
585 spin_unlock_irq(&blkcg->lock);
589 static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
590 int chars_left, bool diskname_only)
592 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
593 chars_left -= strlen(str);
594 if (chars_left <= 0) {
596 "Possibly incorrect cgroup stat display format");
602 case BLKIO_STAT_READ:
603 strlcat(str, " Read", chars_left);
605 case BLKIO_STAT_WRITE:
606 strlcat(str, " Write", chars_left);
608 case BLKIO_STAT_SYNC:
609 strlcat(str, " Sync", chars_left);
611 case BLKIO_STAT_ASYNC:
612 strlcat(str, " Async", chars_left);
614 case BLKIO_STAT_TOTAL:
615 strlcat(str, " Total", chars_left);
618 strlcat(str, " Invalid", chars_left);
622 static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
623 struct cgroup_map_cb *cb, dev_t dev)
625 blkio_get_key_name(0, dev, str, chars_left, true);
626 cb->fill(cb, str, val);
631 static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
632 enum stat_type_cpu type, enum stat_sub_type sub_type)
635 struct blkio_group_stats_cpu *stats_cpu;
638 for_each_possible_cpu(cpu) {
640 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
643 start = u64_stats_fetch_begin(&stats_cpu->syncp);
644 if (type == BLKIO_STAT_CPU_SECTORS)
645 tval = stats_cpu->sectors;
647 tval = stats_cpu->stat_arr_cpu[type][sub_type];
648 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
656 static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
657 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
659 uint64_t disk_total, val;
660 char key_str[MAX_KEY_LEN];
661 enum stat_sub_type sub_type;
663 if (type == BLKIO_STAT_CPU_SECTORS) {
664 val = blkio_read_stat_cpu(blkg, type, 0);
665 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
668 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
670 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
671 val = blkio_read_stat_cpu(blkg, type, sub_type);
672 cb->fill(cb, key_str, val);
675 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
676 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
678 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
679 cb->fill(cb, key_str, disk_total);
683 /* This should be called with blkg->stats_lock held */
684 static uint64_t blkio_get_stat(struct blkio_group *blkg,
685 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
688 char key_str[MAX_KEY_LEN];
689 enum stat_sub_type sub_type;
691 if (type == BLKIO_STAT_TIME)
692 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
693 blkg->stats.time, cb, dev);
694 #ifdef CONFIG_DEBUG_BLK_CGROUP
695 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
696 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
697 blkg->stats.unaccounted_time, cb, dev);
698 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
699 uint64_t sum = blkg->stats.avg_queue_size_sum;
700 uint64_t samples = blkg->stats.avg_queue_size_samples;
702 do_div(sum, samples);
705 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
707 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
708 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
709 blkg->stats.group_wait_time, cb, dev);
710 if (type == BLKIO_STAT_IDLE_TIME)
711 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
712 blkg->stats.idle_time, cb, dev);
713 if (type == BLKIO_STAT_EMPTY_TIME)
714 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
715 blkg->stats.empty_time, cb, dev);
716 if (type == BLKIO_STAT_DEQUEUE)
717 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
718 blkg->stats.dequeue, cb, dev);
721 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
723 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
724 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
726 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
727 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
728 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
729 cb->fill(cb, key_str, disk_total);
733 static int blkio_check_dev_num(dev_t dev)
736 struct gendisk *disk;
738 disk = get_gendisk(dev, &part);
745 static int blkio_policy_parse_and_set(char *buf,
746 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
748 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
750 unsigned long major, minor, temp;
755 memset(s, 0, sizeof(s));
757 while ((p = strsep(&buf, " ")) != NULL) {
763 /* Prevent from inputing too many things */
771 p = strsep(&s[0], ":");
781 ret = strict_strtoul(major_s, 10, &major);
785 ret = strict_strtoul(minor_s, 10, &minor);
789 dev = MKDEV(major, minor);
791 ret = blkio_check_dev_num(dev);
801 case BLKIO_POLICY_PROP:
802 ret = strict_strtoul(s[1], 10, &temp);
803 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
804 temp > BLKIO_WEIGHT_MAX)
808 newpn->fileid = fileid;
809 newpn->val.weight = temp;
811 case BLKIO_POLICY_THROTL:
813 case BLKIO_THROTL_read_bps_device:
814 case BLKIO_THROTL_write_bps_device:
815 ret = strict_strtoull(s[1], 10, &bps);
820 newpn->fileid = fileid;
821 newpn->val.bps = bps;
823 case BLKIO_THROTL_read_iops_device:
824 case BLKIO_THROTL_write_iops_device:
825 ret = strict_strtoull(s[1], 10, &iops);
829 if (iops > THROTL_IOPS_MAX)
833 newpn->fileid = fileid;
834 newpn->val.iops = (unsigned int)iops;
845 unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
848 struct blkio_policy_node *pn;
850 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
851 BLKIO_PROP_weight_device);
853 return pn->val.weight;
855 return blkcg->weight;
857 EXPORT_SYMBOL_GPL(blkcg_get_weight);
859 uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
861 struct blkio_policy_node *pn;
863 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
864 BLKIO_THROTL_read_bps_device);
871 uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
873 struct blkio_policy_node *pn;
874 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
875 BLKIO_THROTL_write_bps_device);
882 unsigned int blkcg_get_read_iops(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_iops_device);
894 unsigned int blkcg_get_write_iops(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_iops_device);
905 /* Checks whether user asked for deleting a policy rule */
906 static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
909 case BLKIO_POLICY_PROP:
910 if (pn->val.weight == 0)
913 case BLKIO_POLICY_THROTL:
915 case BLKIO_THROTL_read_bps_device:
916 case BLKIO_THROTL_write_bps_device:
917 if (pn->val.bps == 0)
920 case BLKIO_THROTL_read_iops_device:
921 case BLKIO_THROTL_write_iops_device:
922 if (pn->val.iops == 0)
933 static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
934 struct blkio_policy_node *newpn)
936 switch(oldpn->plid) {
937 case BLKIO_POLICY_PROP:
938 oldpn->val.weight = newpn->val.weight;
940 case BLKIO_POLICY_THROTL:
941 switch(newpn->fileid) {
942 case BLKIO_THROTL_read_bps_device:
943 case BLKIO_THROTL_write_bps_device:
944 oldpn->val.bps = newpn->val.bps;
946 case BLKIO_THROTL_read_iops_device:
947 case BLKIO_THROTL_write_iops_device:
948 oldpn->val.iops = newpn->val.iops;
957 * Some rules/values in blkg have changed. Propagate those to respective
960 static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
961 struct blkio_group *blkg, struct blkio_policy_node *pn)
963 unsigned int weight, iops;
967 case BLKIO_POLICY_PROP:
968 weight = pn->val.weight ? pn->val.weight :
970 blkio_update_group_weight(blkg, weight);
972 case BLKIO_POLICY_THROTL:
974 case BLKIO_THROTL_read_bps_device:
975 case BLKIO_THROTL_write_bps_device:
976 bps = pn->val.bps ? pn->val.bps : (-1);
977 blkio_update_group_bps(blkg, bps, pn->fileid);
979 case BLKIO_THROTL_read_iops_device:
980 case BLKIO_THROTL_write_iops_device:
981 iops = pn->val.iops ? pn->val.iops : (-1);
982 blkio_update_group_iops(blkg, iops, pn->fileid);
992 * A policy node rule has been updated. Propagate this update to all the
993 * block groups which might be affected by this update.
995 static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
996 struct blkio_policy_node *pn)
998 struct blkio_group *blkg;
999 struct hlist_node *n;
1001 spin_lock(&blkio_list_lock);
1002 spin_lock_irq(&blkcg->lock);
1004 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1005 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
1007 blkio_update_blkg_policy(blkcg, blkg, pn);
1010 spin_unlock_irq(&blkcg->lock);
1011 spin_unlock(&blkio_list_lock);
1014 static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1019 struct blkio_policy_node *newpn, *pn;
1020 struct blkio_cgroup *blkcg;
1022 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1023 int fileid = BLKIOFILE_ATTR(cft->private);
1025 buf = kstrdup(buffer, GFP_KERNEL);
1029 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1035 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
1039 blkcg = cgroup_to_blkio_cgroup(cgrp);
1041 spin_lock_irq(&blkcg->lock);
1043 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
1045 if (!blkio_delete_rule_command(newpn)) {
1046 blkio_policy_insert_node(blkcg, newpn);
1049 spin_unlock_irq(&blkcg->lock);
1050 goto update_io_group;
1053 if (blkio_delete_rule_command(newpn)) {
1054 blkio_policy_delete_node(pn);
1055 spin_unlock_irq(&blkcg->lock);
1056 goto update_io_group;
1058 spin_unlock_irq(&blkcg->lock);
1060 blkio_update_policy_rule(pn, newpn);
1063 blkio_update_policy_node_blkg(blkcg, newpn);
1074 blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
1077 case BLKIO_POLICY_PROP:
1078 if (pn->fileid == BLKIO_PROP_weight_device)
1079 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1080 MINOR(pn->dev), pn->val.weight);
1082 case BLKIO_POLICY_THROTL:
1083 switch(pn->fileid) {
1084 case BLKIO_THROTL_read_bps_device:
1085 case BLKIO_THROTL_write_bps_device:
1086 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1087 MINOR(pn->dev), pn->val.bps);
1089 case BLKIO_THROTL_read_iops_device:
1090 case BLKIO_THROTL_write_iops_device:
1091 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1092 MINOR(pn->dev), pn->val.iops);
1101 /* cgroup files which read their data from policy nodes end up here */
1102 static void blkio_read_policy_node_files(struct cftype *cft,
1103 struct blkio_cgroup *blkcg, struct seq_file *m)
1105 struct blkio_policy_node *pn;
1107 if (!list_empty(&blkcg->policy_list)) {
1108 spin_lock_irq(&blkcg->lock);
1109 list_for_each_entry(pn, &blkcg->policy_list, node) {
1110 if (!pn_matches_cftype(cft, pn))
1112 blkio_print_policy_node(m, pn);
1114 spin_unlock_irq(&blkcg->lock);
1118 static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1121 struct blkio_cgroup *blkcg;
1122 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1123 int name = BLKIOFILE_ATTR(cft->private);
1125 blkcg = cgroup_to_blkio_cgroup(cgrp);
1128 case BLKIO_POLICY_PROP:
1130 case BLKIO_PROP_weight_device:
1131 blkio_read_policy_node_files(cft, blkcg, m);
1137 case BLKIO_POLICY_THROTL:
1139 case BLKIO_THROTL_read_bps_device:
1140 case BLKIO_THROTL_write_bps_device:
1141 case BLKIO_THROTL_read_iops_device:
1142 case BLKIO_THROTL_write_iops_device:
1143 blkio_read_policy_node_files(cft, blkcg, m);
1156 static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1157 struct cftype *cft, struct cgroup_map_cb *cb,
1158 enum stat_type type, bool show_total, bool pcpu)
1160 struct blkio_group *blkg;
1161 struct hlist_node *n;
1162 uint64_t cgroup_total = 0;
1165 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1167 if (!cftype_blkg_same_policy(cft, blkg))
1170 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1173 spin_lock_irq(&blkg->stats_lock);
1174 cgroup_total += blkio_get_stat(blkg, cb,
1176 spin_unlock_irq(&blkg->stats_lock);
1181 cb->fill(cb, "Total", cgroup_total);
1186 /* All map kind of cgroup file get serviced by this function */
1187 static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1188 struct cgroup_map_cb *cb)
1190 struct blkio_cgroup *blkcg;
1191 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1192 int name = BLKIOFILE_ATTR(cft->private);
1194 blkcg = cgroup_to_blkio_cgroup(cgrp);
1197 case BLKIO_POLICY_PROP:
1199 case BLKIO_PROP_time:
1200 return blkio_read_blkg_stats(blkcg, cft, cb,
1201 BLKIO_STAT_TIME, 0, 0);
1202 case BLKIO_PROP_sectors:
1203 return blkio_read_blkg_stats(blkcg, cft, cb,
1204 BLKIO_STAT_CPU_SECTORS, 0, 1);
1205 case BLKIO_PROP_io_service_bytes:
1206 return blkio_read_blkg_stats(blkcg, cft, cb,
1207 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1208 case BLKIO_PROP_io_serviced:
1209 return blkio_read_blkg_stats(blkcg, cft, cb,
1210 BLKIO_STAT_CPU_SERVICED, 1, 1);
1211 case BLKIO_PROP_io_service_time:
1212 return blkio_read_blkg_stats(blkcg, cft, cb,
1213 BLKIO_STAT_SERVICE_TIME, 1, 0);
1214 case BLKIO_PROP_io_wait_time:
1215 return blkio_read_blkg_stats(blkcg, cft, cb,
1216 BLKIO_STAT_WAIT_TIME, 1, 0);
1217 case BLKIO_PROP_io_merged:
1218 return blkio_read_blkg_stats(blkcg, cft, cb,
1219 BLKIO_STAT_MERGED, 1, 0);
1220 case BLKIO_PROP_io_queued:
1221 return blkio_read_blkg_stats(blkcg, cft, cb,
1222 BLKIO_STAT_QUEUED, 1, 0);
1223 #ifdef CONFIG_DEBUG_BLK_CGROUP
1224 case BLKIO_PROP_unaccounted_time:
1225 return blkio_read_blkg_stats(blkcg, cft, cb,
1226 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
1227 case BLKIO_PROP_dequeue:
1228 return blkio_read_blkg_stats(blkcg, cft, cb,
1229 BLKIO_STAT_DEQUEUE, 0, 0);
1230 case BLKIO_PROP_avg_queue_size:
1231 return blkio_read_blkg_stats(blkcg, cft, cb,
1232 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
1233 case BLKIO_PROP_group_wait_time:
1234 return blkio_read_blkg_stats(blkcg, cft, cb,
1235 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
1236 case BLKIO_PROP_idle_time:
1237 return blkio_read_blkg_stats(blkcg, cft, cb,
1238 BLKIO_STAT_IDLE_TIME, 0, 0);
1239 case BLKIO_PROP_empty_time:
1240 return blkio_read_blkg_stats(blkcg, cft, cb,
1241 BLKIO_STAT_EMPTY_TIME, 0, 0);
1247 case BLKIO_POLICY_THROTL:
1249 case BLKIO_THROTL_io_service_bytes:
1250 return blkio_read_blkg_stats(blkcg, cft, cb,
1251 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1252 case BLKIO_THROTL_io_serviced:
1253 return blkio_read_blkg_stats(blkcg, cft, cb,
1254 BLKIO_STAT_CPU_SERVICED, 1, 1);
1266 static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1268 struct blkio_group *blkg;
1269 struct hlist_node *n;
1270 struct blkio_policy_node *pn;
1272 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1275 spin_lock(&blkio_list_lock);
1276 spin_lock_irq(&blkcg->lock);
1277 blkcg->weight = (unsigned int)val;
1279 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1280 pn = blkio_policy_search_node(blkcg, blkg->dev,
1281 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1285 blkio_update_group_weight(blkg, blkcg->weight);
1287 spin_unlock_irq(&blkcg->lock);
1288 spin_unlock(&blkio_list_lock);
1292 static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1293 struct blkio_cgroup *blkcg;
1294 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1295 int name = BLKIOFILE_ATTR(cft->private);
1297 blkcg = cgroup_to_blkio_cgroup(cgrp);
1300 case BLKIO_POLICY_PROP:
1302 case BLKIO_PROP_weight:
1303 return (u64)blkcg->weight;
1313 blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1315 struct blkio_cgroup *blkcg;
1316 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1317 int name = BLKIOFILE_ATTR(cft->private);
1319 blkcg = cgroup_to_blkio_cgroup(cgrp);
1322 case BLKIO_POLICY_PROP:
1324 case BLKIO_PROP_weight:
1325 return blkio_weight_write(blkcg, val);
1335 struct cftype blkio_files[] = {
1337 .name = "weight_device",
1338 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1339 BLKIO_PROP_weight_device),
1340 .read_seq_string = blkiocg_file_read,
1341 .write_string = blkiocg_file_write,
1342 .max_write_len = 256,
1346 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1348 .read_u64 = blkiocg_file_read_u64,
1349 .write_u64 = blkiocg_file_write_u64,
1353 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1355 .read_map = blkiocg_file_read_map,
1359 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1360 BLKIO_PROP_sectors),
1361 .read_map = blkiocg_file_read_map,
1364 .name = "io_service_bytes",
1365 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1366 BLKIO_PROP_io_service_bytes),
1367 .read_map = blkiocg_file_read_map,
1370 .name = "io_serviced",
1371 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1372 BLKIO_PROP_io_serviced),
1373 .read_map = blkiocg_file_read_map,
1376 .name = "io_service_time",
1377 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1378 BLKIO_PROP_io_service_time),
1379 .read_map = blkiocg_file_read_map,
1382 .name = "io_wait_time",
1383 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1384 BLKIO_PROP_io_wait_time),
1385 .read_map = blkiocg_file_read_map,
1388 .name = "io_merged",
1389 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1390 BLKIO_PROP_io_merged),
1391 .read_map = blkiocg_file_read_map,
1394 .name = "io_queued",
1395 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1396 BLKIO_PROP_io_queued),
1397 .read_map = blkiocg_file_read_map,
1400 .name = "reset_stats",
1401 .write_u64 = blkiocg_reset_stats,
1403 #ifdef CONFIG_BLK_DEV_THROTTLING
1405 .name = "throttle.read_bps_device",
1406 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1407 BLKIO_THROTL_read_bps_device),
1408 .read_seq_string = blkiocg_file_read,
1409 .write_string = blkiocg_file_write,
1410 .max_write_len = 256,
1414 .name = "throttle.write_bps_device",
1415 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1416 BLKIO_THROTL_write_bps_device),
1417 .read_seq_string = blkiocg_file_read,
1418 .write_string = blkiocg_file_write,
1419 .max_write_len = 256,
1423 .name = "throttle.read_iops_device",
1424 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1425 BLKIO_THROTL_read_iops_device),
1426 .read_seq_string = blkiocg_file_read,
1427 .write_string = blkiocg_file_write,
1428 .max_write_len = 256,
1432 .name = "throttle.write_iops_device",
1433 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1434 BLKIO_THROTL_write_iops_device),
1435 .read_seq_string = blkiocg_file_read,
1436 .write_string = blkiocg_file_write,
1437 .max_write_len = 256,
1440 .name = "throttle.io_service_bytes",
1441 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1442 BLKIO_THROTL_io_service_bytes),
1443 .read_map = blkiocg_file_read_map,
1446 .name = "throttle.io_serviced",
1447 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1448 BLKIO_THROTL_io_serviced),
1449 .read_map = blkiocg_file_read_map,
1451 #endif /* CONFIG_BLK_DEV_THROTTLING */
1453 #ifdef CONFIG_DEBUG_BLK_CGROUP
1455 .name = "avg_queue_size",
1456 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1457 BLKIO_PROP_avg_queue_size),
1458 .read_map = blkiocg_file_read_map,
1461 .name = "group_wait_time",
1462 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1463 BLKIO_PROP_group_wait_time),
1464 .read_map = blkiocg_file_read_map,
1467 .name = "idle_time",
1468 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1469 BLKIO_PROP_idle_time),
1470 .read_map = blkiocg_file_read_map,
1473 .name = "empty_time",
1474 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1475 BLKIO_PROP_empty_time),
1476 .read_map = blkiocg_file_read_map,
1480 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1481 BLKIO_PROP_dequeue),
1482 .read_map = blkiocg_file_read_map,
1485 .name = "unaccounted_time",
1486 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1487 BLKIO_PROP_unaccounted_time),
1488 .read_map = blkiocg_file_read_map,
1493 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1495 return cgroup_add_files(cgroup, subsys, blkio_files,
1496 ARRAY_SIZE(blkio_files));
1499 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1501 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1502 unsigned long flags;
1503 struct blkio_group *blkg;
1505 struct blkio_policy_type *blkiop;
1506 struct blkio_policy_node *pn, *pntmp;
1510 spin_lock_irqsave(&blkcg->lock, flags);
1512 if (hlist_empty(&blkcg->blkg_list)) {
1513 spin_unlock_irqrestore(&blkcg->lock, flags);
1517 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1519 key = rcu_dereference(blkg->key);
1520 __blkiocg_del_blkio_group(blkg);
1522 spin_unlock_irqrestore(&blkcg->lock, flags);
1525 * This blkio_group is being unlinked as associated cgroup is
1526 * going away. Let all the IO controlling policies know about
1529 spin_lock(&blkio_list_lock);
1530 list_for_each_entry(blkiop, &blkio_list, list) {
1531 if (blkiop->plid != blkg->plid)
1533 blkiop->ops.blkio_unlink_group_fn(key, blkg);
1535 spin_unlock(&blkio_list_lock);
1538 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1539 blkio_policy_delete_node(pn);
1543 free_css_id(&blkio_subsys, &blkcg->css);
1545 if (blkcg != &blkio_root_cgroup)
1549 static struct cgroup_subsys_state *
1550 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1552 struct blkio_cgroup *blkcg;
1553 struct cgroup *parent = cgroup->parent;
1556 blkcg = &blkio_root_cgroup;
1560 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1562 return ERR_PTR(-ENOMEM);
1564 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1566 spin_lock_init(&blkcg->lock);
1567 INIT_HLIST_HEAD(&blkcg->blkg_list);
1569 INIT_LIST_HEAD(&blkcg->policy_list);
1574 * We cannot support shared io contexts, as we have no mean to support
1575 * two tasks with the same ioc in two different groups without major rework
1576 * of the main cic data structures. For now we allow a task to change
1577 * its cgroup only if it's the only owner of its ioc.
1579 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1580 struct cgroup *cgroup, struct task_struct *tsk,
1583 struct io_context *ioc;
1586 /* task_lock() is needed to avoid races with exit_io_context() */
1588 ioc = tsk->io_context;
1589 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1596 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1597 struct cgroup *prev, struct task_struct *tsk,
1600 struct io_context *ioc;
1603 ioc = tsk->io_context;
1605 ioc->cgroup_changed = 1;
1609 void blkio_policy_register(struct blkio_policy_type *blkiop)
1611 spin_lock(&blkio_list_lock);
1612 list_add_tail(&blkiop->list, &blkio_list);
1613 spin_unlock(&blkio_list_lock);
1615 EXPORT_SYMBOL_GPL(blkio_policy_register);
1617 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1619 spin_lock(&blkio_list_lock);
1620 list_del_init(&blkiop->list);
1621 spin_unlock(&blkio_list_lock);
1623 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
1625 static int __init init_cgroup_blkio(void)
1627 return cgroup_load_subsys(&blkio_subsys);
1630 static void __exit exit_cgroup_blkio(void)
1632 cgroup_unload_subsys(&blkio_subsys);
1635 module_init(init_cgroup_blkio);
1636 module_exit(exit_cgroup_blkio);
1637 MODULE_LICENSE("GPL");