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