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