blob: 4fdeb46b44361d33d8a9d2087c6887d66290edce [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001/*
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 Goyal22084192009-12-03 12:59:49 -050014#include <linux/seq_file.h>
15#include <linux/kdev_t.h>
Vivek Goyal9d6a9862009-12-04 10:36:41 -050016#include <linux/module.h>
Stephen Rothwellaccee782009-12-07 19:29:39 +110017#include <linux/err.h>
Divyesh Shah91952912010-04-01 15:01:41 -070018#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Gui Jianfeng34d0f172010-04-13 16:05:49 +080020#include <linux/genhd.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080021#include <linux/delay.h>
Tejun Heo9a9e8a22012-03-19 15:10:56 -070022#include <linux/atomic.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080023#include "blk-cgroup.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080024#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050025
Divyesh Shah84c124d2010-04-09 08:31:19 +020026#define MAX_KEY_LEN 100
27
Vivek Goyal3e252062009-12-04 10:36:42 -050028static DEFINE_SPINLOCK(blkio_list_lock);
29static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050030
Tejun Heo923adde2012-03-05 13:15:13 -080031static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
Vivek Goyal1cd9e032012-03-08 10:53:56 -080034/* List of groups pending per cpu stats allocation */
35static DEFINE_SPINLOCK(alloc_list_lock);
36static LIST_HEAD(alloc_list);
37
38static void blkio_stat_alloc_fn(struct work_struct *);
39static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
40
Vivek Goyal31e4c282009-12-03 12:59:42 -050041struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050042EXPORT_SYMBOL_GPL(blkio_root_cgroup);
43
Tejun Heo035d10b2012-03-05 13:15:04 -080044static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
45
Vivek Goyal062a6442010-09-15 17:06:33 -040046/* for encoding cft->private value on file */
47#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
48/* What policy owns the file, proportional or throttle */
49#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
50#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
51
Vivek Goyal31e4c282009-12-03 12:59:42 -050052struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
53{
54 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
55 struct blkio_cgroup, css);
56}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050057EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050058
Tejun Heo4f85cb92012-03-05 13:15:28 -080059static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020060{
61 return container_of(task_subsys_state(tsk, blkio_subsys_id),
62 struct blkio_cgroup, css);
63}
Tejun Heo4f85cb92012-03-05 13:15:28 -080064
65struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
66{
67 if (bio && bio->bi_css)
68 return container_of(bio->bi_css, struct blkio_cgroup, css);
69 return task_blkio_cgroup(current);
70}
71EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020072
Tejun Heoc1768262012-03-05 13:15:17 -080073static inline void blkio_update_group_weight(struct blkio_group *blkg,
74 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040075{
76 struct blkio_policy_type *blkiop;
77
78 list_for_each_entry(blkiop, &blkio_list, list) {
79 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080080 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -040081 continue;
82 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080083 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020084 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040085 }
86}
87
Tejun Heoc1768262012-03-05 13:15:17 -080088static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
89 u64 bps, int fileid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040090{
91 struct blkio_policy_type *blkiop;
92
93 list_for_each_entry(blkiop, &blkio_list, list) {
94
95 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080096 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040097 continue;
98
99 if (fileid == BLKIO_THROTL_read_bps_device
100 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800101 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200102 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400103
104 if (fileid == BLKIO_THROTL_write_bps_device
105 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800106 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200107 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400108 }
109}
110
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400111static inline void blkio_update_group_iops(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800112 int plid, unsigned int iops,
113 int fileid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400114{
115 struct blkio_policy_type *blkiop;
116
117 list_for_each_entry(blkiop, &blkio_list, list) {
118
119 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800120 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400121 continue;
122
123 if (fileid == BLKIO_THROTL_read_iops_device
124 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800125 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200126 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400127
128 if (fileid == BLKIO_THROTL_write_iops_device
129 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800130 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200131 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400132 }
133}
134
Divyesh Shah91952912010-04-01 15:01:41 -0700135/*
136 * Add to the appropriate stat variable depending on the request type.
Tejun Heoedf1b872012-03-08 10:54:00 -0800137 * This should be called with queue_lock held.
Divyesh Shah91952912010-04-01 15:01:41 -0700138 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200139static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
140 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700141{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200142 if (direction)
143 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700144 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200145 stat[BLKIO_STAT_READ] += add;
146 if (sync)
147 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700148 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200149 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700150}
151
Divyesh Shahcdc11842010-04-08 21:15:10 -0700152/*
153 * Decrements the appropriate stat variable if non-zero depending on the
154 * request type. Panics on value being zero.
Tejun Heoedf1b872012-03-08 10:54:00 -0800155 * This should be called with the queue_lock held.
Divyesh Shahcdc11842010-04-08 21:15:10 -0700156 */
157static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
158{
159 if (direction) {
160 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
161 stat[BLKIO_STAT_WRITE]--;
162 } else {
163 BUG_ON(stat[BLKIO_STAT_READ] == 0);
164 stat[BLKIO_STAT_READ]--;
165 }
166 if (sync) {
167 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
168 stat[BLKIO_STAT_SYNC]--;
169 } else {
170 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
171 stat[BLKIO_STAT_ASYNC]--;
172 }
173}
174
175#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800176/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700177static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800178 struct blkio_policy_type *pol,
179 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700180{
Tejun Heoc1768262012-03-05 13:15:17 -0800181 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800182
183 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700184 return;
185 if (blkg == curr_blkg)
186 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800187 pd->stats.start_group_wait_time = sched_clock();
188 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700189}
190
Tejun Heoedf1b872012-03-08 10:54:00 -0800191/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700192static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
193{
194 unsigned long long now;
195
196 if (!blkio_blkg_waiting(stats))
197 return;
198
199 now = sched_clock();
200 if (time_after64(now, stats->start_group_wait_time))
201 stats->group_wait_time += now - stats->start_group_wait_time;
202 blkio_clear_blkg_waiting(stats);
203}
204
Tejun Heoedf1b872012-03-08 10:54:00 -0800205/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700206static void blkio_end_empty_time(struct blkio_group_stats *stats)
207{
208 unsigned long long now;
209
210 if (!blkio_blkg_empty(stats))
211 return;
212
213 now = sched_clock();
214 if (time_after64(now, stats->start_empty_time))
215 stats->empty_time += now - stats->start_empty_time;
216 blkio_clear_blkg_empty(stats);
217}
218
Tejun Heoc1768262012-03-05 13:15:17 -0800219void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
220 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700221{
Tejun Heoedf1b872012-03-08 10:54:00 -0800222 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700223
Tejun Heoedf1b872012-03-08 10:54:00 -0800224 lockdep_assert_held(blkg->q->queue_lock);
225 BUG_ON(blkio_blkg_idling(stats));
226
227 stats->start_idle_time = sched_clock();
228 blkio_mark_blkg_idling(stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700229}
230EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
231
Tejun Heoc1768262012-03-05 13:15:17 -0800232void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
233 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700234{
Tejun Heoedf1b872012-03-08 10:54:00 -0800235 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700236
Tejun Heoedf1b872012-03-08 10:54:00 -0800237 lockdep_assert_held(blkg->q->queue_lock);
238
Divyesh Shah812df482010-04-08 21:15:35 -0700239 if (blkio_blkg_idling(stats)) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800240 unsigned long long now = sched_clock();
241
242 if (time_after64(now, stats->start_idle_time)) {
243 u64_stats_update_begin(&stats->syncp);
Divyesh Shah812df482010-04-08 21:15:35 -0700244 stats->idle_time += now - stats->start_idle_time;
Tejun Heoedf1b872012-03-08 10:54:00 -0800245 u64_stats_update_end(&stats->syncp);
246 }
Divyesh Shah812df482010-04-08 21:15:35 -0700247 blkio_clear_blkg_idling(stats);
248 }
Divyesh Shah812df482010-04-08 21:15:35 -0700249}
250EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
251
Tejun Heoc1768262012-03-05 13:15:17 -0800252void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
253 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700254{
Tejun Heoedf1b872012-03-08 10:54:00 -0800255 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700256
Tejun Heoedf1b872012-03-08 10:54:00 -0800257 lockdep_assert_held(blkg->q->queue_lock);
258
259 u64_stats_update_begin(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700260 stats->avg_queue_size_sum +=
261 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
262 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
263 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700264 blkio_update_group_wait_time(stats);
Tejun Heoedf1b872012-03-08 10:54:00 -0800265 u64_stats_update_end(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700266}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200267EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
268
Tejun Heoc1768262012-03-05 13:15:17 -0800269void blkiocg_set_start_empty_time(struct blkio_group *blkg,
270 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200271{
Tejun Heoedf1b872012-03-08 10:54:00 -0800272 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200273
Tejun Heoedf1b872012-03-08 10:54:00 -0800274 lockdep_assert_held(blkg->q->queue_lock);
Divyesh Shah28baf442010-04-14 11:22:38 +0200275
276 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
Tejun Heoedf1b872012-03-08 10:54:00 -0800277 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE])
Divyesh Shah28baf442010-04-14 11:22:38 +0200278 return;
Divyesh Shah28baf442010-04-14 11:22:38 +0200279
280 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200281 * group is already marked empty. This can happen if cfqq got new
282 * request in parent group and moved to this group while being added
283 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200284 */
Tejun Heoedf1b872012-03-08 10:54:00 -0800285 if (blkio_blkg_empty(stats))
Vivek Goyale5ff0822010-04-26 19:25:11 +0200286 return;
Vivek Goyale5ff0822010-04-26 19:25:11 +0200287
Divyesh Shah28baf442010-04-14 11:22:38 +0200288 stats->start_empty_time = sched_clock();
289 blkio_mark_blkg_empty(stats);
Divyesh Shah28baf442010-04-14 11:22:38 +0200290}
291EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
292
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200293void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800294 struct blkio_policy_type *pol,
295 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200296{
Tejun Heoc1768262012-03-05 13:15:17 -0800297 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800298
Tejun Heoedf1b872012-03-08 10:54:00 -0800299 lockdep_assert_held(blkg->q->queue_lock);
300
Tejun Heo549d3aa2012-03-05 13:15:16 -0800301 pd->stats.dequeue += dequeue;
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200302}
303EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700304#else
305static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800306 struct blkio_policy_type *pol,
307 struct blkio_group *curr_blkg) { }
308static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700309#endif
310
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200311void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800312 struct blkio_policy_type *pol,
313 struct blkio_group *curr_blkg, bool direction,
314 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700315{
Tejun Heoedf1b872012-03-08 10:54:00 -0800316 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700317
Tejun Heoedf1b872012-03-08 10:54:00 -0800318 lockdep_assert_held(blkg->q->queue_lock);
319
320 u64_stats_update_begin(&stats->syncp);
321 blkio_add_stat(stats->stat_arr[BLKIO_STAT_QUEUED], 1, direction, sync);
322 blkio_end_empty_time(stats);
323 u64_stats_update_end(&stats->syncp);
324
Tejun Heoc1768262012-03-05 13:15:17 -0800325 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700326}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200327EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700328
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200329void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800330 struct blkio_policy_type *pol,
331 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700332{
Tejun Heoedf1b872012-03-08 10:54:00 -0800333 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700334
Tejun Heoedf1b872012-03-08 10:54:00 -0800335 lockdep_assert_held(blkg->q->queue_lock);
336
337 u64_stats_update_begin(&stats->syncp);
338 blkio_check_and_dec_stat(stats->stat_arr[BLKIO_STAT_QUEUED], direction,
339 sync);
340 u64_stats_update_end(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700341}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200342EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700343
Tejun Heoc1768262012-03-05 13:15:17 -0800344void blkiocg_update_timeslice_used(struct blkio_group *blkg,
345 struct blkio_policy_type *pol,
346 unsigned long time,
347 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500348{
Tejun Heoedf1b872012-03-08 10:54:00 -0800349 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700350
Tejun Heoedf1b872012-03-08 10:54:00 -0800351 lockdep_assert_held(blkg->q->queue_lock);
352
353 u64_stats_update_begin(&stats->syncp);
354 stats->time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400355#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800356 stats->unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400357#endif
Tejun Heoedf1b872012-03-08 10:54:00 -0800358 u64_stats_update_end(&stats->syncp);
Vivek Goyal22084192009-12-03 12:59:49 -0500359}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700360EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500361
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400362/*
363 * should be called under rcu read lock or queue lock to make sure blkg pointer
364 * is valid.
365 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200366void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800367 struct blkio_policy_type *pol,
368 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700369{
Tejun Heoc1768262012-03-05 13:15:17 -0800370 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400371 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400372 unsigned long flags;
373
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800374 /* If per cpu stats are not allocated yet, don't do any accounting. */
375 if (pd->stats_cpu == NULL)
376 return;
377
Vivek Goyal575969a2011-05-19 15:38:29 -0400378 /*
379 * Disabling interrupts to provide mutual exclusion between two
380 * writes on same cpu. It probably is not needed for 64bit. Not
381 * optimizing that case yet.
382 */
383 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700384
Tejun Heo549d3aa2012-03-05 13:15:16 -0800385 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400386
Vivek Goyal575969a2011-05-19 15:38:29 -0400387 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400388 stats_cpu->sectors += bytes >> 9;
389 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
390 1, direction, sync);
391 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
392 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400393 u64_stats_update_end(&stats_cpu->syncp);
394 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700395}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200396EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700397
Divyesh Shah84c124d2010-04-09 08:31:19 +0200398void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800399 struct blkio_policy_type *pol,
400 uint64_t start_time,
401 uint64_t io_start_time, bool direction,
402 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700403{
Tejun Heoedf1b872012-03-08 10:54:00 -0800404 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah91952912010-04-01 15:01:41 -0700405 unsigned long long now = sched_clock();
406
Tejun Heoedf1b872012-03-08 10:54:00 -0800407 lockdep_assert_held(blkg->q->queue_lock);
408
409 u64_stats_update_begin(&stats->syncp);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200410 if (time_after64(now, io_start_time))
411 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
412 now - io_start_time, direction, sync);
413 if (time_after64(io_start_time, start_time))
414 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
415 io_start_time - start_time, direction, sync);
Tejun Heoedf1b872012-03-08 10:54:00 -0800416 u64_stats_update_end(&stats->syncp);
Divyesh Shah91952912010-04-01 15:01:41 -0700417}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200418EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700419
Vivek Goyal317389a2011-05-23 10:02:19 +0200420/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800421void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
422 struct blkio_policy_type *pol,
423 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700424{
Tejun Heoedf1b872012-03-08 10:54:00 -0800425 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812d4022010-04-08 21:14:23 -0700426
Tejun Heoedf1b872012-03-08 10:54:00 -0800427 lockdep_assert_held(blkg->q->queue_lock);
428
429 u64_stats_update_begin(&stats->syncp);
Tejun Heo5fe224d2012-03-08 10:53:57 -0800430 blkio_add_stat(stats->stat_arr[BLKIO_STAT_MERGED], 1, direction, sync);
Tejun Heoedf1b872012-03-08 10:54:00 -0800431 u64_stats_update_end(&stats->syncp);
Divyesh Shah812d4022010-04-08 21:14:23 -0700432}
433EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
434
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800435/*
436 * Worker for allocating per cpu stat for blk groups. This is scheduled on
437 * the system_nrt_wq once there are some groups on the alloc_list waiting
438 * for allocation.
439 */
440static void blkio_stat_alloc_fn(struct work_struct *work)
441{
442 static void *pcpu_stats[BLKIO_NR_POLICIES];
443 struct delayed_work *dwork = to_delayed_work(work);
444 struct blkio_group *blkg;
445 int i;
446 bool empty = false;
447
448alloc_stats:
449 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
450 if (pcpu_stats[i] != NULL)
451 continue;
452
453 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
454
455 /* Allocation failed. Try again after some time. */
456 if (pcpu_stats[i] == NULL) {
457 queue_delayed_work(system_nrt_wq, dwork,
458 msecs_to_jiffies(10));
459 return;
460 }
461 }
462
463 spin_lock_irq(&blkio_list_lock);
464 spin_lock(&alloc_list_lock);
465
466 /* cgroup got deleted or queue exited. */
467 if (!list_empty(&alloc_list)) {
468 blkg = list_first_entry(&alloc_list, struct blkio_group,
469 alloc_node);
470 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
471 struct blkg_policy_data *pd = blkg->pd[i];
472
473 if (blkio_policy[i] && pd && !pd->stats_cpu)
474 swap(pd->stats_cpu, pcpu_stats[i]);
475 }
476
477 list_del_init(&blkg->alloc_node);
478 }
479
480 empty = list_empty(&alloc_list);
481
482 spin_unlock(&alloc_list_lock);
483 spin_unlock_irq(&blkio_list_lock);
484
485 if (!empty)
486 goto alloc_stats;
487}
488
Tejun Heo03814112012-03-05 13:15:14 -0800489/**
490 * blkg_free - free a blkg
491 * @blkg: blkg to free
492 *
493 * Free @blkg which may be partially allocated.
494 */
495static void blkg_free(struct blkio_group *blkg)
496{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800497 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800498
499 if (!blkg)
500 return;
501
Tejun Heoe8989fa2012-03-05 13:15:20 -0800502 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
503 struct blkg_policy_data *pd = blkg->pd[i];
504
505 if (pd) {
506 free_percpu(pd->stats_cpu);
507 kfree(pd);
508 }
Tejun Heo03814112012-03-05 13:15:14 -0800509 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800510
Tejun Heo549d3aa2012-03-05 13:15:16 -0800511 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800512}
513
514/**
515 * blkg_alloc - allocate a blkg
516 * @blkcg: block cgroup the new blkg is associated with
517 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800518 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800519 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800520 */
521static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800522 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800523{
524 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800525 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800526
527 /* alloc and init base part */
528 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
529 if (!blkg)
530 return NULL;
531
Tejun Heoc875f4d2012-03-05 13:15:22 -0800532 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800533 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800534 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800535 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800536 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800537 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
538
Tejun Heoe8989fa2012-03-05 13:15:20 -0800539 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
540 struct blkio_policy_type *pol = blkio_policy[i];
541 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800542
Tejun Heoe8989fa2012-03-05 13:15:20 -0800543 if (!pol)
544 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800545
Tejun Heoe8989fa2012-03-05 13:15:20 -0800546 /* alloc per-policy data and attach it to blkg */
547 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
548 q->node);
549 if (!pd) {
550 blkg_free(blkg);
551 return NULL;
552 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800553
Tejun Heoe8989fa2012-03-05 13:15:20 -0800554 blkg->pd[i] = pd;
555 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800556 }
557
Tejun Heo549d3aa2012-03-05 13:15:16 -0800558 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800559 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
560 struct blkio_policy_type *pol = blkio_policy[i];
561
562 if (pol)
563 pol->ops.blkio_init_group_fn(blkg);
564 }
565
Tejun Heo03814112012-03-05 13:15:14 -0800566 return blkg;
567}
568
Tejun Heocd1604f2012-03-05 13:15:06 -0800569struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
570 struct request_queue *q,
571 enum blkio_policy_id plid,
572 bool for_root)
573 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400574{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800575 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400576
Tejun Heocd1604f2012-03-05 13:15:06 -0800577 WARN_ON_ONCE(!rcu_read_lock_held());
578 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500579
Tejun Heocd1604f2012-03-05 13:15:06 -0800580 /*
581 * This could be the first entry point of blkcg implementation and
582 * we shouldn't allow anything to go through for a bypassing queue.
583 * The following can be removed if blkg lookup is guaranteed to
584 * fail on a bypassing queue.
585 */
586 if (unlikely(blk_queue_bypass(q)) && !for_root)
587 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
588
Tejun Heoe8989fa2012-03-05 13:15:20 -0800589 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800590 if (blkg)
591 return blkg;
592
Tejun Heo7ee9c562012-03-05 13:15:11 -0800593 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800594 if (!css_tryget(&blkcg->css))
595 return ERR_PTR(-EINVAL);
596
597 /*
598 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800599 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800600 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800601
602 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800603 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800604 blkg = ERR_PTR(-ENOMEM);
605 goto out;
606 }
607
608 /* insert */
609 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500610 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800611 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800612 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800613
614 spin_lock(&alloc_list_lock);
615 list_add(&blkg->alloc_node, &alloc_list);
616 /* Queue per cpu stat allocation from worker thread. */
617 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
618 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800619out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800620 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500621}
Tejun Heocd1604f2012-03-05 13:15:06 -0800622EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500623
Vivek Goyal31e4c282009-12-03 12:59:42 -0500624/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800625struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800626 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500627{
628 struct blkio_group *blkg;
629 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500630
Tejun Heoca32aef2012-03-05 13:15:03 -0800631 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800632 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500633 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500634 return NULL;
635}
Tejun Heocd1604f2012-03-05 13:15:06 -0800636EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500637
Tejun Heoe8989fa2012-03-05 13:15:20 -0800638static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800639{
Tejun Heo03aa2642012-03-05 13:15:19 -0800640 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800641 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800642
643 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800644 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800645
646 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800647 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800648 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800649 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800650 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800651
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800652 spin_lock(&alloc_list_lock);
653 list_del_init(&blkg->alloc_node);
654 spin_unlock(&alloc_list_lock);
655
Tejun Heo03aa2642012-03-05 13:15:19 -0800656 /*
657 * Put the reference taken at the time of creation so that when all
658 * queues are gone, group can be destroyed.
659 */
660 blkg_put(blkg);
661}
662
Tejun Heoe8989fa2012-03-05 13:15:20 -0800663/*
664 * XXX: This updates blkg policy data in-place for root blkg, which is
665 * necessary across elevator switch and policy registration as root blkgs
666 * aren't shot down. This broken and racy implementation is temporary.
667 * Eventually, blkg shoot down will be replaced by proper in-place update.
668 */
669void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
670{
671 struct blkio_policy_type *pol = blkio_policy[plid];
672 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
673 struct blkg_policy_data *pd;
674
675 if (!blkg)
676 return;
677
678 kfree(blkg->pd[plid]);
679 blkg->pd[plid] = NULL;
680
681 if (!pol)
682 return;
683
684 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
685 WARN_ON_ONCE(!pd);
686
687 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
688 WARN_ON_ONCE(!pd->stats_cpu);
689
690 blkg->pd[plid] = pd;
691 pd->blkg = blkg;
692 pol->ops.blkio_init_group_fn(blkg);
693}
694EXPORT_SYMBOL_GPL(update_root_blkg_pd);
695
Tejun Heo9f13ef62012-03-05 13:15:21 -0800696/**
697 * blkg_destroy_all - destroy all blkgs associated with a request_queue
698 * @q: request_queue of interest
699 * @destroy_root: whether to destroy root blkg or not
700 *
701 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
702 * destroyed; otherwise, root blkg is left alone.
703 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800704void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800705{
706 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800707
Tejun Heo9f13ef62012-03-05 13:15:21 -0800708 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800709
Tejun Heo9f13ef62012-03-05 13:15:21 -0800710 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
711 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800712
Tejun Heo9f13ef62012-03-05 13:15:21 -0800713 /* skip root? */
714 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
715 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800716
Tejun Heo9f13ef62012-03-05 13:15:21 -0800717 spin_lock(&blkcg->lock);
718 blkg_destroy(blkg);
719 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800720 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800721
722 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800723}
Tejun Heo03aa2642012-03-05 13:15:19 -0800724EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800725
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800726static void blkg_rcu_free(struct rcu_head *rcu_head)
727{
728 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
729}
730
731void __blkg_release(struct blkio_group *blkg)
732{
733 /* release the extra blkcg reference this blkg has been holding */
734 css_put(&blkg->blkcg->css);
735
736 /*
737 * A group is freed in rcu manner. But having an rcu lock does not
738 * mean that one can access all the fields of blkg and assume these
739 * are valid. For example, don't try to follow throtl_data and
740 * request queue links.
741 *
742 * Having a reference to blkg under an rcu allows acess to only
743 * values local to groups like group stats and group rate limits
744 */
745 call_rcu(&blkg->rcu_head, blkg_rcu_free);
746}
747EXPORT_SYMBOL_GPL(__blkg_release);
748
Tejun Heoc1768262012-03-05 13:15:17 -0800749static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400750{
Tejun Heoc1768262012-03-05 13:15:17 -0800751 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800752 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800753
754 if (pd->stats_cpu == NULL)
755 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800756
757 for_each_possible_cpu(cpu) {
758 struct blkio_group_stats_cpu *sc =
759 per_cpu_ptr(pd->stats_cpu, cpu);
760
761 sc->sectors = 0;
762 memset(sc->stat_arr_cpu, 0, sizeof(sc->stat_arr_cpu));
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400763 }
764}
765
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700766static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200767blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700768{
Tejun Heo997a0262012-03-08 10:53:58 -0800769 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700770 struct blkio_group *blkg;
771 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700772 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700773
Tejun Heoe8989fa2012-03-05 13:15:20 -0800774 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700775 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800776
777 /*
778 * Note that stat reset is racy - it doesn't synchronize against
779 * stat updates. This is a debug feature which shouldn't exist
780 * anyway. If you get hit by a race, retry.
781 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700782 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800783 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800784
Tejun Heoe8989fa2012-03-05 13:15:20 -0800785 list_for_each_entry(pol, &blkio_list, list) {
786 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800787 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400788
Tejun Heo997a0262012-03-08 10:53:58 -0800789 /* queued stats shouldn't be cleared */
790 for (i = 0; i < ARRAY_SIZE(stats->stat_arr); i++)
791 if (i != BLKIO_STAT_QUEUED)
792 memset(stats->stat_arr[i], 0,
793 sizeof(stats->stat_arr[i]));
794 stats->time = 0;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800795#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo997a0262012-03-08 10:53:58 -0800796 memset((void *)stats + BLKG_STATS_DEBUG_CLEAR_START, 0,
797 BLKG_STATS_DEBUG_CLEAR_SIZE);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800798#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800799 blkio_reset_stats_cpu(blkg, pol->plid);
800 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700801 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400802
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700803 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800804 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700805 return 0;
806}
807
Tejun Heo7a4dd282012-03-05 13:15:09 -0800808static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
809 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700810{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800811 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700812 chars_left -= strlen(str);
813 if (chars_left <= 0) {
814 printk(KERN_WARNING
815 "Possibly incorrect cgroup stat display format");
816 return;
817 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200818 if (diskname_only)
819 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700820 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200821 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700822 strlcat(str, " Read", chars_left);
823 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200824 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700825 strlcat(str, " Write", chars_left);
826 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200827 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700828 strlcat(str, " Sync", chars_left);
829 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200830 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700831 strlcat(str, " Async", chars_left);
832 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200833 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700834 strlcat(str, " Total", chars_left);
835 break;
836 default:
837 strlcat(str, " Invalid", chars_left);
838 }
839}
840
Tejun Heoc1768262012-03-05 13:15:17 -0800841static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400842 enum stat_type_cpu type, enum stat_sub_type sub_type)
843{
Tejun Heoc1768262012-03-05 13:15:17 -0800844 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400845 int cpu;
846 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400847 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400848
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800849 if (pd->stats_cpu == NULL)
850 return val;
851
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400852 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400853 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800854 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400855
Vivek Goyal575969a2011-05-19 15:38:29 -0400856 do {
857 start = u64_stats_fetch_begin(&stats_cpu->syncp);
858 if (type == BLKIO_STAT_CPU_SECTORS)
859 tval = stats_cpu->sectors;
860 else
861 tval = stats_cpu->stat_arr_cpu[type][sub_type];
862 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
863
864 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400865 }
866
867 return val;
868}
869
Tejun Heoc1768262012-03-05 13:15:17 -0800870static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800871 struct cgroup_map_cb *cb, const char *dname,
872 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400873{
874 uint64_t disk_total, val;
875 char key_str[MAX_KEY_LEN];
876 enum stat_sub_type sub_type;
877
878 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800879 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heoc4c76a02012-03-08 10:53:59 -0800880 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
881 cb->fill(cb, key_str, val);
882 return val;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400883 }
884
885 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
886 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800887 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
888 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800889 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400890 cb->fill(cb, key_str, val);
891 }
892
Tejun Heoc1768262012-03-05 13:15:17 -0800893 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
894 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400895
Tejun Heo7a4dd282012-03-05 13:15:09 -0800896 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
897 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400898 cb->fill(cb, key_str, disk_total);
899 return disk_total;
900}
901
Tejun Heoc1768262012-03-05 13:15:17 -0800902static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800903 struct cgroup_map_cb *cb, const char *dname,
904 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700905{
Tejun Heoc4c76a02012-03-08 10:53:59 -0800906 struct blkio_group_stats *stats = &blkg->pd[plid]->stats;
907 uint64_t v = 0, disk_total = 0;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700908 char key_str[MAX_KEY_LEN];
Tejun Heoedf1b872012-03-08 10:54:00 -0800909 unsigned int sync_start;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800910 int st;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700911
Tejun Heoc4c76a02012-03-08 10:53:59 -0800912 if (type >= BLKIO_STAT_ARR_NR) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800913 do {
914 sync_start = u64_stats_fetch_begin(&stats->syncp);
915 switch (type) {
916 case BLKIO_STAT_TIME:
917 v = stats->time;
918 break;
Justin TerAvest9026e522011-03-22 21:26:54 +0100919#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800920 case BLKIO_STAT_UNACCOUNTED_TIME:
921 v = stats->unaccounted_time;
922 break;
923 case BLKIO_STAT_AVG_QUEUE_SIZE: {
924 uint64_t samples = stats->avg_queue_size_samples;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200925
Tejun Heoedf1b872012-03-08 10:54:00 -0800926 if (samples) {
927 v = stats->avg_queue_size_sum;
928 do_div(v, samples);
929 }
930 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800931 }
Tejun Heoedf1b872012-03-08 10:54:00 -0800932 case BLKIO_STAT_IDLE_TIME:
933 v = stats->idle_time;
934 break;
935 case BLKIO_STAT_EMPTY_TIME:
936 v = stats->empty_time;
937 break;
938 case BLKIO_STAT_DEQUEUE:
939 v = stats->dequeue;
940 break;
941 case BLKIO_STAT_GROUP_WAIT_TIME:
942 v = stats->group_wait_time;
943 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800944#endif
Tejun Heoedf1b872012-03-08 10:54:00 -0800945 default:
946 WARN_ON_ONCE(1);
947 }
948 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
Tejun Heoc4c76a02012-03-08 10:53:59 -0800949
950 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
951 cb->fill(cb, key_str, v);
952 return v;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700953 }
Tejun Heoc4c76a02012-03-08 10:53:59 -0800954
955 for (st = BLKIO_STAT_READ; st < BLKIO_STAT_TOTAL; st++) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800956 do {
957 sync_start = u64_stats_fetch_begin(&stats->syncp);
958 v = stats->stat_arr[type][st];
959 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
Tejun Heoc4c76a02012-03-08 10:53:59 -0800960
961 blkio_get_key_name(st, dname, key_str, MAX_KEY_LEN, false);
962 cb->fill(cb, key_str, v);
963 if (st == BLKIO_STAT_READ || st == BLKIO_STAT_WRITE)
964 disk_total += v;
965 }
966
Tejun Heo7a4dd282012-03-05 13:15:09 -0800967 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
968 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700969 cb->fill(cb, key_str, disk_total);
970 return disk_total;
971}
972
Tejun Heo4bfd4822012-03-05 13:15:08 -0800973static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
974 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800975{
Tejun Heoece84242011-10-19 14:31:15 +0200976 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800977 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800978 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800979 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200980 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200981 int i = 0, ret = -EINVAL;
982 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800983 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200984 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800985
986 memset(s, 0, sizeof(s));
987
988 while ((p = strsep(&buf, " ")) != NULL) {
989 if (!*p)
990 continue;
991
992 s[i++] = p;
993
994 /* Prevent from inputing too many things */
995 if (i == 3)
996 break;
997 }
998
999 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001000 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001001
1002 p = strsep(&s[0], ":");
1003 if (p != NULL)
1004 major_s = p;
1005 else
Tejun Heoece84242011-10-19 14:31:15 +02001006 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001007
1008 minor_s = s[0];
1009 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001010 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001011
Tejun Heoece84242011-10-19 14:31:15 +02001012 if (strict_strtoul(major_s, 10, &major))
1013 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001014
Tejun Heoece84242011-10-19 14:31:15 +02001015 if (strict_strtoul(minor_s, 10, &minor))
1016 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001017
1018 dev = MKDEV(major, minor);
1019
Tejun Heoece84242011-10-19 14:31:15 +02001020 if (strict_strtoull(s[1], 10, &temp))
1021 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001022
Tejun Heoe56da7e2012-03-05 13:15:07 -08001023 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001024 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001025 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001026
1027 rcu_read_lock();
1028
Tejun Heo4bfd4822012-03-05 13:15:08 -08001029 spin_lock_irq(disk->queue->queue_lock);
1030 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1031 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001032
Tejun Heo4bfd4822012-03-05 13:15:08 -08001033 if (IS_ERR(blkg)) {
1034 ret = PTR_ERR(blkg);
1035 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001036 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001037
Tejun Heo549d3aa2012-03-05 13:15:16 -08001038 pd = blkg->pd[plid];
1039
Vivek Goyal062a6442010-09-15 17:06:33 -04001040 switch (plid) {
1041 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001042 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1043 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001044 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001045
Tejun Heo549d3aa2012-03-05 13:15:16 -08001046 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001047 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001048 break;
1049 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001050 switch(fileid) {
1051 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001052 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001053 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001054 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001055 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001056 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001057 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001058 break;
1059 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001060 if (temp > THROTL_IOPS_MAX)
1061 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001062 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001063 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001064 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001065 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001066 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001067 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001068 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001069 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001070 break;
1071 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001072 break;
1073 default:
1074 BUG();
1075 }
Tejun Heoece84242011-10-19 14:31:15 +02001076 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001077out_unlock:
1078 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001079out:
1080 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001081
1082 /*
1083 * If queue was bypassing, we should retry. Do so after a short
1084 * msleep(). It isn't strictly necessary but queue can be
1085 * bypassing for some time and it's always nice to avoid busy
1086 * looping.
1087 */
1088 if (ret == -EBUSY) {
1089 msleep(10);
1090 return restart_syscall();
1091 }
Tejun Heoece84242011-10-19 14:31:15 +02001092 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001093}
1094
Vivek Goyal062a6442010-09-15 17:06:33 -04001095static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1096 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001097{
1098 int ret = 0;
1099 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001100 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001101 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1102 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001103
1104 buf = kstrdup(buffer, GFP_KERNEL);
1105 if (!buf)
1106 return -ENOMEM;
1107
Tejun Heo4bfd4822012-03-05 13:15:08 -08001108 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001109 kfree(buf);
1110 return ret;
1111}
1112
Vivek Goyal92616b52012-03-05 13:15:10 -08001113static const char *blkg_dev_name(struct blkio_group *blkg)
1114{
1115 /* some drivers (floppy) instantiate a queue w/o disk registered */
1116 if (blkg->q->backing_dev_info.dev)
1117 return dev_name(blkg->q->backing_dev_info.dev);
1118 return NULL;
1119}
1120
Tejun Heo4bfd4822012-03-05 13:15:08 -08001121static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1122 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001123{
Tejun Heoc1768262012-03-05 13:15:17 -08001124 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001125 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001126 struct blkg_policy_data *pd = blkg->pd[plid];
1127 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001128 int rw = WRITE;
1129
Vivek Goyal92616b52012-03-05 13:15:10 -08001130 if (!dname)
1131 return;
1132
Tejun Heoc1768262012-03-05 13:15:17 -08001133 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001134 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001135 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001136 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001137 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001138 break;
1139 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001140 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001141 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001142 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001143 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001144 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001145 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001146 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001147 break;
1148 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001149 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001150 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001151 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001152 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001153 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001154 break;
1155 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001156 break;
1157 default:
1158 BUG();
1159 }
1160}
1161
1162/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001163static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1164 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001165{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001166 struct blkio_group *blkg;
1167 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001168
Tejun Heo4bfd4822012-03-05 13:15:08 -08001169 spin_lock_irq(&blkcg->lock);
1170 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001171 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001172 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001173}
1174
1175static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1176 struct seq_file *m)
1177{
1178 struct blkio_cgroup *blkcg;
1179 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1180 int name = BLKIOFILE_ATTR(cft->private);
1181
1182 blkcg = cgroup_to_blkio_cgroup(cgrp);
1183
1184 switch(plid) {
1185 case BLKIO_POLICY_PROP:
1186 switch(name) {
1187 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001188 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001189 return 0;
1190 default:
1191 BUG();
1192 }
1193 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001194 case BLKIO_POLICY_THROTL:
1195 switch(name){
1196 case BLKIO_THROTL_read_bps_device:
1197 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001198 case BLKIO_THROTL_read_iops_device:
1199 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001200 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001201 return 0;
1202 default:
1203 BUG();
1204 }
1205 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001206 default:
1207 BUG();
1208 }
1209
1210 return 0;
1211}
1212
1213static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001214 struct cftype *cft, struct cgroup_map_cb *cb,
1215 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001216{
1217 struct blkio_group *blkg;
1218 struct hlist_node *n;
1219 uint64_t cgroup_total = 0;
1220
Tejun Heoc875f4d2012-03-05 13:15:22 -08001221 spin_lock_irq(&blkcg->lock);
1222
1223 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001224 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001225 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001226
Tejun Heoe8989fa2012-03-05 13:15:20 -08001227 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001228 continue;
Tejun Heoedf1b872012-03-08 10:54:00 -08001229 if (pcpu)
Tejun Heoc1768262012-03-05 13:15:17 -08001230 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1231 cb, dname, type);
Tejun Heoedf1b872012-03-08 10:54:00 -08001232 else
Tejun Heoc1768262012-03-05 13:15:17 -08001233 cgroup_total += blkio_get_stat(blkg, plid,
1234 cb, dname, type);
Vivek Goyal062a6442010-09-15 17:06:33 -04001235 }
1236 if (show_total)
1237 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001238
1239 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001240 return 0;
1241}
1242
1243/* All map kind of cgroup file get serviced by this function */
1244static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1245 struct cgroup_map_cb *cb)
1246{
1247 struct blkio_cgroup *blkcg;
1248 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1249 int name = BLKIOFILE_ATTR(cft->private);
1250
1251 blkcg = cgroup_to_blkio_cgroup(cgrp);
1252
1253 switch(plid) {
1254 case BLKIO_POLICY_PROP:
1255 switch(name) {
1256 case BLKIO_PROP_time:
1257 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001258 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001259 case BLKIO_PROP_sectors:
1260 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001261 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001262 case BLKIO_PROP_io_service_bytes:
1263 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001264 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001265 case BLKIO_PROP_io_serviced:
1266 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001267 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001268 case BLKIO_PROP_io_service_time:
1269 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001270 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001271 case BLKIO_PROP_io_wait_time:
1272 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001273 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001274 case BLKIO_PROP_io_merged:
1275 return blkio_read_blkg_stats(blkcg, cft, cb,
Tejun Heo5fe224d2012-03-08 10:53:57 -08001276 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001277 case BLKIO_PROP_io_queued:
1278 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001279 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001280#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001281 case BLKIO_PROP_unaccounted_time:
1282 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001283 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001284 case BLKIO_PROP_dequeue:
1285 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001286 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001287 case BLKIO_PROP_avg_queue_size:
1288 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001289 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001290 case BLKIO_PROP_group_wait_time:
1291 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001292 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001293 case BLKIO_PROP_idle_time:
1294 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001295 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001296 case BLKIO_PROP_empty_time:
1297 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001298 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001299#endif
1300 default:
1301 BUG();
1302 }
1303 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001304 case BLKIO_POLICY_THROTL:
1305 switch(name){
1306 case BLKIO_THROTL_io_service_bytes:
1307 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001308 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001309 case BLKIO_THROTL_io_serviced:
1310 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001311 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001312 default:
1313 BUG();
1314 }
1315 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001316 default:
1317 BUG();
1318 }
1319
1320 return 0;
1321}
1322
Tejun Heo4bfd4822012-03-05 13:15:08 -08001323static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001324{
1325 struct blkio_group *blkg;
1326 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001327
1328 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1329 return -EINVAL;
1330
1331 spin_lock(&blkio_list_lock);
1332 spin_lock_irq(&blkcg->lock);
1333 blkcg->weight = (unsigned int)val;
1334
Tejun Heo549d3aa2012-03-05 13:15:16 -08001335 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001336 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001337
Tejun Heoe8989fa2012-03-05 13:15:20 -08001338 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001339 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001340 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001341
Vivek Goyal062a6442010-09-15 17:06:33 -04001342 spin_unlock_irq(&blkcg->lock);
1343 spin_unlock(&blkio_list_lock);
1344 return 0;
1345}
1346
1347static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1348 struct blkio_cgroup *blkcg;
1349 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1350 int name = BLKIOFILE_ATTR(cft->private);
1351
1352 blkcg = cgroup_to_blkio_cgroup(cgrp);
1353
1354 switch(plid) {
1355 case BLKIO_POLICY_PROP:
1356 switch(name) {
1357 case BLKIO_PROP_weight:
1358 return (u64)blkcg->weight;
1359 }
1360 break;
1361 default:
1362 BUG();
1363 }
1364 return 0;
1365}
1366
1367static int
1368blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1369{
1370 struct blkio_cgroup *blkcg;
1371 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1372 int name = BLKIOFILE_ATTR(cft->private);
1373
1374 blkcg = cgroup_to_blkio_cgroup(cgrp);
1375
1376 switch(plid) {
1377 case BLKIO_POLICY_PROP:
1378 switch(name) {
1379 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001380 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001381 }
1382 break;
1383 default:
1384 BUG();
1385 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001386
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001387 return 0;
1388}
1389
Vivek Goyal31e4c282009-12-03 12:59:42 -05001390struct cftype blkio_files[] = {
1391 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001392 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001393 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1394 BLKIO_PROP_weight_device),
1395 .read_seq_string = blkiocg_file_read,
1396 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001397 .max_write_len = 256,
1398 },
1399 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001400 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001401 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1402 BLKIO_PROP_weight),
1403 .read_u64 = blkiocg_file_read_u64,
1404 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001405 },
Vivek Goyal22084192009-12-03 12:59:49 -05001406 {
1407 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001408 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1409 BLKIO_PROP_time),
1410 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001411 },
1412 {
1413 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001414 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1415 BLKIO_PROP_sectors),
1416 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001417 },
1418 {
1419 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001420 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1421 BLKIO_PROP_io_service_bytes),
1422 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001423 },
1424 {
1425 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001426 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1427 BLKIO_PROP_io_serviced),
1428 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001429 },
1430 {
1431 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001432 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1433 BLKIO_PROP_io_service_time),
1434 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001435 },
1436 {
1437 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001438 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1439 BLKIO_PROP_io_wait_time),
1440 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001441 },
1442 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001443 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001444 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1445 BLKIO_PROP_io_merged),
1446 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001447 },
1448 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001449 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001450 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1451 BLKIO_PROP_io_queued),
1452 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001453 },
1454 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001455 .name = "reset_stats",
1456 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001457 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001458#ifdef CONFIG_BLK_DEV_THROTTLING
1459 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001460 .name = "throttle.read_bps_device",
1461 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1462 BLKIO_THROTL_read_bps_device),
1463 .read_seq_string = blkiocg_file_read,
1464 .write_string = blkiocg_file_write,
1465 .max_write_len = 256,
1466 },
1467
1468 {
1469 .name = "throttle.write_bps_device",
1470 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1471 BLKIO_THROTL_write_bps_device),
1472 .read_seq_string = blkiocg_file_read,
1473 .write_string = blkiocg_file_write,
1474 .max_write_len = 256,
1475 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001476
1477 {
1478 .name = "throttle.read_iops_device",
1479 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1480 BLKIO_THROTL_read_iops_device),
1481 .read_seq_string = blkiocg_file_read,
1482 .write_string = blkiocg_file_write,
1483 .max_write_len = 256,
1484 },
1485
1486 {
1487 .name = "throttle.write_iops_device",
1488 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1489 BLKIO_THROTL_write_iops_device),
1490 .read_seq_string = blkiocg_file_read,
1491 .write_string = blkiocg_file_write,
1492 .max_write_len = 256,
1493 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001494 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001495 .name = "throttle.io_service_bytes",
1496 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1497 BLKIO_THROTL_io_service_bytes),
1498 .read_map = blkiocg_file_read_map,
1499 },
1500 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001501 .name = "throttle.io_serviced",
1502 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1503 BLKIO_THROTL_io_serviced),
1504 .read_map = blkiocg_file_read_map,
1505 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001506#endif /* CONFIG_BLK_DEV_THROTTLING */
1507
Vivek Goyal22084192009-12-03 12:59:49 -05001508#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001509 {
1510 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001511 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1512 BLKIO_PROP_avg_queue_size),
1513 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001514 },
1515 {
Divyesh Shah812df482010-04-08 21:15:35 -07001516 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001517 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1518 BLKIO_PROP_group_wait_time),
1519 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001520 },
1521 {
1522 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001523 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1524 BLKIO_PROP_idle_time),
1525 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001526 },
1527 {
1528 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001529 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1530 BLKIO_PROP_empty_time),
1531 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001532 },
1533 {
Vivek Goyal22084192009-12-03 12:59:49 -05001534 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001535 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1536 BLKIO_PROP_dequeue),
1537 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001538 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001539 {
1540 .name = "unaccounted_time",
1541 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1542 BLKIO_PROP_unaccounted_time),
1543 .read_map = blkiocg_file_read_map,
1544 },
Vivek Goyal22084192009-12-03 12:59:49 -05001545#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07001546 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001547};
1548
Tejun Heo9f13ef62012-03-05 13:15:21 -08001549/**
1550 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -08001551 * @cgroup: cgroup of interest
1552 *
1553 * This function is called when @cgroup is about to go away and responsible
1554 * for shooting down all blkgs associated with @cgroup. blkgs should be
1555 * removed while holding both q and blkcg locks. As blkcg lock is nested
1556 * inside q lock, this function performs reverse double lock dancing.
1557 *
1558 * This is the blkcg counterpart of ioc_release_fn().
1559 */
Tejun Heo959d8512012-04-01 12:30:01 -07001560static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001561{
1562 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1563
Tejun Heo9f13ef62012-03-05 13:15:21 -08001564 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001565
Tejun Heo9f13ef62012-03-05 13:15:21 -08001566 while (!hlist_empty(&blkcg->blkg_list)) {
1567 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1568 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001569 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001570
Tejun Heo9f13ef62012-03-05 13:15:21 -08001571 if (spin_trylock(q->queue_lock)) {
1572 blkg_destroy(blkg);
1573 spin_unlock(q->queue_lock);
1574 } else {
1575 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001576 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001577 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001578 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001579 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001580
Tejun Heo9f13ef62012-03-05 13:15:21 -08001581 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001582 return 0;
1583}
1584
Li Zefan761b3ef2012-01-31 13:47:36 +08001585static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -08001586{
1587 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1588
Ben Blum67523c42010-03-10 15:22:11 -08001589 if (blkcg != &blkio_root_cgroup)
1590 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001591}
1592
Li Zefan761b3ef2012-01-31 13:47:36 +08001593static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001594{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001595 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001596 struct blkio_cgroup *blkcg;
1597 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001598
Li Zefan03415092010-05-07 08:57:00 +02001599 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001600 blkcg = &blkio_root_cgroup;
1601 goto done;
1602 }
1603
Vivek Goyal31e4c282009-12-03 12:59:42 -05001604 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1605 if (!blkcg)
1606 return ERR_PTR(-ENOMEM);
1607
1608 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001609 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001610done:
1611 spin_lock_init(&blkcg->lock);
1612 INIT_HLIST_HEAD(&blkcg->blkg_list);
1613
1614 return &blkcg->css;
1615}
1616
Tejun Heo5efd6112012-03-05 13:15:12 -08001617/**
1618 * blkcg_init_queue - initialize blkcg part of request queue
1619 * @q: request_queue to initialize
1620 *
1621 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1622 * part of new request_queue @q.
1623 *
1624 * RETURNS:
1625 * 0 on success, -errno on failure.
1626 */
1627int blkcg_init_queue(struct request_queue *q)
1628{
Tejun Heo923adde2012-03-05 13:15:13 -08001629 int ret;
1630
Tejun Heo5efd6112012-03-05 13:15:12 -08001631 might_sleep();
1632
Tejun Heo923adde2012-03-05 13:15:13 -08001633 ret = blk_throtl_init(q);
1634 if (ret)
1635 return ret;
1636
1637 mutex_lock(&all_q_mutex);
1638 INIT_LIST_HEAD(&q->all_q_node);
1639 list_add_tail(&q->all_q_node, &all_q_list);
1640 mutex_unlock(&all_q_mutex);
1641
1642 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001643}
1644
1645/**
1646 * blkcg_drain_queue - drain blkcg part of request_queue
1647 * @q: request_queue to drain
1648 *
1649 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1650 */
1651void blkcg_drain_queue(struct request_queue *q)
1652{
1653 lockdep_assert_held(q->queue_lock);
1654
1655 blk_throtl_drain(q);
1656}
1657
1658/**
1659 * blkcg_exit_queue - exit and release blkcg part of request_queue
1660 * @q: request_queue being released
1661 *
1662 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1663 */
1664void blkcg_exit_queue(struct request_queue *q)
1665{
Tejun Heo923adde2012-03-05 13:15:13 -08001666 mutex_lock(&all_q_mutex);
1667 list_del_init(&q->all_q_node);
1668 mutex_unlock(&all_q_mutex);
1669
Tejun Heoe8989fa2012-03-05 13:15:20 -08001670 blkg_destroy_all(q, true);
1671
Tejun Heo5efd6112012-03-05 13:15:12 -08001672 blk_throtl_exit(q);
1673}
1674
Vivek Goyal31e4c282009-12-03 12:59:42 -05001675/*
1676 * We cannot support shared io contexts, as we have no mean to support
1677 * two tasks with the same ioc in two different groups without major rework
1678 * of the main cic data structures. For now we allow a task to change
1679 * its cgroup only if it's the only owner of its ioc.
1680 */
Li Zefan761b3ef2012-01-31 13:47:36 +08001681static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001682{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001683 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001684 struct io_context *ioc;
1685 int ret = 0;
1686
1687 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001688 cgroup_taskset_for_each(task, cgrp, tset) {
1689 task_lock(task);
1690 ioc = task->io_context;
1691 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1692 ret = -EINVAL;
1693 task_unlock(task);
1694 if (ret)
1695 break;
1696 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001697 return ret;
1698}
1699
Tejun Heo923adde2012-03-05 13:15:13 -08001700static void blkcg_bypass_start(void)
1701 __acquires(&all_q_mutex)
1702{
1703 struct request_queue *q;
1704
1705 mutex_lock(&all_q_mutex);
1706
1707 list_for_each_entry(q, &all_q_list, all_q_node) {
1708 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001709 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001710 }
1711}
1712
1713static void blkcg_bypass_end(void)
1714 __releases(&all_q_mutex)
1715{
1716 struct request_queue *q;
1717
1718 list_for_each_entry(q, &all_q_list, all_q_node)
1719 blk_queue_bypass_end(q);
1720
1721 mutex_unlock(&all_q_mutex);
1722}
1723
Tejun Heo676f7c82012-04-01 12:09:55 -07001724struct cgroup_subsys blkio_subsys = {
1725 .name = "blkio",
1726 .create = blkiocg_create,
1727 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -07001728 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001729 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001730 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001731 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -07001732 .module = THIS_MODULE,
1733};
1734EXPORT_SYMBOL_GPL(blkio_subsys);
1735
Vivek Goyal3e252062009-12-04 10:36:42 -05001736void blkio_policy_register(struct blkio_policy_type *blkiop)
1737{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001738 struct request_queue *q;
1739
Tejun Heo923adde2012-03-05 13:15:13 -08001740 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001741 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001742
1743 BUG_ON(blkio_policy[blkiop->plid]);
1744 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001745 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001746
Vivek Goyal3e252062009-12-04 10:36:42 -05001747 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001748 list_for_each_entry(q, &all_q_list, all_q_node)
1749 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001750 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001751}
1752EXPORT_SYMBOL_GPL(blkio_policy_register);
1753
1754void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1755{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001756 struct request_queue *q;
1757
Tejun Heo923adde2012-03-05 13:15:13 -08001758 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001759 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001760
1761 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1762 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001763 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001764
Vivek Goyal3e252062009-12-04 10:36:42 -05001765 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001766 list_for_each_entry(q, &all_q_list, all_q_node)
1767 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001768 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001769}
1770EXPORT_SYMBOL_GPL(blkio_policy_unregister);