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