Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 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> |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
| 15 | #include <linux/kdev_t.h> |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 16 | #include <linux/module.h> |
Stephen Rothwell | accee78 | 2009-12-07 19:29:39 +1100 | [diff] [blame] | 17 | #include <linux/err.h> |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 18 | #include <linux/blkdev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 20 | #include <linux/genhd.h> |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 21 | #include <linux/delay.h> |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 23 | #include "blk-cgroup.h" |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 24 | #include "blk.h" |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 25 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 26 | #define MAX_KEY_LEN 100 |
| 27 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 28 | static DEFINE_SPINLOCK(blkio_list_lock); |
| 29 | static LIST_HEAD(blkio_list); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 30 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 31 | static DEFINE_MUTEX(all_q_mutex); |
| 32 | static LIST_HEAD(all_q_list); |
| 33 | |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 34 | /* List of groups pending per cpu stats allocation */ |
| 35 | static DEFINE_SPINLOCK(alloc_list_lock); |
| 36 | static LIST_HEAD(alloc_list); |
| 37 | |
| 38 | static void blkio_stat_alloc_fn(struct work_struct *); |
| 39 | static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn); |
| 40 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 41 | struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT }; |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
| 43 | |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 44 | static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES]; |
| 45 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 46 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
| 47 | { |
| 48 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), |
| 49 | struct blkio_cgroup, css); |
| 50 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 52 | |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 53 | static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk) |
Vivek Goyal | 70087dc | 2011-05-16 15:24:08 +0200 | [diff] [blame] | 54 | { |
| 55 | return container_of(task_subsys_state(tsk, blkio_subsys_id), |
| 56 | struct blkio_cgroup, css); |
| 57 | } |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 58 | |
| 59 | struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio) |
| 60 | { |
| 61 | if (bio && bio->bi_css) |
| 62 | return container_of(bio->bi_css, struct blkio_cgroup, css); |
| 63 | return task_blkio_cgroup(current); |
| 64 | } |
| 65 | EXPORT_SYMBOL_GPL(bio_blkio_cgroup); |
Vivek Goyal | 70087dc | 2011-05-16 15:24:08 +0200 | [diff] [blame] | 66 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 67 | static inline void blkio_update_group_weight(struct blkio_group *blkg, |
| 68 | int plid, unsigned int weight) |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 69 | { |
| 70 | struct blkio_policy_type *blkiop; |
| 71 | |
| 72 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 73 | /* If this policy does not own the blkg, do not send updates */ |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 74 | if (blkiop->plid != plid) |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 75 | continue; |
| 76 | if (blkiop->ops.blkio_update_group_weight_fn) |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 77 | blkiop->ops.blkio_update_group_weight_fn(blkg->q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 78 | blkg, weight); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 82 | static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 83 | u64 bps, int rw) |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 84 | { |
| 85 | struct blkio_policy_type *blkiop; |
| 86 | |
| 87 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 88 | |
| 89 | /* If this policy does not own the blkg, do not send updates */ |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 90 | if (blkiop->plid != plid) |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 91 | continue; |
| 92 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 93 | if (rw == READ && blkiop->ops.blkio_update_group_read_bps_fn) |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 94 | blkiop->ops.blkio_update_group_read_bps_fn(blkg->q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 95 | blkg, bps); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 96 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 97 | if (rw == WRITE && blkiop->ops.blkio_update_group_write_bps_fn) |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 98 | blkiop->ops.blkio_update_group_write_bps_fn(blkg->q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 99 | blkg, bps); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 103 | static inline void blkio_update_group_iops(struct blkio_group *blkg, int plid, |
| 104 | u64 iops, int rw) |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 105 | { |
| 106 | struct blkio_policy_type *blkiop; |
| 107 | |
| 108 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 109 | |
| 110 | /* If this policy does not own the blkg, do not send updates */ |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 111 | if (blkiop->plid != plid) |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 112 | continue; |
| 113 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 114 | if (rw == READ && blkiop->ops.blkio_update_group_read_iops_fn) |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 115 | blkiop->ops.blkio_update_group_read_iops_fn(blkg->q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 116 | blkg, iops); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 117 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 118 | if (rw == WRITE && blkiop->ops.blkio_update_group_write_iops_fn) |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 119 | blkiop->ops.blkio_update_group_write_iops_fn(blkg->q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 120 | blkg,iops); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 125 | /* This should be called with the queue_lock held. */ |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 126 | static void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 127 | struct blkio_policy_type *pol, |
| 128 | struct blkio_group *curr_blkg) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 129 | { |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 130 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 131 | |
| 132 | if (blkio_blkg_waiting(&pd->stats)) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 133 | return; |
| 134 | if (blkg == curr_blkg) |
| 135 | return; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 136 | pd->stats.start_group_wait_time = sched_clock(); |
| 137 | blkio_mark_blkg_waiting(&pd->stats); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 140 | /* This should be called with the queue_lock held. */ |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 141 | static void blkio_update_group_wait_time(struct blkio_group_stats *stats) |
| 142 | { |
| 143 | unsigned long long now; |
| 144 | |
| 145 | if (!blkio_blkg_waiting(stats)) |
| 146 | return; |
| 147 | |
| 148 | now = sched_clock(); |
| 149 | if (time_after64(now, stats->start_group_wait_time)) |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 150 | blkg_stat_add(&stats->group_wait_time, |
| 151 | now - stats->start_group_wait_time); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 152 | blkio_clear_blkg_waiting(stats); |
| 153 | } |
| 154 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 155 | /* This should be called with the queue_lock held. */ |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 156 | static void blkio_end_empty_time(struct blkio_group_stats *stats) |
| 157 | { |
| 158 | unsigned long long now; |
| 159 | |
| 160 | if (!blkio_blkg_empty(stats)) |
| 161 | return; |
| 162 | |
| 163 | now = sched_clock(); |
| 164 | if (time_after64(now, stats->start_empty_time)) |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 165 | blkg_stat_add(&stats->empty_time, |
| 166 | now - stats->start_empty_time); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 167 | blkio_clear_blkg_empty(stats); |
| 168 | } |
| 169 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 170 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg, |
| 171 | struct blkio_policy_type *pol) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 172 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 173 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 174 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 175 | lockdep_assert_held(blkg->q->queue_lock); |
| 176 | BUG_ON(blkio_blkg_idling(stats)); |
| 177 | |
| 178 | stats->start_idle_time = sched_clock(); |
| 179 | blkio_mark_blkg_idling(stats); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 180 | } |
| 181 | EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats); |
| 182 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 183 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg, |
| 184 | struct blkio_policy_type *pol) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 185 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 186 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 187 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 188 | lockdep_assert_held(blkg->q->queue_lock); |
| 189 | |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 190 | if (blkio_blkg_idling(stats)) { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 191 | unsigned long long now = sched_clock(); |
| 192 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 193 | if (time_after64(now, stats->start_idle_time)) |
| 194 | blkg_stat_add(&stats->idle_time, |
| 195 | now - stats->start_idle_time); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 196 | blkio_clear_blkg_idling(stats); |
| 197 | } |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 198 | } |
| 199 | EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats); |
| 200 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 201 | void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg, |
| 202 | struct blkio_policy_type *pol) |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 203 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 204 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 205 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 206 | lockdep_assert_held(blkg->q->queue_lock); |
| 207 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 208 | blkg_stat_add(&stats->avg_queue_size_sum, |
| 209 | blkg_rwstat_sum(&stats->queued)); |
| 210 | blkg_stat_add(&stats->avg_queue_size_samples, 1); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 211 | blkio_update_group_wait_time(stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 212 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 213 | EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats); |
| 214 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 215 | void blkiocg_set_start_empty_time(struct blkio_group *blkg, |
| 216 | struct blkio_policy_type *pol) |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 217 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 218 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 219 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 220 | lockdep_assert_held(blkg->q->queue_lock); |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 221 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 222 | if (blkg_rwstat_sum(&stats->queued)) |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 223 | return; |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 224 | |
| 225 | /* |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 226 | * group is already marked empty. This can happen if cfqq got new |
| 227 | * request in parent group and moved to this group while being added |
| 228 | * to service tree. Just ignore the event and move on. |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 229 | */ |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 230 | if (blkio_blkg_empty(stats)) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 231 | return; |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 232 | |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 233 | stats->start_empty_time = sched_clock(); |
| 234 | blkio_mark_blkg_empty(stats); |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time); |
| 237 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 238 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 239 | struct blkio_policy_type *pol, |
| 240 | unsigned long dequeue) |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 241 | { |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 242 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 243 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 244 | lockdep_assert_held(blkg->q->queue_lock); |
| 245 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 246 | blkg_stat_add(&pd->stats.dequeue, dequeue); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 247 | } |
| 248 | EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 249 | #else |
| 250 | static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 251 | struct blkio_policy_type *pol, |
| 252 | struct blkio_group *curr_blkg) { } |
| 253 | static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { } |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 254 | #endif |
| 255 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 256 | void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 257 | struct blkio_policy_type *pol, |
| 258 | struct blkio_group *curr_blkg, bool direction, |
| 259 | bool sync) |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 260 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 261 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 262 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 263 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 264 | lockdep_assert_held(blkg->q->queue_lock); |
| 265 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 266 | blkg_rwstat_add(&stats->queued, rw, 1); |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 267 | blkio_end_empty_time(stats); |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 268 | blkio_set_start_group_wait_time(blkg, pol, curr_blkg); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 269 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 271 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 272 | void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 273 | struct blkio_policy_type *pol, |
| 274 | bool direction, bool sync) |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 275 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 276 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 277 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 278 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 279 | lockdep_assert_held(blkg->q->queue_lock); |
| 280 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 281 | blkg_rwstat_add(&stats->queued, rw, -1); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 282 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 283 | EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 284 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 285 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
| 286 | struct blkio_policy_type *pol, |
| 287 | unsigned long time, |
| 288 | unsigned long unaccounted_time) |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 289 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 290 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 291 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 292 | lockdep_assert_held(blkg->q->queue_lock); |
| 293 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 294 | blkg_stat_add(&stats->time, time); |
Vivek Goyal | a23e686 | 2011-05-19 15:38:20 -0400 | [diff] [blame] | 295 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 296 | blkg_stat_add(&stats->unaccounted_time, unaccounted_time); |
Vivek Goyal | a23e686 | 2011-05-19 15:38:20 -0400 | [diff] [blame] | 297 | #endif |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 298 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 299 | EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 300 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 301 | /* |
| 302 | * should be called under rcu read lock or queue lock to make sure blkg pointer |
| 303 | * is valid. |
| 304 | */ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 305 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 306 | struct blkio_policy_type *pol, |
| 307 | uint64_t bytes, bool direction, bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 308 | { |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 309 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 310 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 311 | struct blkio_group_stats_cpu *stats_cpu; |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 312 | unsigned long flags; |
| 313 | |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 314 | /* If per cpu stats are not allocated yet, don't do any accounting. */ |
| 315 | if (pd->stats_cpu == NULL) |
| 316 | return; |
| 317 | |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 318 | /* |
| 319 | * Disabling interrupts to provide mutual exclusion between two |
| 320 | * writes on same cpu. It probably is not needed for 64bit. Not |
| 321 | * optimizing that case yet. |
| 322 | */ |
| 323 | local_irq_save(flags); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 324 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 325 | stats_cpu = this_cpu_ptr(pd->stats_cpu); |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 326 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 327 | blkg_stat_add(&stats_cpu->sectors, bytes >> 9); |
| 328 | blkg_rwstat_add(&stats_cpu->serviced, rw, 1); |
| 329 | blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes); |
| 330 | |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 331 | local_irq_restore(flags); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 332 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 334 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 335 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 336 | struct blkio_policy_type *pol, |
| 337 | uint64_t start_time, |
| 338 | uint64_t io_start_time, bool direction, |
| 339 | bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 340 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 341 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 342 | unsigned long long now = sched_clock(); |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 343 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 344 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 345 | lockdep_assert_held(blkg->q->queue_lock); |
| 346 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 347 | if (time_after64(now, io_start_time)) |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 348 | blkg_rwstat_add(&stats->service_time, rw, now - io_start_time); |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 349 | if (time_after64(io_start_time, start_time)) |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 350 | blkg_rwstat_add(&stats->wait_time, rw, |
| 351 | io_start_time - start_time); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 352 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 353 | EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 354 | |
Vivek Goyal | 317389a | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 355 | /* Merged stats are per cpu. */ |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 356 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, |
| 357 | struct blkio_policy_type *pol, |
| 358 | bool direction, bool sync) |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 359 | { |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 360 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 361 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 362 | |
Tejun Heo | edf1b87 | 2012-03-08 10:54:00 -0800 | [diff] [blame] | 363 | lockdep_assert_held(blkg->q->queue_lock); |
| 364 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 365 | blkg_rwstat_add(&stats->merged, rw, 1); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 366 | } |
| 367 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); |
| 368 | |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 369 | /* |
| 370 | * Worker for allocating per cpu stat for blk groups. This is scheduled on |
| 371 | * the system_nrt_wq once there are some groups on the alloc_list waiting |
| 372 | * for allocation. |
| 373 | */ |
| 374 | static void blkio_stat_alloc_fn(struct work_struct *work) |
| 375 | { |
| 376 | static void *pcpu_stats[BLKIO_NR_POLICIES]; |
| 377 | struct delayed_work *dwork = to_delayed_work(work); |
| 378 | struct blkio_group *blkg; |
| 379 | int i; |
| 380 | bool empty = false; |
| 381 | |
| 382 | alloc_stats: |
| 383 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 384 | if (pcpu_stats[i] != NULL) |
| 385 | continue; |
| 386 | |
| 387 | pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu); |
| 388 | |
| 389 | /* Allocation failed. Try again after some time. */ |
| 390 | if (pcpu_stats[i] == NULL) { |
| 391 | queue_delayed_work(system_nrt_wq, dwork, |
| 392 | msecs_to_jiffies(10)); |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | spin_lock_irq(&blkio_list_lock); |
| 398 | spin_lock(&alloc_list_lock); |
| 399 | |
| 400 | /* cgroup got deleted or queue exited. */ |
| 401 | if (!list_empty(&alloc_list)) { |
| 402 | blkg = list_first_entry(&alloc_list, struct blkio_group, |
| 403 | alloc_node); |
| 404 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 405 | struct blkg_policy_data *pd = blkg->pd[i]; |
| 406 | |
| 407 | if (blkio_policy[i] && pd && !pd->stats_cpu) |
| 408 | swap(pd->stats_cpu, pcpu_stats[i]); |
| 409 | } |
| 410 | |
| 411 | list_del_init(&blkg->alloc_node); |
| 412 | } |
| 413 | |
| 414 | empty = list_empty(&alloc_list); |
| 415 | |
| 416 | spin_unlock(&alloc_list_lock); |
| 417 | spin_unlock_irq(&blkio_list_lock); |
| 418 | |
| 419 | if (!empty) |
| 420 | goto alloc_stats; |
| 421 | } |
| 422 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 423 | /** |
| 424 | * blkg_free - free a blkg |
| 425 | * @blkg: blkg to free |
| 426 | * |
| 427 | * Free @blkg which may be partially allocated. |
| 428 | */ |
| 429 | static void blkg_free(struct blkio_group *blkg) |
| 430 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 431 | int i; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 432 | |
| 433 | if (!blkg) |
| 434 | return; |
| 435 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 436 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 437 | struct blkg_policy_data *pd = blkg->pd[i]; |
| 438 | |
| 439 | if (pd) { |
| 440 | free_percpu(pd->stats_cpu); |
| 441 | kfree(pd); |
| 442 | } |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 443 | } |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 444 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 445 | kfree(blkg); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
| 449 | * blkg_alloc - allocate a blkg |
| 450 | * @blkcg: block cgroup the new blkg is associated with |
| 451 | * @q: request_queue the new blkg is associated with |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 452 | * |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 453 | * Allocate a new blkg assocating @blkcg and @q. |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 454 | */ |
| 455 | static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 456 | struct request_queue *q) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 457 | { |
| 458 | struct blkio_group *blkg; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 459 | int i; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 460 | |
| 461 | /* alloc and init base part */ |
| 462 | blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node); |
| 463 | if (!blkg) |
| 464 | return NULL; |
| 465 | |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 466 | blkg->q = q; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 467 | INIT_LIST_HEAD(&blkg->q_node); |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 468 | INIT_LIST_HEAD(&blkg->alloc_node); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 469 | blkg->blkcg = blkcg; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 470 | blkg->refcnt = 1; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 471 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
| 472 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 473 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 474 | struct blkio_policy_type *pol = blkio_policy[i]; |
| 475 | struct blkg_policy_data *pd; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 476 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 477 | if (!pol) |
| 478 | continue; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 479 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 480 | /* alloc per-policy data and attach it to blkg */ |
| 481 | pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC, |
| 482 | q->node); |
| 483 | if (!pd) { |
| 484 | blkg_free(blkg); |
| 485 | return NULL; |
| 486 | } |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 487 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 488 | blkg->pd[i] = pd; |
| 489 | pd->blkg = blkg; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 492 | /* invoke per-policy init */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 493 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 494 | struct blkio_policy_type *pol = blkio_policy[i]; |
| 495 | |
| 496 | if (pol) |
| 497 | pol->ops.blkio_init_group_fn(blkg); |
| 498 | } |
| 499 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 500 | return blkg; |
| 501 | } |
| 502 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 503 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
| 504 | struct request_queue *q, |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 505 | bool for_root) |
| 506 | __releases(q->queue_lock) __acquires(q->queue_lock) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 507 | { |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 508 | struct blkio_group *blkg; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 509 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 510 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 511 | lockdep_assert_held(q->queue_lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 512 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 513 | /* |
| 514 | * This could be the first entry point of blkcg implementation and |
| 515 | * we shouldn't allow anything to go through for a bypassing queue. |
| 516 | * The following can be removed if blkg lookup is guaranteed to |
| 517 | * fail on a bypassing queue. |
| 518 | */ |
| 519 | if (unlikely(blk_queue_bypass(q)) && !for_root) |
| 520 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); |
| 521 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 522 | blkg = blkg_lookup(blkcg, q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 523 | if (blkg) |
| 524 | return blkg; |
| 525 | |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 526 | /* blkg holds a reference to blkcg */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 527 | if (!css_tryget(&blkcg->css)) |
| 528 | return ERR_PTR(-EINVAL); |
| 529 | |
| 530 | /* |
| 531 | * Allocate and initialize. |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 532 | */ |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 533 | blkg = blkg_alloc(blkcg, q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 534 | |
| 535 | /* did alloc fail? */ |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 536 | if (unlikely(!blkg)) { |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 537 | blkg = ERR_PTR(-ENOMEM); |
| 538 | goto out; |
| 539 | } |
| 540 | |
| 541 | /* insert */ |
| 542 | spin_lock(&blkcg->lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 543 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 544 | list_add(&blkg->q_node, &q->blkg_list); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 545 | spin_unlock(&blkcg->lock); |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 546 | |
| 547 | spin_lock(&alloc_list_lock); |
| 548 | list_add(&blkg->alloc_node, &alloc_list); |
| 549 | /* Queue per cpu stat allocation from worker thread. */ |
| 550 | queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0); |
| 551 | spin_unlock(&alloc_list_lock); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 552 | out: |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 553 | return blkg; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 554 | } |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 555 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 556 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 557 | /* called under rcu_read_lock(). */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 558 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 559 | struct request_queue *q) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 560 | { |
| 561 | struct blkio_group *blkg; |
| 562 | struct hlist_node *n; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 563 | |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 564 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 565 | if (blkg->q == q) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 566 | return blkg; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 569 | EXPORT_SYMBOL_GPL(blkg_lookup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 570 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 571 | static void blkg_destroy(struct blkio_group *blkg) |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 572 | { |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 573 | struct request_queue *q = blkg->q; |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 574 | struct blkio_cgroup *blkcg = blkg->blkcg; |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 575 | |
| 576 | lockdep_assert_held(q->queue_lock); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 577 | lockdep_assert_held(&blkcg->lock); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 578 | |
| 579 | /* Something wrong if we are trying to remove same group twice */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 580 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 581 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 582 | list_del_init(&blkg->q_node); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 583 | hlist_del_init_rcu(&blkg->blkcg_node); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 584 | |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 585 | spin_lock(&alloc_list_lock); |
| 586 | list_del_init(&blkg->alloc_node); |
| 587 | spin_unlock(&alloc_list_lock); |
| 588 | |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 589 | /* |
| 590 | * Put the reference taken at the time of creation so that when all |
| 591 | * queues are gone, group can be destroyed. |
| 592 | */ |
| 593 | blkg_put(blkg); |
| 594 | } |
| 595 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 596 | /* |
| 597 | * XXX: This updates blkg policy data in-place for root blkg, which is |
| 598 | * necessary across elevator switch and policy registration as root blkgs |
| 599 | * aren't shot down. This broken and racy implementation is temporary. |
| 600 | * Eventually, blkg shoot down will be replaced by proper in-place update. |
| 601 | */ |
| 602 | void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid) |
| 603 | { |
| 604 | struct blkio_policy_type *pol = blkio_policy[plid]; |
| 605 | struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q); |
| 606 | struct blkg_policy_data *pd; |
| 607 | |
| 608 | if (!blkg) |
| 609 | return; |
| 610 | |
| 611 | kfree(blkg->pd[plid]); |
| 612 | blkg->pd[plid] = NULL; |
| 613 | |
| 614 | if (!pol) |
| 615 | return; |
| 616 | |
| 617 | pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL); |
| 618 | WARN_ON_ONCE(!pd); |
| 619 | |
| 620 | pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); |
| 621 | WARN_ON_ONCE(!pd->stats_cpu); |
| 622 | |
| 623 | blkg->pd[plid] = pd; |
| 624 | pd->blkg = blkg; |
| 625 | pol->ops.blkio_init_group_fn(blkg); |
| 626 | } |
| 627 | EXPORT_SYMBOL_GPL(update_root_blkg_pd); |
| 628 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 629 | /** |
| 630 | * blkg_destroy_all - destroy all blkgs associated with a request_queue |
| 631 | * @q: request_queue of interest |
| 632 | * @destroy_root: whether to destroy root blkg or not |
| 633 | * |
| 634 | * Destroy blkgs associated with @q. If @destroy_root is %true, all are |
| 635 | * destroyed; otherwise, root blkg is left alone. |
| 636 | */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 637 | void blkg_destroy_all(struct request_queue *q, bool destroy_root) |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 638 | { |
| 639 | struct blkio_group *blkg, *n; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 640 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 641 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 642 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 643 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
| 644 | struct blkio_cgroup *blkcg = blkg->blkcg; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 645 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 646 | /* skip root? */ |
| 647 | if (!destroy_root && blkg->blkcg == &blkio_root_cgroup) |
| 648 | continue; |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 649 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 650 | spin_lock(&blkcg->lock); |
| 651 | blkg_destroy(blkg); |
| 652 | spin_unlock(&blkcg->lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 653 | } |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 654 | |
| 655 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 656 | } |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 657 | EXPORT_SYMBOL_GPL(blkg_destroy_all); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 658 | |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 659 | static void blkg_rcu_free(struct rcu_head *rcu_head) |
| 660 | { |
| 661 | blkg_free(container_of(rcu_head, struct blkio_group, rcu_head)); |
| 662 | } |
| 663 | |
| 664 | void __blkg_release(struct blkio_group *blkg) |
| 665 | { |
| 666 | /* release the extra blkcg reference this blkg has been holding */ |
| 667 | css_put(&blkg->blkcg->css); |
| 668 | |
| 669 | /* |
| 670 | * A group is freed in rcu manner. But having an rcu lock does not |
| 671 | * mean that one can access all the fields of blkg and assume these |
| 672 | * are valid. For example, don't try to follow throtl_data and |
| 673 | * request queue links. |
| 674 | * |
| 675 | * Having a reference to blkg under an rcu allows acess to only |
| 676 | * values local to groups like group stats and group rate limits |
| 677 | */ |
| 678 | call_rcu(&blkg->rcu_head, blkg_rcu_free); |
| 679 | } |
| 680 | EXPORT_SYMBOL_GPL(__blkg_release); |
| 681 | |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 682 | static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid) |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 683 | { |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 684 | struct blkg_policy_data *pd = blkg->pd[plid]; |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 685 | int cpu; |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 686 | |
| 687 | if (pd->stats_cpu == NULL) |
| 688 | return; |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 689 | |
| 690 | for_each_possible_cpu(cpu) { |
| 691 | struct blkio_group_stats_cpu *sc = |
| 692 | per_cpu_ptr(pd->stats_cpu, cpu); |
| 693 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 694 | blkg_rwstat_reset(&sc->service_bytes); |
| 695 | blkg_rwstat_reset(&sc->serviced); |
| 696 | blkg_stat_reset(&sc->sectors); |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 700 | static int |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 701 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 702 | { |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 703 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 704 | struct blkio_group *blkg; |
| 705 | struct hlist_node *n; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 706 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 707 | spin_lock(&blkio_list_lock); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 708 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 709 | |
| 710 | /* |
| 711 | * Note that stat reset is racy - it doesn't synchronize against |
| 712 | * stat updates. This is a debug feature which shouldn't exist |
| 713 | * anyway. If you get hit by a race, retry. |
| 714 | */ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 715 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 716 | struct blkio_policy_type *pol; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 717 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 718 | list_for_each_entry(pol, &blkio_list, list) { |
| 719 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 720 | struct blkio_group_stats *stats = &pd->stats; |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 721 | |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 722 | /* queued stats shouldn't be cleared */ |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 723 | blkg_rwstat_reset(&stats->merged); |
| 724 | blkg_rwstat_reset(&stats->service_time); |
| 725 | blkg_rwstat_reset(&stats->wait_time); |
| 726 | blkg_stat_reset(&stats->time); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 727 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 728 | blkg_stat_reset(&stats->unaccounted_time); |
| 729 | blkg_stat_reset(&stats->avg_queue_size_sum); |
| 730 | blkg_stat_reset(&stats->avg_queue_size_samples); |
| 731 | blkg_stat_reset(&stats->dequeue); |
| 732 | blkg_stat_reset(&stats->group_wait_time); |
| 733 | blkg_stat_reset(&stats->idle_time); |
| 734 | blkg_stat_reset(&stats->empty_time); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 735 | #endif |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 736 | blkio_reset_stats_cpu(blkg, pol->plid); |
| 737 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 738 | } |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 739 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 740 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 741 | spin_unlock(&blkio_list_lock); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 745 | static const char *blkg_dev_name(struct blkio_group *blkg) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 746 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 747 | /* some drivers (floppy) instantiate a queue w/o disk registered */ |
| 748 | if (blkg->q->backing_dev_info.dev) |
| 749 | return dev_name(blkg->q->backing_dev_info.dev); |
| 750 | return NULL; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 753 | /** |
| 754 | * blkcg_print_blkgs - helper for printing per-blkg data |
| 755 | * @sf: seq_file to print to |
| 756 | * @blkcg: blkcg of interest |
| 757 | * @prfill: fill function to print out a blkg |
| 758 | * @pol: policy in question |
| 759 | * @data: data to be passed to @prfill |
| 760 | * @show_total: to print out sum of prfill return values or not |
| 761 | * |
| 762 | * This function invokes @prfill on each blkg of @blkcg if pd for the |
| 763 | * policy specified by @pol exists. @prfill is invoked with @sf, the |
| 764 | * policy data and @data. If @show_total is %true, the sum of the return |
| 765 | * values from @prfill is printed with "Total" label at the end. |
| 766 | * |
| 767 | * This is to be used to construct print functions for |
| 768 | * cftype->read_seq_string method. |
| 769 | */ |
| 770 | static void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg, |
| 771 | u64 (*prfill)(struct seq_file *, |
| 772 | struct blkg_policy_data *, int), |
| 773 | int pol, int data, bool show_total) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 774 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 775 | struct blkio_group *blkg; |
| 776 | struct hlist_node *n; |
| 777 | u64 total = 0; |
| 778 | |
| 779 | spin_lock_irq(&blkcg->lock); |
| 780 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) |
| 781 | if (blkg->pd[pol]) |
| 782 | total += prfill(sf, blkg->pd[pol], data); |
| 783 | spin_unlock_irq(&blkcg->lock); |
| 784 | |
| 785 | if (show_total) |
| 786 | seq_printf(sf, "Total %llu\n", (unsigned long long)total); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * __blkg_prfill_u64 - prfill helper for a single u64 value |
| 791 | * @sf: seq_file to print to |
| 792 | * @pd: policy data of interest |
| 793 | * @v: value to print |
| 794 | * |
| 795 | * Print @v to @sf for the device assocaited with @pd. |
| 796 | */ |
| 797 | static u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, |
| 798 | u64 v) |
| 799 | { |
| 800 | const char *dname = blkg_dev_name(pd->blkg); |
| 801 | |
| 802 | if (!dname) |
| 803 | return 0; |
| 804 | |
| 805 | seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v); |
| 806 | return v; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat |
| 811 | * @sf: seq_file to print to |
| 812 | * @pd: policy data of interest |
| 813 | * @rwstat: rwstat to print |
| 814 | * |
| 815 | * Print @rwstat to @sf for the device assocaited with @pd. |
| 816 | */ |
| 817 | static u64 __blkg_prfill_rwstat(struct seq_file *sf, |
| 818 | struct blkg_policy_data *pd, |
| 819 | const struct blkg_rwstat *rwstat) |
| 820 | { |
| 821 | static const char *rwstr[] = { |
| 822 | [BLKG_RWSTAT_READ] = "Read", |
| 823 | [BLKG_RWSTAT_WRITE] = "Write", |
| 824 | [BLKG_RWSTAT_SYNC] = "Sync", |
| 825 | [BLKG_RWSTAT_ASYNC] = "Async", |
| 826 | }; |
| 827 | const char *dname = blkg_dev_name(pd->blkg); |
| 828 | u64 v; |
| 829 | int i; |
| 830 | |
| 831 | if (!dname) |
| 832 | return 0; |
| 833 | |
| 834 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
| 835 | seq_printf(sf, "%s %s %llu\n", dname, rwstr[i], |
| 836 | (unsigned long long)rwstat->cnt[i]); |
| 837 | |
| 838 | v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE]; |
| 839 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); |
| 840 | return v; |
| 841 | } |
| 842 | |
| 843 | static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, |
| 844 | int off) |
| 845 | { |
| 846 | return __blkg_prfill_u64(sf, pd, |
| 847 | blkg_stat_read((void *)&pd->stats + off)); |
| 848 | } |
| 849 | |
| 850 | static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, |
| 851 | int off) |
| 852 | { |
| 853 | struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off); |
| 854 | |
| 855 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
| 856 | } |
| 857 | |
| 858 | /* print blkg_stat specified by BLKCG_STAT_PRIV() */ |
| 859 | static int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft, |
| 860 | struct seq_file *sf) |
| 861 | { |
| 862 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 863 | |
| 864 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, |
| 865 | BLKCG_STAT_POL(cft->private), |
| 866 | BLKCG_STAT_OFF(cft->private), false); |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* print blkg_rwstat specified by BLKCG_STAT_PRIV() */ |
| 871 | static int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft, |
| 872 | struct seq_file *sf) |
| 873 | { |
| 874 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 875 | |
| 876 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, |
| 877 | BLKCG_STAT_POL(cft->private), |
| 878 | BLKCG_STAT_OFF(cft->private), true); |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | static u64 blkg_prfill_cpu_stat(struct seq_file *sf, |
| 883 | struct blkg_policy_data *pd, int off) |
| 884 | { |
| 885 | u64 v = 0; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 886 | int cpu; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 887 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 888 | for_each_possible_cpu(cpu) { |
| 889 | struct blkio_group_stats_cpu *sc = |
| 890 | per_cpu_ptr(pd->stats_cpu, cpu); |
| 891 | |
| 892 | v += blkg_stat_read((void *)sc + off); |
| 893 | } |
| 894 | |
| 895 | return __blkg_prfill_u64(sf, pd, v); |
| 896 | } |
| 897 | |
| 898 | static u64 blkg_prfill_cpu_rwstat(struct seq_file *sf, |
| 899 | struct blkg_policy_data *pd, int off) |
| 900 | { |
| 901 | struct blkg_rwstat rwstat = { }, tmp; |
| 902 | int i, cpu; |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 903 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 904 | for_each_possible_cpu(cpu) { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 905 | struct blkio_group_stats_cpu *sc = |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 906 | per_cpu_ptr(pd->stats_cpu, cpu); |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 907 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 908 | tmp = blkg_rwstat_read((void *)sc + off); |
| 909 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
| 910 | rwstat.cnt[i] += tmp.cnt[i]; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 911 | } |
| 912 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 913 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 914 | } |
| 915 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 916 | /* print per-cpu blkg_stat specified by BLKCG_STAT_PRIV() */ |
| 917 | static int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft, |
| 918 | struct seq_file *sf) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 919 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 920 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 921 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 922 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_stat, |
| 923 | BLKCG_STAT_POL(cft->private), |
| 924 | BLKCG_STAT_OFF(cft->private), false); |
| 925 | return 0; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 926 | } |
| 927 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 928 | /* print per-cpu blkg_rwstat specified by BLKCG_STAT_PRIV() */ |
| 929 | static int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft, |
| 930 | struct seq_file *sf) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 931 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 932 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 933 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 934 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_rwstat, |
| 935 | BLKCG_STAT_POL(cft->private), |
| 936 | BLKCG_STAT_OFF(cft->private), true); |
| 937 | return 0; |
| 938 | } |
| 939 | |
Justin TerAvest | 9026e52 | 2011-03-22 21:26:54 +0100 | [diff] [blame] | 940 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 941 | static u64 blkg_prfill_avg_queue_size(struct seq_file *sf, |
| 942 | struct blkg_policy_data *pd, int off) |
| 943 | { |
| 944 | u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples); |
| 945 | u64 v = 0; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 946 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 947 | if (samples) { |
| 948 | v = blkg_stat_read(&pd->stats.avg_queue_size_sum); |
| 949 | do_div(v, samples); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 950 | } |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 951 | __blkg_prfill_u64(sf, pd, v); |
| 952 | return 0; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 955 | /* print avg_queue_size */ |
| 956 | static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft, |
| 957 | struct seq_file *sf) |
| 958 | { |
| 959 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 960 | |
| 961 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size, |
| 962 | BLKIO_POLICY_PROP, 0, false); |
| 963 | return 0; |
| 964 | } |
| 965 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
| 966 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 967 | struct blkg_conf_ctx { |
| 968 | struct gendisk *disk; |
| 969 | struct blkio_group *blkg; |
| 970 | u64 v; |
| 971 | }; |
| 972 | |
| 973 | /** |
| 974 | * blkg_conf_prep - parse and prepare for per-blkg config update |
| 975 | * @blkcg: target block cgroup |
| 976 | * @input: input string |
| 977 | * @ctx: blkg_conf_ctx to be filled |
| 978 | * |
| 979 | * Parse per-blkg config update from @input and initialize @ctx with the |
| 980 | * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new |
| 981 | * value. This function returns with RCU read locked and must be paired |
| 982 | * with blkg_conf_finish(). |
| 983 | */ |
| 984 | static int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input, |
| 985 | struct blkg_conf_ctx *ctx) |
| 986 | __acquires(rcu) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 987 | { |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 988 | struct gendisk *disk; |
| 989 | struct blkio_group *blkg; |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame^] | 990 | unsigned int major, minor; |
| 991 | unsigned long long v; |
| 992 | int part, ret; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 993 | |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame^] | 994 | if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3) |
| 995 | return -EINVAL; |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 996 | |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame^] | 997 | disk = get_gendisk(MKDEV(major, minor), &part); |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 998 | if (!disk || part) |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame^] | 999 | return -EINVAL; |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 1000 | |
| 1001 | rcu_read_lock(); |
| 1002 | |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 1003 | spin_lock_irq(disk->queue->queue_lock); |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1004 | blkg = blkg_lookup_create(blkcg, disk->queue, false); |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 1005 | spin_unlock_irq(disk->queue->queue_lock); |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 1006 | |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 1007 | if (IS_ERR(blkg)) { |
| 1008 | ret = PTR_ERR(blkg); |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1009 | rcu_read_unlock(); |
| 1010 | put_disk(disk); |
| 1011 | /* |
| 1012 | * If queue was bypassing, we should retry. Do so after a |
| 1013 | * short msleep(). It isn't strictly necessary but queue |
| 1014 | * can be bypassing for some time and it's always nice to |
| 1015 | * avoid busy looping. |
| 1016 | */ |
| 1017 | if (ret == -EBUSY) { |
| 1018 | msleep(10); |
| 1019 | ret = restart_syscall(); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1020 | } |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame^] | 1021 | return ret; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1022 | } |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 1023 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1024 | ctx->disk = disk; |
| 1025 | ctx->blkg = blkg; |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame^] | 1026 | ctx->v = v; |
| 1027 | return 0; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1030 | /** |
| 1031 | * blkg_conf_finish - finish up per-blkg config update |
| 1032 | * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep() |
| 1033 | * |
| 1034 | * Finish up after per-blkg config update. This function must be paired |
| 1035 | * with blkg_conf_prep(). |
| 1036 | */ |
| 1037 | static void blkg_conf_finish(struct blkg_conf_ctx *ctx) |
| 1038 | __releases(rcu) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1039 | { |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1040 | rcu_read_unlock(); |
| 1041 | put_disk(ctx->disk); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1044 | /* for propio conf */ |
| 1045 | static u64 blkg_prfill_weight_device(struct seq_file *sf, |
| 1046 | struct blkg_policy_data *pd, int off) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1047 | { |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1048 | if (!pd->conf.weight) |
| 1049 | return 0; |
| 1050 | return __blkg_prfill_u64(sf, pd, pd->conf.weight); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1051 | } |
| 1052 | |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1053 | static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft, |
| 1054 | struct seq_file *sf) |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1055 | { |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1056 | blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), |
| 1057 | blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0, |
| 1058 | false); |
| 1059 | return 0; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1062 | static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft, |
| 1063 | struct seq_file *sf) |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1064 | { |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1065 | seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1069 | static int blkcg_set_weight_device(struct cgroup *cgrp, struct cftype *cft, |
| 1070 | const char *buf) |
| 1071 | { |
| 1072 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1073 | struct blkg_policy_data *pd; |
| 1074 | struct blkg_conf_ctx ctx; |
| 1075 | int ret; |
| 1076 | |
| 1077 | ret = blkg_conf_prep(blkcg, buf, &ctx); |
| 1078 | if (ret) |
| 1079 | return ret; |
| 1080 | |
| 1081 | ret = -EINVAL; |
| 1082 | pd = ctx.blkg->pd[BLKIO_POLICY_PROP]; |
| 1083 | if (pd && (!ctx.v || (ctx.v >= BLKIO_WEIGHT_MIN && |
| 1084 | ctx.v <= BLKIO_WEIGHT_MAX))) { |
| 1085 | pd->conf.weight = ctx.v; |
| 1086 | blkio_update_group_weight(ctx.blkg, BLKIO_POLICY_PROP, |
| 1087 | ctx.v ?: blkcg->weight); |
| 1088 | ret = 0; |
| 1089 | } |
| 1090 | |
| 1091 | blkg_conf_finish(&ctx); |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
Tejun Heo | 627f29f | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1095 | static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val) |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1096 | { |
Tejun Heo | 627f29f | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1097 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1098 | struct blkio_group *blkg; |
| 1099 | struct hlist_node *n; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1100 | |
| 1101 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) |
| 1102 | return -EINVAL; |
| 1103 | |
| 1104 | spin_lock(&blkio_list_lock); |
| 1105 | spin_lock_irq(&blkcg->lock); |
| 1106 | blkcg->weight = (unsigned int)val; |
| 1107 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 1108 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | 627f29f | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1109 | struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP]; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 1110 | |
Tejun Heo | 627f29f | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1111 | if (pd && !pd->conf.weight) |
| 1112 | blkio_update_group_weight(blkg, BLKIO_POLICY_PROP, |
| 1113 | blkcg->weight); |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 1114 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1115 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1116 | spin_unlock_irq(&blkcg->lock); |
| 1117 | spin_unlock(&blkio_list_lock); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1121 | /* for blk-throttle conf */ |
| 1122 | #ifdef CONFIG_BLK_DEV_THROTTLING |
| 1123 | static u64 blkg_prfill_conf_u64(struct seq_file *sf, |
| 1124 | struct blkg_policy_data *pd, int off) |
| 1125 | { |
| 1126 | u64 v = *(u64 *)((void *)&pd->conf + off); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1127 | |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1128 | if (!v) |
| 1129 | return 0; |
| 1130 | return __blkg_prfill_u64(sf, pd, v); |
| 1131 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1132 | |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1133 | static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
| 1134 | struct seq_file *sf) |
| 1135 | { |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1136 | blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), |
| 1137 | blkg_prfill_conf_u64, BLKIO_POLICY_THROTL, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1138 | cft->private, false); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1141 | |
| 1142 | static int blkcg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
| 1143 | const char *buf, int rw, |
| 1144 | void (*update)(struct blkio_group *, int, u64, int)) |
| 1145 | { |
| 1146 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1147 | struct blkg_policy_data *pd; |
| 1148 | struct blkg_conf_ctx ctx; |
| 1149 | int ret; |
| 1150 | |
| 1151 | ret = blkg_conf_prep(blkcg, buf, &ctx); |
| 1152 | if (ret) |
| 1153 | return ret; |
| 1154 | |
| 1155 | ret = -EINVAL; |
| 1156 | pd = ctx.blkg->pd[BLKIO_POLICY_THROTL]; |
| 1157 | if (pd) { |
| 1158 | *(u64 *)((void *)&pd->conf + cft->private) = ctx.v; |
| 1159 | update(ctx.blkg, BLKIO_POLICY_THROTL, ctx.v ?: -1, rw); |
| 1160 | ret = 0; |
| 1161 | } |
| 1162 | |
| 1163 | blkg_conf_finish(&ctx); |
| 1164 | return ret; |
| 1165 | } |
| 1166 | |
| 1167 | static int blkcg_set_conf_bps_r(struct cgroup *cgrp, struct cftype *cft, |
| 1168 | const char *buf) |
| 1169 | { |
| 1170 | return blkcg_set_conf_u64(cgrp, cft, buf, READ, blkio_update_group_bps); |
| 1171 | } |
| 1172 | |
| 1173 | static int blkcg_set_conf_bps_w(struct cgroup *cgrp, struct cftype *cft, |
| 1174 | const char *buf) |
| 1175 | { |
| 1176 | return blkcg_set_conf_u64(cgrp, cft, buf, WRITE, blkio_update_group_bps); |
| 1177 | } |
| 1178 | |
| 1179 | static int blkcg_set_conf_iops_r(struct cgroup *cgrp, struct cftype *cft, |
| 1180 | const char *buf) |
| 1181 | { |
| 1182 | return blkcg_set_conf_u64(cgrp, cft, buf, READ, blkio_update_group_iops); |
| 1183 | } |
| 1184 | |
| 1185 | static int blkcg_set_conf_iops_w(struct cgroup *cgrp, struct cftype *cft, |
| 1186 | const char *buf) |
| 1187 | { |
| 1188 | return blkcg_set_conf_u64(cgrp, cft, buf, WRITE, blkio_update_group_iops); |
| 1189 | } |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1190 | #endif |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1191 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1192 | struct cftype blkio_files[] = { |
| 1193 | { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1194 | .name = "weight_device", |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1195 | .read_seq_string = blkcg_print_weight_device, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1196 | .write_string = blkcg_set_weight_device, |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1197 | .max_write_len = 256, |
| 1198 | }, |
| 1199 | { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1200 | .name = "weight", |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1201 | .read_seq_string = blkcg_print_weight, |
Tejun Heo | 627f29f | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1202 | .write_u64 = blkcg_set_weight, |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1203 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1204 | { |
| 1205 | .name = "time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1206 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1207 | offsetof(struct blkio_group_stats, time)), |
| 1208 | .read_seq_string = blkcg_print_stat, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1209 | }, |
| 1210 | { |
| 1211 | .name = "sectors", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1212 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1213 | offsetof(struct blkio_group_stats_cpu, sectors)), |
| 1214 | .read_seq_string = blkcg_print_cpu_stat, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1215 | }, |
| 1216 | { |
| 1217 | .name = "io_service_bytes", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1218 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1219 | offsetof(struct blkio_group_stats_cpu, service_bytes)), |
| 1220 | .read_seq_string = blkcg_print_cpu_rwstat, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1221 | }, |
| 1222 | { |
| 1223 | .name = "io_serviced", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1224 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1225 | offsetof(struct blkio_group_stats_cpu, serviced)), |
| 1226 | .read_seq_string = blkcg_print_cpu_rwstat, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1227 | }, |
| 1228 | { |
| 1229 | .name = "io_service_time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1230 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1231 | offsetof(struct blkio_group_stats, service_time)), |
| 1232 | .read_seq_string = blkcg_print_rwstat, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1233 | }, |
| 1234 | { |
| 1235 | .name = "io_wait_time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1236 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1237 | offsetof(struct blkio_group_stats, wait_time)), |
| 1238 | .read_seq_string = blkcg_print_rwstat, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 1239 | }, |
| 1240 | { |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 1241 | .name = "io_merged", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1242 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1243 | offsetof(struct blkio_group_stats, merged)), |
| 1244 | .read_seq_string = blkcg_print_rwstat, |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 1245 | }, |
| 1246 | { |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1247 | .name = "io_queued", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1248 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1249 | offsetof(struct blkio_group_stats, queued)), |
| 1250 | .read_seq_string = blkcg_print_rwstat, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1251 | }, |
| 1252 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 1253 | .name = "reset_stats", |
| 1254 | .write_u64 = blkiocg_reset_stats, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1255 | }, |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1256 | #ifdef CONFIG_BLK_DEV_THROTTLING |
| 1257 | { |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1258 | .name = "throttle.read_bps_device", |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1259 | .private = offsetof(struct blkio_group_conf, bps[READ]), |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1260 | .read_seq_string = blkcg_print_conf_u64, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1261 | .write_string = blkcg_set_conf_bps_r, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1262 | .max_write_len = 256, |
| 1263 | }, |
| 1264 | |
| 1265 | { |
| 1266 | .name = "throttle.write_bps_device", |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1267 | .private = offsetof(struct blkio_group_conf, bps[WRITE]), |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1268 | .read_seq_string = blkcg_print_conf_u64, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1269 | .write_string = blkcg_set_conf_bps_w, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1270 | .max_write_len = 256, |
| 1271 | }, |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1272 | |
| 1273 | { |
| 1274 | .name = "throttle.read_iops_device", |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1275 | .private = offsetof(struct blkio_group_conf, iops[READ]), |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1276 | .read_seq_string = blkcg_print_conf_u64, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1277 | .write_string = blkcg_set_conf_iops_r, |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1278 | .max_write_len = 256, |
| 1279 | }, |
| 1280 | |
| 1281 | { |
| 1282 | .name = "throttle.write_iops_device", |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1283 | .private = offsetof(struct blkio_group_conf, iops[WRITE]), |
Tejun Heo | c4682ae | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1284 | .read_seq_string = blkcg_print_conf_u64, |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1285 | .write_string = blkcg_set_conf_iops_w, |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1286 | .max_write_len = 256, |
| 1287 | }, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1288 | { |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1289 | .name = "throttle.io_service_bytes", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1290 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
| 1291 | offsetof(struct blkio_group_stats_cpu, service_bytes)), |
| 1292 | .read_seq_string = blkcg_print_cpu_rwstat, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1293 | }, |
| 1294 | { |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1295 | .name = "throttle.io_serviced", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1296 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
| 1297 | offsetof(struct blkio_group_stats_cpu, serviced)), |
| 1298 | .read_seq_string = blkcg_print_cpu_rwstat, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1299 | }, |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1300 | #endif /* CONFIG_BLK_DEV_THROTTLING */ |
| 1301 | |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1302 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1303 | { |
| 1304 | .name = "avg_queue_size", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1305 | .read_seq_string = blkcg_print_avg_queue_size, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1306 | }, |
| 1307 | { |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1308 | .name = "group_wait_time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1309 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1310 | offsetof(struct blkio_group_stats, group_wait_time)), |
| 1311 | .read_seq_string = blkcg_print_stat, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1312 | }, |
| 1313 | { |
| 1314 | .name = "idle_time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1315 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1316 | offsetof(struct blkio_group_stats, idle_time)), |
| 1317 | .read_seq_string = blkcg_print_stat, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1318 | }, |
| 1319 | { |
| 1320 | .name = "empty_time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1321 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1322 | offsetof(struct blkio_group_stats, empty_time)), |
| 1323 | .read_seq_string = blkcg_print_stat, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1324 | }, |
| 1325 | { |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1326 | .name = "dequeue", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1327 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1328 | offsetof(struct blkio_group_stats, dequeue)), |
| 1329 | .read_seq_string = blkcg_print_stat, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1330 | }, |
Justin TerAvest | 9026e52 | 2011-03-22 21:26:54 +0100 | [diff] [blame] | 1331 | { |
| 1332 | .name = "unaccounted_time", |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 1333 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
| 1334 | offsetof(struct blkio_group_stats, unaccounted_time)), |
| 1335 | .read_seq_string = blkcg_print_stat, |
Justin TerAvest | 9026e52 | 2011-03-22 21:26:54 +0100 | [diff] [blame] | 1336 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1337 | #endif |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1338 | { } /* terminate */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1339 | }; |
| 1340 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1341 | /** |
| 1342 | * blkiocg_pre_destroy - cgroup pre_destroy callback |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1343 | * @cgroup: cgroup of interest |
| 1344 | * |
| 1345 | * This function is called when @cgroup is about to go away and responsible |
| 1346 | * for shooting down all blkgs associated with @cgroup. blkgs should be |
| 1347 | * removed while holding both q and blkcg locks. As blkcg lock is nested |
| 1348 | * inside q lock, this function performs reverse double lock dancing. |
| 1349 | * |
| 1350 | * This is the blkcg counterpart of ioc_release_fn(). |
| 1351 | */ |
Tejun Heo | 959d851 | 2012-04-01 12:30:01 -0700 | [diff] [blame] | 1352 | static int blkiocg_pre_destroy(struct cgroup *cgroup) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1353 | { |
| 1354 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 1355 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1356 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 1357 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1358 | while (!hlist_empty(&blkcg->blkg_list)) { |
| 1359 | struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first, |
| 1360 | struct blkio_group, blkcg_node); |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 1361 | struct request_queue *q = blkg->q; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1362 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1363 | if (spin_trylock(q->queue_lock)) { |
| 1364 | blkg_destroy(blkg); |
| 1365 | spin_unlock(q->queue_lock); |
| 1366 | } else { |
| 1367 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1368 | cpu_relax(); |
Dan Carpenter | a556793 | 2012-03-29 20:57:08 +0200 | [diff] [blame] | 1369 | spin_lock_irq(&blkcg->lock); |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1370 | } |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1371 | } |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1372 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 1373 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 1374 | return 0; |
| 1375 | } |
| 1376 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1377 | static void blkiocg_destroy(struct cgroup *cgroup) |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 1378 | { |
| 1379 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 1380 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1381 | if (blkcg != &blkio_root_cgroup) |
| 1382 | kfree(blkcg); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1383 | } |
| 1384 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1385 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1386 | { |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 1387 | static atomic64_t id_seq = ATOMIC64_INIT(0); |
Li Zefan | 0341509 | 2010-05-07 08:57:00 +0200 | [diff] [blame] | 1388 | struct blkio_cgroup *blkcg; |
| 1389 | struct cgroup *parent = cgroup->parent; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1390 | |
Li Zefan | 0341509 | 2010-05-07 08:57:00 +0200 | [diff] [blame] | 1391 | if (!parent) { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1392 | blkcg = &blkio_root_cgroup; |
| 1393 | goto done; |
| 1394 | } |
| 1395 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1396 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
| 1397 | if (!blkcg) |
| 1398 | return ERR_PTR(-ENOMEM); |
| 1399 | |
| 1400 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 1401 | blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1402 | done: |
| 1403 | spin_lock_init(&blkcg->lock); |
| 1404 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
| 1405 | |
| 1406 | return &blkcg->css; |
| 1407 | } |
| 1408 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 1409 | /** |
| 1410 | * blkcg_init_queue - initialize blkcg part of request queue |
| 1411 | * @q: request_queue to initialize |
| 1412 | * |
| 1413 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg |
| 1414 | * part of new request_queue @q. |
| 1415 | * |
| 1416 | * RETURNS: |
| 1417 | * 0 on success, -errno on failure. |
| 1418 | */ |
| 1419 | int blkcg_init_queue(struct request_queue *q) |
| 1420 | { |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1421 | int ret; |
| 1422 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 1423 | might_sleep(); |
| 1424 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1425 | ret = blk_throtl_init(q); |
| 1426 | if (ret) |
| 1427 | return ret; |
| 1428 | |
| 1429 | mutex_lock(&all_q_mutex); |
| 1430 | INIT_LIST_HEAD(&q->all_q_node); |
| 1431 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1432 | mutex_unlock(&all_q_mutex); |
| 1433 | |
| 1434 | return 0; |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * blkcg_drain_queue - drain blkcg part of request_queue |
| 1439 | * @q: request_queue to drain |
| 1440 | * |
| 1441 | * Called from blk_drain_queue(). Responsible for draining blkcg part. |
| 1442 | */ |
| 1443 | void blkcg_drain_queue(struct request_queue *q) |
| 1444 | { |
| 1445 | lockdep_assert_held(q->queue_lock); |
| 1446 | |
| 1447 | blk_throtl_drain(q); |
| 1448 | } |
| 1449 | |
| 1450 | /** |
| 1451 | * blkcg_exit_queue - exit and release blkcg part of request_queue |
| 1452 | * @q: request_queue being released |
| 1453 | * |
| 1454 | * Called from blk_release_queue(). Responsible for exiting blkcg part. |
| 1455 | */ |
| 1456 | void blkcg_exit_queue(struct request_queue *q) |
| 1457 | { |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1458 | mutex_lock(&all_q_mutex); |
| 1459 | list_del_init(&q->all_q_node); |
| 1460 | mutex_unlock(&all_q_mutex); |
| 1461 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1462 | blkg_destroy_all(q, true); |
| 1463 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 1464 | blk_throtl_exit(q); |
| 1465 | } |
| 1466 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1467 | /* |
| 1468 | * We cannot support shared io contexts, as we have no mean to support |
| 1469 | * two tasks with the same ioc in two different groups without major rework |
| 1470 | * of the main cic data structures. For now we allow a task to change |
| 1471 | * its cgroup only if it's the only owner of its ioc. |
| 1472 | */ |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1473 | static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1474 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1475 | struct task_struct *task; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1476 | struct io_context *ioc; |
| 1477 | int ret = 0; |
| 1478 | |
| 1479 | /* task_lock() is needed to avoid races with exit_io_context() */ |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1480 | cgroup_taskset_for_each(task, cgrp, tset) { |
| 1481 | task_lock(task); |
| 1482 | ioc = task->io_context; |
| 1483 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) |
| 1484 | ret = -EINVAL; |
| 1485 | task_unlock(task); |
| 1486 | if (ret) |
| 1487 | break; |
| 1488 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1489 | return ret; |
| 1490 | } |
| 1491 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1492 | static void blkcg_bypass_start(void) |
| 1493 | __acquires(&all_q_mutex) |
| 1494 | { |
| 1495 | struct request_queue *q; |
| 1496 | |
| 1497 | mutex_lock(&all_q_mutex); |
| 1498 | |
| 1499 | list_for_each_entry(q, &all_q_list, all_q_node) { |
| 1500 | blk_queue_bypass_start(q); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1501 | blkg_destroy_all(q, false); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | static void blkcg_bypass_end(void) |
| 1506 | __releases(&all_q_mutex) |
| 1507 | { |
| 1508 | struct request_queue *q; |
| 1509 | |
| 1510 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1511 | blk_queue_bypass_end(q); |
| 1512 | |
| 1513 | mutex_unlock(&all_q_mutex); |
| 1514 | } |
| 1515 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1516 | struct cgroup_subsys blkio_subsys = { |
| 1517 | .name = "blkio", |
| 1518 | .create = blkiocg_create, |
| 1519 | .can_attach = blkiocg_can_attach, |
Tejun Heo | 959d851 | 2012-04-01 12:30:01 -0700 | [diff] [blame] | 1520 | .pre_destroy = blkiocg_pre_destroy, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1521 | .destroy = blkiocg_destroy, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1522 | .subsys_id = blkio_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1523 | .base_cftypes = blkio_files, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1524 | .module = THIS_MODULE, |
| 1525 | }; |
| 1526 | EXPORT_SYMBOL_GPL(blkio_subsys); |
| 1527 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1528 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
| 1529 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1530 | struct request_queue *q; |
| 1531 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1532 | blkcg_bypass_start(); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1533 | spin_lock(&blkio_list_lock); |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 1534 | |
| 1535 | BUG_ON(blkio_policy[blkiop->plid]); |
| 1536 | blkio_policy[blkiop->plid] = blkiop; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1537 | list_add_tail(&blkiop->list, &blkio_list); |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 1538 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1539 | spin_unlock(&blkio_list_lock); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1540 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1541 | update_root_blkg_pd(q, blkiop->plid); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1542 | blkcg_bypass_end(); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1543 | } |
| 1544 | EXPORT_SYMBOL_GPL(blkio_policy_register); |
| 1545 | |
| 1546 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) |
| 1547 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1548 | struct request_queue *q; |
| 1549 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1550 | blkcg_bypass_start(); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1551 | spin_lock(&blkio_list_lock); |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 1552 | |
| 1553 | BUG_ON(blkio_policy[blkiop->plid] != blkiop); |
| 1554 | blkio_policy[blkiop->plid] = NULL; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1555 | list_del_init(&blkiop->list); |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 1556 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1557 | spin_unlock(&blkio_list_lock); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1558 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1559 | update_root_blkg_pd(q, blkiop->plid); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 1560 | blkcg_bypass_end(); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1561 | } |
| 1562 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |