blob: 09ac462ba89edadafc1272a997302affbd54dd4b [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 Shahcdc11842010-04-08 21:15:10 -0700135#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800136/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700137static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800138 struct blkio_policy_type *pol,
139 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700140{
Tejun Heoc1768262012-03-05 13:15:17 -0800141 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800142
143 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700144 return;
145 if (blkg == curr_blkg)
146 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800147 pd->stats.start_group_wait_time = sched_clock();
148 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700149}
150
Tejun Heoedf1b872012-03-08 10:54:00 -0800151/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700152static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
153{
154 unsigned long long now;
155
156 if (!blkio_blkg_waiting(stats))
157 return;
158
159 now = sched_clock();
160 if (time_after64(now, stats->start_group_wait_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700161 blkg_stat_add(&stats->group_wait_time,
162 now - stats->start_group_wait_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700163 blkio_clear_blkg_waiting(stats);
164}
165
Tejun Heoedf1b872012-03-08 10:54:00 -0800166/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700167static void blkio_end_empty_time(struct blkio_group_stats *stats)
168{
169 unsigned long long now;
170
171 if (!blkio_blkg_empty(stats))
172 return;
173
174 now = sched_clock();
175 if (time_after64(now, stats->start_empty_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700176 blkg_stat_add(&stats->empty_time,
177 now - stats->start_empty_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700178 blkio_clear_blkg_empty(stats);
179}
180
Tejun Heoc1768262012-03-05 13:15:17 -0800181void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
182 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700183{
Tejun Heoedf1b872012-03-08 10:54:00 -0800184 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700185
Tejun Heoedf1b872012-03-08 10:54:00 -0800186 lockdep_assert_held(blkg->q->queue_lock);
187 BUG_ON(blkio_blkg_idling(stats));
188
189 stats->start_idle_time = sched_clock();
190 blkio_mark_blkg_idling(stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700191}
192EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
193
Tejun Heoc1768262012-03-05 13:15:17 -0800194void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
195 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700196{
Tejun Heoedf1b872012-03-08 10:54:00 -0800197 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700198
Tejun Heoedf1b872012-03-08 10:54:00 -0800199 lockdep_assert_held(blkg->q->queue_lock);
200
Divyesh Shah812df482010-04-08 21:15:35 -0700201 if (blkio_blkg_idling(stats)) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800202 unsigned long long now = sched_clock();
203
Tejun Heoedcb0722012-04-01 14:38:42 -0700204 if (time_after64(now, stats->start_idle_time))
205 blkg_stat_add(&stats->idle_time,
206 now - stats->start_idle_time);
Divyesh Shah812df482010-04-08 21:15:35 -0700207 blkio_clear_blkg_idling(stats);
208 }
Divyesh Shah812df482010-04-08 21:15:35 -0700209}
210EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
211
Tejun Heoc1768262012-03-05 13:15:17 -0800212void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
213 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700214{
Tejun Heoedf1b872012-03-08 10:54:00 -0800215 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700216
Tejun Heoedf1b872012-03-08 10:54:00 -0800217 lockdep_assert_held(blkg->q->queue_lock);
218
Tejun Heoedcb0722012-04-01 14:38:42 -0700219 blkg_stat_add(&stats->avg_queue_size_sum,
220 blkg_rwstat_sum(&stats->queued));
221 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Divyesh Shah812df482010-04-08 21:15:35 -0700222 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700223}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200224EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
225
Tejun Heoc1768262012-03-05 13:15:17 -0800226void blkiocg_set_start_empty_time(struct blkio_group *blkg,
227 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200228{
Tejun Heoedf1b872012-03-08 10:54:00 -0800229 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200230
Tejun Heoedf1b872012-03-08 10:54:00 -0800231 lockdep_assert_held(blkg->q->queue_lock);
Divyesh Shah28baf442010-04-14 11:22:38 +0200232
Tejun Heoedcb0722012-04-01 14:38:42 -0700233 if (blkg_rwstat_sum(&stats->queued))
Divyesh Shah28baf442010-04-14 11:22:38 +0200234 return;
Divyesh Shah28baf442010-04-14 11:22:38 +0200235
236 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200237 * group is already marked empty. This can happen if cfqq got new
238 * request in parent group and moved to this group while being added
239 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200240 */
Tejun Heoedf1b872012-03-08 10:54:00 -0800241 if (blkio_blkg_empty(stats))
Vivek Goyale5ff0822010-04-26 19:25:11 +0200242 return;
Vivek Goyale5ff0822010-04-26 19:25:11 +0200243
Divyesh Shah28baf442010-04-14 11:22:38 +0200244 stats->start_empty_time = sched_clock();
245 blkio_mark_blkg_empty(stats);
Divyesh Shah28baf442010-04-14 11:22:38 +0200246}
247EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
248
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200249void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800250 struct blkio_policy_type *pol,
251 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200252{
Tejun Heoc1768262012-03-05 13:15:17 -0800253 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800254
Tejun Heoedf1b872012-03-08 10:54:00 -0800255 lockdep_assert_held(blkg->q->queue_lock);
256
Tejun Heoedcb0722012-04-01 14:38:42 -0700257 blkg_stat_add(&pd->stats.dequeue, dequeue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200258}
259EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700260#else
261static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800262 struct blkio_policy_type *pol,
263 struct blkio_group *curr_blkg) { }
264static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700265#endif
266
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200267void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800268 struct blkio_policy_type *pol,
269 struct blkio_group *curr_blkg, bool direction,
270 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700271{
Tejun Heoedf1b872012-03-08 10:54:00 -0800272 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700273 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700274
Tejun Heoedf1b872012-03-08 10:54:00 -0800275 lockdep_assert_held(blkg->q->queue_lock);
276
Tejun Heoedcb0722012-04-01 14:38:42 -0700277 blkg_rwstat_add(&stats->queued, rw, 1);
Tejun Heoedf1b872012-03-08 10:54:00 -0800278 blkio_end_empty_time(stats);
Tejun Heoc1768262012-03-05 13:15:17 -0800279 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700280}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200281EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700282
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200283void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800284 struct blkio_policy_type *pol,
285 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700286{
Tejun Heoedf1b872012-03-08 10:54:00 -0800287 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700288 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700289
Tejun Heoedf1b872012-03-08 10:54:00 -0800290 lockdep_assert_held(blkg->q->queue_lock);
291
Tejun Heoedcb0722012-04-01 14:38:42 -0700292 blkg_rwstat_add(&stats->queued, rw, -1);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700293}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200294EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700295
Tejun Heoc1768262012-03-05 13:15:17 -0800296void blkiocg_update_timeslice_used(struct blkio_group *blkg,
297 struct blkio_policy_type *pol,
298 unsigned long time,
299 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500300{
Tejun Heoedf1b872012-03-08 10:54:00 -0800301 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700302
Tejun Heoedf1b872012-03-08 10:54:00 -0800303 lockdep_assert_held(blkg->q->queue_lock);
304
Tejun Heoedcb0722012-04-01 14:38:42 -0700305 blkg_stat_add(&stats->time, time);
Vivek Goyala23e6862011-05-19 15:38:20 -0400306#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700307 blkg_stat_add(&stats->unaccounted_time, unaccounted_time);
Vivek Goyala23e6862011-05-19 15:38:20 -0400308#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500309}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700310EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500311
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400312/*
313 * should be called under rcu read lock or queue lock to make sure blkg pointer
314 * is valid.
315 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200316void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800317 struct blkio_policy_type *pol,
318 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700319{
Tejun Heoedcb0722012-04-01 14:38:42 -0700320 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Tejun Heoc1768262012-03-05 13:15:17 -0800321 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400322 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400323 unsigned long flags;
324
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800325 /* If per cpu stats are not allocated yet, don't do any accounting. */
326 if (pd->stats_cpu == NULL)
327 return;
328
Vivek Goyal575969a2011-05-19 15:38:29 -0400329 /*
330 * Disabling interrupts to provide mutual exclusion between two
331 * writes on same cpu. It probably is not needed for 64bit. Not
332 * optimizing that case yet.
333 */
334 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700335
Tejun Heo549d3aa2012-03-05 13:15:16 -0800336 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400337
Tejun Heoedcb0722012-04-01 14:38:42 -0700338 blkg_stat_add(&stats_cpu->sectors, bytes >> 9);
339 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
340 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
341
Vivek Goyal575969a2011-05-19 15:38:29 -0400342 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700343}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200344EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700345
Divyesh Shah84c124d2010-04-09 08:31:19 +0200346void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800347 struct blkio_policy_type *pol,
348 uint64_t start_time,
349 uint64_t io_start_time, bool direction,
350 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700351{
Tejun Heoedf1b872012-03-08 10:54:00 -0800352 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah91952912010-04-01 15:01:41 -0700353 unsigned long long now = sched_clock();
Tejun Heoedcb0722012-04-01 14:38:42 -0700354 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shah91952912010-04-01 15:01:41 -0700355
Tejun Heoedf1b872012-03-08 10:54:00 -0800356 lockdep_assert_held(blkg->q->queue_lock);
357
Divyesh Shah84c124d2010-04-09 08:31:19 +0200358 if (time_after64(now, io_start_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700359 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200360 if (time_after64(io_start_time, start_time))
Tejun Heoedcb0722012-04-01 14:38:42 -0700361 blkg_rwstat_add(&stats->wait_time, rw,
362 io_start_time - start_time);
Divyesh Shah91952912010-04-01 15:01:41 -0700363}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200364EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700365
Vivek Goyal317389a2011-05-23 10:02:19 +0200366/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800367void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
368 struct blkio_policy_type *pol,
369 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700370{
Tejun Heoedf1b872012-03-08 10:54:00 -0800371 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heoedcb0722012-04-01 14:38:42 -0700372 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Divyesh Shah812d4022010-04-08 21:14:23 -0700373
Tejun Heoedf1b872012-03-08 10:54:00 -0800374 lockdep_assert_held(blkg->q->queue_lock);
375
Tejun Heoedcb0722012-04-01 14:38:42 -0700376 blkg_rwstat_add(&stats->merged, rw, 1);
Divyesh Shah812d4022010-04-08 21:14:23 -0700377}
378EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
379
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800380/*
381 * Worker for allocating per cpu stat for blk groups. This is scheduled on
382 * the system_nrt_wq once there are some groups on the alloc_list waiting
383 * for allocation.
384 */
385static void blkio_stat_alloc_fn(struct work_struct *work)
386{
387 static void *pcpu_stats[BLKIO_NR_POLICIES];
388 struct delayed_work *dwork = to_delayed_work(work);
389 struct blkio_group *blkg;
390 int i;
391 bool empty = false;
392
393alloc_stats:
394 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
395 if (pcpu_stats[i] != NULL)
396 continue;
397
398 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
399
400 /* Allocation failed. Try again after some time. */
401 if (pcpu_stats[i] == NULL) {
402 queue_delayed_work(system_nrt_wq, dwork,
403 msecs_to_jiffies(10));
404 return;
405 }
406 }
407
408 spin_lock_irq(&blkio_list_lock);
409 spin_lock(&alloc_list_lock);
410
411 /* cgroup got deleted or queue exited. */
412 if (!list_empty(&alloc_list)) {
413 blkg = list_first_entry(&alloc_list, struct blkio_group,
414 alloc_node);
415 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
416 struct blkg_policy_data *pd = blkg->pd[i];
417
418 if (blkio_policy[i] && pd && !pd->stats_cpu)
419 swap(pd->stats_cpu, pcpu_stats[i]);
420 }
421
422 list_del_init(&blkg->alloc_node);
423 }
424
425 empty = list_empty(&alloc_list);
426
427 spin_unlock(&alloc_list_lock);
428 spin_unlock_irq(&blkio_list_lock);
429
430 if (!empty)
431 goto alloc_stats;
432}
433
Tejun Heo03814112012-03-05 13:15:14 -0800434/**
435 * blkg_free - free a blkg
436 * @blkg: blkg to free
437 *
438 * Free @blkg which may be partially allocated.
439 */
440static void blkg_free(struct blkio_group *blkg)
441{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800442 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800443
444 if (!blkg)
445 return;
446
Tejun Heoe8989fa2012-03-05 13:15:20 -0800447 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
448 struct blkg_policy_data *pd = blkg->pd[i];
449
450 if (pd) {
451 free_percpu(pd->stats_cpu);
452 kfree(pd);
453 }
Tejun Heo03814112012-03-05 13:15:14 -0800454 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800455
Tejun Heo549d3aa2012-03-05 13:15:16 -0800456 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800457}
458
459/**
460 * blkg_alloc - allocate a blkg
461 * @blkcg: block cgroup the new blkg is associated with
462 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800463 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800464 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800465 */
466static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800467 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800468{
469 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800470 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800471
472 /* alloc and init base part */
473 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
474 if (!blkg)
475 return NULL;
476
Tejun Heoc875f4d2012-03-05 13:15:22 -0800477 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800478 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800479 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800480 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800481 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800482 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
483
Tejun Heoe8989fa2012-03-05 13:15:20 -0800484 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
485 struct blkio_policy_type *pol = blkio_policy[i];
486 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800487
Tejun Heoe8989fa2012-03-05 13:15:20 -0800488 if (!pol)
489 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800490
Tejun Heoe8989fa2012-03-05 13:15:20 -0800491 /* alloc per-policy data and attach it to blkg */
492 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
493 q->node);
494 if (!pd) {
495 blkg_free(blkg);
496 return NULL;
497 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800498
Tejun Heoe8989fa2012-03-05 13:15:20 -0800499 blkg->pd[i] = pd;
500 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800501 }
502
Tejun Heo549d3aa2012-03-05 13:15:16 -0800503 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800504 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
505 struct blkio_policy_type *pol = blkio_policy[i];
506
507 if (pol)
508 pol->ops.blkio_init_group_fn(blkg);
509 }
510
Tejun Heo03814112012-03-05 13:15:14 -0800511 return blkg;
512}
513
Tejun Heocd1604f2012-03-05 13:15:06 -0800514struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
515 struct request_queue *q,
Tejun Heocd1604f2012-03-05 13:15:06 -0800516 bool for_root)
517 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400518{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800519 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400520
Tejun Heocd1604f2012-03-05 13:15:06 -0800521 WARN_ON_ONCE(!rcu_read_lock_held());
522 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500523
Tejun Heocd1604f2012-03-05 13:15:06 -0800524 /*
525 * This could be the first entry point of blkcg implementation and
526 * we shouldn't allow anything to go through for a bypassing queue.
527 * The following can be removed if blkg lookup is guaranteed to
528 * fail on a bypassing queue.
529 */
530 if (unlikely(blk_queue_bypass(q)) && !for_root)
531 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
532
Tejun Heoe8989fa2012-03-05 13:15:20 -0800533 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800534 if (blkg)
535 return blkg;
536
Tejun Heo7ee9c562012-03-05 13:15:11 -0800537 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800538 if (!css_tryget(&blkcg->css))
539 return ERR_PTR(-EINVAL);
540
541 /*
542 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800543 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800544 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800545
546 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800547 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800548 blkg = ERR_PTR(-ENOMEM);
549 goto out;
550 }
551
552 /* insert */
553 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500554 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800555 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800556 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800557
558 spin_lock(&alloc_list_lock);
559 list_add(&blkg->alloc_node, &alloc_list);
560 /* Queue per cpu stat allocation from worker thread. */
561 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
562 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800563out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800564 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500565}
Tejun Heocd1604f2012-03-05 13:15:06 -0800566EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500567
Vivek Goyal31e4c282009-12-03 12:59:42 -0500568/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800569struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800570 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500571{
572 struct blkio_group *blkg;
573 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500574
Tejun Heoca32aef2012-03-05 13:15:03 -0800575 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800576 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500577 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500578 return NULL;
579}
Tejun Heocd1604f2012-03-05 13:15:06 -0800580EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500581
Tejun Heoe8989fa2012-03-05 13:15:20 -0800582static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800583{
Tejun Heo03aa264a2012-03-05 13:15:19 -0800584 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800585 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800586
587 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800588 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800589
590 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800591 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800592 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800593 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800594 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800595
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800596 spin_lock(&alloc_list_lock);
597 list_del_init(&blkg->alloc_node);
598 spin_unlock(&alloc_list_lock);
599
Tejun Heo03aa264a2012-03-05 13:15:19 -0800600 /*
601 * Put the reference taken at the time of creation so that when all
602 * queues are gone, group can be destroyed.
603 */
604 blkg_put(blkg);
605}
606
Tejun Heoe8989fa2012-03-05 13:15:20 -0800607/*
608 * XXX: This updates blkg policy data in-place for root blkg, which is
609 * necessary across elevator switch and policy registration as root blkgs
610 * aren't shot down. This broken and racy implementation is temporary.
611 * Eventually, blkg shoot down will be replaced by proper in-place update.
612 */
613void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
614{
615 struct blkio_policy_type *pol = blkio_policy[plid];
616 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
617 struct blkg_policy_data *pd;
618
619 if (!blkg)
620 return;
621
622 kfree(blkg->pd[plid]);
623 blkg->pd[plid] = NULL;
624
625 if (!pol)
626 return;
627
628 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
629 WARN_ON_ONCE(!pd);
630
631 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
632 WARN_ON_ONCE(!pd->stats_cpu);
633
634 blkg->pd[plid] = pd;
635 pd->blkg = blkg;
636 pol->ops.blkio_init_group_fn(blkg);
637}
638EXPORT_SYMBOL_GPL(update_root_blkg_pd);
639
Tejun Heo9f13ef62012-03-05 13:15:21 -0800640/**
641 * blkg_destroy_all - destroy all blkgs associated with a request_queue
642 * @q: request_queue of interest
643 * @destroy_root: whether to destroy root blkg or not
644 *
645 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
646 * destroyed; otherwise, root blkg is left alone.
647 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800648void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa264a2012-03-05 13:15:19 -0800649{
650 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800651
Tejun Heo9f13ef62012-03-05 13:15:21 -0800652 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800653
Tejun Heo9f13ef62012-03-05 13:15:21 -0800654 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
655 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800656
Tejun Heo9f13ef62012-03-05 13:15:21 -0800657 /* skip root? */
658 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
659 continue;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800660
Tejun Heo9f13ef62012-03-05 13:15:21 -0800661 spin_lock(&blkcg->lock);
662 blkg_destroy(blkg);
663 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800664 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800665
666 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800667}
Tejun Heo03aa264a2012-03-05 13:15:19 -0800668EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800669
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800670static void blkg_rcu_free(struct rcu_head *rcu_head)
671{
672 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
673}
674
675void __blkg_release(struct blkio_group *blkg)
676{
677 /* release the extra blkcg reference this blkg has been holding */
678 css_put(&blkg->blkcg->css);
679
680 /*
681 * A group is freed in rcu manner. But having an rcu lock does not
682 * mean that one can access all the fields of blkg and assume these
683 * are valid. For example, don't try to follow throtl_data and
684 * request queue links.
685 *
686 * Having a reference to blkg under an rcu allows acess to only
687 * values local to groups like group stats and group rate limits
688 */
689 call_rcu(&blkg->rcu_head, blkg_rcu_free);
690}
691EXPORT_SYMBOL_GPL(__blkg_release);
692
Tejun Heoc1768262012-03-05 13:15:17 -0800693static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400694{
Tejun Heoc1768262012-03-05 13:15:17 -0800695 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800696 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800697
698 if (pd->stats_cpu == NULL)
699 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800700
701 for_each_possible_cpu(cpu) {
702 struct blkio_group_stats_cpu *sc =
703 per_cpu_ptr(pd->stats_cpu, cpu);
704
Tejun Heoedcb0722012-04-01 14:38:42 -0700705 blkg_rwstat_reset(&sc->service_bytes);
706 blkg_rwstat_reset(&sc->serviced);
707 blkg_stat_reset(&sc->sectors);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400708 }
709}
710
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700711static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200712blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700713{
Tejun Heo997a0262012-03-08 10:53:58 -0800714 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700715 struct blkio_group *blkg;
716 struct hlist_node *n;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700717
Tejun Heoe8989fa2012-03-05 13:15:20 -0800718 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700719 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800720
721 /*
722 * Note that stat reset is racy - it doesn't synchronize against
723 * stat updates. This is a debug feature which shouldn't exist
724 * anyway. If you get hit by a race, retry.
725 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700726 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800727 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800728
Tejun Heoe8989fa2012-03-05 13:15:20 -0800729 list_for_each_entry(pol, &blkio_list, list) {
730 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800731 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400732
Tejun Heo997a0262012-03-08 10:53:58 -0800733 /* queued stats shouldn't be cleared */
Tejun Heoedcb0722012-04-01 14:38:42 -0700734 blkg_rwstat_reset(&stats->merged);
735 blkg_rwstat_reset(&stats->service_time);
736 blkg_rwstat_reset(&stats->wait_time);
737 blkg_stat_reset(&stats->time);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800738#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700739 blkg_stat_reset(&stats->unaccounted_time);
740 blkg_stat_reset(&stats->avg_queue_size_sum);
741 blkg_stat_reset(&stats->avg_queue_size_samples);
742 blkg_stat_reset(&stats->dequeue);
743 blkg_stat_reset(&stats->group_wait_time);
744 blkg_stat_reset(&stats->idle_time);
745 blkg_stat_reset(&stats->empty_time);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800746#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800747 blkio_reset_stats_cpu(blkg, pol->plid);
748 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700749 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400750
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700751 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800752 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700753 return 0;
754}
755
Tejun Heoedcb0722012-04-01 14:38:42 -0700756static void blkio_get_key_name(enum blkg_rwstat_type type, const char *dname,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800757 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700758{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800759 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700760 chars_left -= strlen(str);
761 if (chars_left <= 0) {
762 printk(KERN_WARNING
763 "Possibly incorrect cgroup stat display format");
764 return;
765 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200766 if (diskname_only)
767 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700768 switch (type) {
Tejun Heoedcb0722012-04-01 14:38:42 -0700769 case BLKG_RWSTAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700770 strlcat(str, " Read", chars_left);
771 break;
Tejun Heoedcb0722012-04-01 14:38:42 -0700772 case BLKG_RWSTAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700773 strlcat(str, " Write", chars_left);
774 break;
Tejun Heoedcb0722012-04-01 14:38:42 -0700775 case BLKG_RWSTAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700776 strlcat(str, " Sync", chars_left);
777 break;
Tejun Heoedcb0722012-04-01 14:38:42 -0700778 case BLKG_RWSTAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700779 strlcat(str, " Async", chars_left);
780 break;
Tejun Heoedcb0722012-04-01 14:38:42 -0700781 case BLKG_RWSTAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700782 strlcat(str, " Total", chars_left);
783 break;
784 default:
785 strlcat(str, " Invalid", chars_left);
786 }
787}
788
Tejun Heoc1768262012-03-05 13:15:17 -0800789static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heoedcb0722012-04-01 14:38:42 -0700790 enum stat_type_cpu type,
791 enum blkg_rwstat_type sub_type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400792{
Tejun Heoc1768262012-03-05 13:15:17 -0800793 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heoedcb0722012-04-01 14:38:42 -0700794 u64 val = 0;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400795 int cpu;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400796
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800797 if (pd->stats_cpu == NULL)
798 return val;
799
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400800 for_each_possible_cpu(cpu) {
Tejun Heoedcb0722012-04-01 14:38:42 -0700801 struct blkio_group_stats_cpu *stats_cpu =
802 per_cpu_ptr(pd->stats_cpu, cpu);
803 struct blkg_rwstat rws;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400804
Tejun Heoedcb0722012-04-01 14:38:42 -0700805 switch (type) {
806 case BLKIO_STAT_CPU_SECTORS:
807 val += blkg_stat_read(&stats_cpu->sectors);
808 break;
809 case BLKIO_STAT_CPU_SERVICE_BYTES:
810 rws = blkg_rwstat_read(&stats_cpu->service_bytes);
811 val += rws.cnt[sub_type];
812 break;
813 case BLKIO_STAT_CPU_SERVICED:
814 rws = blkg_rwstat_read(&stats_cpu->serviced);
815 val += rws.cnt[sub_type];
816 break;
817 }
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400818 }
819
820 return val;
821}
822
Tejun Heoc1768262012-03-05 13:15:17 -0800823static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800824 struct cgroup_map_cb *cb, const char *dname,
825 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400826{
827 uint64_t disk_total, val;
828 char key_str[MAX_KEY_LEN];
Tejun Heoedcb0722012-04-01 14:38:42 -0700829 enum blkg_rwstat_type sub_type;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400830
831 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800832 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heoc4c76a02012-03-08 10:53:59 -0800833 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
834 cb->fill(cb, key_str, val);
835 return val;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400836 }
837
Tejun Heoedcb0722012-04-01 14:38:42 -0700838 for (sub_type = BLKG_RWSTAT_READ; sub_type < BLKG_RWSTAT_NR;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400839 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800840 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
841 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800842 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400843 cb->fill(cb, key_str, val);
844 }
845
Tejun Heoedcb0722012-04-01 14:38:42 -0700846 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKG_RWSTAT_READ) +
847 blkio_read_stat_cpu(blkg, plid, type, BLKG_RWSTAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400848
Tejun Heoedcb0722012-04-01 14:38:42 -0700849 blkio_get_key_name(BLKG_RWSTAT_TOTAL, dname, key_str, MAX_KEY_LEN,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800850 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400851 cb->fill(cb, key_str, disk_total);
852 return disk_total;
853}
854
Tejun Heoc1768262012-03-05 13:15:17 -0800855static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800856 struct cgroup_map_cb *cb, const char *dname,
857 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700858{
Tejun Heoc4c76a02012-03-08 10:53:59 -0800859 struct blkio_group_stats *stats = &blkg->pd[plid]->stats;
860 uint64_t v = 0, disk_total = 0;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700861 char key_str[MAX_KEY_LEN];
Tejun Heoedcb0722012-04-01 14:38:42 -0700862 struct blkg_rwstat rws = { };
Tejun Heoc4c76a02012-03-08 10:53:59 -0800863 int st;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700864
Tejun Heoc4c76a02012-03-08 10:53:59 -0800865 if (type >= BLKIO_STAT_ARR_NR) {
Tejun Heoedcb0722012-04-01 14:38:42 -0700866 switch (type) {
867 case BLKIO_STAT_TIME:
868 v = blkg_stat_read(&stats->time);
869 break;
Justin TerAvest9026e522011-03-22 21:26:54 +0100870#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedcb0722012-04-01 14:38:42 -0700871 case BLKIO_STAT_UNACCOUNTED_TIME:
872 v = blkg_stat_read(&stats->unaccounted_time);
873 break;
874 case BLKIO_STAT_AVG_QUEUE_SIZE: {
875 uint64_t samples;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200876
Tejun Heoedcb0722012-04-01 14:38:42 -0700877 samples = blkg_stat_read(&stats->avg_queue_size_samples);
878 if (samples) {
879 v = blkg_stat_read(&stats->avg_queue_size_sum);
880 do_div(v, samples);
Tejun Heoc4c76a02012-03-08 10:53:59 -0800881 }
Tejun Heoedcb0722012-04-01 14:38:42 -0700882 break;
883 }
884 case BLKIO_STAT_IDLE_TIME:
885 v = blkg_stat_read(&stats->idle_time);
886 break;
887 case BLKIO_STAT_EMPTY_TIME:
888 v = blkg_stat_read(&stats->empty_time);
889 break;
890 case BLKIO_STAT_DEQUEUE:
891 v = blkg_stat_read(&stats->dequeue);
892 break;
893 case BLKIO_STAT_GROUP_WAIT_TIME:
894 v = blkg_stat_read(&stats->group_wait_time);
895 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800896#endif
Tejun Heoedcb0722012-04-01 14:38:42 -0700897 default:
898 WARN_ON_ONCE(1);
899 }
Tejun Heoc4c76a02012-03-08 10:53:59 -0800900
901 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
902 cb->fill(cb, key_str, v);
903 return v;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700904 }
Tejun Heoc4c76a02012-03-08 10:53:59 -0800905
Tejun Heoedcb0722012-04-01 14:38:42 -0700906 switch (type) {
907 case BLKIO_STAT_MERGED:
908 rws = blkg_rwstat_read(&stats->merged);
909 break;
910 case BLKIO_STAT_SERVICE_TIME:
911 rws = blkg_rwstat_read(&stats->service_time);
912 break;
913 case BLKIO_STAT_WAIT_TIME:
914 rws = blkg_rwstat_read(&stats->wait_time);
915 break;
916 case BLKIO_STAT_QUEUED:
917 rws = blkg_rwstat_read(&stats->queued);
918 break;
919 default:
920 WARN_ON_ONCE(true);
921 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800922 }
923
Tejun Heoedcb0722012-04-01 14:38:42 -0700924 for (st = BLKG_RWSTAT_READ; st < BLKG_RWSTAT_NR; st++) {
925 blkio_get_key_name(st, dname, key_str, MAX_KEY_LEN, false);
926 cb->fill(cb, key_str, rws.cnt[st]);
927 if (st == BLKG_RWSTAT_READ || st == BLKG_RWSTAT_WRITE)
928 disk_total += rws.cnt[st];
929 }
930
931 blkio_get_key_name(BLKG_RWSTAT_TOTAL, dname, key_str, MAX_KEY_LEN,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800932 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700933 cb->fill(cb, key_str, disk_total);
934 return disk_total;
935}
936
Tejun Heo4bfd4822012-03-05 13:15:08 -0800937static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
938 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800939{
Tejun Heoece84242011-10-19 14:31:15 +0200940 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800941 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800942 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800943 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200944 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200945 int i = 0, ret = -EINVAL;
946 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800947 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200948 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800949
950 memset(s, 0, sizeof(s));
951
952 while ((p = strsep(&buf, " ")) != NULL) {
953 if (!*p)
954 continue;
955
956 s[i++] = p;
957
958 /* Prevent from inputing too many things */
959 if (i == 3)
960 break;
961 }
962
963 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +0200964 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800965
966 p = strsep(&s[0], ":");
967 if (p != NULL)
968 major_s = p;
969 else
Tejun Heoece84242011-10-19 14:31:15 +0200970 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800971
972 minor_s = s[0];
973 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +0200974 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800975
Tejun Heoece84242011-10-19 14:31:15 +0200976 if (strict_strtoul(major_s, 10, &major))
977 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800978
Tejun Heoece84242011-10-19 14:31:15 +0200979 if (strict_strtoul(minor_s, 10, &minor))
980 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800981
982 dev = MKDEV(major, minor);
983
Tejun Heoece84242011-10-19 14:31:15 +0200984 if (strict_strtoull(s[1], 10, &temp))
985 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200986
Tejun Heoe56da7e2012-03-05 13:15:07 -0800987 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800988 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800989 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800990
991 rcu_read_lock();
992
Tejun Heo4bfd4822012-03-05 13:15:08 -0800993 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoaaec55a2012-04-01 14:38:42 -0700994 blkg = blkg_lookup_create(blkcg, disk->queue, false);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800995 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800996
Tejun Heo4bfd4822012-03-05 13:15:08 -0800997 if (IS_ERR(blkg)) {
998 ret = PTR_ERR(blkg);
999 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001000 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001001
Tejun Heo549d3aa2012-03-05 13:15:16 -08001002 pd = blkg->pd[plid];
1003
Vivek Goyal062a6442010-09-15 17:06:33 -04001004 switch (plid) {
1005 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001006 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1007 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001008 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001009
Tejun Heo549d3aa2012-03-05 13:15:16 -08001010 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001011 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001012 break;
1013 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001014 switch(fileid) {
1015 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001016 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001017 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001018 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001019 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001020 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001021 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001022 break;
1023 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001024 if (temp > THROTL_IOPS_MAX)
1025 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001026 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001027 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001028 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001029 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001030 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001031 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001032 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001033 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001034 break;
1035 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001036 break;
1037 default:
1038 BUG();
1039 }
Tejun Heoece84242011-10-19 14:31:15 +02001040 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001041out_unlock:
1042 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001043out:
1044 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001045
1046 /*
1047 * If queue was bypassing, we should retry. Do so after a short
1048 * msleep(). It isn't strictly necessary but queue can be
1049 * bypassing for some time and it's always nice to avoid busy
1050 * looping.
1051 */
1052 if (ret == -EBUSY) {
1053 msleep(10);
1054 return restart_syscall();
1055 }
Tejun Heoece84242011-10-19 14:31:15 +02001056 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001057}
1058
Vivek Goyal062a6442010-09-15 17:06:33 -04001059static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1060 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001061{
1062 int ret = 0;
1063 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001064 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001065 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1066 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001067
1068 buf = kstrdup(buffer, GFP_KERNEL);
1069 if (!buf)
1070 return -ENOMEM;
1071
Tejun Heo4bfd4822012-03-05 13:15:08 -08001072 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001073 kfree(buf);
1074 return ret;
1075}
1076
Vivek Goyal92616b52012-03-05 13:15:10 -08001077static const char *blkg_dev_name(struct blkio_group *blkg)
1078{
1079 /* some drivers (floppy) instantiate a queue w/o disk registered */
1080 if (blkg->q->backing_dev_info.dev)
1081 return dev_name(blkg->q->backing_dev_info.dev);
1082 return NULL;
1083}
1084
Tejun Heo4bfd4822012-03-05 13:15:08 -08001085static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1086 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001087{
Tejun Heoc1768262012-03-05 13:15:17 -08001088 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001089 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001090 struct blkg_policy_data *pd = blkg->pd[plid];
1091 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001092 int rw = WRITE;
1093
Vivek Goyal92616b52012-03-05 13:15:10 -08001094 if (!dname)
1095 return;
1096
Tejun Heoc1768262012-03-05 13:15:17 -08001097 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001098 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001099 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001100 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001101 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001102 break;
1103 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001104 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001105 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001106 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001107 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001108 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001109 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001110 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001111 break;
1112 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001113 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001114 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001115 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001116 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001117 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001118 break;
1119 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001120 break;
1121 default:
1122 BUG();
1123 }
1124}
1125
1126/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001127static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1128 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001129{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001130 struct blkio_group *blkg;
1131 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001132
Tejun Heo4bfd4822012-03-05 13:15:08 -08001133 spin_lock_irq(&blkcg->lock);
1134 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001135 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001136 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001137}
1138
1139static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1140 struct seq_file *m)
1141{
1142 struct blkio_cgroup *blkcg;
1143 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1144 int name = BLKIOFILE_ATTR(cft->private);
1145
1146 blkcg = cgroup_to_blkio_cgroup(cgrp);
1147
1148 switch(plid) {
1149 case BLKIO_POLICY_PROP:
1150 switch(name) {
1151 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001152 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001153 return 0;
1154 default:
1155 BUG();
1156 }
1157 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001158 case BLKIO_POLICY_THROTL:
1159 switch(name){
1160 case BLKIO_THROTL_read_bps_device:
1161 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001162 case BLKIO_THROTL_read_iops_device:
1163 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001164 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001165 return 0;
1166 default:
1167 BUG();
1168 }
1169 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001170 default:
1171 BUG();
1172 }
1173
1174 return 0;
1175}
1176
1177static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001178 struct cftype *cft, struct cgroup_map_cb *cb,
1179 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001180{
1181 struct blkio_group *blkg;
1182 struct hlist_node *n;
1183 uint64_t cgroup_total = 0;
1184
Tejun Heoc875f4d2012-03-05 13:15:22 -08001185 spin_lock_irq(&blkcg->lock);
1186
1187 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001188 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001189 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001190
Tejun Heoe8989fa2012-03-05 13:15:20 -08001191 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001192 continue;
Tejun Heoedf1b872012-03-08 10:54:00 -08001193 if (pcpu)
Tejun Heoc1768262012-03-05 13:15:17 -08001194 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1195 cb, dname, type);
Tejun Heoedf1b872012-03-08 10:54:00 -08001196 else
Tejun Heoc1768262012-03-05 13:15:17 -08001197 cgroup_total += blkio_get_stat(blkg, plid,
1198 cb, dname, type);
Vivek Goyal062a6442010-09-15 17:06:33 -04001199 }
1200 if (show_total)
1201 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001202
1203 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001204 return 0;
1205}
1206
1207/* All map kind of cgroup file get serviced by this function */
1208static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1209 struct cgroup_map_cb *cb)
1210{
1211 struct blkio_cgroup *blkcg;
1212 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1213 int name = BLKIOFILE_ATTR(cft->private);
1214
1215 blkcg = cgroup_to_blkio_cgroup(cgrp);
1216
1217 switch(plid) {
1218 case BLKIO_POLICY_PROP:
1219 switch(name) {
1220 case BLKIO_PROP_time:
1221 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001222 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001223 case BLKIO_PROP_sectors:
1224 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001225 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001226 case BLKIO_PROP_io_service_bytes:
1227 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001228 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001229 case BLKIO_PROP_io_serviced:
1230 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001231 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001232 case BLKIO_PROP_io_service_time:
1233 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001234 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001235 case BLKIO_PROP_io_wait_time:
1236 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001237 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001238 case BLKIO_PROP_io_merged:
1239 return blkio_read_blkg_stats(blkcg, cft, cb,
Tejun Heo5fe224d2012-03-08 10:53:57 -08001240 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001241 case BLKIO_PROP_io_queued:
1242 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001243 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001244#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001245 case BLKIO_PROP_unaccounted_time:
1246 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001247 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001248 case BLKIO_PROP_dequeue:
1249 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001250 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001251 case BLKIO_PROP_avg_queue_size:
1252 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001253 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001254 case BLKIO_PROP_group_wait_time:
1255 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001256 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001257 case BLKIO_PROP_idle_time:
1258 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001259 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001260 case BLKIO_PROP_empty_time:
1261 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001262 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001263#endif
1264 default:
1265 BUG();
1266 }
1267 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001268 case BLKIO_POLICY_THROTL:
1269 switch(name){
1270 case BLKIO_THROTL_io_service_bytes:
1271 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001272 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001273 case BLKIO_THROTL_io_serviced:
1274 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001275 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001276 default:
1277 BUG();
1278 }
1279 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001280 default:
1281 BUG();
1282 }
1283
1284 return 0;
1285}
1286
Tejun Heo4bfd4822012-03-05 13:15:08 -08001287static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001288{
1289 struct blkio_group *blkg;
1290 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001291
1292 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1293 return -EINVAL;
1294
1295 spin_lock(&blkio_list_lock);
1296 spin_lock_irq(&blkcg->lock);
1297 blkcg->weight = (unsigned int)val;
1298
Tejun Heo549d3aa2012-03-05 13:15:16 -08001299 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001300 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001301
Tejun Heoe8989fa2012-03-05 13:15:20 -08001302 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001303 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001304 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001305
Vivek Goyal062a6442010-09-15 17:06:33 -04001306 spin_unlock_irq(&blkcg->lock);
1307 spin_unlock(&blkio_list_lock);
1308 return 0;
1309}
1310
1311static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1312 struct blkio_cgroup *blkcg;
1313 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1314 int name = BLKIOFILE_ATTR(cft->private);
1315
1316 blkcg = cgroup_to_blkio_cgroup(cgrp);
1317
1318 switch(plid) {
1319 case BLKIO_POLICY_PROP:
1320 switch(name) {
1321 case BLKIO_PROP_weight:
1322 return (u64)blkcg->weight;
1323 }
1324 break;
1325 default:
1326 BUG();
1327 }
1328 return 0;
1329}
1330
1331static int
1332blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1333{
1334 struct blkio_cgroup *blkcg;
1335 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1336 int name = BLKIOFILE_ATTR(cft->private);
1337
1338 blkcg = cgroup_to_blkio_cgroup(cgrp);
1339
1340 switch(plid) {
1341 case BLKIO_POLICY_PROP:
1342 switch(name) {
1343 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001344 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001345 }
1346 break;
1347 default:
1348 BUG();
1349 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001350
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001351 return 0;
1352}
1353
Vivek Goyal31e4c282009-12-03 12:59:42 -05001354struct cftype blkio_files[] = {
1355 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001356 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001357 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1358 BLKIO_PROP_weight_device),
1359 .read_seq_string = blkiocg_file_read,
1360 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001361 .max_write_len = 256,
1362 },
1363 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001364 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001365 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1366 BLKIO_PROP_weight),
1367 .read_u64 = blkiocg_file_read_u64,
1368 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001369 },
Vivek Goyal22084192009-12-03 12:59:49 -05001370 {
1371 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001372 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1373 BLKIO_PROP_time),
1374 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001375 },
1376 {
1377 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001378 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1379 BLKIO_PROP_sectors),
1380 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001381 },
1382 {
1383 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001384 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1385 BLKIO_PROP_io_service_bytes),
1386 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001387 },
1388 {
1389 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001390 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1391 BLKIO_PROP_io_serviced),
1392 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001393 },
1394 {
1395 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001396 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1397 BLKIO_PROP_io_service_time),
1398 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001399 },
1400 {
1401 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001402 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1403 BLKIO_PROP_io_wait_time),
1404 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001405 },
1406 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001407 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001408 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1409 BLKIO_PROP_io_merged),
1410 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001411 },
1412 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001413 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001414 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1415 BLKIO_PROP_io_queued),
1416 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001417 },
1418 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001419 .name = "reset_stats",
1420 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001421 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001422#ifdef CONFIG_BLK_DEV_THROTTLING
1423 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001424 .name = "throttle.read_bps_device",
1425 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1426 BLKIO_THROTL_read_bps_device),
1427 .read_seq_string = blkiocg_file_read,
1428 .write_string = blkiocg_file_write,
1429 .max_write_len = 256,
1430 },
1431
1432 {
1433 .name = "throttle.write_bps_device",
1434 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1435 BLKIO_THROTL_write_bps_device),
1436 .read_seq_string = blkiocg_file_read,
1437 .write_string = blkiocg_file_write,
1438 .max_write_len = 256,
1439 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001440
1441 {
1442 .name = "throttle.read_iops_device",
1443 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1444 BLKIO_THROTL_read_iops_device),
1445 .read_seq_string = blkiocg_file_read,
1446 .write_string = blkiocg_file_write,
1447 .max_write_len = 256,
1448 },
1449
1450 {
1451 .name = "throttle.write_iops_device",
1452 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1453 BLKIO_THROTL_write_iops_device),
1454 .read_seq_string = blkiocg_file_read,
1455 .write_string = blkiocg_file_write,
1456 .max_write_len = 256,
1457 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001458 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001459 .name = "throttle.io_service_bytes",
1460 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1461 BLKIO_THROTL_io_service_bytes),
1462 .read_map = blkiocg_file_read_map,
1463 },
1464 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001465 .name = "throttle.io_serviced",
1466 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1467 BLKIO_THROTL_io_serviced),
1468 .read_map = blkiocg_file_read_map,
1469 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001470#endif /* CONFIG_BLK_DEV_THROTTLING */
1471
Vivek Goyal22084192009-12-03 12:59:49 -05001472#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001473 {
1474 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001475 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1476 BLKIO_PROP_avg_queue_size),
1477 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001478 },
1479 {
Divyesh Shah812df482010-04-08 21:15:35 -07001480 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001481 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1482 BLKIO_PROP_group_wait_time),
1483 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001484 },
1485 {
1486 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001487 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1488 BLKIO_PROP_idle_time),
1489 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001490 },
1491 {
1492 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001493 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1494 BLKIO_PROP_empty_time),
1495 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001496 },
1497 {
Vivek Goyal22084192009-12-03 12:59:49 -05001498 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001499 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1500 BLKIO_PROP_dequeue),
1501 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001502 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001503 {
1504 .name = "unaccounted_time",
1505 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1506 BLKIO_PROP_unaccounted_time),
1507 .read_map = blkiocg_file_read_map,
1508 },
Vivek Goyal22084192009-12-03 12:59:49 -05001509#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07001510 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001511};
1512
Tejun Heo9f13ef62012-03-05 13:15:21 -08001513/**
1514 * blkiocg_pre_destroy - cgroup pre_destroy callback
Tejun Heo9f13ef62012-03-05 13:15:21 -08001515 * @cgroup: cgroup of interest
1516 *
1517 * This function is called when @cgroup is about to go away and responsible
1518 * for shooting down all blkgs associated with @cgroup. blkgs should be
1519 * removed while holding both q and blkcg locks. As blkcg lock is nested
1520 * inside q lock, this function performs reverse double lock dancing.
1521 *
1522 * This is the blkcg counterpart of ioc_release_fn().
1523 */
Tejun Heo959d8512012-04-01 12:30:01 -07001524static int blkiocg_pre_destroy(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001525{
1526 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1527
Tejun Heo9f13ef62012-03-05 13:15:21 -08001528 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001529
Tejun Heo9f13ef62012-03-05 13:15:21 -08001530 while (!hlist_empty(&blkcg->blkg_list)) {
1531 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1532 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001533 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001534
Tejun Heo9f13ef62012-03-05 13:15:21 -08001535 if (spin_trylock(q->queue_lock)) {
1536 blkg_destroy(blkg);
1537 spin_unlock(q->queue_lock);
1538 } else {
1539 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001540 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001541 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001542 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001543 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001544
Tejun Heo9f13ef62012-03-05 13:15:21 -08001545 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001546 return 0;
1547}
1548
Li Zefan761b3ef2012-01-31 13:47:36 +08001549static void blkiocg_destroy(struct cgroup *cgroup)
Tejun Heo7ee9c562012-03-05 13:15:11 -08001550{
1551 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1552
Ben Blum67523c42010-03-10 15:22:11 -08001553 if (blkcg != &blkio_root_cgroup)
1554 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001555}
1556
Li Zefan761b3ef2012-01-31 13:47:36 +08001557static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001558{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001559 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001560 struct blkio_cgroup *blkcg;
1561 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001562
Li Zefan03415092010-05-07 08:57:00 +02001563 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001564 blkcg = &blkio_root_cgroup;
1565 goto done;
1566 }
1567
Vivek Goyal31e4c282009-12-03 12:59:42 -05001568 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1569 if (!blkcg)
1570 return ERR_PTR(-ENOMEM);
1571
1572 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001573 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001574done:
1575 spin_lock_init(&blkcg->lock);
1576 INIT_HLIST_HEAD(&blkcg->blkg_list);
1577
1578 return &blkcg->css;
1579}
1580
Tejun Heo5efd6112012-03-05 13:15:12 -08001581/**
1582 * blkcg_init_queue - initialize blkcg part of request queue
1583 * @q: request_queue to initialize
1584 *
1585 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1586 * part of new request_queue @q.
1587 *
1588 * RETURNS:
1589 * 0 on success, -errno on failure.
1590 */
1591int blkcg_init_queue(struct request_queue *q)
1592{
Tejun Heo923adde2012-03-05 13:15:13 -08001593 int ret;
1594
Tejun Heo5efd6112012-03-05 13:15:12 -08001595 might_sleep();
1596
Tejun Heo923adde2012-03-05 13:15:13 -08001597 ret = blk_throtl_init(q);
1598 if (ret)
1599 return ret;
1600
1601 mutex_lock(&all_q_mutex);
1602 INIT_LIST_HEAD(&q->all_q_node);
1603 list_add_tail(&q->all_q_node, &all_q_list);
1604 mutex_unlock(&all_q_mutex);
1605
1606 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001607}
1608
1609/**
1610 * blkcg_drain_queue - drain blkcg part of request_queue
1611 * @q: request_queue to drain
1612 *
1613 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1614 */
1615void blkcg_drain_queue(struct request_queue *q)
1616{
1617 lockdep_assert_held(q->queue_lock);
1618
1619 blk_throtl_drain(q);
1620}
1621
1622/**
1623 * blkcg_exit_queue - exit and release blkcg part of request_queue
1624 * @q: request_queue being released
1625 *
1626 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1627 */
1628void blkcg_exit_queue(struct request_queue *q)
1629{
Tejun Heo923adde2012-03-05 13:15:13 -08001630 mutex_lock(&all_q_mutex);
1631 list_del_init(&q->all_q_node);
1632 mutex_unlock(&all_q_mutex);
1633
Tejun Heoe8989fa2012-03-05 13:15:20 -08001634 blkg_destroy_all(q, true);
1635
Tejun Heo5efd6112012-03-05 13:15:12 -08001636 blk_throtl_exit(q);
1637}
1638
Vivek Goyal31e4c282009-12-03 12:59:42 -05001639/*
1640 * We cannot support shared io contexts, as we have no mean to support
1641 * two tasks with the same ioc in two different groups without major rework
1642 * of the main cic data structures. For now we allow a task to change
1643 * its cgroup only if it's the only owner of its ioc.
1644 */
Li Zefan761b3ef2012-01-31 13:47:36 +08001645static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001646{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001647 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001648 struct io_context *ioc;
1649 int ret = 0;
1650
1651 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001652 cgroup_taskset_for_each(task, cgrp, tset) {
1653 task_lock(task);
1654 ioc = task->io_context;
1655 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1656 ret = -EINVAL;
1657 task_unlock(task);
1658 if (ret)
1659 break;
1660 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001661 return ret;
1662}
1663
Tejun Heo923adde2012-03-05 13:15:13 -08001664static void blkcg_bypass_start(void)
1665 __acquires(&all_q_mutex)
1666{
1667 struct request_queue *q;
1668
1669 mutex_lock(&all_q_mutex);
1670
1671 list_for_each_entry(q, &all_q_list, all_q_node) {
1672 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001673 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001674 }
1675}
1676
1677static void blkcg_bypass_end(void)
1678 __releases(&all_q_mutex)
1679{
1680 struct request_queue *q;
1681
1682 list_for_each_entry(q, &all_q_list, all_q_node)
1683 blk_queue_bypass_end(q);
1684
1685 mutex_unlock(&all_q_mutex);
1686}
1687
Tejun Heo676f7c82012-04-01 12:09:55 -07001688struct cgroup_subsys blkio_subsys = {
1689 .name = "blkio",
1690 .create = blkiocg_create,
1691 .can_attach = blkiocg_can_attach,
Tejun Heo959d8512012-04-01 12:30:01 -07001692 .pre_destroy = blkiocg_pre_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001693 .destroy = blkiocg_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -07001694 .subsys_id = blkio_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001695 .base_cftypes = blkio_files,
Tejun Heo676f7c82012-04-01 12:09:55 -07001696 .module = THIS_MODULE,
1697};
1698EXPORT_SYMBOL_GPL(blkio_subsys);
1699
Vivek Goyal3e252062009-12-04 10:36:42 -05001700void blkio_policy_register(struct blkio_policy_type *blkiop)
1701{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001702 struct request_queue *q;
1703
Tejun Heo923adde2012-03-05 13:15:13 -08001704 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001705 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001706
1707 BUG_ON(blkio_policy[blkiop->plid]);
1708 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001709 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001710
Vivek Goyal3e252062009-12-04 10:36:42 -05001711 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001712 list_for_each_entry(q, &all_q_list, all_q_node)
1713 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001714 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001715}
1716EXPORT_SYMBOL_GPL(blkio_policy_register);
1717
1718void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1719{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001720 struct request_queue *q;
1721
Tejun Heo923adde2012-03-05 13:15:13 -08001722 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001723 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001724
1725 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1726 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001727 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001728
Vivek Goyal3e252062009-12-04 10:36:42 -05001729 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001730 list_for_each_entry(q, &all_q_list, all_q_node)
1731 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001732 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001733}
1734EXPORT_SYMBOL_GPL(blkio_policy_unregister);