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 | |
| 40 | struct cgroup_subsys blkio_subsys = { |
| 41 | .name = "blkio", |
| 42 | .create = blkiocg_create, |
| 43 | .can_attach = blkiocg_can_attach, |
| 44 | .attach = blkiocg_attach, |
| 45 | .destroy = blkiocg_destroy, |
| 46 | .populate = blkiocg_populate, |
| 47 | #ifdef CONFIG_BLK_CGROUP |
| 48 | /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */ |
| 49 | .subsys_id = blkio_subsys_id, |
| 50 | #endif |
| 51 | .use_id = 1, |
| 52 | .module = THIS_MODULE, |
| 53 | }; |
| 54 | EXPORT_SYMBOL_GPL(blkio_subsys); |
| 55 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 56 | static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg, |
| 57 | struct blkio_policy_node *pn) |
| 58 | { |
| 59 | list_add(&pn->node, &blkcg->policy_list); |
| 60 | } |
| 61 | |
| 62 | /* Must be called with blkcg->lock held */ |
| 63 | static inline void blkio_policy_delete_node(struct blkio_policy_node *pn) |
| 64 | { |
| 65 | list_del(&pn->node); |
| 66 | } |
| 67 | |
| 68 | /* Must be called with blkcg->lock held */ |
| 69 | static struct blkio_policy_node * |
| 70 | blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev) |
| 71 | { |
| 72 | struct blkio_policy_node *pn; |
| 73 | |
| 74 | list_for_each_entry(pn, &blkcg->policy_list, node) { |
| 75 | if (pn->dev == dev) |
| 76 | return pn; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 82 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
| 83 | { |
| 84 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), |
| 85 | struct blkio_cgroup, css); |
| 86 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 88 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 89 | void blkio_group_init(struct blkio_group *blkg) |
| 90 | { |
| 91 | spin_lock_init(&blkg->stats_lock); |
| 92 | } |
| 93 | EXPORT_SYMBOL_GPL(blkio_group_init); |
| 94 | |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Add to the appropriate stat variable depending on the request type. |
| 97 | * This should be called with the blkg->stats_lock held. |
| 98 | */ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 99 | static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction, |
| 100 | bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 101 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 102 | if (direction) |
| 103 | stat[BLKIO_STAT_WRITE] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 104 | else |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 105 | stat[BLKIO_STAT_READ] += add; |
| 106 | if (sync) |
| 107 | stat[BLKIO_STAT_SYNC] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 108 | else |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 109 | stat[BLKIO_STAT_ASYNC] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 112 | /* |
| 113 | * Decrements the appropriate stat variable if non-zero depending on the |
| 114 | * request type. Panics on value being zero. |
| 115 | * This should be called with the blkg->stats_lock held. |
| 116 | */ |
| 117 | static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync) |
| 118 | { |
| 119 | if (direction) { |
| 120 | BUG_ON(stat[BLKIO_STAT_WRITE] == 0); |
| 121 | stat[BLKIO_STAT_WRITE]--; |
| 122 | } else { |
| 123 | BUG_ON(stat[BLKIO_STAT_READ] == 0); |
| 124 | stat[BLKIO_STAT_READ]--; |
| 125 | } |
| 126 | if (sync) { |
| 127 | BUG_ON(stat[BLKIO_STAT_SYNC] == 0); |
| 128 | stat[BLKIO_STAT_SYNC]--; |
| 129 | } else { |
| 130 | BUG_ON(stat[BLKIO_STAT_ASYNC] == 0); |
| 131 | stat[BLKIO_STAT_ASYNC]--; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 136 | /* This should be called with the blkg->stats_lock held. */ |
| 137 | static void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
| 138 | struct blkio_group *curr_blkg) |
| 139 | { |
| 140 | if (blkio_blkg_waiting(&blkg->stats)) |
| 141 | return; |
| 142 | if (blkg == curr_blkg) |
| 143 | return; |
| 144 | blkg->stats.start_group_wait_time = sched_clock(); |
| 145 | blkio_mark_blkg_waiting(&blkg->stats); |
| 146 | } |
| 147 | |
| 148 | /* This should be called with the blkg->stats_lock held. */ |
| 149 | static void blkio_update_group_wait_time(struct blkio_group_stats *stats) |
| 150 | { |
| 151 | unsigned long long now; |
| 152 | |
| 153 | if (!blkio_blkg_waiting(stats)) |
| 154 | return; |
| 155 | |
| 156 | now = sched_clock(); |
| 157 | if (time_after64(now, stats->start_group_wait_time)) |
| 158 | stats->group_wait_time += now - stats->start_group_wait_time; |
| 159 | blkio_clear_blkg_waiting(stats); |
| 160 | } |
| 161 | |
| 162 | /* This should be called with the blkg->stats_lock held. */ |
| 163 | static void blkio_end_empty_time(struct blkio_group_stats *stats) |
| 164 | { |
| 165 | unsigned long long now; |
| 166 | |
| 167 | if (!blkio_blkg_empty(stats)) |
| 168 | return; |
| 169 | |
| 170 | now = sched_clock(); |
| 171 | if (time_after64(now, stats->start_empty_time)) |
| 172 | stats->empty_time += now - stats->start_empty_time; |
| 173 | blkio_clear_blkg_empty(stats); |
| 174 | } |
| 175 | |
| 176 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg) |
| 177 | { |
| 178 | unsigned long flags; |
| 179 | |
| 180 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 181 | BUG_ON(blkio_blkg_idling(&blkg->stats)); |
| 182 | blkg->stats.start_idle_time = sched_clock(); |
| 183 | blkio_mark_blkg_idling(&blkg->stats); |
| 184 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats); |
| 187 | |
| 188 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg) |
| 189 | { |
| 190 | unsigned long flags; |
| 191 | unsigned long long now; |
| 192 | struct blkio_group_stats *stats; |
| 193 | |
| 194 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 195 | stats = &blkg->stats; |
| 196 | if (blkio_blkg_idling(stats)) { |
| 197 | now = sched_clock(); |
| 198 | if (time_after64(now, stats->start_idle_time)) |
| 199 | stats->idle_time += now - stats->start_idle_time; |
| 200 | blkio_clear_blkg_idling(stats); |
| 201 | } |
| 202 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats); |
| 205 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 206 | void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg) |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 207 | { |
| 208 | unsigned long flags; |
| 209 | struct blkio_group_stats *stats; |
| 210 | |
| 211 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 212 | stats = &blkg->stats; |
| 213 | stats->avg_queue_size_sum += |
| 214 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] + |
| 215 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]; |
| 216 | stats->avg_queue_size_samples++; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 217 | blkio_update_group_wait_time(stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 218 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 219 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 220 | EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats); |
| 221 | |
Divyesh Shah | 28baf44 | 2010-04-14 11:22:38 +0200 | [diff] [blame^] | 222 | void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore) |
| 223 | { |
| 224 | unsigned long flags; |
| 225 | struct blkio_group_stats *stats; |
| 226 | |
| 227 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 228 | stats = &blkg->stats; |
| 229 | |
| 230 | if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] || |
| 231 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) { |
| 232 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * If ignore is set, we do not panic on the empty flag being set |
| 238 | * already. This is to avoid cases where there are superfluous timeslice |
| 239 | * complete events (for eg., forced_dispatch in CFQ) when no IOs are |
| 240 | * served which could result in triggering the empty check incorrectly. |
| 241 | */ |
| 242 | BUG_ON(!ignore && blkio_blkg_empty(stats)); |
| 243 | stats->start_empty_time = sched_clock(); |
| 244 | blkio_mark_blkg_empty(stats); |
| 245 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time); |
| 248 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 249 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
| 250 | unsigned long dequeue) |
| 251 | { |
| 252 | blkg->stats.dequeue += dequeue; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 255 | #else |
| 256 | static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
| 257 | struct blkio_group *curr_blkg) {} |
| 258 | static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {} |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 259 | #endif |
| 260 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 261 | void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 262 | struct blkio_group *curr_blkg, bool direction, |
| 263 | bool sync) |
| 264 | { |
| 265 | unsigned long flags; |
| 266 | |
| 267 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 268 | blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction, |
| 269 | sync); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 270 | blkio_end_empty_time(&blkg->stats); |
| 271 | blkio_set_start_group_wait_time(blkg, curr_blkg); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 272 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 273 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 274 | EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 275 | |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 276 | void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 277 | bool direction, bool sync) |
| 278 | { |
| 279 | unsigned long flags; |
| 280 | |
| 281 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 282 | blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], |
| 283 | direction, sync); |
| 284 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 285 | } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 286 | EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 287 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 288 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time) |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 289 | { |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 290 | unsigned long flags; |
| 291 | |
| 292 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 293 | blkg->stats.time += time; |
| 294 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 295 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 296 | EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 297 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 298 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
| 299 | uint64_t bytes, bool direction, bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 300 | { |
| 301 | struct blkio_group_stats *stats; |
| 302 | unsigned long flags; |
| 303 | |
| 304 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 305 | stats = &blkg->stats; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 306 | stats->sectors += bytes >> 9; |
| 307 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction, |
| 308 | sync); |
| 309 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes, |
| 310 | direction, sync); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 311 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 312 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 314 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 315 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
| 316 | 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] | 317 | { |
| 318 | struct blkio_group_stats *stats; |
| 319 | unsigned long flags; |
| 320 | unsigned long long now = sched_clock(); |
| 321 | |
| 322 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 323 | stats = &blkg->stats; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 324 | if (time_after64(now, io_start_time)) |
| 325 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME], |
| 326 | now - io_start_time, direction, sync); |
| 327 | if (time_after64(io_start_time, start_time)) |
| 328 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME], |
| 329 | io_start_time - start_time, direction, sync); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 330 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 331 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 333 | |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 334 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction, |
| 335 | bool sync) |
| 336 | { |
| 337 | unsigned long flags; |
| 338 | |
| 339 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 340 | blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction, |
| 341 | sync); |
| 342 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 343 | } |
| 344 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); |
| 345 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 346 | void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 347 | struct blkio_group *blkg, void *key, dev_t dev) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 348 | { |
| 349 | unsigned long flags; |
| 350 | |
| 351 | spin_lock_irqsave(&blkcg->lock, flags); |
| 352 | rcu_assign_pointer(blkg->key, key); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 353 | blkg->blkcg_id = css_id(&blkcg->css); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 354 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
| 355 | spin_unlock_irqrestore(&blkcg->lock, flags); |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 356 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 357 | /* Need to take css reference ? */ |
| 358 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
| 359 | #endif |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 360 | blkg->dev = dev; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 361 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 363 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 364 | static void __blkiocg_del_blkio_group(struct blkio_group *blkg) |
| 365 | { |
| 366 | hlist_del_init_rcu(&blkg->blkcg_node); |
| 367 | blkg->blkcg_id = 0; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1 |
| 372 | * indicating that blk_group was unhashed by the time we got to it. |
| 373 | */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 374 | int blkiocg_del_blkio_group(struct blkio_group *blkg) |
| 375 | { |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 376 | struct blkio_cgroup *blkcg; |
| 377 | unsigned long flags; |
| 378 | struct cgroup_subsys_state *css; |
| 379 | int ret = 1; |
| 380 | |
| 381 | rcu_read_lock(); |
| 382 | css = css_lookup(&blkio_subsys, blkg->blkcg_id); |
| 383 | if (!css) |
| 384 | goto out; |
| 385 | |
| 386 | blkcg = container_of(css, struct blkio_cgroup, css); |
| 387 | spin_lock_irqsave(&blkcg->lock, flags); |
| 388 | if (!hlist_unhashed(&blkg->blkcg_node)) { |
| 389 | __blkiocg_del_blkio_group(blkg); |
| 390 | ret = 0; |
| 391 | } |
| 392 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 393 | out: |
| 394 | rcu_read_unlock(); |
| 395 | return ret; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 396 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 397 | EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 398 | |
| 399 | /* called under rcu_read_lock(). */ |
| 400 | struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) |
| 401 | { |
| 402 | struct blkio_group *blkg; |
| 403 | struct hlist_node *n; |
| 404 | void *__key; |
| 405 | |
| 406 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 407 | __key = blkg->key; |
| 408 | if (__key == key) |
| 409 | return blkg; |
| 410 | } |
| 411 | |
| 412 | return NULL; |
| 413 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(blkiocg_lookup_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 415 | |
| 416 | #define SHOW_FUNCTION(__VAR) \ |
| 417 | static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \ |
| 418 | struct cftype *cftype) \ |
| 419 | { \ |
| 420 | struct blkio_cgroup *blkcg; \ |
| 421 | \ |
| 422 | blkcg = cgroup_to_blkio_cgroup(cgroup); \ |
| 423 | return (u64)blkcg->__VAR; \ |
| 424 | } |
| 425 | |
| 426 | SHOW_FUNCTION(weight); |
| 427 | #undef SHOW_FUNCTION |
| 428 | |
| 429 | static int |
| 430 | blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
| 431 | { |
| 432 | struct blkio_cgroup *blkcg; |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 433 | struct blkio_group *blkg; |
| 434 | struct hlist_node *n; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 435 | struct blkio_policy_type *blkiop; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 436 | struct blkio_policy_node *pn; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 437 | |
| 438 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | blkcg = cgroup_to_blkio_cgroup(cgroup); |
Gui Jianfeng | bcf4dd4 | 2010-02-01 09:58:54 +0100 | [diff] [blame] | 442 | spin_lock(&blkio_list_lock); |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 443 | spin_lock_irq(&blkcg->lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 444 | blkcg->weight = (unsigned int)val; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 445 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 446 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 447 | pn = blkio_policy_search_node(blkcg, blkg->dev); |
| 448 | |
| 449 | if (pn) |
| 450 | continue; |
| 451 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 452 | list_for_each_entry(blkiop, &blkio_list, list) |
| 453 | blkiop->ops.blkio_update_group_weight_fn(blkg, |
| 454 | blkcg->weight); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 455 | } |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 456 | spin_unlock_irq(&blkcg->lock); |
Gui Jianfeng | bcf4dd4 | 2010-02-01 09:58:54 +0100 | [diff] [blame] | 457 | spin_unlock(&blkio_list_lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 461 | static int |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 462 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 463 | { |
| 464 | struct blkio_cgroup *blkcg; |
| 465 | struct blkio_group *blkg; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 466 | struct blkio_group_stats *stats; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 467 | struct hlist_node *n; |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 468 | uint64_t queued[BLKIO_STAT_TOTAL]; |
| 469 | int i; |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 470 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 471 | bool idling, waiting, empty; |
| 472 | unsigned long long now = sched_clock(); |
| 473 | #endif |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 474 | |
| 475 | blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 476 | spin_lock_irq(&blkcg->lock); |
| 477 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 478 | spin_lock(&blkg->stats_lock); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 479 | stats = &blkg->stats; |
| 480 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 481 | idling = blkio_blkg_idling(stats); |
| 482 | waiting = blkio_blkg_waiting(stats); |
| 483 | empty = blkio_blkg_empty(stats); |
| 484 | #endif |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 485 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 486 | queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i]; |
| 487 | memset(stats, 0, sizeof(struct blkio_group_stats)); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 488 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 489 | stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i]; |
| 490 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 491 | if (idling) { |
| 492 | blkio_mark_blkg_idling(stats); |
| 493 | stats->start_idle_time = now; |
| 494 | } |
| 495 | if (waiting) { |
| 496 | blkio_mark_blkg_waiting(stats); |
| 497 | stats->start_group_wait_time = now; |
| 498 | } |
| 499 | if (empty) { |
| 500 | blkio_mark_blkg_empty(stats); |
| 501 | stats->start_empty_time = now; |
| 502 | } |
| 503 | #endif |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 504 | spin_unlock(&blkg->stats_lock); |
| 505 | } |
| 506 | spin_unlock_irq(&blkcg->lock); |
| 507 | return 0; |
| 508 | } |
| 509 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 510 | static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str, |
| 511 | int chars_left, bool diskname_only) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 512 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 513 | snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev)); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 514 | chars_left -= strlen(str); |
| 515 | if (chars_left <= 0) { |
| 516 | printk(KERN_WARNING |
| 517 | "Possibly incorrect cgroup stat display format"); |
| 518 | return; |
| 519 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 520 | if (diskname_only) |
| 521 | return; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 522 | switch (type) { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 523 | case BLKIO_STAT_READ: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 524 | strlcat(str, " Read", chars_left); |
| 525 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 526 | case BLKIO_STAT_WRITE: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 527 | strlcat(str, " Write", chars_left); |
| 528 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 529 | case BLKIO_STAT_SYNC: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 530 | strlcat(str, " Sync", chars_left); |
| 531 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 532 | case BLKIO_STAT_ASYNC: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 533 | strlcat(str, " Async", chars_left); |
| 534 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 535 | case BLKIO_STAT_TOTAL: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 536 | strlcat(str, " Total", chars_left); |
| 537 | break; |
| 538 | default: |
| 539 | strlcat(str, " Invalid", chars_left); |
| 540 | } |
| 541 | } |
| 542 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 543 | static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val, |
| 544 | struct cgroup_map_cb *cb, dev_t dev) |
| 545 | { |
| 546 | blkio_get_key_name(0, dev, str, chars_left, true); |
| 547 | cb->fill(cb, str, val); |
| 548 | return val; |
| 549 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 550 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 551 | /* This should be called with blkg->stats_lock held */ |
| 552 | static uint64_t blkio_get_stat(struct blkio_group *blkg, |
| 553 | struct cgroup_map_cb *cb, dev_t dev, enum stat_type type) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 554 | { |
| 555 | uint64_t disk_total; |
| 556 | char key_str[MAX_KEY_LEN]; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 557 | enum stat_sub_type sub_type; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 558 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 559 | if (type == BLKIO_STAT_TIME) |
| 560 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 561 | blkg->stats.time, cb, dev); |
| 562 | if (type == BLKIO_STAT_SECTORS) |
| 563 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 564 | blkg->stats.sectors, cb, dev); |
| 565 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 566 | if (type == BLKIO_STAT_AVG_QUEUE_SIZE) { |
| 567 | uint64_t sum = blkg->stats.avg_queue_size_sum; |
| 568 | uint64_t samples = blkg->stats.avg_queue_size_samples; |
| 569 | if (samples) |
| 570 | do_div(sum, samples); |
| 571 | else |
| 572 | sum = 0; |
| 573 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev); |
| 574 | } |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 575 | if (type == BLKIO_STAT_GROUP_WAIT_TIME) |
| 576 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 577 | blkg->stats.group_wait_time, cb, dev); |
| 578 | if (type == BLKIO_STAT_IDLE_TIME) |
| 579 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 580 | blkg->stats.idle_time, cb, dev); |
| 581 | if (type == BLKIO_STAT_EMPTY_TIME) |
| 582 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 583 | blkg->stats.empty_time, cb, dev); |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 584 | if (type == BLKIO_STAT_DEQUEUE) |
| 585 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 586 | blkg->stats.dequeue, cb, dev); |
| 587 | #endif |
| 588 | |
| 589 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; |
| 590 | sub_type++) { |
| 591 | blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false); |
| 592 | cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 593 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 594 | disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] + |
| 595 | blkg->stats.stat_arr[type][BLKIO_STAT_WRITE]; |
| 596 | 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] | 597 | cb->fill(cb, key_str, disk_total); |
| 598 | return disk_total; |
| 599 | } |
| 600 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 601 | #define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 602 | static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 603 | struct cftype *cftype, struct cgroup_map_cb *cb) \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 604 | { \ |
| 605 | struct blkio_cgroup *blkcg; \ |
| 606 | struct blkio_group *blkg; \ |
| 607 | struct hlist_node *n; \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 608 | uint64_t cgroup_total = 0; \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 609 | \ |
| 610 | if (!cgroup_lock_live_group(cgroup)) \ |
| 611 | return -ENODEV; \ |
| 612 | \ |
| 613 | blkcg = cgroup_to_blkio_cgroup(cgroup); \ |
| 614 | rcu_read_lock(); \ |
| 615 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 616 | if (blkg->dev) { \ |
| 617 | spin_lock_irq(&blkg->stats_lock); \ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 618 | cgroup_total += blkio_get_stat(blkg, cb, \ |
| 619 | blkg->dev, type); \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 620 | spin_unlock_irq(&blkg->stats_lock); \ |
| 621 | } \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 622 | } \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 623 | if (show_total) \ |
| 624 | cb->fill(cb, "Total", cgroup_total); \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 625 | rcu_read_unlock(); \ |
| 626 | cgroup_unlock(); \ |
| 627 | return 0; \ |
| 628 | } |
| 629 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 630 | SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0); |
| 631 | SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0); |
| 632 | SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1); |
| 633 | SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1); |
| 634 | SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1); |
| 635 | SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 636 | SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 637 | SHOW_FUNCTION_PER_GROUP(io_queued, BLKIO_STAT_QUEUED, 1); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 638 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 639 | SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0); |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 640 | SHOW_FUNCTION_PER_GROUP(avg_queue_size, BLKIO_STAT_AVG_QUEUE_SIZE, 0); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 641 | SHOW_FUNCTION_PER_GROUP(group_wait_time, BLKIO_STAT_GROUP_WAIT_TIME, 0); |
| 642 | SHOW_FUNCTION_PER_GROUP(idle_time, BLKIO_STAT_IDLE_TIME, 0); |
| 643 | SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 644 | #endif |
| 645 | #undef SHOW_FUNCTION_PER_GROUP |
| 646 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 647 | static int blkio_check_dev_num(dev_t dev) |
| 648 | { |
| 649 | int part = 0; |
| 650 | struct gendisk *disk; |
| 651 | |
| 652 | disk = get_gendisk(dev, &part); |
| 653 | if (!disk || part) |
| 654 | return -ENODEV; |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int blkio_policy_parse_and_set(char *buf, |
| 660 | struct blkio_policy_node *newpn) |
| 661 | { |
| 662 | char *s[4], *p, *major_s = NULL, *minor_s = NULL; |
| 663 | int ret; |
| 664 | unsigned long major, minor, temp; |
| 665 | int i = 0; |
| 666 | dev_t dev; |
| 667 | |
| 668 | memset(s, 0, sizeof(s)); |
| 669 | |
| 670 | while ((p = strsep(&buf, " ")) != NULL) { |
| 671 | if (!*p) |
| 672 | continue; |
| 673 | |
| 674 | s[i++] = p; |
| 675 | |
| 676 | /* Prevent from inputing too many things */ |
| 677 | if (i == 3) |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | if (i != 2) |
| 682 | return -EINVAL; |
| 683 | |
| 684 | p = strsep(&s[0], ":"); |
| 685 | if (p != NULL) |
| 686 | major_s = p; |
| 687 | else |
| 688 | return -EINVAL; |
| 689 | |
| 690 | minor_s = s[0]; |
| 691 | if (!minor_s) |
| 692 | return -EINVAL; |
| 693 | |
| 694 | ret = strict_strtoul(major_s, 10, &major); |
| 695 | if (ret) |
| 696 | return -EINVAL; |
| 697 | |
| 698 | ret = strict_strtoul(minor_s, 10, &minor); |
| 699 | if (ret) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | dev = MKDEV(major, minor); |
| 703 | |
| 704 | ret = blkio_check_dev_num(dev); |
| 705 | if (ret) |
| 706 | return ret; |
| 707 | |
| 708 | newpn->dev = dev; |
| 709 | |
| 710 | if (s[1] == NULL) |
| 711 | return -EINVAL; |
| 712 | |
| 713 | ret = strict_strtoul(s[1], 10, &temp); |
| 714 | if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) || |
| 715 | temp > BLKIO_WEIGHT_MAX) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | newpn->weight = temp; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg, |
| 724 | dev_t dev) |
| 725 | { |
| 726 | struct blkio_policy_node *pn; |
| 727 | |
| 728 | pn = blkio_policy_search_node(blkcg, dev); |
| 729 | if (pn) |
| 730 | return pn->weight; |
| 731 | else |
| 732 | return blkcg->weight; |
| 733 | } |
| 734 | EXPORT_SYMBOL_GPL(blkcg_get_weight); |
| 735 | |
| 736 | |
| 737 | static int blkiocg_weight_device_write(struct cgroup *cgrp, struct cftype *cft, |
| 738 | const char *buffer) |
| 739 | { |
| 740 | int ret = 0; |
| 741 | char *buf; |
| 742 | struct blkio_policy_node *newpn, *pn; |
| 743 | struct blkio_cgroup *blkcg; |
| 744 | struct blkio_group *blkg; |
| 745 | int keep_newpn = 0; |
| 746 | struct hlist_node *n; |
| 747 | struct blkio_policy_type *blkiop; |
| 748 | |
| 749 | buf = kstrdup(buffer, GFP_KERNEL); |
| 750 | if (!buf) |
| 751 | return -ENOMEM; |
| 752 | |
| 753 | newpn = kzalloc(sizeof(*newpn), GFP_KERNEL); |
| 754 | if (!newpn) { |
| 755 | ret = -ENOMEM; |
| 756 | goto free_buf; |
| 757 | } |
| 758 | |
| 759 | ret = blkio_policy_parse_and_set(buf, newpn); |
| 760 | if (ret) |
| 761 | goto free_newpn; |
| 762 | |
| 763 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 764 | |
| 765 | spin_lock_irq(&blkcg->lock); |
| 766 | |
| 767 | pn = blkio_policy_search_node(blkcg, newpn->dev); |
| 768 | if (!pn) { |
| 769 | if (newpn->weight != 0) { |
| 770 | blkio_policy_insert_node(blkcg, newpn); |
| 771 | keep_newpn = 1; |
| 772 | } |
| 773 | spin_unlock_irq(&blkcg->lock); |
| 774 | goto update_io_group; |
| 775 | } |
| 776 | |
| 777 | if (newpn->weight == 0) { |
| 778 | /* weight == 0 means deleteing a specific weight */ |
| 779 | blkio_policy_delete_node(pn); |
| 780 | spin_unlock_irq(&blkcg->lock); |
| 781 | goto update_io_group; |
| 782 | } |
| 783 | spin_unlock_irq(&blkcg->lock); |
| 784 | |
| 785 | pn->weight = newpn->weight; |
| 786 | |
| 787 | update_io_group: |
| 788 | /* update weight for each cfqg */ |
| 789 | spin_lock(&blkio_list_lock); |
| 790 | spin_lock_irq(&blkcg->lock); |
| 791 | |
| 792 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 793 | if (newpn->dev == blkg->dev) { |
| 794 | list_for_each_entry(blkiop, &blkio_list, list) |
| 795 | blkiop->ops.blkio_update_group_weight_fn(blkg, |
| 796 | newpn->weight ? |
| 797 | newpn->weight : |
| 798 | blkcg->weight); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | spin_unlock_irq(&blkcg->lock); |
| 803 | spin_unlock(&blkio_list_lock); |
| 804 | |
| 805 | free_newpn: |
| 806 | if (!keep_newpn) |
| 807 | kfree(newpn); |
| 808 | free_buf: |
| 809 | kfree(buf); |
| 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft, |
| 814 | struct seq_file *m) |
| 815 | { |
| 816 | struct blkio_cgroup *blkcg; |
| 817 | struct blkio_policy_node *pn; |
| 818 | |
| 819 | seq_printf(m, "dev\tweight\n"); |
| 820 | |
| 821 | blkcg = cgroup_to_blkio_cgroup(cgrp); |
| 822 | if (list_empty(&blkcg->policy_list)) |
| 823 | goto out; |
| 824 | |
| 825 | spin_lock_irq(&blkcg->lock); |
| 826 | list_for_each_entry(pn, &blkcg->policy_list, node) { |
| 827 | seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev), |
| 828 | MINOR(pn->dev), pn->weight); |
| 829 | } |
| 830 | spin_unlock_irq(&blkcg->lock); |
| 831 | |
| 832 | out: |
| 833 | return 0; |
| 834 | } |
| 835 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 836 | struct cftype blkio_files[] = { |
| 837 | { |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 838 | .name = "weight_device", |
| 839 | .read_seq_string = blkiocg_weight_device_read, |
| 840 | .write_string = blkiocg_weight_device_write, |
| 841 | .max_write_len = 256, |
| 842 | }, |
| 843 | { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 844 | .name = "weight", |
| 845 | .read_u64 = blkiocg_weight_read, |
| 846 | .write_u64 = blkiocg_weight_write, |
| 847 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 848 | { |
| 849 | .name = "time", |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 850 | .read_map = blkiocg_time_read, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 851 | }, |
| 852 | { |
| 853 | .name = "sectors", |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 854 | .read_map = blkiocg_sectors_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 855 | }, |
| 856 | { |
| 857 | .name = "io_service_bytes", |
| 858 | .read_map = blkiocg_io_service_bytes_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 859 | }, |
| 860 | { |
| 861 | .name = "io_serviced", |
| 862 | .read_map = blkiocg_io_serviced_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 863 | }, |
| 864 | { |
| 865 | .name = "io_service_time", |
| 866 | .read_map = blkiocg_io_service_time_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 867 | }, |
| 868 | { |
| 869 | .name = "io_wait_time", |
| 870 | .read_map = blkiocg_io_wait_time_read, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 871 | }, |
| 872 | { |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 873 | .name = "io_merged", |
| 874 | .read_map = blkiocg_io_merged_read, |
| 875 | }, |
| 876 | { |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 877 | .name = "io_queued", |
| 878 | .read_map = blkiocg_io_queued_read, |
| 879 | }, |
| 880 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 881 | .name = "reset_stats", |
| 882 | .write_u64 = blkiocg_reset_stats, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 883 | }, |
| 884 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 885 | { |
| 886 | .name = "avg_queue_size", |
| 887 | .read_map = blkiocg_avg_queue_size_read, |
| 888 | }, |
| 889 | { |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 890 | .name = "group_wait_time", |
| 891 | .read_map = blkiocg_group_wait_time_read, |
| 892 | }, |
| 893 | { |
| 894 | .name = "idle_time", |
| 895 | .read_map = blkiocg_idle_time_read, |
| 896 | }, |
| 897 | { |
| 898 | .name = "empty_time", |
| 899 | .read_map = blkiocg_empty_time_read, |
| 900 | }, |
| 901 | { |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 902 | .name = "dequeue", |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 903 | .read_map = blkiocg_dequeue_read, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 904 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 905 | #endif |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 906 | }; |
| 907 | |
| 908 | static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 909 | { |
| 910 | return cgroup_add_files(cgroup, subsys, blkio_files, |
| 911 | ARRAY_SIZE(blkio_files)); |
| 912 | } |
| 913 | |
| 914 | static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 915 | { |
| 916 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 917 | unsigned long flags; |
| 918 | struct blkio_group *blkg; |
| 919 | void *key; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 920 | struct blkio_policy_type *blkiop; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 921 | struct blkio_policy_node *pn, *pntmp; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 922 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 923 | rcu_read_lock(); |
| 924 | remove_entry: |
| 925 | spin_lock_irqsave(&blkcg->lock, flags); |
| 926 | |
| 927 | if (hlist_empty(&blkcg->blkg_list)) { |
| 928 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 929 | goto done; |
| 930 | } |
| 931 | |
| 932 | blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group, |
| 933 | blkcg_node); |
| 934 | key = rcu_dereference(blkg->key); |
| 935 | __blkiocg_del_blkio_group(blkg); |
| 936 | |
| 937 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 938 | |
| 939 | /* |
| 940 | * This blkio_group is being unlinked as associated cgroup is going |
| 941 | * away. Let all the IO controlling policies know about this event. |
| 942 | * |
| 943 | * Currently this is static call to one io controlling policy. Once |
| 944 | * we have more policies in place, we need some dynamic registration |
| 945 | * of callback function. |
| 946 | */ |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 947 | spin_lock(&blkio_list_lock); |
| 948 | list_for_each_entry(blkiop, &blkio_list, list) |
| 949 | blkiop->ops.blkio_unlink_group_fn(key, blkg); |
| 950 | spin_unlock(&blkio_list_lock); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 951 | goto remove_entry; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 952 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 953 | done: |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 954 | list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) { |
| 955 | blkio_policy_delete_node(pn); |
| 956 | kfree(pn); |
| 957 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 958 | free_css_id(&blkio_subsys, &blkcg->css); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 959 | rcu_read_unlock(); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 960 | if (blkcg != &blkio_root_cgroup) |
| 961 | kfree(blkcg); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static struct cgroup_subsys_state * |
| 965 | blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 966 | { |
| 967 | struct blkio_cgroup *blkcg, *parent_blkcg; |
| 968 | |
| 969 | if (!cgroup->parent) { |
| 970 | blkcg = &blkio_root_cgroup; |
| 971 | goto done; |
| 972 | } |
| 973 | |
| 974 | /* Currently we do not support hierarchy deeper than two level (0,1) */ |
| 975 | parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent); |
| 976 | if (css_depth(&parent_blkcg->css) > 0) |
| 977 | return ERR_PTR(-EINVAL); |
| 978 | |
| 979 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
| 980 | if (!blkcg) |
| 981 | return ERR_PTR(-ENOMEM); |
| 982 | |
| 983 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; |
| 984 | done: |
| 985 | spin_lock_init(&blkcg->lock); |
| 986 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
| 987 | |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 988 | INIT_LIST_HEAD(&blkcg->policy_list); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 989 | return &blkcg->css; |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * We cannot support shared io contexts, as we have no mean to support |
| 994 | * two tasks with the same ioc in two different groups without major rework |
| 995 | * of the main cic data structures. For now we allow a task to change |
| 996 | * its cgroup only if it's the only owner of its ioc. |
| 997 | */ |
| 998 | static int blkiocg_can_attach(struct cgroup_subsys *subsys, |
| 999 | struct cgroup *cgroup, struct task_struct *tsk, |
| 1000 | bool threadgroup) |
| 1001 | { |
| 1002 | struct io_context *ioc; |
| 1003 | int ret = 0; |
| 1004 | |
| 1005 | /* task_lock() is needed to avoid races with exit_io_context() */ |
| 1006 | task_lock(tsk); |
| 1007 | ioc = tsk->io_context; |
| 1008 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) |
| 1009 | ret = -EINVAL; |
| 1010 | task_unlock(tsk); |
| 1011 | |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
| 1015 | static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup, |
| 1016 | struct cgroup *prev, struct task_struct *tsk, |
| 1017 | bool threadgroup) |
| 1018 | { |
| 1019 | struct io_context *ioc; |
| 1020 | |
| 1021 | task_lock(tsk); |
| 1022 | ioc = tsk->io_context; |
| 1023 | if (ioc) |
| 1024 | ioc->cgroup_changed = 1; |
| 1025 | task_unlock(tsk); |
| 1026 | } |
| 1027 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1028 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
| 1029 | { |
| 1030 | spin_lock(&blkio_list_lock); |
| 1031 | list_add_tail(&blkiop->list, &blkio_list); |
| 1032 | spin_unlock(&blkio_list_lock); |
| 1033 | } |
| 1034 | EXPORT_SYMBOL_GPL(blkio_policy_register); |
| 1035 | |
| 1036 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) |
| 1037 | { |
| 1038 | spin_lock(&blkio_list_lock); |
| 1039 | list_del_init(&blkiop->list); |
| 1040 | spin_unlock(&blkio_list_lock); |
| 1041 | } |
| 1042 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1043 | |
| 1044 | static int __init init_cgroup_blkio(void) |
| 1045 | { |
| 1046 | return cgroup_load_subsys(&blkio_subsys); |
| 1047 | } |
| 1048 | |
| 1049 | static void __exit exit_cgroup_blkio(void) |
| 1050 | { |
| 1051 | cgroup_unload_subsys(&blkio_subsys); |
| 1052 | } |
| 1053 | |
| 1054 | module_init(init_cgroup_blkio); |
| 1055 | module_exit(exit_cgroup_blkio); |
| 1056 | MODULE_LICENSE("GPL"); |