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