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