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> |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 19 | #include "blk-cgroup.h" |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 20 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 21 | #define MAX_KEY_LEN 100 |
| 22 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 23 | static DEFINE_SPINLOCK(blkio_list_lock); |
| 24 | static LIST_HEAD(blkio_list); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 25 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 26 | struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT }; |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 27 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
| 28 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 29 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *, |
| 30 | struct cgroup *); |
| 31 | static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *, |
| 32 | struct task_struct *, bool); |
| 33 | static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *, |
| 34 | struct cgroup *, struct task_struct *, bool); |
| 35 | static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *); |
| 36 | static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *); |
| 37 | |
| 38 | struct cgroup_subsys blkio_subsys = { |
| 39 | .name = "blkio", |
| 40 | .create = blkiocg_create, |
| 41 | .can_attach = blkiocg_can_attach, |
| 42 | .attach = blkiocg_attach, |
| 43 | .destroy = blkiocg_destroy, |
| 44 | .populate = blkiocg_populate, |
| 45 | #ifdef CONFIG_BLK_CGROUP |
| 46 | /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */ |
| 47 | .subsys_id = blkio_subsys_id, |
| 48 | #endif |
| 49 | .use_id = 1, |
| 50 | .module = THIS_MODULE, |
| 51 | }; |
| 52 | EXPORT_SYMBOL_GPL(blkio_subsys); |
| 53 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 54 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
| 55 | { |
| 56 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), |
| 57 | struct blkio_cgroup, css); |
| 58 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 59 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 60 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 61 | void blkio_group_init(struct blkio_group *blkg) |
| 62 | { |
| 63 | spin_lock_init(&blkg->stats_lock); |
| 64 | } |
| 65 | EXPORT_SYMBOL_GPL(blkio_group_init); |
| 66 | |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 67 | /* |
| 68 | * Add to the appropriate stat variable depending on the request type. |
| 69 | * This should be called with the blkg->stats_lock held. |
| 70 | */ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 71 | static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction, |
| 72 | bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 73 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 74 | if (direction) |
| 75 | stat[BLKIO_STAT_WRITE] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 76 | else |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 77 | stat[BLKIO_STAT_READ] += add; |
| 78 | if (sync) |
| 79 | stat[BLKIO_STAT_SYNC] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 80 | else |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 81 | stat[BLKIO_STAT_ASYNC] += add; |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 84 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time) |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 85 | { |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 86 | unsigned long flags; |
| 87 | |
| 88 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 89 | blkg->stats.time += time; |
| 90 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 91 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 92 | EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 93 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 94 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
| 95 | uint64_t bytes, bool direction, bool sync) |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 96 | { |
| 97 | struct blkio_group_stats *stats; |
| 98 | unsigned long flags; |
| 99 | |
| 100 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 101 | stats = &blkg->stats; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 102 | stats->sectors += bytes >> 9; |
| 103 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction, |
| 104 | sync); |
| 105 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes, |
| 106 | direction, sync); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 107 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 108 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 109 | EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 110 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 111 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
| 112 | 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] | 113 | { |
| 114 | struct blkio_group_stats *stats; |
| 115 | unsigned long flags; |
| 116 | unsigned long long now = sched_clock(); |
| 117 | |
| 118 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 119 | stats = &blkg->stats; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 120 | if (time_after64(now, io_start_time)) |
| 121 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME], |
| 122 | now - io_start_time, direction, sync); |
| 123 | if (time_after64(io_start_time, start_time)) |
| 124 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME], |
| 125 | io_start_time - start_time, direction, sync); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 126 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 127 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 128 | EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 129 | |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame^] | 130 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction, |
| 131 | bool sync) |
| 132 | { |
| 133 | unsigned long flags; |
| 134 | |
| 135 | spin_lock_irqsave(&blkg->stats_lock, flags); |
| 136 | blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction, |
| 137 | sync); |
| 138 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); |
| 141 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 142 | void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 143 | struct blkio_group *blkg, void *key, dev_t dev) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 144 | { |
| 145 | unsigned long flags; |
| 146 | |
| 147 | spin_lock_irqsave(&blkcg->lock, flags); |
| 148 | rcu_assign_pointer(blkg->key, key); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 149 | blkg->blkcg_id = css_id(&blkcg->css); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 150 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
| 151 | spin_unlock_irqrestore(&blkcg->lock, flags); |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 152 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 153 | /* Need to take css reference ? */ |
| 154 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
| 155 | #endif |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 156 | blkg->dev = dev; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 157 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 158 | EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 159 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 160 | static void __blkiocg_del_blkio_group(struct blkio_group *blkg) |
| 161 | { |
| 162 | hlist_del_init_rcu(&blkg->blkcg_node); |
| 163 | blkg->blkcg_id = 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1 |
| 168 | * indicating that blk_group was unhashed by the time we got to it. |
| 169 | */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 170 | int blkiocg_del_blkio_group(struct blkio_group *blkg) |
| 171 | { |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 172 | struct blkio_cgroup *blkcg; |
| 173 | unsigned long flags; |
| 174 | struct cgroup_subsys_state *css; |
| 175 | int ret = 1; |
| 176 | |
| 177 | rcu_read_lock(); |
| 178 | css = css_lookup(&blkio_subsys, blkg->blkcg_id); |
| 179 | if (!css) |
| 180 | goto out; |
| 181 | |
| 182 | blkcg = container_of(css, struct blkio_cgroup, css); |
| 183 | spin_lock_irqsave(&blkcg->lock, flags); |
| 184 | if (!hlist_unhashed(&blkg->blkcg_node)) { |
| 185 | __blkiocg_del_blkio_group(blkg); |
| 186 | ret = 0; |
| 187 | } |
| 188 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 189 | out: |
| 190 | rcu_read_unlock(); |
| 191 | return ret; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 192 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 193 | EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 194 | |
| 195 | /* called under rcu_read_lock(). */ |
| 196 | struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) |
| 197 | { |
| 198 | struct blkio_group *blkg; |
| 199 | struct hlist_node *n; |
| 200 | void *__key; |
| 201 | |
| 202 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 203 | __key = blkg->key; |
| 204 | if (__key == key) |
| 205 | return blkg; |
| 206 | } |
| 207 | |
| 208 | return NULL; |
| 209 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 210 | EXPORT_SYMBOL_GPL(blkiocg_lookup_group); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 211 | |
| 212 | #define SHOW_FUNCTION(__VAR) \ |
| 213 | static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \ |
| 214 | struct cftype *cftype) \ |
| 215 | { \ |
| 216 | struct blkio_cgroup *blkcg; \ |
| 217 | \ |
| 218 | blkcg = cgroup_to_blkio_cgroup(cgroup); \ |
| 219 | return (u64)blkcg->__VAR; \ |
| 220 | } |
| 221 | |
| 222 | SHOW_FUNCTION(weight); |
| 223 | #undef SHOW_FUNCTION |
| 224 | |
| 225 | static int |
| 226 | blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
| 227 | { |
| 228 | struct blkio_cgroup *blkcg; |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 229 | struct blkio_group *blkg; |
| 230 | struct hlist_node *n; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 231 | struct blkio_policy_type *blkiop; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 232 | |
| 233 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | blkcg = cgroup_to_blkio_cgroup(cgroup); |
Gui Jianfeng | bcf4dd4 | 2010-02-01 09:58:54 +0100 | [diff] [blame] | 237 | spin_lock(&blkio_list_lock); |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 238 | spin_lock_irq(&blkcg->lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 239 | blkcg->weight = (unsigned int)val; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 240 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 241 | list_for_each_entry(blkiop, &blkio_list, list) |
| 242 | blkiop->ops.blkio_update_group_weight_fn(blkg, |
| 243 | blkcg->weight); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 244 | } |
Vivek Goyal | f8d461d | 2009-12-03 12:59:52 -0500 | [diff] [blame] | 245 | spin_unlock_irq(&blkcg->lock); |
Gui Jianfeng | bcf4dd4 | 2010-02-01 09:58:54 +0100 | [diff] [blame] | 246 | spin_unlock(&blkio_list_lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 250 | static int |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 251 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 252 | { |
| 253 | struct blkio_cgroup *blkcg; |
| 254 | struct blkio_group *blkg; |
| 255 | struct hlist_node *n; |
| 256 | struct blkio_group_stats *stats; |
| 257 | |
| 258 | blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 259 | spin_lock_irq(&blkcg->lock); |
| 260 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
| 261 | spin_lock(&blkg->stats_lock); |
| 262 | stats = &blkg->stats; |
| 263 | memset(stats, 0, sizeof(struct blkio_group_stats)); |
| 264 | spin_unlock(&blkg->stats_lock); |
| 265 | } |
| 266 | spin_unlock_irq(&blkcg->lock); |
| 267 | return 0; |
| 268 | } |
| 269 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 270 | static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str, |
| 271 | int chars_left, bool diskname_only) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 272 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 273 | snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev)); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 274 | chars_left -= strlen(str); |
| 275 | if (chars_left <= 0) { |
| 276 | printk(KERN_WARNING |
| 277 | "Possibly incorrect cgroup stat display format"); |
| 278 | return; |
| 279 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 280 | if (diskname_only) |
| 281 | return; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 282 | switch (type) { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 283 | case BLKIO_STAT_READ: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 284 | strlcat(str, " Read", chars_left); |
| 285 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 286 | case BLKIO_STAT_WRITE: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 287 | strlcat(str, " Write", chars_left); |
| 288 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 289 | case BLKIO_STAT_SYNC: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 290 | strlcat(str, " Sync", chars_left); |
| 291 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 292 | case BLKIO_STAT_ASYNC: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 293 | strlcat(str, " Async", chars_left); |
| 294 | break; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 295 | case BLKIO_STAT_TOTAL: |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 296 | strlcat(str, " Total", chars_left); |
| 297 | break; |
| 298 | default: |
| 299 | strlcat(str, " Invalid", chars_left); |
| 300 | } |
| 301 | } |
| 302 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 303 | static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val, |
| 304 | struct cgroup_map_cb *cb, dev_t dev) |
| 305 | { |
| 306 | blkio_get_key_name(0, dev, str, chars_left, true); |
| 307 | cb->fill(cb, str, val); |
| 308 | return val; |
| 309 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 310 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 311 | /* This should be called with blkg->stats_lock held */ |
| 312 | static uint64_t blkio_get_stat(struct blkio_group *blkg, |
| 313 | struct cgroup_map_cb *cb, dev_t dev, enum stat_type type) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 314 | { |
| 315 | uint64_t disk_total; |
| 316 | char key_str[MAX_KEY_LEN]; |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 317 | enum stat_sub_type sub_type; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 318 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 319 | if (type == BLKIO_STAT_TIME) |
| 320 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 321 | blkg->stats.time, cb, dev); |
| 322 | if (type == BLKIO_STAT_SECTORS) |
| 323 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 324 | blkg->stats.sectors, cb, dev); |
| 325 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 326 | if (type == BLKIO_STAT_DEQUEUE) |
| 327 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
| 328 | blkg->stats.dequeue, cb, dev); |
| 329 | #endif |
| 330 | |
| 331 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; |
| 332 | sub_type++) { |
| 333 | blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false); |
| 334 | cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 335 | } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 336 | disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] + |
| 337 | blkg->stats.stat_arr[type][BLKIO_STAT_WRITE]; |
| 338 | 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] | 339 | cb->fill(cb, key_str, disk_total); |
| 340 | return disk_total; |
| 341 | } |
| 342 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 343 | #define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 344 | static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 345 | struct cftype *cftype, struct cgroup_map_cb *cb) \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 346 | { \ |
| 347 | struct blkio_cgroup *blkcg; \ |
| 348 | struct blkio_group *blkg; \ |
| 349 | struct hlist_node *n; \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 350 | uint64_t cgroup_total = 0; \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 351 | \ |
| 352 | if (!cgroup_lock_live_group(cgroup)) \ |
| 353 | return -ENODEV; \ |
| 354 | \ |
| 355 | blkcg = cgroup_to_blkio_cgroup(cgroup); \ |
| 356 | rcu_read_lock(); \ |
| 357 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 358 | if (blkg->dev) { \ |
| 359 | spin_lock_irq(&blkg->stats_lock); \ |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 360 | cgroup_total += blkio_get_stat(blkg, cb, \ |
| 361 | blkg->dev, type); \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 362 | spin_unlock_irq(&blkg->stats_lock); \ |
| 363 | } \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 364 | } \ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 365 | if (show_total) \ |
| 366 | cb->fill(cb, "Total", cgroup_total); \ |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 367 | rcu_read_unlock(); \ |
| 368 | cgroup_unlock(); \ |
| 369 | return 0; \ |
| 370 | } |
| 371 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 372 | SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0); |
| 373 | SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0); |
| 374 | SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1); |
| 375 | SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1); |
| 376 | SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1); |
| 377 | SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame^] | 378 | SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 379 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 380 | SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 381 | #endif |
| 382 | #undef SHOW_FUNCTION_PER_GROUP |
| 383 | |
| 384 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 385 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 386 | unsigned long dequeue) |
| 387 | { |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 388 | blkg->stats.dequeue += dequeue; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 389 | } |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 390 | EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats); |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 391 | #endif |
| 392 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 393 | struct cftype blkio_files[] = { |
| 394 | { |
| 395 | .name = "weight", |
| 396 | .read_u64 = blkiocg_weight_read, |
| 397 | .write_u64 = blkiocg_weight_write, |
| 398 | }, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 399 | { |
| 400 | .name = "time", |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 401 | .read_map = blkiocg_time_read, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 402 | }, |
| 403 | { |
| 404 | .name = "sectors", |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 405 | .read_map = blkiocg_sectors_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 406 | }, |
| 407 | { |
| 408 | .name = "io_service_bytes", |
| 409 | .read_map = blkiocg_io_service_bytes_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 410 | }, |
| 411 | { |
| 412 | .name = "io_serviced", |
| 413 | .read_map = blkiocg_io_serviced_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 414 | }, |
| 415 | { |
| 416 | .name = "io_service_time", |
| 417 | .read_map = blkiocg_io_service_time_read, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 418 | }, |
| 419 | { |
| 420 | .name = "io_wait_time", |
| 421 | .read_map = blkiocg_io_wait_time_read, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 422 | }, |
| 423 | { |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame^] | 424 | .name = "io_merged", |
| 425 | .read_map = blkiocg_io_merged_read, |
| 426 | }, |
| 427 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 428 | .name = "reset_stats", |
| 429 | .write_u64 = blkiocg_reset_stats, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 430 | }, |
| 431 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 432 | { |
| 433 | .name = "dequeue", |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 434 | .read_map = blkiocg_dequeue_read, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 435 | }, |
| 436 | #endif |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 440 | { |
| 441 | return cgroup_add_files(cgroup, subsys, blkio_files, |
| 442 | ARRAY_SIZE(blkio_files)); |
| 443 | } |
| 444 | |
| 445 | static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 446 | { |
| 447 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 448 | unsigned long flags; |
| 449 | struct blkio_group *blkg; |
| 450 | void *key; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 451 | struct blkio_policy_type *blkiop; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 452 | |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 453 | rcu_read_lock(); |
| 454 | remove_entry: |
| 455 | spin_lock_irqsave(&blkcg->lock, flags); |
| 456 | |
| 457 | if (hlist_empty(&blkcg->blkg_list)) { |
| 458 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 459 | goto done; |
| 460 | } |
| 461 | |
| 462 | blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group, |
| 463 | blkcg_node); |
| 464 | key = rcu_dereference(blkg->key); |
| 465 | __blkiocg_del_blkio_group(blkg); |
| 466 | |
| 467 | spin_unlock_irqrestore(&blkcg->lock, flags); |
| 468 | |
| 469 | /* |
| 470 | * This blkio_group is being unlinked as associated cgroup is going |
| 471 | * away. Let all the IO controlling policies know about this event. |
| 472 | * |
| 473 | * Currently this is static call to one io controlling policy. Once |
| 474 | * we have more policies in place, we need some dynamic registration |
| 475 | * of callback function. |
| 476 | */ |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 477 | spin_lock(&blkio_list_lock); |
| 478 | list_for_each_entry(blkiop, &blkio_list, list) |
| 479 | blkiop->ops.blkio_unlink_group_fn(key, blkg); |
| 480 | spin_unlock(&blkio_list_lock); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 481 | goto remove_entry; |
| 482 | done: |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 483 | free_css_id(&blkio_subsys, &blkcg->css); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 484 | rcu_read_unlock(); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 485 | if (blkcg != &blkio_root_cgroup) |
| 486 | kfree(blkcg); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static struct cgroup_subsys_state * |
| 490 | blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup) |
| 491 | { |
| 492 | struct blkio_cgroup *blkcg, *parent_blkcg; |
| 493 | |
| 494 | if (!cgroup->parent) { |
| 495 | blkcg = &blkio_root_cgroup; |
| 496 | goto done; |
| 497 | } |
| 498 | |
| 499 | /* Currently we do not support hierarchy deeper than two level (0,1) */ |
| 500 | parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent); |
| 501 | if (css_depth(&parent_blkcg->css) > 0) |
| 502 | return ERR_PTR(-EINVAL); |
| 503 | |
| 504 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
| 505 | if (!blkcg) |
| 506 | return ERR_PTR(-ENOMEM); |
| 507 | |
| 508 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; |
| 509 | done: |
| 510 | spin_lock_init(&blkcg->lock); |
| 511 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
| 512 | |
| 513 | return &blkcg->css; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * We cannot support shared io contexts, as we have no mean to support |
| 518 | * two tasks with the same ioc in two different groups without major rework |
| 519 | * of the main cic data structures. For now we allow a task to change |
| 520 | * its cgroup only if it's the only owner of its ioc. |
| 521 | */ |
| 522 | static int blkiocg_can_attach(struct cgroup_subsys *subsys, |
| 523 | struct cgroup *cgroup, struct task_struct *tsk, |
| 524 | bool threadgroup) |
| 525 | { |
| 526 | struct io_context *ioc; |
| 527 | int ret = 0; |
| 528 | |
| 529 | /* task_lock() is needed to avoid races with exit_io_context() */ |
| 530 | task_lock(tsk); |
| 531 | ioc = tsk->io_context; |
| 532 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) |
| 533 | ret = -EINVAL; |
| 534 | task_unlock(tsk); |
| 535 | |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup, |
| 540 | struct cgroup *prev, struct task_struct *tsk, |
| 541 | bool threadgroup) |
| 542 | { |
| 543 | struct io_context *ioc; |
| 544 | |
| 545 | task_lock(tsk); |
| 546 | ioc = tsk->io_context; |
| 547 | if (ioc) |
| 548 | ioc->cgroup_changed = 1; |
| 549 | task_unlock(tsk); |
| 550 | } |
| 551 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 552 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
| 553 | { |
| 554 | spin_lock(&blkio_list_lock); |
| 555 | list_add_tail(&blkiop->list, &blkio_list); |
| 556 | spin_unlock(&blkio_list_lock); |
| 557 | } |
| 558 | EXPORT_SYMBOL_GPL(blkio_policy_register); |
| 559 | |
| 560 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) |
| 561 | { |
| 562 | spin_lock(&blkio_list_lock); |
| 563 | list_del_init(&blkiop->list); |
| 564 | spin_unlock(&blkio_list_lock); |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 567 | |
| 568 | static int __init init_cgroup_blkio(void) |
| 569 | { |
| 570 | return cgroup_load_subsys(&blkio_subsys); |
| 571 | } |
| 572 | |
| 573 | static void __exit exit_cgroup_blkio(void) |
| 574 | { |
| 575 | cgroup_unload_subsys(&blkio_subsys); |
| 576 | } |
| 577 | |
| 578 | module_init(init_cgroup_blkio); |
| 579 | module_exit(exit_cgroup_blkio); |
| 580 | MODULE_LICENSE("GPL"); |