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