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> |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 20 | #include "blk-cgroup.h" |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 21 | #include <linux/genhd.h> |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 22 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 23 | #define MAX_KEY_LEN 100 |
| 24 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 25 | static DEFINE_SPINLOCK(blkio_list_lock); |
| 26 | static LIST_HEAD(blkio_list); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 27 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 28 | struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT }; |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 29 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
| 30 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 31 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *, |
| 32 | struct cgroup *); |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 33 | static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *, |
| 34 | struct cgroup_taskset *); |
| 35 | static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *, |
| 36 | struct cgroup_taskset *); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 37 | static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *); |
| 38 | static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *); |
| 39 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 40 | /* for encoding cft->private value on file */ |
| 41 | #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 42 | /* What policy owns the file, proportional or throttle */ |
| 43 | #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff) |
| 44 | #define BLKIOFILE_ATTR(val) ((val) & 0xffff) |
| 45 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 46 | struct cgroup_subsys blkio_subsys = { |
| 47 | .name = "blkio", |
| 48 | .create = blkiocg_create, |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 49 | .can_attach = blkiocg_can_attach, |
| 50 | .attach = blkiocg_attach, |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 51 | .destroy = blkiocg_destroy, |
| 52 | .populate = blkiocg_populate, |
| 53 | #ifdef CONFIG_BLK_CGROUP |
| 54 | /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */ |
| 55 | .subsys_id = blkio_subsys_id, |
| 56 | #endif |
| 57 | .use_id = 1, |
| 58 | .module = THIS_MODULE, |
| 59 | }; |
| 60 | EXPORT_SYMBOL_GPL(blkio_subsys); |
| 61 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 62 | static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg, |
| 63 | struct blkio_policy_node *pn) |
| 64 | { |
| 65 | list_add(&pn->node, &blkcg->policy_list); |
| 66 | } |
| 67 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 68 | static inline bool cftype_blkg_same_policy(struct cftype *cft, |
| 69 | struct blkio_group *blkg) |
| 70 | { |
| 71 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 72 | |
| 73 | if (blkg->plid == plid) |
| 74 | return 1; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* Determines if policy node matches cgroup file being accessed */ |
| 80 | static inline bool pn_matches_cftype(struct cftype *cft, |
| 81 | struct blkio_policy_node *pn) |
| 82 | { |
| 83 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 84 | int fileid = BLKIOFILE_ATTR(cft->private); |
| 85 | |
| 86 | return (plid == pn->plid && fileid == pn->fileid); |
| 87 | } |
| 88 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 89 | /* Must be called with blkcg->lock held */ |
| 90 | static inline void blkio_policy_delete_node(struct blkio_policy_node *pn) |
| 91 | { |
| 92 | list_del(&pn->node); |
| 93 | } |
| 94 | |
| 95 | /* Must be called with blkcg->lock held */ |
| 96 | static struct blkio_policy_node * |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 97 | blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev, |
| 98 | enum blkio_policy_id plid, int fileid) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 99 | { |
| 100 | struct blkio_policy_node *pn; |
| 101 | |
| 102 | list_for_each_entry(pn, &blkcg->policy_list, node) { |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 103 | if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 104 | return pn; |
| 105 | } |
| 106 | |
| 107 | return NULL; |
| 108 | } |
| 109 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 110 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
| 111 | { |
| 112 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), |
| 113 | struct blkio_cgroup, css); |
| 114 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 115 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 116 | |
Vivek Goyal | 70087dc | 2011-05-16 15:24:08 +0200 | [diff] [blame] | 117 | struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk) |
| 118 | { |
| 119 | return container_of(task_subsys_state(tsk, blkio_subsys_id), |
| 120 | struct blkio_cgroup, css); |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(task_blkio_cgroup); |
| 123 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 124 | static inline void |
| 125 | blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight) |
| 126 | { |
| 127 | struct blkio_policy_type *blkiop; |
| 128 | |
| 129 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 130 | /* If this policy does not own the blkg, do not send updates */ |
| 131 | if (blkiop->plid != blkg->plid) |
| 132 | continue; |
| 133 | if (blkiop->ops.blkio_update_group_weight_fn) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 134 | blkiop->ops.blkio_update_group_weight_fn(blkg->key, |
| 135 | blkg, weight); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 139 | static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps, |
| 140 | int fileid) |
| 141 | { |
| 142 | struct blkio_policy_type *blkiop; |
| 143 | |
| 144 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 145 | |
| 146 | /* If this policy does not own the blkg, do not send updates */ |
| 147 | if (blkiop->plid != blkg->plid) |
| 148 | continue; |
| 149 | |
| 150 | if (fileid == BLKIO_THROTL_read_bps_device |
| 151 | && blkiop->ops.blkio_update_group_read_bps_fn) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 152 | blkiop->ops.blkio_update_group_read_bps_fn(blkg->key, |
| 153 | blkg, bps); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 154 | |
| 155 | if (fileid == BLKIO_THROTL_write_bps_device |
| 156 | && blkiop->ops.blkio_update_group_write_bps_fn) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 157 | blkiop->ops.blkio_update_group_write_bps_fn(blkg->key, |
| 158 | blkg, bps); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 162 | static inline void blkio_update_group_iops(struct blkio_group *blkg, |
| 163 | unsigned int iops, int fileid) |
| 164 | { |
| 165 | struct blkio_policy_type *blkiop; |
| 166 | |
| 167 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 168 | |
| 169 | /* If this policy does not own the blkg, do not send updates */ |
| 170 | if (blkiop->plid != blkg->plid) |
| 171 | continue; |
| 172 | |
| 173 | if (fileid == BLKIO_THROTL_read_iops_device |
| 174 | && blkiop->ops.blkio_update_group_read_iops_fn) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 175 | blkiop->ops.blkio_update_group_read_iops_fn(blkg->key, |
| 176 | blkg, iops); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 177 | |
| 178 | if (fileid == BLKIO_THROTL_write_iops_device |
| 179 | && blkiop->ops.blkio_update_group_write_iops_fn) |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 180 | blkiop->ops.blkio_update_group_write_iops_fn(blkg->key, |
| 181 | blkg,iops); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 185 | /* |
| 186 | * Add to the appropriate stat variable depending on the request type. |
| 187 | * This should be called with the blkg->stats_lock held. |
| 188 | */ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 189 | static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction, |
| 190 | bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 191 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 192 | if (direction) |
| 193 | stat[BLKIO_STAT_WRITE] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 194 | else |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 195 | stat[BLKIO_STAT_READ] += add; |
| 196 | if (sync) |
| 197 | stat[BLKIO_STAT_SYNC] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 198 | else |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 199 | stat[BLKIO_STAT_ASYNC] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Decrements the appropriate stat variable if non-zero depending on the |
| 204 | * request type. Panics on value being zero. |
| 205 | * This should be called with the blkg->stats_lock held. |
| 206 | */ |
| 207 | static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync) |
| 208 | { |
| 209 | if (direction) { |
| 210 | BUG_ON(stat[BLKIO_STAT_WRITE] == 0); |
| 211 | stat[BLKIO_STAT_WRITE]--; |
| 212 | } else { |
| 213 | BUG_ON(stat[BLKIO_STAT_READ] == 0); |
| 214 | stat[BLKIO_STAT_READ]--; |
| 215 | } |
| 216 | if (sync) { |
| 217 | BUG_ON(stat[BLKIO_STAT_SYNC] == 0); |
| 218 | stat[BLKIO_STAT_SYNC]--; |
| 219 | } else { |
| 220 | BUG_ON(stat[BLKIO_STAT_ASYNC] == 0); |
| 221 | stat[BLKIO_STAT_ASYNC]--; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 226 | /* This should be called with the blkg->stats_lock held. */ |
| 227 | static void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
| 228 | struct blkio_group *curr_blkg) |
| 229 | { |
| 230 | if (blkio_blkg_waiting(&blkg->stats)) |
| 231 | return; |
| 232 | if (blkg == curr_blkg) |
| 233 | return; |
| 234 | blkg->stats.start_group_wait_time = sched_clock(); |
| 235 | blkio_mark_blkg_waiting(&blkg->stats); |
| 236 | } |
| 237 | |
| 238 | /* This should be called with the blkg->stats_lock held. */ |
| 239 | static void blkio_update_group_wait_time(struct blkio_group_stats *stats) |
| 240 | { |
| 241 | unsigned long long now; |
| 242 | |
| 243 | if (!blkio_blkg_waiting(stats)) |
| 244 | return; |
| 245 | |
| 246 | now = sched_clock(); |
| 247 | if (time_after64(now, stats->start_group_wait_time)) |
| 248 | stats->group_wait_time += now - stats->start_group_wait_time; |
| 249 | blkio_clear_blkg_waiting(stats); |
| 250 | } |
| 251 | |
| 252 | /* This should be called with the blkg->stats_lock held. */ |
| 253 | static void blkio_end_empty_time(struct blkio_group_stats *stats) |
| 254 | { |
| 255 | unsigned long long now; |
| 256 | |
| 257 | if (!blkio_blkg_empty(stats)) |
| 258 | return; |
| 259 | |
| 260 | now = sched_clock(); |
| 261 | if (time_after64(now, stats->start_empty_time)) |
| 262 | stats->empty_time += now - stats->start_empty_time; |
| 263 | blkio_clear_blkg_empty(stats); |
| 264 | } |
| 265 | |
| 266 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg) |
| 267 | { |
| 268 | unsigned long flags; |
| 269 | |
| 270 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 271 | BUG_ON(blkio_blkg_idling(&blkg->stats)); |
| 272 | blkg->stats.start_idle_time = sched_clock(); |
| 273 | blkio_mark_blkg_idling(&blkg->stats); |
| 274 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats); |
| 277 | |
| 278 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg) |
| 279 | { |
| 280 | unsigned long flags; |
| 281 | unsigned long long now; |
| 282 | struct blkio_group_stats *stats; |
| 283 | |
| 284 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 285 | stats = &blkg->stats; |
| 286 | if (blkio_blkg_idling(stats)) { |
| 287 | now = sched_clock(); |
| 288 | if (time_after64(now, stats->start_idle_time)) |
| 289 | stats->idle_time += now - stats->start_idle_time; |
| 290 | blkio_clear_blkg_idling(stats); |
| 291 | } |
| 292 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 293 | } |
| 294 | EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats); |
| 295 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 296 | void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg) |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 297 | { |
| 298 | unsigned long flags; |
| 299 | struct blkio_group_stats *stats; |
| 300 | |
| 301 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 302 | stats = &blkg->stats; |
| 303 | stats->avg_queue_size_sum += |
| 304 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] + |
| 305 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]; |
| 306 | stats->avg_queue_size_samples++; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 307 | blkio_update_group_wait_time(stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 308 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 309 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 310 | EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats); |
| 311 | |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 312 | void blkiocg_set_start_empty_time(struct blkio_group *blkg) |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 313 | { |
| 314 | unsigned long flags; |
| 315 | struct blkio_group_stats *stats; |
| 316 | |
| 317 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 318 | stats = &blkg->stats; |
| 319 | |
| 320 | if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] || |
| 321 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) { |
| 322 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | /* |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 327 | * group is already marked empty. This can happen if cfqq got new |
| 328 | * request in parent group and moved to this group while being added |
| 329 | * to service tree. Just ignore the event and move on. |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 330 | */ |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 331 | if(blkio_blkg_empty(stats)) { |
| 332 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 333 | return; |
| 334 | } |
| 335 | |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame] | 336 | stats->start_empty_time = sched_clock(); |
| 337 | blkio_mark_blkg_empty(stats); |
| 338 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time); |
| 341 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 342 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
| 343 | unsigned long dequeue) |
| 344 | { |
| 345 | blkg->stats.dequeue += dequeue; |
| 346 | } |
| 347 | EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 348 | #else |
| 349 | static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
| 350 | struct blkio_group *curr_blkg) {} |
| 351 | static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {} |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 352 | #endif |
| 353 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 354 | void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 355 | struct blkio_group *curr_blkg, bool direction, |
| 356 | bool sync) |
| 357 | { |
| 358 | unsigned long flags; |
| 359 | |
| 360 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 361 | blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction, |
| 362 | sync); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 363 | blkio_end_empty_time(&blkg->stats); |
| 364 | blkio_set_start_group_wait_time(blkg, curr_blkg); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 365 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 366 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 367 | EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 368 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 369 | void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 370 | bool direction, bool sync) |
| 371 | { |
| 372 | unsigned long flags; |
| 373 | |
| 374 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 375 | blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], |
| 376 | direction, sync); |
| 377 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 378 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 379 | EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 380 | |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 381 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time, |
| 382 | unsigned long unaccounted_time) |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 383 | { |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 384 | unsigned long flags; |
| 385 | |
| 386 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 387 | blkg->stats.time += time; |
Vivek Goyal | a23e686 | 2011-05-19 15:38:20 -0400 | [diff] [blame] | 388 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 389 | blkg->stats.unaccounted_time += unaccounted_time; |
Vivek Goyal | a23e686 | 2011-05-19 15:38:20 -0400 | [diff] [blame] | 390 | #endif |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 391 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 392 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 393 | EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 394 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 395 | /* |
| 396 | * should be called under rcu read lock or queue lock to make sure blkg pointer |
| 397 | * is valid. |
| 398 | */ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 399 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
| 400 | uint64_t bytes, bool direction, bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 401 | { |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 402 | struct blkio_group_stats_cpu *stats_cpu; |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 403 | unsigned long flags; |
| 404 | |
| 405 | /* |
| 406 | * Disabling interrupts to provide mutual exclusion between two |
| 407 | * writes on same cpu. It probably is not needed for 64bit. Not |
| 408 | * optimizing that case yet. |
| 409 | */ |
| 410 | local_irq_save(flags); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 411 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 412 | stats_cpu = this_cpu_ptr(blkg->stats_cpu); |
| 413 | |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 414 | u64_stats_update_begin(&stats_cpu->syncp); |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 415 | stats_cpu->sectors += bytes >> 9; |
| 416 | blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED], |
| 417 | 1, direction, sync); |
| 418 | blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES], |
| 419 | bytes, direction, sync); |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 420 | u64_stats_update_end(&stats_cpu->syncp); |
| 421 | local_irq_restore(flags); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 422 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 423 | EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 424 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 425 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
| 426 | uint64_t start_time, uint64_t io_start_time, bool direction, bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 427 | { |
| 428 | struct blkio_group_stats *stats; |
| 429 | unsigned long flags; |
| 430 | unsigned long long now = sched_clock(); |
| 431 | |
| 432 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 433 | stats = &blkg->stats; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 434 | if (time_after64(now, io_start_time)) |
| 435 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME], |
| 436 | now - io_start_time, direction, sync); |
| 437 | if (time_after64(io_start_time, start_time)) |
| 438 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME], |
| 439 | io_start_time - start_time, direction, sync); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 440 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 441 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 442 | EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 443 | |
Vivek Goyal | 317389a | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 444 | /* Merged stats are per cpu. */ |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 445 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction, |
| 446 | bool sync) |
| 447 | { |
Vivek Goyal | 317389a | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 448 | struct blkio_group_stats_cpu *stats_cpu; |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 449 | unsigned long flags; |
| 450 | |
Vivek Goyal | 317389a | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 451 | /* |
| 452 | * Disabling interrupts to provide mutual exclusion between two |
| 453 | * writes on same cpu. It probably is not needed for 64bit. Not |
| 454 | * optimizing that case yet. |
| 455 | */ |
| 456 | local_irq_save(flags); |
| 457 | |
| 458 | stats_cpu = this_cpu_ptr(blkg->stats_cpu); |
| 459 | |
| 460 | u64_stats_update_begin(&stats_cpu->syncp); |
| 461 | blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1, |
| 462 | direction, sync); |
| 463 | u64_stats_update_end(&stats_cpu->syncp); |
| 464 | local_irq_restore(flags); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 465 | } |
| 466 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); |
| 467 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 468 | /* |
| 469 | * This function allocates the per cpu stats for blkio_group. Should be called |
| 470 | * from sleepable context as alloc_per_cpu() requires that. |
| 471 | */ |
| 472 | int blkio_alloc_blkg_stats(struct blkio_group *blkg) |
| 473 | { |
| 474 | /* Allocate memory for per cpu stats */ |
| 475 | blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); |
| 476 | if (!blkg->stats_cpu) |
| 477 | return -ENOMEM; |
| 478 | return 0; |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats); |
| 481 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 482 | void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg, |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 483 | struct blkio_group *blkg, void *key, dev_t dev, |
| 484 | enum blkio_policy_id plid) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 485 | { |
| 486 | unsigned long flags; |
| 487 | |
| 488 | spin_lock_irqsave(&blkcg->lock, flags); |
Divyesh Shah | 8d2a91f | 2010-04-16 08:10:51 +0200 | [diff] [blame] | 489 | spin_lock_init(&blkg->stats_lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 490 | rcu_assign_pointer(blkg->key, key); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 491 | blkg->blkcg_id = css_id(&blkcg->css); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 492 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 493 | blkg->plid = plid; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 494 | spin_unlock_irqrestore(&blkcg->lock, flags); |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 495 | /* Need to take css reference ? */ |
| 496 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 497 | blkg->dev = dev; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 498 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 499 | EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 500 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 501 | static void __blkiocg_del_blkio_group(struct blkio_group *blkg) |
| 502 | { |
| 503 | hlist_del_init_rcu(&blkg->blkcg_node); |
| 504 | blkg->blkcg_id = 0; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1 |
| 509 | * indicating that blk_group was unhashed by the time we got to it. |
| 510 | */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 511 | int blkiocg_del_blkio_group(struct blkio_group *blkg) |
| 512 | { |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 513 | struct blkio_cgroup *blkcg; |
| 514 | unsigned long flags; |
| 515 | struct cgroup_subsys_state *css; |
| 516 | int ret = 1; |
| 517 | |
| 518 | rcu_read_lock(); |
| 519 | css = css_lookup(&blkio_subsys, blkg->blkcg_id); |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 520 | if (css) { |
| 521 | blkcg = container_of(css, struct blkio_cgroup, css); |
| 522 | spin_lock_irqsave(&blkcg->lock, flags); |
| 523 | if (!hlist_unhashed(&blkg->blkcg_node)) { |
| 524 | __blkiocg_del_blkio_group(blkg); |
| 525 | ret = 0; |
| 526 | } |
| 527 | spin_unlock_irqrestore(&blkcg->lock, flags); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 528 | } |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 529 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 530 | rcu_read_unlock(); |
| 531 | return ret; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 532 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 533 | EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 534 | |
| 535 | /* called under rcu_read_lock(). */ |
| 536 | struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) |
| 537 | { |
| 538 | struct blkio_group *blkg; |
| 539 | struct hlist_node *n; |
| 540 | void *__key; |
| 541 | |
| 542 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 543 | __key = blkg->key; |
| 544 | if (__key == key) |
| 545 | return blkg; |
| 546 | } |
| 547 | |
| 548 | return NULL; |
| 549 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 550 | EXPORT_SYMBOL_GPL(blkiocg_lookup_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 551 | |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 552 | static void blkio_reset_stats_cpu(struct blkio_group *blkg) |
| 553 | { |
| 554 | struct blkio_group_stats_cpu *stats_cpu; |
| 555 | int i, j, k; |
| 556 | /* |
| 557 | * Note: On 64 bit arch this should not be an issue. This has the |
| 558 | * possibility of returning some inconsistent value on 32bit arch |
| 559 | * as 64bit update on 32bit is non atomic. Taking care of this |
| 560 | * corner case makes code very complicated, like sending IPIs to |
| 561 | * cpus, taking care of stats of offline cpus etc. |
| 562 | * |
| 563 | * reset stats is anyway more of a debug feature and this sounds a |
| 564 | * corner case. So I am not complicating the code yet until and |
| 565 | * unless this becomes a real issue. |
| 566 | */ |
| 567 | for_each_possible_cpu(i) { |
| 568 | stats_cpu = per_cpu_ptr(blkg->stats_cpu, i); |
| 569 | stats_cpu->sectors = 0; |
| 570 | for(j = 0; j < BLKIO_STAT_CPU_NR; j++) |
| 571 | for (k = 0; k < BLKIO_STAT_TOTAL; k++) |
| 572 | stats_cpu->stat_arr_cpu[j][k] = 0; |
| 573 | } |
| 574 | } |
| 575 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 576 | static int |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 577 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 578 | { |
| 579 | struct blkio_cgroup *blkcg; |
| 580 | struct blkio_group *blkg; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 581 | struct blkio_group_stats *stats; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 582 | struct hlist_node *n; |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 583 | uint64_t queued[BLKIO_STAT_TOTAL]; |
| 584 | int i; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 585 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 586 | bool idling, waiting, empty; |
| 587 | unsigned long long now = sched_clock(); |
| 588 | #endif |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 589 | |
| 590 | blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 591 | spin_lock_irq(&blkcg->lock); |
| 592 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 593 | spin_lock(&blkg->stats_lock); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 594 | stats = &blkg->stats; |
| 595 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 596 | idling = blkio_blkg_idling(stats); |
| 597 | waiting = blkio_blkg_waiting(stats); |
| 598 | empty = blkio_blkg_empty(stats); |
| 599 | #endif |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 600 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 601 | queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i]; |
| 602 | memset(stats, 0, sizeof(struct blkio_group_stats)); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 603 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 604 | stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i]; |
| 605 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 606 | if (idling) { |
| 607 | blkio_mark_blkg_idling(stats); |
| 608 | stats->start_idle_time = now; |
| 609 | } |
| 610 | if (waiting) { |
| 611 | blkio_mark_blkg_waiting(stats); |
| 612 | stats->start_group_wait_time = now; |
| 613 | } |
| 614 | if (empty) { |
| 615 | blkio_mark_blkg_empty(stats); |
| 616 | stats->start_empty_time = now; |
| 617 | } |
| 618 | #endif |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 619 | spin_unlock(&blkg->stats_lock); |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 620 | |
| 621 | /* Reset Per cpu stats which don't take blkg->stats_lock */ |
| 622 | blkio_reset_stats_cpu(blkg); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 623 | } |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 624 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 625 | spin_unlock_irq(&blkcg->lock); |
| 626 | return 0; |
| 627 | } |
| 628 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 629 | static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str, |
| 630 | int chars_left, bool diskname_only) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 631 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 632 | snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev)); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 633 | chars_left -= strlen(str); |
| 634 | if (chars_left <= 0) { |
| 635 | printk(KERN_WARNING |
| 636 | "Possibly incorrect cgroup stat display format"); |
| 637 | return; |
| 638 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 639 | if (diskname_only) |
| 640 | return; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 641 | switch (type) { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 642 | case BLKIO_STAT_READ: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 643 | strlcat(str, " Read", chars_left); |
| 644 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 645 | case BLKIO_STAT_WRITE: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 646 | strlcat(str, " Write", chars_left); |
| 647 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 648 | case BLKIO_STAT_SYNC: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 649 | strlcat(str, " Sync", chars_left); |
| 650 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 651 | case BLKIO_STAT_ASYNC: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 652 | strlcat(str, " Async", chars_left); |
| 653 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 654 | case BLKIO_STAT_TOTAL: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 655 | strlcat(str, " Total", chars_left); |
| 656 | break; |
| 657 | default: |
| 658 | strlcat(str, " Invalid", chars_left); |
| 659 | } |
| 660 | } |
| 661 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 662 | static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val, |
| 663 | struct cgroup_map_cb *cb, dev_t dev) |
| 664 | { |
| 665 | blkio_get_key_name(0, dev, str, chars_left, true); |
| 666 | cb->fill(cb, str, val); |
| 667 | return val; |
| 668 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 669 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 670 | |
| 671 | static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, |
| 672 | enum stat_type_cpu type, enum stat_sub_type sub_type) |
| 673 | { |
| 674 | int cpu; |
| 675 | struct blkio_group_stats_cpu *stats_cpu; |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 676 | u64 val = 0, tval; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 677 | |
| 678 | for_each_possible_cpu(cpu) { |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 679 | unsigned int start; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 680 | stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu); |
| 681 | |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 682 | do { |
| 683 | start = u64_stats_fetch_begin(&stats_cpu->syncp); |
| 684 | if (type == BLKIO_STAT_CPU_SECTORS) |
| 685 | tval = stats_cpu->sectors; |
| 686 | else |
| 687 | tval = stats_cpu->stat_arr_cpu[type][sub_type]; |
| 688 | } while(u64_stats_fetch_retry(&stats_cpu->syncp, start)); |
| 689 | |
| 690 | val += tval; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return val; |
| 694 | } |
| 695 | |
| 696 | static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, |
| 697 | struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type) |
| 698 | { |
| 699 | uint64_t disk_total, val; |
| 700 | char key_str[MAX_KEY_LEN]; |
| 701 | enum stat_sub_type sub_type; |
| 702 | |
| 703 | if (type == BLKIO_STAT_CPU_SECTORS) { |
| 704 | val = blkio_read_stat_cpu(blkg, type, 0); |
| 705 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev); |
| 706 | } |
| 707 | |
| 708 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; |
| 709 | sub_type++) { |
| 710 | blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false); |
| 711 | val = blkio_read_stat_cpu(blkg, type, sub_type); |
| 712 | cb->fill(cb, key_str, val); |
| 713 | } |
| 714 | |
| 715 | disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) + |
| 716 | blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE); |
| 717 | |
| 718 | blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false); |
| 719 | cb->fill(cb, key_str, disk_total); |
| 720 | return disk_total; |
| 721 | } |
| 722 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 723 | /* This should be called with blkg->stats_lock held */ |
| 724 | static uint64_t blkio_get_stat(struct blkio_group *blkg, |
| 725 | struct cgroup_map_cb *cb, dev_t dev, enum stat_type type) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 726 | { |
| 727 | uint64_t disk_total; |
| 728 | char key_str[MAX_KEY_LEN]; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 729 | enum stat_sub_type sub_type; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 730 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 731 | if (type == BLKIO_STAT_TIME) |
| 732 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 733 | blkg->stats.time, cb, dev); |
Justin TerAvest | 9026e52 | 2011-03-22 21:26:54 +0100 | [diff] [blame] | 734 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 735 | if (type == BLKIO_STAT_UNACCOUNTED_TIME) |
| 736 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 737 | blkg->stats.unaccounted_time, cb, dev); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 738 | if (type == BLKIO_STAT_AVG_QUEUE_SIZE) { |
| 739 | uint64_t sum = blkg->stats.avg_queue_size_sum; |
| 740 | uint64_t samples = blkg->stats.avg_queue_size_samples; |
| 741 | if (samples) |
| 742 | do_div(sum, samples); |
| 743 | else |
| 744 | sum = 0; |
| 745 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev); |
| 746 | } |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 747 | if (type == BLKIO_STAT_GROUP_WAIT_TIME) |
| 748 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 749 | blkg->stats.group_wait_time, cb, dev); |
| 750 | if (type == BLKIO_STAT_IDLE_TIME) |
| 751 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 752 | blkg->stats.idle_time, cb, dev); |
| 753 | if (type == BLKIO_STAT_EMPTY_TIME) |
| 754 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 755 | blkg->stats.empty_time, cb, dev); |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 756 | if (type == BLKIO_STAT_DEQUEUE) |
| 757 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 758 | blkg->stats.dequeue, cb, dev); |
| 759 | #endif |
| 760 | |
| 761 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; |
| 762 | sub_type++) { |
| 763 | blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false); |
| 764 | cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 765 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 766 | disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] + |
| 767 | blkg->stats.stat_arr[type][BLKIO_STAT_WRITE]; |
| 768 | blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 769 | cb->fill(cb, key_str, disk_total); |
| 770 | return disk_total; |
| 771 | } |
| 772 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 773 | static int blkio_policy_parse_and_set(char *buf, |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 774 | struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 775 | { |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 776 | struct gendisk *disk = NULL; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 777 | char *s[4], *p, *major_s = NULL, *minor_s = NULL; |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 778 | unsigned long major, minor; |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 779 | int i = 0, ret = -EINVAL; |
| 780 | int part; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 781 | dev_t dev; |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 782 | u64 temp; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 783 | |
| 784 | memset(s, 0, sizeof(s)); |
| 785 | |
| 786 | while ((p = strsep(&buf, " ")) != NULL) { |
| 787 | if (!*p) |
| 788 | continue; |
| 789 | |
| 790 | s[i++] = p; |
| 791 | |
| 792 | /* Prevent from inputing too many things */ |
| 793 | if (i == 3) |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | if (i != 2) |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 798 | goto out; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 799 | |
| 800 | p = strsep(&s[0], ":"); |
| 801 | if (p != NULL) |
| 802 | major_s = p; |
| 803 | else |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 804 | goto out; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 805 | |
| 806 | minor_s = s[0]; |
| 807 | if (!minor_s) |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 808 | goto out; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 809 | |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 810 | if (strict_strtoul(major_s, 10, &major)) |
| 811 | goto out; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 812 | |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 813 | if (strict_strtoul(minor_s, 10, &minor)) |
| 814 | goto out; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 815 | |
| 816 | dev = MKDEV(major, minor); |
| 817 | |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 818 | if (strict_strtoull(s[1], 10, &temp)) |
| 819 | goto out; |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 820 | |
| 821 | /* For rule removal, do not check for device presence. */ |
| 822 | if (temp) { |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 823 | disk = get_gendisk(dev, &part); |
| 824 | if (!disk || part) { |
| 825 | ret = -ENODEV; |
| 826 | goto out; |
| 827 | } |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 828 | } |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 829 | |
| 830 | newpn->dev = dev; |
| 831 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 832 | switch (plid) { |
| 833 | case BLKIO_POLICY_PROP: |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 834 | if ((temp < BLKIO_WEIGHT_MIN && temp > 0) || |
| 835 | temp > BLKIO_WEIGHT_MAX) |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 836 | goto out; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 837 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 838 | newpn->plid = plid; |
| 839 | newpn->fileid = fileid; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 840 | newpn->val.weight = temp; |
| 841 | break; |
| 842 | case BLKIO_POLICY_THROTL: |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 843 | switch(fileid) { |
| 844 | case BLKIO_THROTL_read_bps_device: |
| 845 | case BLKIO_THROTL_write_bps_device: |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 846 | newpn->plid = plid; |
| 847 | newpn->fileid = fileid; |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 848 | newpn->val.bps = temp; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 849 | break; |
| 850 | case BLKIO_THROTL_read_iops_device: |
| 851 | case BLKIO_THROTL_write_iops_device: |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 852 | if (temp > THROTL_IOPS_MAX) |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 853 | goto out; |
Vivek Goyal | 9355aed | 2010-10-01 21:16:41 +0200 | [diff] [blame] | 854 | |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 855 | newpn->plid = plid; |
| 856 | newpn->fileid = fileid; |
Wanlong Gao | d11bb44 | 2011-09-21 10:22:10 +0200 | [diff] [blame] | 857 | newpn->val.iops = (unsigned int)temp; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 858 | break; |
| 859 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 860 | break; |
| 861 | default: |
| 862 | BUG(); |
| 863 | } |
Tejun Heo | ece8424 | 2011-10-19 14:31:15 +0200 | [diff] [blame] | 864 | ret = 0; |
| 865 | out: |
| 866 | put_disk(disk); |
| 867 | return ret; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg, |
| 871 | dev_t dev) |
| 872 | { |
| 873 | struct blkio_policy_node *pn; |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 874 | unsigned long flags; |
| 875 | unsigned int weight; |
| 876 | |
| 877 | spin_lock_irqsave(&blkcg->lock, flags); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 878 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 879 | pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP, |
| 880 | BLKIO_PROP_weight_device); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 881 | if (pn) |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 882 | weight = pn->val.weight; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 883 | else |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 884 | weight = blkcg->weight; |
| 885 | |
| 886 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 887 | |
| 888 | return weight; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 889 | } |
| 890 | EXPORT_SYMBOL_GPL(blkcg_get_weight); |
| 891 | |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 892 | uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev) |
| 893 | { |
| 894 | struct blkio_policy_node *pn; |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 895 | unsigned long flags; |
| 896 | uint64_t bps = -1; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 897 | |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 898 | spin_lock_irqsave(&blkcg->lock, flags); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 899 | pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL, |
| 900 | BLKIO_THROTL_read_bps_device); |
| 901 | if (pn) |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 902 | bps = pn->val.bps; |
| 903 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 904 | |
| 905 | return bps; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev) |
| 909 | { |
| 910 | struct blkio_policy_node *pn; |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 911 | unsigned long flags; |
| 912 | uint64_t bps = -1; |
| 913 | |
| 914 | spin_lock_irqsave(&blkcg->lock, flags); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 915 | pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL, |
| 916 | BLKIO_THROTL_write_bps_device); |
| 917 | if (pn) |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 918 | bps = pn->val.bps; |
| 919 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 920 | |
| 921 | return bps; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 922 | } |
| 923 | |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 924 | unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev) |
| 925 | { |
| 926 | struct blkio_policy_node *pn; |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 927 | unsigned long flags; |
| 928 | unsigned int iops = -1; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 929 | |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 930 | spin_lock_irqsave(&blkcg->lock, flags); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 931 | pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL, |
| 932 | BLKIO_THROTL_read_iops_device); |
| 933 | if (pn) |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 934 | iops = pn->val.iops; |
| 935 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 936 | |
| 937 | return iops; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev) |
| 941 | { |
| 942 | struct blkio_policy_node *pn; |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 943 | unsigned long flags; |
| 944 | unsigned int iops = -1; |
| 945 | |
| 946 | spin_lock_irqsave(&blkcg->lock, flags); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 947 | pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL, |
| 948 | BLKIO_THROTL_write_iops_device); |
| 949 | if (pn) |
Vivek Goyal | a38eb63 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 950 | iops = pn->val.iops; |
| 951 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 952 | |
| 953 | return iops; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 954 | } |
| 955 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 956 | /* Checks whether user asked for deleting a policy rule */ |
| 957 | static bool blkio_delete_rule_command(struct blkio_policy_node *pn) |
| 958 | { |
| 959 | switch(pn->plid) { |
| 960 | case BLKIO_POLICY_PROP: |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 961 | if (pn->val.weight == 0) |
| 962 | return 1; |
| 963 | break; |
| 964 | case BLKIO_POLICY_THROTL: |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 965 | switch(pn->fileid) { |
| 966 | case BLKIO_THROTL_read_bps_device: |
| 967 | case BLKIO_THROTL_write_bps_device: |
| 968 | if (pn->val.bps == 0) |
| 969 | return 1; |
| 970 | break; |
| 971 | case BLKIO_THROTL_read_iops_device: |
| 972 | case BLKIO_THROTL_write_iops_device: |
| 973 | if (pn->val.iops == 0) |
| 974 | return 1; |
| 975 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 976 | break; |
| 977 | default: |
| 978 | BUG(); |
| 979 | } |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 980 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static void blkio_update_policy_rule(struct blkio_policy_node *oldpn, |
| 985 | struct blkio_policy_node *newpn) |
| 986 | { |
| 987 | switch(oldpn->plid) { |
| 988 | case BLKIO_POLICY_PROP: |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 989 | oldpn->val.weight = newpn->val.weight; |
| 990 | break; |
| 991 | case BLKIO_POLICY_THROTL: |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 992 | switch(newpn->fileid) { |
| 993 | case BLKIO_THROTL_read_bps_device: |
| 994 | case BLKIO_THROTL_write_bps_device: |
| 995 | oldpn->val.bps = newpn->val.bps; |
| 996 | break; |
| 997 | case BLKIO_THROTL_read_iops_device: |
| 998 | case BLKIO_THROTL_write_iops_device: |
| 999 | oldpn->val.iops = newpn->val.iops; |
| 1000 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1001 | break; |
| 1002 | default: |
| 1003 | BUG(); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1008 | * Some rules/values in blkg have changed. Propagate those to respective |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1009 | * policies. |
| 1010 | */ |
| 1011 | static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg, |
| 1012 | struct blkio_group *blkg, struct blkio_policy_node *pn) |
| 1013 | { |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1014 | unsigned int weight, iops; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1015 | u64 bps; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1016 | |
| 1017 | switch(pn->plid) { |
| 1018 | case BLKIO_POLICY_PROP: |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1019 | weight = pn->val.weight ? pn->val.weight : |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1020 | blkcg->weight; |
| 1021 | blkio_update_group_weight(blkg, weight); |
| 1022 | break; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1023 | case BLKIO_POLICY_THROTL: |
| 1024 | switch(pn->fileid) { |
| 1025 | case BLKIO_THROTL_read_bps_device: |
| 1026 | case BLKIO_THROTL_write_bps_device: |
| 1027 | bps = pn->val.bps ? pn->val.bps : (-1); |
| 1028 | blkio_update_group_bps(blkg, bps, pn->fileid); |
| 1029 | break; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1030 | case BLKIO_THROTL_read_iops_device: |
| 1031 | case BLKIO_THROTL_write_iops_device: |
| 1032 | iops = pn->val.iops ? pn->val.iops : (-1); |
| 1033 | blkio_update_group_iops(blkg, iops, pn->fileid); |
| 1034 | break; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1035 | } |
| 1036 | break; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1037 | default: |
| 1038 | BUG(); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1043 | * A policy node rule has been updated. Propagate this update to all the |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1044 | * block groups which might be affected by this update. |
| 1045 | */ |
| 1046 | static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg, |
| 1047 | struct blkio_policy_node *pn) |
| 1048 | { |
| 1049 | struct blkio_group *blkg; |
| 1050 | struct hlist_node *n; |
| 1051 | |
| 1052 | spin_lock(&blkio_list_lock); |
| 1053 | spin_lock_irq(&blkcg->lock); |
| 1054 | |
| 1055 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 1056 | if (pn->dev != blkg->dev || pn->plid != blkg->plid) |
| 1057 | continue; |
| 1058 | blkio_update_blkg_policy(blkcg, blkg, pn); |
| 1059 | } |
| 1060 | |
| 1061 | spin_unlock_irq(&blkcg->lock); |
| 1062 | spin_unlock(&blkio_list_lock); |
| 1063 | } |
| 1064 | |
| 1065 | static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft, |
| 1066 | const char *buffer) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1067 | { |
| 1068 | int ret = 0; |
| 1069 | char *buf; |
| 1070 | struct blkio_policy_node *newpn, *pn; |
| 1071 | struct blkio_cgroup *blkcg; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1072 | int keep_newpn = 0; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1073 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 1074 | int fileid = BLKIOFILE_ATTR(cft->private); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1075 | |
| 1076 | buf = kstrdup(buffer, GFP_KERNEL); |
| 1077 | if (!buf) |
| 1078 | return -ENOMEM; |
| 1079 | |
| 1080 | newpn = kzalloc(sizeof(*newpn), GFP_KERNEL); |
| 1081 | if (!newpn) { |
| 1082 | ret = -ENOMEM; |
| 1083 | goto free_buf; |
| 1084 | } |
| 1085 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1086 | ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1087 | if (ret) |
| 1088 | goto free_newpn; |
| 1089 | |
| 1090 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1091 | |
| 1092 | spin_lock_irq(&blkcg->lock); |
| 1093 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1094 | pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1095 | if (!pn) { |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1096 | if (!blkio_delete_rule_command(newpn)) { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1097 | blkio_policy_insert_node(blkcg, newpn); |
| 1098 | keep_newpn = 1; |
| 1099 | } |
| 1100 | spin_unlock_irq(&blkcg->lock); |
| 1101 | goto update_io_group; |
| 1102 | } |
| 1103 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1104 | if (blkio_delete_rule_command(newpn)) { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1105 | blkio_policy_delete_node(pn); |
Vivek Goyal | e060f00 | 2011-10-25 15:48:12 +0200 | [diff] [blame] | 1106 | kfree(pn); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1107 | spin_unlock_irq(&blkcg->lock); |
| 1108 | goto update_io_group; |
| 1109 | } |
| 1110 | spin_unlock_irq(&blkcg->lock); |
| 1111 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1112 | blkio_update_policy_rule(pn, newpn); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1113 | |
| 1114 | update_io_group: |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1115 | blkio_update_policy_node_blkg(blkcg, newpn); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1116 | |
| 1117 | free_newpn: |
| 1118 | if (!keep_newpn) |
| 1119 | kfree(newpn); |
| 1120 | free_buf: |
| 1121 | kfree(buf); |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1125 | static void |
| 1126 | blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1127 | { |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1128 | switch(pn->plid) { |
| 1129 | case BLKIO_POLICY_PROP: |
| 1130 | if (pn->fileid == BLKIO_PROP_weight_device) |
| 1131 | seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev), |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1132 | MINOR(pn->dev), pn->val.weight); |
| 1133 | break; |
| 1134 | case BLKIO_POLICY_THROTL: |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1135 | switch(pn->fileid) { |
| 1136 | case BLKIO_THROTL_read_bps_device: |
| 1137 | case BLKIO_THROTL_write_bps_device: |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1138 | seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev), |
| 1139 | MINOR(pn->dev), pn->val.bps); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1140 | break; |
| 1141 | case BLKIO_THROTL_read_iops_device: |
| 1142 | case BLKIO_THROTL_write_iops_device: |
| 1143 | seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev), |
| 1144 | MINOR(pn->dev), pn->val.iops); |
| 1145 | break; |
| 1146 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1147 | break; |
| 1148 | default: |
| 1149 | BUG(); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* cgroup files which read their data from policy nodes end up here */ |
| 1154 | static void blkio_read_policy_node_files(struct cftype *cft, |
| 1155 | struct blkio_cgroup *blkcg, struct seq_file *m) |
| 1156 | { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1157 | struct blkio_policy_node *pn; |
| 1158 | |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1159 | if (!list_empty(&blkcg->policy_list)) { |
| 1160 | spin_lock_irq(&blkcg->lock); |
| 1161 | list_for_each_entry(pn, &blkcg->policy_list, node) { |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1162 | if (!pn_matches_cftype(cft, pn)) |
| 1163 | continue; |
| 1164 | blkio_print_policy_node(m, pn); |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1165 | } |
| 1166 | spin_unlock_irq(&blkcg->lock); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1167 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft, |
| 1171 | struct seq_file *m) |
| 1172 | { |
| 1173 | struct blkio_cgroup *blkcg; |
| 1174 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 1175 | int name = BLKIOFILE_ATTR(cft->private); |
| 1176 | |
| 1177 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1178 | |
| 1179 | switch(plid) { |
| 1180 | case BLKIO_POLICY_PROP: |
| 1181 | switch(name) { |
| 1182 | case BLKIO_PROP_weight_device: |
| 1183 | blkio_read_policy_node_files(cft, blkcg, m); |
| 1184 | return 0; |
| 1185 | default: |
| 1186 | BUG(); |
| 1187 | } |
| 1188 | break; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1189 | case BLKIO_POLICY_THROTL: |
| 1190 | switch(name){ |
| 1191 | case BLKIO_THROTL_read_bps_device: |
| 1192 | case BLKIO_THROTL_write_bps_device: |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1193 | case BLKIO_THROTL_read_iops_device: |
| 1194 | case BLKIO_THROTL_write_iops_device: |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1195 | blkio_read_policy_node_files(cft, blkcg, m); |
| 1196 | return 0; |
| 1197 | default: |
| 1198 | BUG(); |
| 1199 | } |
| 1200 | break; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1201 | default: |
| 1202 | BUG(); |
| 1203 | } |
| 1204 | |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1209 | struct cftype *cft, struct cgroup_map_cb *cb, |
| 1210 | enum stat_type type, bool show_total, bool pcpu) |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1211 | { |
| 1212 | struct blkio_group *blkg; |
| 1213 | struct hlist_node *n; |
| 1214 | uint64_t cgroup_total = 0; |
| 1215 | |
| 1216 | rcu_read_lock(); |
| 1217 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 1218 | if (blkg->dev) { |
| 1219 | if (!cftype_blkg_same_policy(cft, blkg)) |
| 1220 | continue; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1221 | if (pcpu) |
| 1222 | cgroup_total += blkio_get_stat_cpu(blkg, cb, |
| 1223 | blkg->dev, type); |
| 1224 | else { |
| 1225 | spin_lock_irq(&blkg->stats_lock); |
| 1226 | cgroup_total += blkio_get_stat(blkg, cb, |
| 1227 | blkg->dev, type); |
| 1228 | spin_unlock_irq(&blkg->stats_lock); |
| 1229 | } |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | if (show_total) |
| 1233 | cb->fill(cb, "Total", cgroup_total); |
| 1234 | rcu_read_unlock(); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | /* All map kind of cgroup file get serviced by this function */ |
| 1239 | static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft, |
| 1240 | struct cgroup_map_cb *cb) |
| 1241 | { |
| 1242 | struct blkio_cgroup *blkcg; |
| 1243 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 1244 | int name = BLKIOFILE_ATTR(cft->private); |
| 1245 | |
| 1246 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1247 | |
| 1248 | switch(plid) { |
| 1249 | case BLKIO_POLICY_PROP: |
| 1250 | switch(name) { |
| 1251 | case BLKIO_PROP_time: |
| 1252 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1253 | BLKIO_STAT_TIME, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1254 | case BLKIO_PROP_sectors: |
| 1255 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1256 | BLKIO_STAT_CPU_SECTORS, 0, 1); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1257 | case BLKIO_PROP_io_service_bytes: |
| 1258 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1259 | BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1260 | case BLKIO_PROP_io_serviced: |
| 1261 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1262 | BLKIO_STAT_CPU_SERVICED, 1, 1); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1263 | case BLKIO_PROP_io_service_time: |
| 1264 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1265 | BLKIO_STAT_SERVICE_TIME, 1, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1266 | case BLKIO_PROP_io_wait_time: |
| 1267 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1268 | BLKIO_STAT_WAIT_TIME, 1, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1269 | case BLKIO_PROP_io_merged: |
| 1270 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 317389a | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 1271 | BLKIO_STAT_CPU_MERGED, 1, 1); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1272 | case BLKIO_PROP_io_queued: |
| 1273 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1274 | BLKIO_STAT_QUEUED, 1, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1275 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Justin TerAvest | 9026e52 | 2011-03-22 21:26:54 +0100 | [diff] [blame] | 1276 | case BLKIO_PROP_unaccounted_time: |
| 1277 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1278 | BLKIO_STAT_UNACCOUNTED_TIME, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1279 | case BLKIO_PROP_dequeue: |
| 1280 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1281 | BLKIO_STAT_DEQUEUE, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1282 | case BLKIO_PROP_avg_queue_size: |
| 1283 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1284 | BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1285 | case BLKIO_PROP_group_wait_time: |
| 1286 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1287 | BLKIO_STAT_GROUP_WAIT_TIME, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1288 | case BLKIO_PROP_idle_time: |
| 1289 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1290 | BLKIO_STAT_IDLE_TIME, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1291 | case BLKIO_PROP_empty_time: |
| 1292 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1293 | BLKIO_STAT_EMPTY_TIME, 0, 0); |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1294 | #endif |
| 1295 | default: |
| 1296 | BUG(); |
| 1297 | } |
| 1298 | break; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1299 | case BLKIO_POLICY_THROTL: |
| 1300 | switch(name){ |
| 1301 | case BLKIO_THROTL_io_service_bytes: |
| 1302 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1303 | BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1304 | case BLKIO_THROTL_io_serviced: |
| 1305 | return blkio_read_blkg_stats(blkcg, cft, cb, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 1306 | BLKIO_STAT_CPU_SERVICED, 1, 1); |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1307 | default: |
| 1308 | BUG(); |
| 1309 | } |
| 1310 | break; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1311 | default: |
| 1312 | BUG(); |
| 1313 | } |
| 1314 | |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val) |
| 1319 | { |
| 1320 | struct blkio_group *blkg; |
| 1321 | struct hlist_node *n; |
| 1322 | struct blkio_policy_node *pn; |
| 1323 | |
| 1324 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) |
| 1325 | return -EINVAL; |
| 1326 | |
| 1327 | spin_lock(&blkio_list_lock); |
| 1328 | spin_lock_irq(&blkcg->lock); |
| 1329 | blkcg->weight = (unsigned int)val; |
| 1330 | |
| 1331 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 1332 | pn = blkio_policy_search_node(blkcg, blkg->dev, |
| 1333 | BLKIO_POLICY_PROP, BLKIO_PROP_weight_device); |
| 1334 | if (pn) |
| 1335 | continue; |
| 1336 | |
| 1337 | blkio_update_group_weight(blkg, blkcg->weight); |
| 1338 | } |
| 1339 | spin_unlock_irq(&blkcg->lock); |
| 1340 | spin_unlock(&blkio_list_lock); |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) { |
| 1345 | struct blkio_cgroup *blkcg; |
| 1346 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 1347 | int name = BLKIOFILE_ATTR(cft->private); |
| 1348 | |
| 1349 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1350 | |
| 1351 | switch(plid) { |
| 1352 | case BLKIO_POLICY_PROP: |
| 1353 | switch(name) { |
| 1354 | case BLKIO_PROP_weight: |
| 1355 | return (u64)blkcg->weight; |
| 1356 | } |
| 1357 | break; |
| 1358 | default: |
| 1359 | BUG(); |
| 1360 | } |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | static int |
| 1365 | blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val) |
| 1366 | { |
| 1367 | struct blkio_cgroup *blkcg; |
| 1368 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
| 1369 | int name = BLKIOFILE_ATTR(cft->private); |
| 1370 | |
| 1371 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 1372 | |
| 1373 | switch(plid) { |
| 1374 | case BLKIO_POLICY_PROP: |
| 1375 | switch(name) { |
| 1376 | case BLKIO_PROP_weight: |
| 1377 | return blkio_weight_write(blkcg, val); |
| 1378 | } |
| 1379 | break; |
| 1380 | default: |
| 1381 | BUG(); |
| 1382 | } |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1383 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1384 | return 0; |
| 1385 | } |
| 1386 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1387 | struct cftype blkio_files[] = { |
| 1388 | { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1389 | .name = "weight_device", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1390 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1391 | BLKIO_PROP_weight_device), |
| 1392 | .read_seq_string = blkiocg_file_read, |
| 1393 | .write_string = blkiocg_file_write, |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1394 | .max_write_len = 256, |
| 1395 | }, |
| 1396 | { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1397 | .name = "weight", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1398 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1399 | BLKIO_PROP_weight), |
| 1400 | .read_u64 = blkiocg_file_read_u64, |
| 1401 | .write_u64 = blkiocg_file_write_u64, |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1402 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1403 | { |
| 1404 | .name = "time", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1405 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1406 | BLKIO_PROP_time), |
| 1407 | .read_map = blkiocg_file_read_map, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1408 | }, |
| 1409 | { |
| 1410 | .name = "sectors", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1411 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1412 | BLKIO_PROP_sectors), |
| 1413 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1414 | }, |
| 1415 | { |
| 1416 | .name = "io_service_bytes", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1417 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1418 | BLKIO_PROP_io_service_bytes), |
| 1419 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1420 | }, |
| 1421 | { |
| 1422 | .name = "io_serviced", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1423 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1424 | BLKIO_PROP_io_serviced), |
| 1425 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1426 | }, |
| 1427 | { |
| 1428 | .name = "io_service_time", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1429 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1430 | BLKIO_PROP_io_service_time), |
| 1431 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 1432 | }, |
| 1433 | { |
| 1434 | .name = "io_wait_time", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1435 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1436 | BLKIO_PROP_io_wait_time), |
| 1437 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 1438 | }, |
| 1439 | { |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 1440 | .name = "io_merged", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1441 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1442 | BLKIO_PROP_io_merged), |
| 1443 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 1444 | }, |
| 1445 | { |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1446 | .name = "io_queued", |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1447 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1448 | BLKIO_PROP_io_queued), |
| 1449 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1450 | }, |
| 1451 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 1452 | .name = "reset_stats", |
| 1453 | .write_u64 = blkiocg_reset_stats, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1454 | }, |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1455 | #ifdef CONFIG_BLK_DEV_THROTTLING |
| 1456 | { |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1457 | .name = "throttle.read_bps_device", |
| 1458 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, |
| 1459 | BLKIO_THROTL_read_bps_device), |
| 1460 | .read_seq_string = blkiocg_file_read, |
| 1461 | .write_string = blkiocg_file_write, |
| 1462 | .max_write_len = 256, |
| 1463 | }, |
| 1464 | |
| 1465 | { |
| 1466 | .name = "throttle.write_bps_device", |
| 1467 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, |
| 1468 | BLKIO_THROTL_write_bps_device), |
| 1469 | .read_seq_string = blkiocg_file_read, |
| 1470 | .write_string = blkiocg_file_write, |
| 1471 | .max_write_len = 256, |
| 1472 | }, |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 1473 | |
| 1474 | { |
| 1475 | .name = "throttle.read_iops_device", |
| 1476 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, |
| 1477 | BLKIO_THROTL_read_iops_device), |
| 1478 | .read_seq_string = blkiocg_file_read, |
| 1479 | .write_string = blkiocg_file_write, |
| 1480 | .max_write_len = 256, |
| 1481 | }, |
| 1482 | |
| 1483 | { |
| 1484 | .name = "throttle.write_iops_device", |
| 1485 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, |
| 1486 | BLKIO_THROTL_write_iops_device), |
| 1487 | .read_seq_string = blkiocg_file_read, |
| 1488 | .write_string = blkiocg_file_write, |
| 1489 | .max_write_len = 256, |
| 1490 | }, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1491 | { |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1492 | .name = "throttle.io_service_bytes", |
| 1493 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, |
| 1494 | BLKIO_THROTL_io_service_bytes), |
| 1495 | .read_map = blkiocg_file_read_map, |
| 1496 | }, |
| 1497 | { |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 1498 | .name = "throttle.io_serviced", |
| 1499 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, |
| 1500 | BLKIO_THROTL_io_serviced), |
| 1501 | .read_map = blkiocg_file_read_map, |
| 1502 | }, |
Vivek Goyal | 13f9825 | 2010-10-01 14:49:41 +0200 | [diff] [blame] | 1503 | #endif /* CONFIG_BLK_DEV_THROTTLING */ |
| 1504 | |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1505 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1506 | { |
| 1507 | .name = "avg_queue_size", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1508 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1509 | BLKIO_PROP_avg_queue_size), |
| 1510 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1511 | }, |
| 1512 | { |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1513 | .name = "group_wait_time", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1514 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1515 | BLKIO_PROP_group_wait_time), |
| 1516 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1517 | }, |
| 1518 | { |
| 1519 | .name = "idle_time", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1520 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1521 | BLKIO_PROP_idle_time), |
| 1522 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1523 | }, |
| 1524 | { |
| 1525 | .name = "empty_time", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1526 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1527 | BLKIO_PROP_empty_time), |
| 1528 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 1529 | }, |
| 1530 | { |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1531 | .name = "dequeue", |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 1532 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1533 | BLKIO_PROP_dequeue), |
| 1534 | .read_map = blkiocg_file_read_map, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 1535 | }, |
Justin TerAvest | 9026e52 | 2011-03-22 21:26:54 +0100 | [diff] [blame] | 1536 | { |
| 1537 | .name = "unaccounted_time", |
| 1538 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
| 1539 | BLKIO_PROP_unaccounted_time), |
| 1540 | .read_map = blkiocg_file_read_map, |
| 1541 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 1542 | #endif |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1543 | }; |
| 1544 | |
| 1545 | static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 1546 | { |
| 1547 | return cgroup_add_files(cgroup, subsys, blkio_files, |
| 1548 | ARRAY_SIZE(blkio_files)); |
| 1549 | } |
| 1550 | |
| 1551 | static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 1552 | { |
| 1553 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1554 | unsigned long flags; |
| 1555 | struct blkio_group *blkg; |
| 1556 | void *key; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1557 | struct blkio_policy_type *blkiop; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1558 | struct blkio_policy_node *pn, *pntmp; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1559 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1560 | rcu_read_lock(); |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1561 | do { |
| 1562 | spin_lock_irqsave(&blkcg->lock, flags); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1563 | |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1564 | if (hlist_empty(&blkcg->blkg_list)) { |
| 1565 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 1566 | break; |
| 1567 | } |
| 1568 | |
| 1569 | blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group, |
| 1570 | blkcg_node); |
| 1571 | key = rcu_dereference(blkg->key); |
| 1572 | __blkiocg_del_blkio_group(blkg); |
| 1573 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1574 | spin_unlock_irqrestore(&blkcg->lock, flags); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1575 | |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1576 | /* |
| 1577 | * This blkio_group is being unlinked as associated cgroup is |
| 1578 | * going away. Let all the IO controlling policies know about |
Vivek Goyal | 61014e9 | 2010-10-01 14:49:44 +0200 | [diff] [blame] | 1579 | * this event. |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1580 | */ |
| 1581 | spin_lock(&blkio_list_lock); |
Vivek Goyal | 61014e9 | 2010-10-01 14:49:44 +0200 | [diff] [blame] | 1582 | list_for_each_entry(blkiop, &blkio_list, list) { |
| 1583 | if (blkiop->plid != blkg->plid) |
| 1584 | continue; |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1585 | blkiop->ops.blkio_unlink_group_fn(key, blkg); |
Vivek Goyal | 61014e9 | 2010-10-01 14:49:44 +0200 | [diff] [blame] | 1586 | } |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1587 | spin_unlock(&blkio_list_lock); |
| 1588 | } while (1); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1589 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1590 | list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) { |
| 1591 | blkio_policy_delete_node(pn); |
| 1592 | kfree(pn); |
| 1593 | } |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 1594 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1595 | free_css_id(&blkio_subsys, &blkcg->css); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1596 | rcu_read_unlock(); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1597 | if (blkcg != &blkio_root_cgroup) |
| 1598 | kfree(blkcg); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | static struct cgroup_subsys_state * |
| 1602 | blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 1603 | { |
Li Zefan | 0341509 | 2010-05-07 08:57:00 +0200 | [diff] [blame] | 1604 | struct blkio_cgroup *blkcg; |
| 1605 | struct cgroup *parent = cgroup->parent; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1606 | |
Li Zefan | 0341509 | 2010-05-07 08:57:00 +0200 | [diff] [blame] | 1607 | if (!parent) { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1608 | blkcg = &blkio_root_cgroup; |
| 1609 | goto done; |
| 1610 | } |
| 1611 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1612 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
| 1613 | if (!blkcg) |
| 1614 | return ERR_PTR(-ENOMEM); |
| 1615 | |
| 1616 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; |
| 1617 | done: |
| 1618 | spin_lock_init(&blkcg->lock); |
| 1619 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
| 1620 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 1621 | INIT_LIST_HEAD(&blkcg->policy_list); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1622 | return &blkcg->css; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * We cannot support shared io contexts, as we have no mean to support |
| 1627 | * two tasks with the same ioc in two different groups without major rework |
| 1628 | * of the main cic data structures. For now we allow a task to change |
| 1629 | * its cgroup only if it's the only owner of its ioc. |
| 1630 | */ |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1631 | static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 1632 | struct cgroup_taskset *tset) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1633 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1634 | struct task_struct *task; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1635 | struct io_context *ioc; |
| 1636 | int ret = 0; |
| 1637 | |
| 1638 | /* task_lock() is needed to avoid races with exit_io_context() */ |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1639 | cgroup_taskset_for_each(task, cgrp, tset) { |
| 1640 | task_lock(task); |
| 1641 | ioc = task->io_context; |
| 1642 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) |
| 1643 | ret = -EINVAL; |
| 1644 | task_unlock(task); |
| 1645 | if (ret) |
| 1646 | break; |
| 1647 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1648 | return ret; |
| 1649 | } |
| 1650 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1651 | static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
| 1652 | struct cgroup_taskset *tset) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1653 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1654 | struct task_struct *task; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1655 | struct io_context *ioc; |
| 1656 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1657 | cgroup_taskset_for_each(task, cgrp, tset) { |
Linus Torvalds | b3c9dd1 | 2012-01-15 12:24:45 -0800 | [diff] [blame] | 1658 | /* we don't lose anything even if ioc allocation fails */ |
| 1659 | ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); |
| 1660 | if (ioc) { |
| 1661 | ioc_cgroup_changed(ioc); |
| 1662 | put_io_context(ioc, NULL); |
| 1663 | } |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1664 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1665 | } |
| 1666 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1667 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
| 1668 | { |
| 1669 | spin_lock(&blkio_list_lock); |
| 1670 | list_add_tail(&blkiop->list, &blkio_list); |
| 1671 | spin_unlock(&blkio_list_lock); |
| 1672 | } |
| 1673 | EXPORT_SYMBOL_GPL(blkio_policy_register); |
| 1674 | |
| 1675 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) |
| 1676 | { |
| 1677 | spin_lock(&blkio_list_lock); |
| 1678 | list_del_init(&blkiop->list); |
| 1679 | spin_unlock(&blkio_list_lock); |
| 1680 | } |
| 1681 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1682 | |
| 1683 | static int __init init_cgroup_blkio(void) |
| 1684 | { |
| 1685 | return cgroup_load_subsys(&blkio_subsys); |
| 1686 | } |
| 1687 | |
| 1688 | static void __exit exit_cgroup_blkio(void) |
| 1689 | { |
| 1690 | cgroup_unload_subsys(&blkio_subsys); |
| 1691 | } |
| 1692 | |
| 1693 | module_init(init_cgroup_blkio); |
| 1694 | module_exit(exit_cgroup_blkio); |
| 1695 | MODULE_LICENSE("GPL"); |