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