blob: 0eb39981e7c21035f04020f701e968c3b38d22c8 [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>
22#include "blk-cgroup.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080023#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050024
Divyesh Shah84c124d2010-04-09 08:31:19 +020025#define MAX_KEY_LEN 100
26
Vivek Goyal3e252062009-12-04 10:36:42 -050027static DEFINE_SPINLOCK(blkio_list_lock);
28static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050029
Tejun Heo923adde2012-03-05 13:15:13 -080030static DEFINE_MUTEX(all_q_mutex);
31static LIST_HEAD(all_q_list);
32
Vivek Goyal31e4c282009-12-03 12:59:42 -050033struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050034EXPORT_SYMBOL_GPL(blkio_root_cgroup);
35
Tejun Heo035d10b2012-03-05 13:15:04 -080036static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
37
Ben Blum67523c42010-03-10 15:22:11 -080038static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
39 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080040static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
41 struct cgroup_taskset *);
42static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
43 struct cgroup_taskset *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080044static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080045static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
46static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
47
Vivek Goyal062a6442010-09-15 17:06:33 -040048/* for encoding cft->private value on file */
49#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
50/* What policy owns the file, proportional or throttle */
51#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
52#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
53
Ben Blum67523c42010-03-10 15:22:11 -080054struct cgroup_subsys blkio_subsys = {
55 .name = "blkio",
56 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080057 .can_attach = blkiocg_can_attach,
58 .attach = blkiocg_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080059 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080060 .destroy = blkiocg_destroy,
61 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080062 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080063 .module = THIS_MODULE,
64};
65EXPORT_SYMBOL_GPL(blkio_subsys);
66
Vivek Goyal31e4c282009-12-03 12:59:42 -050067struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
68{
69 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
70 struct blkio_cgroup, css);
71}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050072EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050073
Vivek Goyal70087dc2011-05-16 15:24:08 +020074struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
75{
76 return container_of(task_subsys_state(tsk, blkio_subsys_id),
77 struct blkio_cgroup, css);
78}
79EXPORT_SYMBOL_GPL(task_blkio_cgroup);
80
Vivek Goyal062a6442010-09-15 17:06:33 -040081static inline void
82blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
83{
84 struct blkio_policy_type *blkiop;
85
86 list_for_each_entry(blkiop, &blkio_list, list) {
87 /* If this policy does not own the blkg, do not send updates */
88 if (blkiop->plid != blkg->plid)
89 continue;
90 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080091 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020092 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040093 }
94}
95
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040096static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
97 int fileid)
98{
99 struct blkio_policy_type *blkiop;
100
101 list_for_each_entry(blkiop, &blkio_list, list) {
102
103 /* If this policy does not own the blkg, do not send updates */
104 if (blkiop->plid != blkg->plid)
105 continue;
106
107 if (fileid == BLKIO_THROTL_read_bps_device
108 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800109 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200110 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400111
112 if (fileid == BLKIO_THROTL_write_bps_device
113 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800114 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200115 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400116 }
117}
118
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400119static inline void blkio_update_group_iops(struct blkio_group *blkg,
120 unsigned int iops, int fileid)
121{
122 struct blkio_policy_type *blkiop;
123
124 list_for_each_entry(blkiop, &blkio_list, list) {
125
126 /* If this policy does not own the blkg, do not send updates */
127 if (blkiop->plid != blkg->plid)
128 continue;
129
130 if (fileid == BLKIO_THROTL_read_iops_device
131 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800132 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200133 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400134
135 if (fileid == BLKIO_THROTL_write_iops_device
136 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800137 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200138 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400139 }
140}
141
Divyesh Shah91952912010-04-01 15:01:41 -0700142/*
143 * Add to the appropriate stat variable depending on the request type.
144 * This should be called with the blkg->stats_lock held.
145 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200146static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
147 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700148{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200149 if (direction)
150 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700151 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200152 stat[BLKIO_STAT_READ] += add;
153 if (sync)
154 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700155 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200156 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700157}
158
Divyesh Shahcdc11842010-04-08 21:15:10 -0700159/*
160 * Decrements the appropriate stat variable if non-zero depending on the
161 * request type. Panics on value being zero.
162 * This should be called with the blkg->stats_lock held.
163 */
164static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
165{
166 if (direction) {
167 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
168 stat[BLKIO_STAT_WRITE]--;
169 } else {
170 BUG_ON(stat[BLKIO_STAT_READ] == 0);
171 stat[BLKIO_STAT_READ]--;
172 }
173 if (sync) {
174 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
175 stat[BLKIO_STAT_SYNC]--;
176 } else {
177 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
178 stat[BLKIO_STAT_ASYNC]--;
179 }
180}
181
182#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700183/* This should be called with the blkg->stats_lock held. */
184static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
185 struct blkio_group *curr_blkg)
186{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800187 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
188
189 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700190 return;
191 if (blkg == curr_blkg)
192 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800193 pd->stats.start_group_wait_time = sched_clock();
194 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700195}
196
197/* This should be called with the blkg->stats_lock held. */
198static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
199{
200 unsigned long long now;
201
202 if (!blkio_blkg_waiting(stats))
203 return;
204
205 now = sched_clock();
206 if (time_after64(now, stats->start_group_wait_time))
207 stats->group_wait_time += now - stats->start_group_wait_time;
208 blkio_clear_blkg_waiting(stats);
209}
210
211/* This should be called with the blkg->stats_lock held. */
212static void blkio_end_empty_time(struct blkio_group_stats *stats)
213{
214 unsigned long long now;
215
216 if (!blkio_blkg_empty(stats))
217 return;
218
219 now = sched_clock();
220 if (time_after64(now, stats->start_empty_time))
221 stats->empty_time += now - stats->start_empty_time;
222 blkio_clear_blkg_empty(stats);
223}
224
225void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
226{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800227 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shah812df482010-04-08 21:15:35 -0700228 unsigned long flags;
229
230 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800231 BUG_ON(blkio_blkg_idling(&pd->stats));
232 pd->stats.start_idle_time = sched_clock();
233 blkio_mark_blkg_idling(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700234 spin_unlock_irqrestore(&blkg->stats_lock, flags);
235}
236EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
237
238void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
239{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800240 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shah812df482010-04-08 21:15:35 -0700241 unsigned long flags;
242 unsigned long long now;
243 struct blkio_group_stats *stats;
244
245 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800246 stats = &pd->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700247 if (blkio_blkg_idling(stats)) {
248 now = sched_clock();
249 if (time_after64(now, stats->start_idle_time))
250 stats->idle_time += now - stats->start_idle_time;
251 blkio_clear_blkg_idling(stats);
252 }
253 spin_unlock_irqrestore(&blkg->stats_lock, flags);
254}
255EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
256
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200257void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700258{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800259 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700260 unsigned long flags;
261 struct blkio_group_stats *stats;
262
263 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800264 stats = &pd->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700265 stats->avg_queue_size_sum +=
266 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
267 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
268 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700269 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700270 spin_unlock_irqrestore(&blkg->stats_lock, flags);
271}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200272EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
273
Vivek Goyale5ff0822010-04-26 19:25:11 +0200274void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200275{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800276 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shah28baf442010-04-14 11:22:38 +0200277 unsigned long flags;
278 struct blkio_group_stats *stats;
279
280 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800281 stats = &pd->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200282
283 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
284 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
285 spin_unlock_irqrestore(&blkg->stats_lock, flags);
286 return;
287 }
288
289 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200290 * group is already marked empty. This can happen if cfqq got new
291 * request in parent group and moved to this group while being added
292 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200293 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200294 if(blkio_blkg_empty(stats)) {
295 spin_unlock_irqrestore(&blkg->stats_lock, flags);
296 return;
297 }
298
Divyesh Shah28baf442010-04-14 11:22:38 +0200299 stats->start_empty_time = sched_clock();
300 blkio_mark_blkg_empty(stats);
301 spin_unlock_irqrestore(&blkg->stats_lock, flags);
302}
303EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
304
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200305void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
306 unsigned long dequeue)
307{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800308 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
309
310 pd->stats.dequeue += dequeue;
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200311}
312EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700313#else
314static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
315 struct blkio_group *curr_blkg) {}
316static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700317#endif
318
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200319void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700320 struct blkio_group *curr_blkg, bool direction,
321 bool sync)
322{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800323 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700324 unsigned long flags;
325
326 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800327 blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700328 sync);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800329 blkio_end_empty_time(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700330 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700331 spin_unlock_irqrestore(&blkg->stats_lock, flags);
332}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200333EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700334
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200335void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700336 bool direction, bool sync)
337{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800338 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700339 unsigned long flags;
340
341 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800342 blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED],
Divyesh Shahcdc11842010-04-08 21:15:10 -0700343 direction, sync);
344 spin_unlock_irqrestore(&blkg->stats_lock, flags);
345}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200346EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700347
Justin TerAvest167400d2011-03-12 16:54:00 +0100348void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
349 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500350{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800351 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700352 unsigned long flags;
353
354 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800355 pd->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400356#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo549d3aa2012-03-05 13:15:16 -0800357 pd->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400358#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700359 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500360}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700361EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500362
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400363/*
364 * should be called under rcu read lock or queue lock to make sure blkg pointer
365 * is valid.
366 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200367void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
368 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700369{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800370 struct blkg_policy_data *pd = blkg->pd[blkg->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
374 /*
375 * Disabling interrupts to provide mutual exclusion between two
376 * writes on same cpu. It probably is not needed for 64bit. Not
377 * optimizing that case yet.
378 */
379 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700380
Tejun Heo549d3aa2012-03-05 13:15:16 -0800381 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400382
Vivek Goyal575969a2011-05-19 15:38:29 -0400383 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400384 stats_cpu->sectors += bytes >> 9;
385 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
386 1, direction, sync);
387 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
388 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400389 u64_stats_update_end(&stats_cpu->syncp);
390 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700391}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200392EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700393
Divyesh Shah84c124d2010-04-09 08:31:19 +0200394void blkiocg_update_completion_stats(struct blkio_group *blkg,
395 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700396{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800397 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shah91952912010-04-01 15:01:41 -0700398 struct blkio_group_stats *stats;
399 unsigned long flags;
400 unsigned long long now = sched_clock();
401
402 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800403 stats = &pd->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200404 if (time_after64(now, io_start_time))
405 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
406 now - io_start_time, direction, sync);
407 if (time_after64(io_start_time, start_time))
408 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
409 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700410 spin_unlock_irqrestore(&blkg->stats_lock, flags);
411}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200412EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700413
Vivek Goyal317389a2011-05-23 10:02:19 +0200414/* Merged stats are per cpu. */
Divyesh Shah812d4022010-04-08 21:14:23 -0700415void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
416 bool sync)
417{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800418 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Vivek Goyal317389a2011-05-23 10:02:19 +0200419 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700420 unsigned long flags;
421
Vivek Goyal317389a2011-05-23 10:02:19 +0200422 /*
423 * Disabling interrupts to provide mutual exclusion between two
424 * writes on same cpu. It probably is not needed for 64bit. Not
425 * optimizing that case yet.
426 */
427 local_irq_save(flags);
428
Tejun Heo549d3aa2012-03-05 13:15:16 -0800429 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal317389a2011-05-23 10:02:19 +0200430
431 u64_stats_update_begin(&stats_cpu->syncp);
432 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
433 direction, sync);
434 u64_stats_update_end(&stats_cpu->syncp);
435 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700436}
437EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
438
Tejun Heo03814112012-03-05 13:15:14 -0800439/**
440 * blkg_free - free a blkg
441 * @blkg: blkg to free
442 *
443 * Free @blkg which may be partially allocated.
444 */
445static void blkg_free(struct blkio_group *blkg)
446{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800447 struct blkg_policy_data *pd;
448
449 if (!blkg)
450 return;
451
452 pd = blkg->pd[blkg->plid];
453 if (pd) {
454 free_percpu(pd->stats_cpu);
455 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -0800456 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800457 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800458}
459
460/**
461 * blkg_alloc - allocate a blkg
462 * @blkcg: block cgroup the new blkg is associated with
463 * @q: request_queue the new blkg is associated with
464 * @pol: policy the new blkg is associated with
465 *
466 * Allocate a new blkg assocating @blkcg and @q for @pol.
467 *
468 * FIXME: Should be called with queue locked but currently isn't due to
469 * percpu stat breakage.
470 */
471static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
472 struct request_queue *q,
473 struct blkio_policy_type *pol)
474{
475 struct blkio_group *blkg;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800476 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800477
478 /* alloc and init base part */
479 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
480 if (!blkg)
481 return NULL;
482
483 spin_lock_init(&blkg->stats_lock);
484 rcu_assign_pointer(blkg->q, q);
485 blkg->blkcg = blkcg;
486 blkg->plid = pol->plid;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800487 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800488 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
489
Tejun Heo549d3aa2012-03-05 13:15:16 -0800490 /* alloc per-policy data and attach it to blkg */
491 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
492 q->node);
493 if (!pd) {
Tejun Heo03814112012-03-05 13:15:14 -0800494 blkg_free(blkg);
495 return NULL;
496 }
497
Tejun Heo549d3aa2012-03-05 13:15:16 -0800498 blkg->pd[pol->plid] = pd;
499 pd->blkg = blkg;
500
Tejun Heo03814112012-03-05 13:15:14 -0800501 /* broken, read comment in the callsite */
Tejun Heo549d3aa2012-03-05 13:15:16 -0800502
503 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
504 if (!pd->stats_cpu) {
Tejun Heo03814112012-03-05 13:15:14 -0800505 blkg_free(blkg);
506 return NULL;
507 }
508
Tejun Heo549d3aa2012-03-05 13:15:16 -0800509 /* invoke per-policy init */
Tejun Heo03814112012-03-05 13:15:14 -0800510 pol->ops.blkio_init_group_fn(blkg);
511 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,
516 enum blkio_policy_id plid,
517 bool for_root)
518 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400519{
Tejun Heocd1604f2012-03-05 13:15:06 -0800520 struct blkio_policy_type *pol = blkio_policy[plid];
521 struct blkio_group *blkg, *new_blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400522
Tejun Heocd1604f2012-03-05 13:15:06 -0800523 WARN_ON_ONCE(!rcu_read_lock_held());
524 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500525
Tejun Heocd1604f2012-03-05 13:15:06 -0800526 /*
527 * This could be the first entry point of blkcg implementation and
528 * we shouldn't allow anything to go through for a bypassing queue.
529 * The following can be removed if blkg lookup is guaranteed to
530 * fail on a bypassing queue.
531 */
532 if (unlikely(blk_queue_bypass(q)) && !for_root)
533 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
534
535 blkg = blkg_lookup(blkcg, q, plid);
536 if (blkg)
537 return blkg;
538
Tejun Heo7ee9c562012-03-05 13:15:11 -0800539 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800540 if (!css_tryget(&blkcg->css))
541 return ERR_PTR(-EINVAL);
542
543 /*
544 * Allocate and initialize.
545 *
546 * FIXME: The following is broken. Percpu memory allocation
547 * requires %GFP_KERNEL context and can't be performed from IO
548 * path. Allocation here should inherently be atomic and the
549 * following lock dancing can be removed once the broken percpu
550 * allocation is fixed.
551 */
552 spin_unlock_irq(q->queue_lock);
553 rcu_read_unlock();
554
Tejun Heo03814112012-03-05 13:15:14 -0800555 new_blkg = blkg_alloc(blkcg, q, pol);
Tejun Heocd1604f2012-03-05 13:15:06 -0800556
557 rcu_read_lock();
558 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800559
560 /* did bypass get turned on inbetween? */
561 if (unlikely(blk_queue_bypass(q)) && !for_root) {
562 blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
563 goto out;
564 }
565
566 /* did someone beat us to it? */
567 blkg = blkg_lookup(blkcg, q, plid);
568 if (unlikely(blkg))
569 goto out;
570
571 /* did alloc fail? */
Tejun Heo03814112012-03-05 13:15:14 -0800572 if (unlikely(!new_blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800573 blkg = ERR_PTR(-ENOMEM);
574 goto out;
575 }
576
577 /* insert */
578 spin_lock(&blkcg->lock);
579 swap(blkg, new_blkg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500580 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800581 pol->ops.blkio_link_group_fn(q, blkg);
582 spin_unlock(&blkcg->lock);
583out:
Tejun Heo03814112012-03-05 13:15:14 -0800584 blkg_free(new_blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800585 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500586}
Tejun Heocd1604f2012-03-05 13:15:06 -0800587EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500588
Vivek Goyalb1c35762009-12-03 12:59:47 -0500589static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
590{
591 hlist_del_init_rcu(&blkg->blkcg_node);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500592}
593
594/*
595 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
596 * indicating that blk_group was unhashed by the time we got to it.
597 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500598int blkiocg_del_blkio_group(struct blkio_group *blkg)
599{
Tejun Heo7ee9c562012-03-05 13:15:11 -0800600 struct blkio_cgroup *blkcg = blkg->blkcg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500601 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500602 int ret = 1;
603
Tejun Heo7ee9c562012-03-05 13:15:11 -0800604 spin_lock_irqsave(&blkcg->lock, flags);
605 if (!hlist_unhashed(&blkg->blkcg_node)) {
606 __blkiocg_del_blkio_group(blkg);
607 ret = 0;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500608 }
Tejun Heo7ee9c562012-03-05 13:15:11 -0800609 spin_unlock_irqrestore(&blkcg->lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200610
Vivek Goyalb1c35762009-12-03 12:59:47 -0500611 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500612}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500613EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500614
615/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800616struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
617 struct request_queue *q,
618 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500619{
620 struct blkio_group *blkg;
621 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500622
Tejun Heoca32aef2012-03-05 13:15:03 -0800623 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
624 if (blkg->q == q && blkg->plid == plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500625 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500626 return NULL;
627}
Tejun Heocd1604f2012-03-05 13:15:06 -0800628EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500629
Tejun Heo72e06c22012-03-05 13:15:00 -0800630void blkg_destroy_all(struct request_queue *q)
631{
632 struct blkio_policy_type *pol;
633
634 while (true) {
635 bool done = true;
636
637 spin_lock(&blkio_list_lock);
638 spin_lock_irq(q->queue_lock);
639
640 /*
641 * clear_queue_fn() might return with non-empty group list
642 * if it raced cgroup removal and lost. cgroup removal is
643 * guaranteed to make forward progress and retrying after a
644 * while is enough. This ugliness is scheduled to be
645 * removed after locking update.
646 */
647 list_for_each_entry(pol, &blkio_list, list)
648 if (!pol->ops.blkio_clear_queue_fn(q))
649 done = false;
650
651 spin_unlock_irq(q->queue_lock);
652 spin_unlock(&blkio_list_lock);
653
654 if (done)
655 break;
656
657 msleep(10); /* just some random duration I like */
658 }
659}
660
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800661static void blkg_rcu_free(struct rcu_head *rcu_head)
662{
663 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
664}
665
666void __blkg_release(struct blkio_group *blkg)
667{
668 /* release the extra blkcg reference this blkg has been holding */
669 css_put(&blkg->blkcg->css);
670
671 /*
672 * A group is freed in rcu manner. But having an rcu lock does not
673 * mean that one can access all the fields of blkg and assume these
674 * are valid. For example, don't try to follow throtl_data and
675 * request queue links.
676 *
677 * Having a reference to blkg under an rcu allows acess to only
678 * values local to groups like group stats and group rate limits
679 */
680 call_rcu(&blkg->rcu_head, blkg_rcu_free);
681}
682EXPORT_SYMBOL_GPL(__blkg_release);
683
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400684static void blkio_reset_stats_cpu(struct blkio_group *blkg)
685{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800686 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400687 struct blkio_group_stats_cpu *stats_cpu;
688 int i, j, k;
689 /*
690 * Note: On 64 bit arch this should not be an issue. This has the
691 * possibility of returning some inconsistent value on 32bit arch
692 * as 64bit update on 32bit is non atomic. Taking care of this
693 * corner case makes code very complicated, like sending IPIs to
694 * cpus, taking care of stats of offline cpus etc.
695 *
696 * reset stats is anyway more of a debug feature and this sounds a
697 * corner case. So I am not complicating the code yet until and
698 * unless this becomes a real issue.
699 */
700 for_each_possible_cpu(i) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800701 stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400702 stats_cpu->sectors = 0;
703 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
704 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
705 stats_cpu->stat_arr_cpu[j][k] = 0;
706 }
707}
708
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700709static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200710blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700711{
712 struct blkio_cgroup *blkcg;
713 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700714 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700715 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700716 uint64_t queued[BLKIO_STAT_TOTAL];
717 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700718#ifdef CONFIG_DEBUG_BLK_CGROUP
719 bool idling, waiting, empty;
720 unsigned long long now = sched_clock();
721#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700722
723 blkcg = cgroup_to_blkio_cgroup(cgroup);
724 spin_lock_irq(&blkcg->lock);
725 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800726 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
727
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700728 spin_lock(&blkg->stats_lock);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800729 stats = &pd->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700730#ifdef CONFIG_DEBUG_BLK_CGROUP
731 idling = blkio_blkg_idling(stats);
732 waiting = blkio_blkg_waiting(stats);
733 empty = blkio_blkg_empty(stats);
734#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700735 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700736 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
737 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700738 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700739 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
740#ifdef CONFIG_DEBUG_BLK_CGROUP
741 if (idling) {
742 blkio_mark_blkg_idling(stats);
743 stats->start_idle_time = now;
744 }
745 if (waiting) {
746 blkio_mark_blkg_waiting(stats);
747 stats->start_group_wait_time = now;
748 }
749 if (empty) {
750 blkio_mark_blkg_empty(stats);
751 stats->start_empty_time = now;
752 }
753#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700754 spin_unlock(&blkg->stats_lock);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400755
756 /* Reset Per cpu stats which don't take blkg->stats_lock */
757 blkio_reset_stats_cpu(blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700758 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400759
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700760 spin_unlock_irq(&blkcg->lock);
761 return 0;
762}
763
Tejun Heo7a4dd282012-03-05 13:15:09 -0800764static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
765 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700766{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800767 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700768 chars_left -= strlen(str);
769 if (chars_left <= 0) {
770 printk(KERN_WARNING
771 "Possibly incorrect cgroup stat display format");
772 return;
773 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200774 if (diskname_only)
775 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700776 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200777 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700778 strlcat(str, " Read", chars_left);
779 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200780 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700781 strlcat(str, " Write", chars_left);
782 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200783 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700784 strlcat(str, " Sync", chars_left);
785 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200786 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700787 strlcat(str, " Async", chars_left);
788 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200789 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700790 strlcat(str, " Total", chars_left);
791 break;
792 default:
793 strlcat(str, " Invalid", chars_left);
794 }
795}
796
Divyesh Shah84c124d2010-04-09 08:31:19 +0200797static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800798 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200799{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800800 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200801 cb->fill(cb, str, val);
802 return val;
803}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700804
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400805
806static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
807 enum stat_type_cpu type, enum stat_sub_type sub_type)
808{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800809 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400810 int cpu;
811 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400812 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400813
814 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400815 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800816 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400817
Vivek Goyal575969a2011-05-19 15:38:29 -0400818 do {
819 start = u64_stats_fetch_begin(&stats_cpu->syncp);
820 if (type == BLKIO_STAT_CPU_SECTORS)
821 tval = stats_cpu->sectors;
822 else
823 tval = stats_cpu->stat_arr_cpu[type][sub_type];
824 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
825
826 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400827 }
828
829 return val;
830}
831
832static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800833 struct cgroup_map_cb *cb, const char *dname,
834 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400835{
836 uint64_t disk_total, val;
837 char key_str[MAX_KEY_LEN];
838 enum stat_sub_type sub_type;
839
840 if (type == BLKIO_STAT_CPU_SECTORS) {
841 val = blkio_read_stat_cpu(blkg, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800842 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
843 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400844 }
845
846 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
847 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800848 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
849 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400850 val = blkio_read_stat_cpu(blkg, type, sub_type);
851 cb->fill(cb, key_str, val);
852 }
853
854 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
855 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
856
Tejun Heo7a4dd282012-03-05 13:15:09 -0800857 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
858 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400859 cb->fill(cb, key_str, disk_total);
860 return disk_total;
861}
862
Divyesh Shah84c124d2010-04-09 08:31:19 +0200863/* This should be called with blkg->stats_lock held */
864static uint64_t blkio_get_stat(struct blkio_group *blkg,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800865 struct cgroup_map_cb *cb, const char *dname,
866 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700867{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800868 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700869 uint64_t disk_total;
870 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200871 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700872
Divyesh Shah84c124d2010-04-09 08:31:19 +0200873 if (type == BLKIO_STAT_TIME)
874 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800875 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100876#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100877 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
878 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800879 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700880 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800881 uint64_t sum = pd->stats.avg_queue_size_sum;
882 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700883 if (samples)
884 do_div(sum, samples);
885 else
886 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800887 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
888 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700889 }
Divyesh Shah812df482010-04-08 21:15:35 -0700890 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
891 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800892 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700893 if (type == BLKIO_STAT_IDLE_TIME)
894 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800895 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700896 if (type == BLKIO_STAT_EMPTY_TIME)
897 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800898 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200899 if (type == BLKIO_STAT_DEQUEUE)
900 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800901 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200902#endif
903
904 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
905 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800906 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
907 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800908 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700909 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800910 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
911 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800912 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
913 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700914 cb->fill(cb, key_str, disk_total);
915 return disk_total;
916}
917
Tejun Heo4bfd4822012-03-05 13:15:08 -0800918static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
919 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800920{
Tejun Heoece84242011-10-19 14:31:15 +0200921 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800922 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800923 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800924 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200925 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200926 int i = 0, ret = -EINVAL;
927 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800928 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200929 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800930
931 memset(s, 0, sizeof(s));
932
933 while ((p = strsep(&buf, " ")) != NULL) {
934 if (!*p)
935 continue;
936
937 s[i++] = p;
938
939 /* Prevent from inputing too many things */
940 if (i == 3)
941 break;
942 }
943
944 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +0200945 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800946
947 p = strsep(&s[0], ":");
948 if (p != NULL)
949 major_s = p;
950 else
Tejun Heoece84242011-10-19 14:31:15 +0200951 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800952
953 minor_s = s[0];
954 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +0200955 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800956
Tejun Heoece84242011-10-19 14:31:15 +0200957 if (strict_strtoul(major_s, 10, &major))
958 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800959
Tejun Heoece84242011-10-19 14:31:15 +0200960 if (strict_strtoul(minor_s, 10, &minor))
961 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800962
963 dev = MKDEV(major, minor);
964
Tejun Heoece84242011-10-19 14:31:15 +0200965 if (strict_strtoull(s[1], 10, &temp))
966 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200967
Tejun Heoe56da7e2012-03-05 13:15:07 -0800968 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800969 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800970 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800971
972 rcu_read_lock();
973
Tejun Heo4bfd4822012-03-05 13:15:08 -0800974 spin_lock_irq(disk->queue->queue_lock);
975 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
976 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800977
Tejun Heo4bfd4822012-03-05 13:15:08 -0800978 if (IS_ERR(blkg)) {
979 ret = PTR_ERR(blkg);
980 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200981 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800982
Tejun Heo549d3aa2012-03-05 13:15:16 -0800983 pd = blkg->pd[plid];
984
Vivek Goyal062a6442010-09-15 17:06:33 -0400985 switch (plid) {
986 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200987 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
988 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800989 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800990
Tejun Heo549d3aa2012-03-05 13:15:16 -0800991 pd->conf.weight = temp;
Tejun Heo4bfd4822012-03-05 13:15:08 -0800992 blkio_update_group_weight(blkg, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400993 break;
994 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400995 switch(fileid) {
996 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -0800997 pd->conf.bps[READ] = temp;
Tejun Heo4bfd4822012-03-05 13:15:08 -0800998 blkio_update_group_bps(blkg, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800999 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001000 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001001 pd->conf.bps[WRITE] = temp;
Tejun Heo4bfd4822012-03-05 13:15:08 -08001002 blkio_update_group_bps(blkg, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001003 break;
1004 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001005 if (temp > THROTL_IOPS_MAX)
1006 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001007 pd->conf.iops[READ] = temp;
Tejun Heo4bfd4822012-03-05 13:15:08 -08001008 blkio_update_group_iops(blkg, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001009 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001010 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001011 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001012 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001013 pd->conf.iops[WRITE] = temp;
Tejun Heo4bfd4822012-03-05 13:15:08 -08001014 blkio_update_group_iops(blkg, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001015 break;
1016 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001017 break;
1018 default:
1019 BUG();
1020 }
Tejun Heoece84242011-10-19 14:31:15 +02001021 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001022out_unlock:
1023 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001024out:
1025 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001026
1027 /*
1028 * If queue was bypassing, we should retry. Do so after a short
1029 * msleep(). It isn't strictly necessary but queue can be
1030 * bypassing for some time and it's always nice to avoid busy
1031 * looping.
1032 */
1033 if (ret == -EBUSY) {
1034 msleep(10);
1035 return restart_syscall();
1036 }
Tejun Heoece84242011-10-19 14:31:15 +02001037 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001038}
1039
Vivek Goyal062a6442010-09-15 17:06:33 -04001040static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1041 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001042{
1043 int ret = 0;
1044 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001045 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001046 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1047 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001048
1049 buf = kstrdup(buffer, GFP_KERNEL);
1050 if (!buf)
1051 return -ENOMEM;
1052
Tejun Heo4bfd4822012-03-05 13:15:08 -08001053 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001054 kfree(buf);
1055 return ret;
1056}
1057
Vivek Goyal92616b52012-03-05 13:15:10 -08001058static const char *blkg_dev_name(struct blkio_group *blkg)
1059{
1060 /* some drivers (floppy) instantiate a queue w/o disk registered */
1061 if (blkg->q->backing_dev_info.dev)
1062 return dev_name(blkg->q->backing_dev_info.dev);
1063 return NULL;
1064}
1065
Tejun Heo4bfd4822012-03-05 13:15:08 -08001066static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1067 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001068{
Tejun Heo549d3aa2012-03-05 13:15:16 -08001069 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
Vivek Goyal92616b52012-03-05 13:15:10 -08001070 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001071 int fileid = BLKIOFILE_ATTR(cft->private);
1072 int rw = WRITE;
1073
Vivek Goyal92616b52012-03-05 13:15:10 -08001074 if (!dname)
1075 return;
1076
Tejun Heo4bfd4822012-03-05 13:15:08 -08001077 switch (blkg->plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001078 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001079 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001080 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001081 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001082 break;
1083 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001084 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001085 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001086 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001087 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001088 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001089 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001090 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001091 break;
1092 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001093 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001094 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001095 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001096 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001097 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001098 break;
1099 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001100 break;
1101 default:
1102 BUG();
1103 }
1104}
1105
1106/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001107static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1108 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001109{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001110 struct blkio_group *blkg;
1111 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001112
Tejun Heo4bfd4822012-03-05 13:15:08 -08001113 spin_lock_irq(&blkcg->lock);
1114 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
1115 if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
1116 blkio_print_group_conf(cft, blkg, m);
1117 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001118}
1119
1120static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1121 struct seq_file *m)
1122{
1123 struct blkio_cgroup *blkcg;
1124 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1125 int name = BLKIOFILE_ATTR(cft->private);
1126
1127 blkcg = cgroup_to_blkio_cgroup(cgrp);
1128
1129 switch(plid) {
1130 case BLKIO_POLICY_PROP:
1131 switch(name) {
1132 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001133 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001134 return 0;
1135 default:
1136 BUG();
1137 }
1138 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001139 case BLKIO_POLICY_THROTL:
1140 switch(name){
1141 case BLKIO_THROTL_read_bps_device:
1142 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001143 case BLKIO_THROTL_read_iops_device:
1144 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001145 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001146 return 0;
1147 default:
1148 BUG();
1149 }
1150 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001151 default:
1152 BUG();
1153 }
1154
1155 return 0;
1156}
1157
1158static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001159 struct cftype *cft, struct cgroup_map_cb *cb,
1160 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001161{
1162 struct blkio_group *blkg;
1163 struct hlist_node *n;
1164 uint64_t cgroup_total = 0;
1165
1166 rcu_read_lock();
1167 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001168 const char *dname = blkg_dev_name(blkg);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001169
Vivek Goyal92616b52012-03-05 13:15:10 -08001170 if (!dname || BLKIOFILE_POLICY(cft->private) != blkg->plid)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001171 continue;
1172 if (pcpu)
1173 cgroup_total += blkio_get_stat_cpu(blkg, cb, dname,
1174 type);
1175 else {
1176 spin_lock_irq(&blkg->stats_lock);
1177 cgroup_total += blkio_get_stat(blkg, cb, dname, type);
1178 spin_unlock_irq(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001179 }
1180 }
1181 if (show_total)
1182 cb->fill(cb, "Total", cgroup_total);
1183 rcu_read_unlock();
1184 return 0;
1185}
1186
1187/* All map kind of cgroup file get serviced by this function */
1188static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1189 struct cgroup_map_cb *cb)
1190{
1191 struct blkio_cgroup *blkcg;
1192 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1193 int name = BLKIOFILE_ATTR(cft->private);
1194
1195 blkcg = cgroup_to_blkio_cgroup(cgrp);
1196
1197 switch(plid) {
1198 case BLKIO_POLICY_PROP:
1199 switch(name) {
1200 case BLKIO_PROP_time:
1201 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001202 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001203 case BLKIO_PROP_sectors:
1204 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001205 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001206 case BLKIO_PROP_io_service_bytes:
1207 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001208 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001209 case BLKIO_PROP_io_serviced:
1210 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001211 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001212 case BLKIO_PROP_io_service_time:
1213 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001214 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001215 case BLKIO_PROP_io_wait_time:
1216 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001217 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001218 case BLKIO_PROP_io_merged:
1219 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001220 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001221 case BLKIO_PROP_io_queued:
1222 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001223 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001224#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001225 case BLKIO_PROP_unaccounted_time:
1226 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001227 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001228 case BLKIO_PROP_dequeue:
1229 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001230 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001231 case BLKIO_PROP_avg_queue_size:
1232 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001233 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001234 case BLKIO_PROP_group_wait_time:
1235 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001236 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001237 case BLKIO_PROP_idle_time:
1238 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001239 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001240 case BLKIO_PROP_empty_time:
1241 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001242 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001243#endif
1244 default:
1245 BUG();
1246 }
1247 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001248 case BLKIO_POLICY_THROTL:
1249 switch(name){
1250 case BLKIO_THROTL_io_service_bytes:
1251 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001252 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001253 case BLKIO_THROTL_io_serviced:
1254 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001255 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001256 default:
1257 BUG();
1258 }
1259 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001260 default:
1261 BUG();
1262 }
1263
1264 return 0;
1265}
1266
Tejun Heo4bfd4822012-03-05 13:15:08 -08001267static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001268{
1269 struct blkio_group *blkg;
1270 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001271
1272 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1273 return -EINVAL;
1274
1275 spin_lock(&blkio_list_lock);
1276 spin_lock_irq(&blkcg->lock);
1277 blkcg->weight = (unsigned int)val;
1278
Tejun Heo549d3aa2012-03-05 13:15:16 -08001279 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1280 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
1281
1282 if (blkg->plid == plid && !pd->conf.weight)
Tejun Heo4bfd4822012-03-05 13:15:08 -08001283 blkio_update_group_weight(blkg, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001284 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001285
Vivek Goyal062a6442010-09-15 17:06:33 -04001286 spin_unlock_irq(&blkcg->lock);
1287 spin_unlock(&blkio_list_lock);
1288 return 0;
1289}
1290
1291static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1292 struct blkio_cgroup *blkcg;
1293 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1294 int name = BLKIOFILE_ATTR(cft->private);
1295
1296 blkcg = cgroup_to_blkio_cgroup(cgrp);
1297
1298 switch(plid) {
1299 case BLKIO_POLICY_PROP:
1300 switch(name) {
1301 case BLKIO_PROP_weight:
1302 return (u64)blkcg->weight;
1303 }
1304 break;
1305 default:
1306 BUG();
1307 }
1308 return 0;
1309}
1310
1311static int
1312blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1313{
1314 struct blkio_cgroup *blkcg;
1315 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1316 int name = BLKIOFILE_ATTR(cft->private);
1317
1318 blkcg = cgroup_to_blkio_cgroup(cgrp);
1319
1320 switch(plid) {
1321 case BLKIO_POLICY_PROP:
1322 switch(name) {
1323 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001324 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001325 }
1326 break;
1327 default:
1328 BUG();
1329 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001330
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001331 return 0;
1332}
1333
Vivek Goyal31e4c282009-12-03 12:59:42 -05001334struct cftype blkio_files[] = {
1335 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001336 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001337 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1338 BLKIO_PROP_weight_device),
1339 .read_seq_string = blkiocg_file_read,
1340 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001341 .max_write_len = 256,
1342 },
1343 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001344 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001345 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1346 BLKIO_PROP_weight),
1347 .read_u64 = blkiocg_file_read_u64,
1348 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001349 },
Vivek Goyal22084192009-12-03 12:59:49 -05001350 {
1351 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001352 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1353 BLKIO_PROP_time),
1354 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001355 },
1356 {
1357 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001358 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1359 BLKIO_PROP_sectors),
1360 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001361 },
1362 {
1363 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001364 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1365 BLKIO_PROP_io_service_bytes),
1366 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001367 },
1368 {
1369 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001370 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1371 BLKIO_PROP_io_serviced),
1372 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001373 },
1374 {
1375 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001376 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1377 BLKIO_PROP_io_service_time),
1378 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001379 },
1380 {
1381 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001382 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1383 BLKIO_PROP_io_wait_time),
1384 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001385 },
1386 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001387 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001388 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1389 BLKIO_PROP_io_merged),
1390 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001391 },
1392 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001393 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001394 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1395 BLKIO_PROP_io_queued),
1396 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001397 },
1398 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001399 .name = "reset_stats",
1400 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001401 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001402#ifdef CONFIG_BLK_DEV_THROTTLING
1403 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001404 .name = "throttle.read_bps_device",
1405 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1406 BLKIO_THROTL_read_bps_device),
1407 .read_seq_string = blkiocg_file_read,
1408 .write_string = blkiocg_file_write,
1409 .max_write_len = 256,
1410 },
1411
1412 {
1413 .name = "throttle.write_bps_device",
1414 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1415 BLKIO_THROTL_write_bps_device),
1416 .read_seq_string = blkiocg_file_read,
1417 .write_string = blkiocg_file_write,
1418 .max_write_len = 256,
1419 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001420
1421 {
1422 .name = "throttle.read_iops_device",
1423 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1424 BLKIO_THROTL_read_iops_device),
1425 .read_seq_string = blkiocg_file_read,
1426 .write_string = blkiocg_file_write,
1427 .max_write_len = 256,
1428 },
1429
1430 {
1431 .name = "throttle.write_iops_device",
1432 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1433 BLKIO_THROTL_write_iops_device),
1434 .read_seq_string = blkiocg_file_read,
1435 .write_string = blkiocg_file_write,
1436 .max_write_len = 256,
1437 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001438 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001439 .name = "throttle.io_service_bytes",
1440 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1441 BLKIO_THROTL_io_service_bytes),
1442 .read_map = blkiocg_file_read_map,
1443 },
1444 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001445 .name = "throttle.io_serviced",
1446 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1447 BLKIO_THROTL_io_serviced),
1448 .read_map = blkiocg_file_read_map,
1449 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001450#endif /* CONFIG_BLK_DEV_THROTTLING */
1451
Vivek Goyal22084192009-12-03 12:59:49 -05001452#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001453 {
1454 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001455 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1456 BLKIO_PROP_avg_queue_size),
1457 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001458 },
1459 {
Divyesh Shah812df482010-04-08 21:15:35 -07001460 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001461 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1462 BLKIO_PROP_group_wait_time),
1463 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001464 },
1465 {
1466 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001467 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1468 BLKIO_PROP_idle_time),
1469 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001470 },
1471 {
1472 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001473 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1474 BLKIO_PROP_empty_time),
1475 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001476 },
1477 {
Vivek Goyal22084192009-12-03 12:59:49 -05001478 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001479 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1480 BLKIO_PROP_dequeue),
1481 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001482 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001483 {
1484 .name = "unaccounted_time",
1485 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1486 BLKIO_PROP_unaccounted_time),
1487 .read_map = blkiocg_file_read_map,
1488 },
Vivek Goyal22084192009-12-03 12:59:49 -05001489#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001490};
1491
1492static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1493{
1494 return cgroup_add_files(cgroup, subsys, blkio_files,
1495 ARRAY_SIZE(blkio_files));
1496}
1497
Tejun Heo7ee9c562012-03-05 13:15:11 -08001498static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1499 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001500{
1501 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001502 unsigned long flags;
1503 struct blkio_group *blkg;
Tejun Heoca32aef2012-03-05 13:15:03 -08001504 struct request_queue *q;
Vivek Goyal3e252062009-12-04 10:36:42 -05001505 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001506
Vivek Goyalb1c35762009-12-03 12:59:47 -05001507 rcu_read_lock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001508
Jens Axboe0f3942a2010-05-03 14:28:55 +02001509 do {
1510 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001511
Jens Axboe0f3942a2010-05-03 14:28:55 +02001512 if (hlist_empty(&blkcg->blkg_list)) {
1513 spin_unlock_irqrestore(&blkcg->lock, flags);
1514 break;
1515 }
1516
1517 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1518 blkcg_node);
Tejun Heoca32aef2012-03-05 13:15:03 -08001519 q = rcu_dereference(blkg->q);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001520 __blkiocg_del_blkio_group(blkg);
1521
Vivek Goyalb1c35762009-12-03 12:59:47 -05001522 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001523
Jens Axboe0f3942a2010-05-03 14:28:55 +02001524 /*
1525 * This blkio_group is being unlinked as associated cgroup is
1526 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001527 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001528 */
1529 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001530 list_for_each_entry(blkiop, &blkio_list, list) {
1531 if (blkiop->plid != blkg->plid)
1532 continue;
Tejun Heoca32aef2012-03-05 13:15:03 -08001533 blkiop->ops.blkio_unlink_group_fn(q, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001534 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001535 spin_unlock(&blkio_list_lock);
1536 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001537
Vivek Goyalb1c35762009-12-03 12:59:47 -05001538 rcu_read_unlock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001539
1540 return 0;
1541}
1542
1543static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1544{
1545 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1546
Ben Blum67523c42010-03-10 15:22:11 -08001547 if (blkcg != &blkio_root_cgroup)
1548 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001549}
1550
1551static struct cgroup_subsys_state *
1552blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1553{
Li Zefan03415092010-05-07 08:57:00 +02001554 struct blkio_cgroup *blkcg;
1555 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001556
Li Zefan03415092010-05-07 08:57:00 +02001557 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001558 blkcg = &blkio_root_cgroup;
1559 goto done;
1560 }
1561
Vivek Goyal31e4c282009-12-03 12:59:42 -05001562 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1563 if (!blkcg)
1564 return ERR_PTR(-ENOMEM);
1565
1566 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1567done:
1568 spin_lock_init(&blkcg->lock);
1569 INIT_HLIST_HEAD(&blkcg->blkg_list);
1570
1571 return &blkcg->css;
1572}
1573
Tejun Heo5efd6112012-03-05 13:15:12 -08001574/**
1575 * blkcg_init_queue - initialize blkcg part of request queue
1576 * @q: request_queue to initialize
1577 *
1578 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1579 * part of new request_queue @q.
1580 *
1581 * RETURNS:
1582 * 0 on success, -errno on failure.
1583 */
1584int blkcg_init_queue(struct request_queue *q)
1585{
Tejun Heo923adde2012-03-05 13:15:13 -08001586 int ret;
1587
Tejun Heo5efd6112012-03-05 13:15:12 -08001588 might_sleep();
1589
Tejun Heo923adde2012-03-05 13:15:13 -08001590 ret = blk_throtl_init(q);
1591 if (ret)
1592 return ret;
1593
1594 mutex_lock(&all_q_mutex);
1595 INIT_LIST_HEAD(&q->all_q_node);
1596 list_add_tail(&q->all_q_node, &all_q_list);
1597 mutex_unlock(&all_q_mutex);
1598
1599 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001600}
1601
1602/**
1603 * blkcg_drain_queue - drain blkcg part of request_queue
1604 * @q: request_queue to drain
1605 *
1606 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1607 */
1608void blkcg_drain_queue(struct request_queue *q)
1609{
1610 lockdep_assert_held(q->queue_lock);
1611
1612 blk_throtl_drain(q);
1613}
1614
1615/**
1616 * blkcg_exit_queue - exit and release blkcg part of request_queue
1617 * @q: request_queue being released
1618 *
1619 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1620 */
1621void blkcg_exit_queue(struct request_queue *q)
1622{
Tejun Heo923adde2012-03-05 13:15:13 -08001623 mutex_lock(&all_q_mutex);
1624 list_del_init(&q->all_q_node);
1625 mutex_unlock(&all_q_mutex);
1626
Tejun Heo5efd6112012-03-05 13:15:12 -08001627 blk_throtl_exit(q);
1628}
1629
Vivek Goyal31e4c282009-12-03 12:59:42 -05001630/*
1631 * We cannot support shared io contexts, as we have no mean to support
1632 * two tasks with the same ioc in two different groups without major rework
1633 * of the main cic data structures. For now we allow a task to change
1634 * its cgroup only if it's the only owner of its ioc.
1635 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001636static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1637 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001638{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001639 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001640 struct io_context *ioc;
1641 int ret = 0;
1642
1643 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001644 cgroup_taskset_for_each(task, cgrp, tset) {
1645 task_lock(task);
1646 ioc = task->io_context;
1647 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1648 ret = -EINVAL;
1649 task_unlock(task);
1650 if (ret)
1651 break;
1652 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001653 return ret;
1654}
1655
Tejun Heobb9d97b2011-12-12 18:12:21 -08001656static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1657 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001658{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001659 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001660 struct io_context *ioc;
1661
Tejun Heobb9d97b2011-12-12 18:12:21 -08001662 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001663 /* we don't lose anything even if ioc allocation fails */
1664 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1665 if (ioc) {
1666 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001667 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001668 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001669 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001670}
1671
Tejun Heo923adde2012-03-05 13:15:13 -08001672static void blkcg_bypass_start(void)
1673 __acquires(&all_q_mutex)
1674{
1675 struct request_queue *q;
1676
1677 mutex_lock(&all_q_mutex);
1678
1679 list_for_each_entry(q, &all_q_list, all_q_node) {
1680 blk_queue_bypass_start(q);
1681 blkg_destroy_all(q);
1682 }
1683}
1684
1685static void blkcg_bypass_end(void)
1686 __releases(&all_q_mutex)
1687{
1688 struct request_queue *q;
1689
1690 list_for_each_entry(q, &all_q_list, all_q_node)
1691 blk_queue_bypass_end(q);
1692
1693 mutex_unlock(&all_q_mutex);
1694}
1695
Vivek Goyal3e252062009-12-04 10:36:42 -05001696void blkio_policy_register(struct blkio_policy_type *blkiop)
1697{
Tejun Heo923adde2012-03-05 13:15:13 -08001698 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001699 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001700
1701 BUG_ON(blkio_policy[blkiop->plid]);
1702 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001703 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001704
Vivek Goyal3e252062009-12-04 10:36:42 -05001705 spin_unlock(&blkio_list_lock);
Tejun Heo923adde2012-03-05 13:15:13 -08001706 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001707}
1708EXPORT_SYMBOL_GPL(blkio_policy_register);
1709
1710void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1711{
Tejun Heo923adde2012-03-05 13:15:13 -08001712 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001713 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001714
1715 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1716 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001717 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001718
Vivek Goyal3e252062009-12-04 10:36:42 -05001719 spin_unlock(&blkio_list_lock);
Tejun Heo923adde2012-03-05 13:15:13 -08001720 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001721}
1722EXPORT_SYMBOL_GPL(blkio_policy_unregister);