blob: 759bc58154c2382abddf8302b99e075c2d0a04ed [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 Goyal1cd9e032012-03-08 10:53:56 -080033/* List of groups pending per cpu stats allocation */
34static DEFINE_SPINLOCK(alloc_list_lock);
35static LIST_HEAD(alloc_list);
36
37static void blkio_stat_alloc_fn(struct work_struct *);
38static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
39
Vivek Goyal31e4c282009-12-03 12:59:42 -050040struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050041EXPORT_SYMBOL_GPL(blkio_root_cgroup);
42
Tejun Heo035d10b2012-03-05 13:15:04 -080043static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
44
Ben Blum67523c42010-03-10 15:22:11 -080045static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
46 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080047static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
48 struct cgroup_taskset *);
49static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
50 struct cgroup_taskset *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080051static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080052static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
53static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
54
Vivek Goyal062a6442010-09-15 17:06:33 -040055/* for encoding cft->private value on file */
56#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
57/* What policy owns the file, proportional or throttle */
58#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
59#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
60
Ben Blum67523c42010-03-10 15:22:11 -080061struct cgroup_subsys blkio_subsys = {
62 .name = "blkio",
63 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080064 .can_attach = blkiocg_can_attach,
65 .attach = blkiocg_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080066 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080067 .destroy = blkiocg_destroy,
68 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080069 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080070 .module = THIS_MODULE,
71};
72EXPORT_SYMBOL_GPL(blkio_subsys);
73
Vivek Goyal31e4c282009-12-03 12:59:42 -050074struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
75{
76 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
77 struct blkio_cgroup, css);
78}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050079EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050080
Tejun Heo4f85cb92012-03-05 13:15:28 -080081static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020082{
83 return container_of(task_subsys_state(tsk, blkio_subsys_id),
84 struct blkio_cgroup, css);
85}
Tejun Heo4f85cb92012-03-05 13:15:28 -080086
87struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
88{
89 if (bio && bio->bi_css)
90 return container_of(bio->bi_css, struct blkio_cgroup, css);
91 return task_blkio_cgroup(current);
92}
93EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020094
Tejun Heoc1768262012-03-05 13:15:17 -080095static inline void blkio_update_group_weight(struct blkio_group *blkg,
96 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040097{
98 struct blkio_policy_type *blkiop;
99
100 list_for_each_entry(blkiop, &blkio_list, list) {
101 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800102 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -0400103 continue;
104 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800105 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200106 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -0400107 }
108}
109
Tejun Heoc1768262012-03-05 13:15:17 -0800110static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
111 u64 bps, int fileid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400112{
113 struct blkio_policy_type *blkiop;
114
115 list_for_each_entry(blkiop, &blkio_list, list) {
116
117 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800118 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400119 continue;
120
121 if (fileid == BLKIO_THROTL_read_bps_device
122 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800123 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200124 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400125
126 if (fileid == BLKIO_THROTL_write_bps_device
127 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800128 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200129 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400130 }
131}
132
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400133static inline void blkio_update_group_iops(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800134 int plid, unsigned int iops,
135 int fileid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400136{
137 struct blkio_policy_type *blkiop;
138
139 list_for_each_entry(blkiop, &blkio_list, list) {
140
141 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800142 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400143 continue;
144
145 if (fileid == BLKIO_THROTL_read_iops_device
146 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800147 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200148 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400149
150 if (fileid == BLKIO_THROTL_write_iops_device
151 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800152 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200153 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400154 }
155}
156
Divyesh Shah91952912010-04-01 15:01:41 -0700157/*
158 * Add to the appropriate stat variable depending on the request type.
159 * This should be called with the blkg->stats_lock held.
160 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200161static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
162 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700163{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200164 if (direction)
165 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700166 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200167 stat[BLKIO_STAT_READ] += add;
168 if (sync)
169 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700170 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200171 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700172}
173
Divyesh Shahcdc11842010-04-08 21:15:10 -0700174/*
175 * Decrements the appropriate stat variable if non-zero depending on the
176 * request type. Panics on value being zero.
177 * This should be called with the blkg->stats_lock held.
178 */
179static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
180{
181 if (direction) {
182 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
183 stat[BLKIO_STAT_WRITE]--;
184 } else {
185 BUG_ON(stat[BLKIO_STAT_READ] == 0);
186 stat[BLKIO_STAT_READ]--;
187 }
188 if (sync) {
189 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
190 stat[BLKIO_STAT_SYNC]--;
191 } else {
192 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
193 stat[BLKIO_STAT_ASYNC]--;
194 }
195}
196
197#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700198/* This should be called with the blkg->stats_lock held. */
199static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800200 struct blkio_policy_type *pol,
201 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700202{
Tejun Heoc1768262012-03-05 13:15:17 -0800203 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800204
205 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700206 return;
207 if (blkg == curr_blkg)
208 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800209 pd->stats.start_group_wait_time = sched_clock();
210 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700211}
212
213/* This should be called with the blkg->stats_lock held. */
214static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
215{
216 unsigned long long now;
217
218 if (!blkio_blkg_waiting(stats))
219 return;
220
221 now = sched_clock();
222 if (time_after64(now, stats->start_group_wait_time))
223 stats->group_wait_time += now - stats->start_group_wait_time;
224 blkio_clear_blkg_waiting(stats);
225}
226
227/* This should be called with the blkg->stats_lock held. */
228static void blkio_end_empty_time(struct blkio_group_stats *stats)
229{
230 unsigned long long now;
231
232 if (!blkio_blkg_empty(stats))
233 return;
234
235 now = sched_clock();
236 if (time_after64(now, stats->start_empty_time))
237 stats->empty_time += now - stats->start_empty_time;
238 blkio_clear_blkg_empty(stats);
239}
240
Tejun Heoc1768262012-03-05 13:15:17 -0800241void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
242 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700243{
Tejun Heoc1768262012-03-05 13:15:17 -0800244 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah812df482010-04-08 21:15:35 -0700245 unsigned long flags;
246
247 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800248 BUG_ON(blkio_blkg_idling(&pd->stats));
249 pd->stats.start_idle_time = sched_clock();
250 blkio_mark_blkg_idling(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700251 spin_unlock_irqrestore(&blkg->stats_lock, flags);
252}
253EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
254
Tejun Heoc1768262012-03-05 13:15:17 -0800255void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
256 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700257{
Tejun Heoc1768262012-03-05 13:15:17 -0800258 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah812df482010-04-08 21:15:35 -0700259 unsigned long flags;
260 unsigned long long now;
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 Shah812df482010-04-08 21:15:35 -0700265 if (blkio_blkg_idling(stats)) {
266 now = sched_clock();
267 if (time_after64(now, stats->start_idle_time))
268 stats->idle_time += now - stats->start_idle_time;
269 blkio_clear_blkg_idling(stats);
270 }
271 spin_unlock_irqrestore(&blkg->stats_lock, flags);
272}
273EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
274
Tejun Heoc1768262012-03-05 13:15:17 -0800275void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
276 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700277{
Tejun Heoc1768262012-03-05 13:15:17 -0800278 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700279 unsigned long flags;
280 struct blkio_group_stats *stats;
281
282 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800283 stats = &pd->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700284 stats->avg_queue_size_sum +=
285 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
286 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
287 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700288 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700289 spin_unlock_irqrestore(&blkg->stats_lock, flags);
290}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200291EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
292
Tejun Heoc1768262012-03-05 13:15:17 -0800293void blkiocg_set_start_empty_time(struct blkio_group *blkg,
294 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200295{
Tejun Heoc1768262012-03-05 13:15:17 -0800296 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah28baf442010-04-14 11:22:38 +0200297 unsigned long flags;
298 struct blkio_group_stats *stats;
299
300 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800301 stats = &pd->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200302
303 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
304 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
305 spin_unlock_irqrestore(&blkg->stats_lock, flags);
306 return;
307 }
308
309 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200310 * group is already marked empty. This can happen if cfqq got new
311 * request in parent group and moved to this group while being added
312 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200313 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200314 if(blkio_blkg_empty(stats)) {
315 spin_unlock_irqrestore(&blkg->stats_lock, flags);
316 return;
317 }
318
Divyesh Shah28baf442010-04-14 11:22:38 +0200319 stats->start_empty_time = sched_clock();
320 blkio_mark_blkg_empty(stats);
321 spin_unlock_irqrestore(&blkg->stats_lock, flags);
322}
323EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
324
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200325void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800326 struct blkio_policy_type *pol,
327 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200328{
Tejun Heoc1768262012-03-05 13:15:17 -0800329 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800330
331 pd->stats.dequeue += dequeue;
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200332}
333EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700334#else
335static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800336 struct blkio_policy_type *pol,
337 struct blkio_group *curr_blkg) { }
338static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700339#endif
340
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200341void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800342 struct blkio_policy_type *pol,
343 struct blkio_group *curr_blkg, bool direction,
344 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700345{
Tejun Heoc1768262012-03-05 13:15:17 -0800346 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700347 unsigned long flags;
348
349 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800350 blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700351 sync);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800352 blkio_end_empty_time(&pd->stats);
Tejun Heoc1768262012-03-05 13:15:17 -0800353 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700354 spin_unlock_irqrestore(&blkg->stats_lock, flags);
355}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200356EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700357
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200358void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800359 struct blkio_policy_type *pol,
360 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700361{
Tejun Heoc1768262012-03-05 13:15:17 -0800362 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700363 unsigned long flags;
364
365 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800366 blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED],
Divyesh Shahcdc11842010-04-08 21:15:10 -0700367 direction, sync);
368 spin_unlock_irqrestore(&blkg->stats_lock, flags);
369}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200370EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700371
Tejun Heoc1768262012-03-05 13:15:17 -0800372void blkiocg_update_timeslice_used(struct blkio_group *blkg,
373 struct blkio_policy_type *pol,
374 unsigned long time,
375 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500376{
Tejun Heoc1768262012-03-05 13:15:17 -0800377 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700378 unsigned long flags;
379
380 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800381 pd->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400382#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo549d3aa2012-03-05 13:15:16 -0800383 pd->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400384#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700385 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500386}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700387EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500388
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400389/*
390 * should be called under rcu read lock or queue lock to make sure blkg pointer
391 * is valid.
392 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200393void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800394 struct blkio_policy_type *pol,
395 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700396{
Tejun Heoc1768262012-03-05 13:15:17 -0800397 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400398 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400399 unsigned long flags;
400
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800401 /* If per cpu stats are not allocated yet, don't do any accounting. */
402 if (pd->stats_cpu == NULL)
403 return;
404
Vivek Goyal575969a2011-05-19 15:38:29 -0400405 /*
406 * Disabling interrupts to provide mutual exclusion between two
407 * writes on same cpu. It probably is not needed for 64bit. Not
408 * optimizing that case yet.
409 */
410 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700411
Tejun Heo549d3aa2012-03-05 13:15:16 -0800412 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400413
Vivek Goyal575969a2011-05-19 15:38:29 -0400414 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400415 stats_cpu->sectors += bytes >> 9;
416 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
417 1, direction, sync);
418 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
419 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400420 u64_stats_update_end(&stats_cpu->syncp);
421 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700422}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200423EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700424
Divyesh Shah84c124d2010-04-09 08:31:19 +0200425void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800426 struct blkio_policy_type *pol,
427 uint64_t start_time,
428 uint64_t io_start_time, bool direction,
429 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700430{
Tejun Heoc1768262012-03-05 13:15:17 -0800431 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah91952912010-04-01 15:01:41 -0700432 struct blkio_group_stats *stats;
433 unsigned long flags;
434 unsigned long long now = sched_clock();
435
436 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800437 stats = &pd->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200438 if (time_after64(now, io_start_time))
439 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
440 now - io_start_time, direction, sync);
441 if (time_after64(io_start_time, start_time))
442 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
443 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700444 spin_unlock_irqrestore(&blkg->stats_lock, flags);
445}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200446EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700447
Vivek Goyal317389a2011-05-23 10:02:19 +0200448/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800449void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
450 struct blkio_policy_type *pol,
451 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700452{
Tejun Heoc1768262012-03-05 13:15:17 -0800453 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo5fe224d2012-03-08 10:53:57 -0800454 struct blkio_group_stats *stats;
Divyesh Shah812d4022010-04-08 21:14:23 -0700455 unsigned long flags;
456
Tejun Heo5fe224d2012-03-08 10:53:57 -0800457 spin_lock_irqsave(&blkg->stats_lock, flags);
458 stats = &pd->stats;
459 blkio_add_stat(stats->stat_arr[BLKIO_STAT_MERGED], 1, direction, sync);
460 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700461}
462EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
463
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800464/*
465 * Worker for allocating per cpu stat for blk groups. This is scheduled on
466 * the system_nrt_wq once there are some groups on the alloc_list waiting
467 * for allocation.
468 */
469static void blkio_stat_alloc_fn(struct work_struct *work)
470{
471 static void *pcpu_stats[BLKIO_NR_POLICIES];
472 struct delayed_work *dwork = to_delayed_work(work);
473 struct blkio_group *blkg;
474 int i;
475 bool empty = false;
476
477alloc_stats:
478 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
479 if (pcpu_stats[i] != NULL)
480 continue;
481
482 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
483
484 /* Allocation failed. Try again after some time. */
485 if (pcpu_stats[i] == NULL) {
486 queue_delayed_work(system_nrt_wq, dwork,
487 msecs_to_jiffies(10));
488 return;
489 }
490 }
491
492 spin_lock_irq(&blkio_list_lock);
493 spin_lock(&alloc_list_lock);
494
495 /* cgroup got deleted or queue exited. */
496 if (!list_empty(&alloc_list)) {
497 blkg = list_first_entry(&alloc_list, struct blkio_group,
498 alloc_node);
499 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
500 struct blkg_policy_data *pd = blkg->pd[i];
501
502 if (blkio_policy[i] && pd && !pd->stats_cpu)
503 swap(pd->stats_cpu, pcpu_stats[i]);
504 }
505
506 list_del_init(&blkg->alloc_node);
507 }
508
509 empty = list_empty(&alloc_list);
510
511 spin_unlock(&alloc_list_lock);
512 spin_unlock_irq(&blkio_list_lock);
513
514 if (!empty)
515 goto alloc_stats;
516}
517
Tejun Heo03814112012-03-05 13:15:14 -0800518/**
519 * blkg_free - free a blkg
520 * @blkg: blkg to free
521 *
522 * Free @blkg which may be partially allocated.
523 */
524static void blkg_free(struct blkio_group *blkg)
525{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800526 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800527
528 if (!blkg)
529 return;
530
Tejun Heoe8989fa2012-03-05 13:15:20 -0800531 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
532 struct blkg_policy_data *pd = blkg->pd[i];
533
534 if (pd) {
535 free_percpu(pd->stats_cpu);
536 kfree(pd);
537 }
Tejun Heo03814112012-03-05 13:15:14 -0800538 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800539
Tejun Heo549d3aa2012-03-05 13:15:16 -0800540 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800541}
542
543/**
544 * blkg_alloc - allocate a blkg
545 * @blkcg: block cgroup the new blkg is associated with
546 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800547 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800548 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800549 */
550static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800551 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800552{
553 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800554 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800555
556 /* alloc and init base part */
557 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
558 if (!blkg)
559 return NULL;
560
561 spin_lock_init(&blkg->stats_lock);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800562 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800563 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800564 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800565 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800566 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800567 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
568
Tejun Heoe8989fa2012-03-05 13:15:20 -0800569 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
570 struct blkio_policy_type *pol = blkio_policy[i];
571 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800572
Tejun Heoe8989fa2012-03-05 13:15:20 -0800573 if (!pol)
574 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800575
Tejun Heoe8989fa2012-03-05 13:15:20 -0800576 /* alloc per-policy data and attach it to blkg */
577 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
578 q->node);
579 if (!pd) {
580 blkg_free(blkg);
581 return NULL;
582 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800583
Tejun Heoe8989fa2012-03-05 13:15:20 -0800584 blkg->pd[i] = pd;
585 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800586 }
587
Tejun Heo549d3aa2012-03-05 13:15:16 -0800588 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800589 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
590 struct blkio_policy_type *pol = blkio_policy[i];
591
592 if (pol)
593 pol->ops.blkio_init_group_fn(blkg);
594 }
595
Tejun Heo03814112012-03-05 13:15:14 -0800596 return blkg;
597}
598
Tejun Heocd1604f2012-03-05 13:15:06 -0800599struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
600 struct request_queue *q,
601 enum blkio_policy_id plid,
602 bool for_root)
603 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400604{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800605 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400606
Tejun Heocd1604f2012-03-05 13:15:06 -0800607 WARN_ON_ONCE(!rcu_read_lock_held());
608 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500609
Tejun Heocd1604f2012-03-05 13:15:06 -0800610 /*
611 * This could be the first entry point of blkcg implementation and
612 * we shouldn't allow anything to go through for a bypassing queue.
613 * The following can be removed if blkg lookup is guaranteed to
614 * fail on a bypassing queue.
615 */
616 if (unlikely(blk_queue_bypass(q)) && !for_root)
617 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
618
Tejun Heoe8989fa2012-03-05 13:15:20 -0800619 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800620 if (blkg)
621 return blkg;
622
Tejun Heo7ee9c562012-03-05 13:15:11 -0800623 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800624 if (!css_tryget(&blkcg->css))
625 return ERR_PTR(-EINVAL);
626
627 /*
628 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800629 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800630 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800631
632 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800633 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800634 blkg = ERR_PTR(-ENOMEM);
635 goto out;
636 }
637
638 /* insert */
639 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500640 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800641 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800642 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800643
644 spin_lock(&alloc_list_lock);
645 list_add(&blkg->alloc_node, &alloc_list);
646 /* Queue per cpu stat allocation from worker thread. */
647 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
648 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800649out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800650 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500651}
Tejun Heocd1604f2012-03-05 13:15:06 -0800652EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500653
Vivek Goyal31e4c282009-12-03 12:59:42 -0500654/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800655struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800656 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500657{
658 struct blkio_group *blkg;
659 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500660
Tejun Heoca32aef2012-03-05 13:15:03 -0800661 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800662 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500663 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500664 return NULL;
665}
Tejun Heocd1604f2012-03-05 13:15:06 -0800666EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500667
Tejun Heoe8989fa2012-03-05 13:15:20 -0800668static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800669{
Tejun Heo03aa2642012-03-05 13:15:19 -0800670 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800671 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800672
673 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800674 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800675
676 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800677 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800678 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800679 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800680 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800681
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800682 spin_lock(&alloc_list_lock);
683 list_del_init(&blkg->alloc_node);
684 spin_unlock(&alloc_list_lock);
685
Tejun Heo03aa2642012-03-05 13:15:19 -0800686 /*
687 * Put the reference taken at the time of creation so that when all
688 * queues are gone, group can be destroyed.
689 */
690 blkg_put(blkg);
691}
692
Tejun Heoe8989fa2012-03-05 13:15:20 -0800693/*
694 * XXX: This updates blkg policy data in-place for root blkg, which is
695 * necessary across elevator switch and policy registration as root blkgs
696 * aren't shot down. This broken and racy implementation is temporary.
697 * Eventually, blkg shoot down will be replaced by proper in-place update.
698 */
699void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
700{
701 struct blkio_policy_type *pol = blkio_policy[plid];
702 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
703 struct blkg_policy_data *pd;
704
705 if (!blkg)
706 return;
707
708 kfree(blkg->pd[plid]);
709 blkg->pd[plid] = NULL;
710
711 if (!pol)
712 return;
713
714 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
715 WARN_ON_ONCE(!pd);
716
717 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
718 WARN_ON_ONCE(!pd->stats_cpu);
719
720 blkg->pd[plid] = pd;
721 pd->blkg = blkg;
722 pol->ops.blkio_init_group_fn(blkg);
723}
724EXPORT_SYMBOL_GPL(update_root_blkg_pd);
725
Tejun Heo9f13ef62012-03-05 13:15:21 -0800726/**
727 * blkg_destroy_all - destroy all blkgs associated with a request_queue
728 * @q: request_queue of interest
729 * @destroy_root: whether to destroy root blkg or not
730 *
731 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
732 * destroyed; otherwise, root blkg is left alone.
733 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800734void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800735{
736 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800737
Tejun Heo9f13ef62012-03-05 13:15:21 -0800738 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800739
Tejun Heo9f13ef62012-03-05 13:15:21 -0800740 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
741 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800742
Tejun Heo9f13ef62012-03-05 13:15:21 -0800743 /* skip root? */
744 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
745 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800746
Tejun Heo9f13ef62012-03-05 13:15:21 -0800747 spin_lock(&blkcg->lock);
748 blkg_destroy(blkg);
749 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800750 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800751
752 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800753}
Tejun Heo03aa2642012-03-05 13:15:19 -0800754EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800755
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800756static void blkg_rcu_free(struct rcu_head *rcu_head)
757{
758 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
759}
760
761void __blkg_release(struct blkio_group *blkg)
762{
763 /* release the extra blkcg reference this blkg has been holding */
764 css_put(&blkg->blkcg->css);
765
766 /*
767 * A group is freed in rcu manner. But having an rcu lock does not
768 * mean that one can access all the fields of blkg and assume these
769 * are valid. For example, don't try to follow throtl_data and
770 * request queue links.
771 *
772 * Having a reference to blkg under an rcu allows acess to only
773 * values local to groups like group stats and group rate limits
774 */
775 call_rcu(&blkg->rcu_head, blkg_rcu_free);
776}
777EXPORT_SYMBOL_GPL(__blkg_release);
778
Tejun Heoc1768262012-03-05 13:15:17 -0800779static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400780{
Tejun Heoc1768262012-03-05 13:15:17 -0800781 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800782 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800783
784 if (pd->stats_cpu == NULL)
785 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800786
787 for_each_possible_cpu(cpu) {
788 struct blkio_group_stats_cpu *sc =
789 per_cpu_ptr(pd->stats_cpu, cpu);
790
791 sc->sectors = 0;
792 memset(sc->stat_arr_cpu, 0, sizeof(sc->stat_arr_cpu));
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400793 }
794}
795
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700796static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200797blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700798{
Tejun Heo997a0262012-03-08 10:53:58 -0800799 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700800 struct blkio_group *blkg;
801 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700802 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700803
Tejun Heoe8989fa2012-03-05 13:15:20 -0800804 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700805 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800806
807 /*
808 * Note that stat reset is racy - it doesn't synchronize against
809 * stat updates. This is a debug feature which shouldn't exist
810 * anyway. If you get hit by a race, retry.
811 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700812 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800813 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800814
Tejun Heoe8989fa2012-03-05 13:15:20 -0800815 list_for_each_entry(pol, &blkio_list, list) {
816 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800817 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400818
Tejun Heo997a0262012-03-08 10:53:58 -0800819 /* queued stats shouldn't be cleared */
820 for (i = 0; i < ARRAY_SIZE(stats->stat_arr); i++)
821 if (i != BLKIO_STAT_QUEUED)
822 memset(stats->stat_arr[i], 0,
823 sizeof(stats->stat_arr[i]));
824 stats->time = 0;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800825#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo997a0262012-03-08 10:53:58 -0800826 memset((void *)stats + BLKG_STATS_DEBUG_CLEAR_START, 0,
827 BLKG_STATS_DEBUG_CLEAR_SIZE);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800828#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800829 blkio_reset_stats_cpu(blkg, pol->plid);
830 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700831 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400832
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700833 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800834 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700835 return 0;
836}
837
Tejun Heo7a4dd282012-03-05 13:15:09 -0800838static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
839 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700840{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800841 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700842 chars_left -= strlen(str);
843 if (chars_left <= 0) {
844 printk(KERN_WARNING
845 "Possibly incorrect cgroup stat display format");
846 return;
847 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200848 if (diskname_only)
849 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700850 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200851 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700852 strlcat(str, " Read", chars_left);
853 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200854 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700855 strlcat(str, " Write", chars_left);
856 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200857 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700858 strlcat(str, " Sync", chars_left);
859 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200860 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700861 strlcat(str, " Async", chars_left);
862 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200863 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700864 strlcat(str, " Total", chars_left);
865 break;
866 default:
867 strlcat(str, " Invalid", chars_left);
868 }
869}
870
Divyesh Shah84c124d2010-04-09 08:31:19 +0200871static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800872 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200873{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800874 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200875 cb->fill(cb, str, val);
876 return val;
877}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700878
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400879
Tejun Heoc1768262012-03-05 13:15:17 -0800880static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400881 enum stat_type_cpu type, enum stat_sub_type sub_type)
882{
Tejun Heoc1768262012-03-05 13:15:17 -0800883 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400884 int cpu;
885 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400886 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400887
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800888 if (pd->stats_cpu == NULL)
889 return val;
890
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400891 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400892 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800893 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400894
Vivek Goyal575969a2011-05-19 15:38:29 -0400895 do {
896 start = u64_stats_fetch_begin(&stats_cpu->syncp);
897 if (type == BLKIO_STAT_CPU_SECTORS)
898 tval = stats_cpu->sectors;
899 else
900 tval = stats_cpu->stat_arr_cpu[type][sub_type];
901 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
902
903 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400904 }
905
906 return val;
907}
908
Tejun Heoc1768262012-03-05 13:15:17 -0800909static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800910 struct cgroup_map_cb *cb, const char *dname,
911 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400912{
913 uint64_t disk_total, val;
914 char key_str[MAX_KEY_LEN];
915 enum stat_sub_type sub_type;
916
917 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800918 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800919 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
920 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400921 }
922
923 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
924 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800925 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
926 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800927 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400928 cb->fill(cb, key_str, val);
929 }
930
Tejun Heoc1768262012-03-05 13:15:17 -0800931 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
932 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400933
Tejun Heo7a4dd282012-03-05 13:15:09 -0800934 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
935 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400936 cb->fill(cb, key_str, disk_total);
937 return disk_total;
938}
939
Divyesh Shah84c124d2010-04-09 08:31:19 +0200940/* This should be called with blkg->stats_lock held */
Tejun Heoc1768262012-03-05 13:15:17 -0800941static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800942 struct cgroup_map_cb *cb, const char *dname,
943 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700944{
Tejun Heoc1768262012-03-05 13:15:17 -0800945 struct blkg_policy_data *pd = blkg->pd[plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700946 uint64_t disk_total;
947 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200948 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700949
Divyesh Shah84c124d2010-04-09 08:31:19 +0200950 if (type == BLKIO_STAT_TIME)
951 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800952 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100953#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100954 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
955 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800956 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700957 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800958 uint64_t sum = pd->stats.avg_queue_size_sum;
959 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700960 if (samples)
961 do_div(sum, samples);
962 else
963 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800964 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
965 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700966 }
Divyesh Shah812df482010-04-08 21:15:35 -0700967 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
968 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800969 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700970 if (type == BLKIO_STAT_IDLE_TIME)
971 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800972 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700973 if (type == BLKIO_STAT_EMPTY_TIME)
974 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800975 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200976 if (type == BLKIO_STAT_DEQUEUE)
977 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800978 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200979#endif
980
981 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
982 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800983 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
984 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800985 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700986 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800987 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
988 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800989 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
990 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700991 cb->fill(cb, key_str, disk_total);
992 return disk_total;
993}
994
Tejun Heo4bfd4822012-03-05 13:15:08 -0800995static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
996 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800997{
Tejun Heoece84242011-10-19 14:31:15 +0200998 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800999 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001000 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001001 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001002 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +02001003 int i = 0, ret = -EINVAL;
1004 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001005 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001006 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001007
1008 memset(s, 0, sizeof(s));
1009
1010 while ((p = strsep(&buf, " ")) != NULL) {
1011 if (!*p)
1012 continue;
1013
1014 s[i++] = p;
1015
1016 /* Prevent from inputing too many things */
1017 if (i == 3)
1018 break;
1019 }
1020
1021 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001022 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001023
1024 p = strsep(&s[0], ":");
1025 if (p != NULL)
1026 major_s = p;
1027 else
Tejun Heoece84242011-10-19 14:31:15 +02001028 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001029
1030 minor_s = s[0];
1031 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001032 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001033
Tejun Heoece84242011-10-19 14:31:15 +02001034 if (strict_strtoul(major_s, 10, &major))
1035 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001036
Tejun Heoece84242011-10-19 14:31:15 +02001037 if (strict_strtoul(minor_s, 10, &minor))
1038 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001039
1040 dev = MKDEV(major, minor);
1041
Tejun Heoece84242011-10-19 14:31:15 +02001042 if (strict_strtoull(s[1], 10, &temp))
1043 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001044
Tejun Heoe56da7e2012-03-05 13:15:07 -08001045 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001046 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001047 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001048
1049 rcu_read_lock();
1050
Tejun Heo4bfd4822012-03-05 13:15:08 -08001051 spin_lock_irq(disk->queue->queue_lock);
1052 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1053 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001054
Tejun Heo4bfd4822012-03-05 13:15:08 -08001055 if (IS_ERR(blkg)) {
1056 ret = PTR_ERR(blkg);
1057 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001058 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001059
Tejun Heo549d3aa2012-03-05 13:15:16 -08001060 pd = blkg->pd[plid];
1061
Vivek Goyal062a6442010-09-15 17:06:33 -04001062 switch (plid) {
1063 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001064 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1065 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001066 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001067
Tejun Heo549d3aa2012-03-05 13:15:16 -08001068 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001069 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001070 break;
1071 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001072 switch(fileid) {
1073 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001074 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001075 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001076 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001077 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001078 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001079 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001080 break;
1081 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001082 if (temp > THROTL_IOPS_MAX)
1083 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001084 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001085 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001086 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001087 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001088 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001089 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001090 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001091 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001092 break;
1093 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001094 break;
1095 default:
1096 BUG();
1097 }
Tejun Heoece84242011-10-19 14:31:15 +02001098 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001099out_unlock:
1100 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001101out:
1102 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001103
1104 /*
1105 * If queue was bypassing, we should retry. Do so after a short
1106 * msleep(). It isn't strictly necessary but queue can be
1107 * bypassing for some time and it's always nice to avoid busy
1108 * looping.
1109 */
1110 if (ret == -EBUSY) {
1111 msleep(10);
1112 return restart_syscall();
1113 }
Tejun Heoece84242011-10-19 14:31:15 +02001114 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001115}
1116
Vivek Goyal062a6442010-09-15 17:06:33 -04001117static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1118 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001119{
1120 int ret = 0;
1121 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001122 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001123 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1124 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001125
1126 buf = kstrdup(buffer, GFP_KERNEL);
1127 if (!buf)
1128 return -ENOMEM;
1129
Tejun Heo4bfd4822012-03-05 13:15:08 -08001130 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001131 kfree(buf);
1132 return ret;
1133}
1134
Vivek Goyal92616b52012-03-05 13:15:10 -08001135static const char *blkg_dev_name(struct blkio_group *blkg)
1136{
1137 /* some drivers (floppy) instantiate a queue w/o disk registered */
1138 if (blkg->q->backing_dev_info.dev)
1139 return dev_name(blkg->q->backing_dev_info.dev);
1140 return NULL;
1141}
1142
Tejun Heo4bfd4822012-03-05 13:15:08 -08001143static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1144 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001145{
Tejun Heoc1768262012-03-05 13:15:17 -08001146 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001147 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001148 struct blkg_policy_data *pd = blkg->pd[plid];
1149 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001150 int rw = WRITE;
1151
Vivek Goyal92616b52012-03-05 13:15:10 -08001152 if (!dname)
1153 return;
1154
Tejun Heoc1768262012-03-05 13:15:17 -08001155 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001156 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001157 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001158 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001159 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001160 break;
1161 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001162 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001163 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001164 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001165 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001166 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001167 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001168 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001169 break;
1170 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001171 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001172 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001173 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001174 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001175 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001176 break;
1177 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001178 break;
1179 default:
1180 BUG();
1181 }
1182}
1183
1184/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001185static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1186 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001187{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001188 struct blkio_group *blkg;
1189 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001190
Tejun Heo4bfd4822012-03-05 13:15:08 -08001191 spin_lock_irq(&blkcg->lock);
1192 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001193 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001194 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001195}
1196
1197static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1198 struct seq_file *m)
1199{
1200 struct blkio_cgroup *blkcg;
1201 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1202 int name = BLKIOFILE_ATTR(cft->private);
1203
1204 blkcg = cgroup_to_blkio_cgroup(cgrp);
1205
1206 switch(plid) {
1207 case BLKIO_POLICY_PROP:
1208 switch(name) {
1209 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001210 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001211 return 0;
1212 default:
1213 BUG();
1214 }
1215 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001216 case BLKIO_POLICY_THROTL:
1217 switch(name){
1218 case BLKIO_THROTL_read_bps_device:
1219 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001220 case BLKIO_THROTL_read_iops_device:
1221 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001222 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001223 return 0;
1224 default:
1225 BUG();
1226 }
1227 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001228 default:
1229 BUG();
1230 }
1231
1232 return 0;
1233}
1234
1235static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001236 struct cftype *cft, struct cgroup_map_cb *cb,
1237 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001238{
1239 struct blkio_group *blkg;
1240 struct hlist_node *n;
1241 uint64_t cgroup_total = 0;
1242
Tejun Heoc875f4d2012-03-05 13:15:22 -08001243 spin_lock_irq(&blkcg->lock);
1244
1245 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001246 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001247 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001248
Tejun Heoe8989fa2012-03-05 13:15:20 -08001249 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001250 continue;
Tejun Heoc1768262012-03-05 13:15:17 -08001251 if (pcpu) {
1252 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1253 cb, dname, type);
1254 } else {
Tejun Heoc875f4d2012-03-05 13:15:22 -08001255 spin_lock(&blkg->stats_lock);
Tejun Heoc1768262012-03-05 13:15:17 -08001256 cgroup_total += blkio_get_stat(blkg, plid,
1257 cb, dname, type);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001258 spin_unlock(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001259 }
1260 }
1261 if (show_total)
1262 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001263
1264 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001265 return 0;
1266}
1267
1268/* All map kind of cgroup file get serviced by this function */
1269static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1270 struct cgroup_map_cb *cb)
1271{
1272 struct blkio_cgroup *blkcg;
1273 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1274 int name = BLKIOFILE_ATTR(cft->private);
1275
1276 blkcg = cgroup_to_blkio_cgroup(cgrp);
1277
1278 switch(plid) {
1279 case BLKIO_POLICY_PROP:
1280 switch(name) {
1281 case BLKIO_PROP_time:
1282 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001283 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001284 case BLKIO_PROP_sectors:
1285 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001286 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001287 case BLKIO_PROP_io_service_bytes:
1288 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001289 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001290 case BLKIO_PROP_io_serviced:
1291 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001292 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001293 case BLKIO_PROP_io_service_time:
1294 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001295 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001296 case BLKIO_PROP_io_wait_time:
1297 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001298 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001299 case BLKIO_PROP_io_merged:
1300 return blkio_read_blkg_stats(blkcg, cft, cb,
Tejun Heo5fe224d2012-03-08 10:53:57 -08001301 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001302 case BLKIO_PROP_io_queued:
1303 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001304 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001305#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001306 case BLKIO_PROP_unaccounted_time:
1307 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001308 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001309 case BLKIO_PROP_dequeue:
1310 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001311 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001312 case BLKIO_PROP_avg_queue_size:
1313 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001314 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001315 case BLKIO_PROP_group_wait_time:
1316 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001317 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001318 case BLKIO_PROP_idle_time:
1319 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001320 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001321 case BLKIO_PROP_empty_time:
1322 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001323 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001324#endif
1325 default:
1326 BUG();
1327 }
1328 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001329 case BLKIO_POLICY_THROTL:
1330 switch(name){
1331 case BLKIO_THROTL_io_service_bytes:
1332 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001333 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001334 case BLKIO_THROTL_io_serviced:
1335 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001336 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001337 default:
1338 BUG();
1339 }
1340 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001341 default:
1342 BUG();
1343 }
1344
1345 return 0;
1346}
1347
Tejun Heo4bfd4822012-03-05 13:15:08 -08001348static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001349{
1350 struct blkio_group *blkg;
1351 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001352
1353 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1354 return -EINVAL;
1355
1356 spin_lock(&blkio_list_lock);
1357 spin_lock_irq(&blkcg->lock);
1358 blkcg->weight = (unsigned int)val;
1359
Tejun Heo549d3aa2012-03-05 13:15:16 -08001360 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001361 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001362
Tejun Heoe8989fa2012-03-05 13:15:20 -08001363 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001364 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001365 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001366
Vivek Goyal062a6442010-09-15 17:06:33 -04001367 spin_unlock_irq(&blkcg->lock);
1368 spin_unlock(&blkio_list_lock);
1369 return 0;
1370}
1371
1372static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1373 struct blkio_cgroup *blkcg;
1374 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1375 int name = BLKIOFILE_ATTR(cft->private);
1376
1377 blkcg = cgroup_to_blkio_cgroup(cgrp);
1378
1379 switch(plid) {
1380 case BLKIO_POLICY_PROP:
1381 switch(name) {
1382 case BLKIO_PROP_weight:
1383 return (u64)blkcg->weight;
1384 }
1385 break;
1386 default:
1387 BUG();
1388 }
1389 return 0;
1390}
1391
1392static int
1393blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1394{
1395 struct blkio_cgroup *blkcg;
1396 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1397 int name = BLKIOFILE_ATTR(cft->private);
1398
1399 blkcg = cgroup_to_blkio_cgroup(cgrp);
1400
1401 switch(plid) {
1402 case BLKIO_POLICY_PROP:
1403 switch(name) {
1404 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001405 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001406 }
1407 break;
1408 default:
1409 BUG();
1410 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001411
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001412 return 0;
1413}
1414
Vivek Goyal31e4c282009-12-03 12:59:42 -05001415struct cftype blkio_files[] = {
1416 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001417 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001418 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1419 BLKIO_PROP_weight_device),
1420 .read_seq_string = blkiocg_file_read,
1421 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001422 .max_write_len = 256,
1423 },
1424 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001425 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001426 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1427 BLKIO_PROP_weight),
1428 .read_u64 = blkiocg_file_read_u64,
1429 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001430 },
Vivek Goyal22084192009-12-03 12:59:49 -05001431 {
1432 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001433 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1434 BLKIO_PROP_time),
1435 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001436 },
1437 {
1438 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001439 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1440 BLKIO_PROP_sectors),
1441 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001442 },
1443 {
1444 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001445 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1446 BLKIO_PROP_io_service_bytes),
1447 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001448 },
1449 {
1450 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001451 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1452 BLKIO_PROP_io_serviced),
1453 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001454 },
1455 {
1456 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001457 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1458 BLKIO_PROP_io_service_time),
1459 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001460 },
1461 {
1462 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001463 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1464 BLKIO_PROP_io_wait_time),
1465 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001466 },
1467 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001468 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001469 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1470 BLKIO_PROP_io_merged),
1471 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001472 },
1473 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001474 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001475 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1476 BLKIO_PROP_io_queued),
1477 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001478 },
1479 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001480 .name = "reset_stats",
1481 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001482 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001483#ifdef CONFIG_BLK_DEV_THROTTLING
1484 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001485 .name = "throttle.read_bps_device",
1486 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1487 BLKIO_THROTL_read_bps_device),
1488 .read_seq_string = blkiocg_file_read,
1489 .write_string = blkiocg_file_write,
1490 .max_write_len = 256,
1491 },
1492
1493 {
1494 .name = "throttle.write_bps_device",
1495 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1496 BLKIO_THROTL_write_bps_device),
1497 .read_seq_string = blkiocg_file_read,
1498 .write_string = blkiocg_file_write,
1499 .max_write_len = 256,
1500 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001501
1502 {
1503 .name = "throttle.read_iops_device",
1504 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1505 BLKIO_THROTL_read_iops_device),
1506 .read_seq_string = blkiocg_file_read,
1507 .write_string = blkiocg_file_write,
1508 .max_write_len = 256,
1509 },
1510
1511 {
1512 .name = "throttle.write_iops_device",
1513 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1514 BLKIO_THROTL_write_iops_device),
1515 .read_seq_string = blkiocg_file_read,
1516 .write_string = blkiocg_file_write,
1517 .max_write_len = 256,
1518 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001519 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001520 .name = "throttle.io_service_bytes",
1521 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1522 BLKIO_THROTL_io_service_bytes),
1523 .read_map = blkiocg_file_read_map,
1524 },
1525 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001526 .name = "throttle.io_serviced",
1527 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1528 BLKIO_THROTL_io_serviced),
1529 .read_map = blkiocg_file_read_map,
1530 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001531#endif /* CONFIG_BLK_DEV_THROTTLING */
1532
Vivek Goyal22084192009-12-03 12:59:49 -05001533#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001534 {
1535 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001536 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1537 BLKIO_PROP_avg_queue_size),
1538 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001539 },
1540 {
Divyesh Shah812df482010-04-08 21:15:35 -07001541 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001542 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1543 BLKIO_PROP_group_wait_time),
1544 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001545 },
1546 {
1547 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001548 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1549 BLKIO_PROP_idle_time),
1550 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001551 },
1552 {
1553 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001554 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1555 BLKIO_PROP_empty_time),
1556 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001557 },
1558 {
Vivek Goyal22084192009-12-03 12:59:49 -05001559 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001560 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1561 BLKIO_PROP_dequeue),
1562 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001563 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001564 {
1565 .name = "unaccounted_time",
1566 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1567 BLKIO_PROP_unaccounted_time),
1568 .read_map = blkiocg_file_read_map,
1569 },
Vivek Goyal22084192009-12-03 12:59:49 -05001570#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001571};
1572
1573static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1574{
1575 return cgroup_add_files(cgroup, subsys, blkio_files,
1576 ARRAY_SIZE(blkio_files));
1577}
1578
Tejun Heo9f13ef62012-03-05 13:15:21 -08001579/**
1580 * blkiocg_pre_destroy - cgroup pre_destroy callback
1581 * @subsys: cgroup subsys
1582 * @cgroup: cgroup of interest
1583 *
1584 * This function is called when @cgroup is about to go away and responsible
1585 * for shooting down all blkgs associated with @cgroup. blkgs should be
1586 * removed while holding both q and blkcg locks. As blkcg lock is nested
1587 * inside q lock, this function performs reverse double lock dancing.
1588 *
1589 * This is the blkcg counterpart of ioc_release_fn().
1590 */
Tejun Heo7ee9c562012-03-05 13:15:11 -08001591static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1592 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001593{
1594 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1595
Tejun Heo9f13ef62012-03-05 13:15:21 -08001596 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001597
Tejun Heo9f13ef62012-03-05 13:15:21 -08001598 while (!hlist_empty(&blkcg->blkg_list)) {
1599 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1600 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001601 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001602
Tejun Heo9f13ef62012-03-05 13:15:21 -08001603 if (spin_trylock(q->queue_lock)) {
1604 blkg_destroy(blkg);
1605 spin_unlock(q->queue_lock);
1606 } else {
1607 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001608 cpu_relax();
Tejun Heo9f13ef62012-03-05 13:15:21 -08001609 spin_lock(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001610 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001611 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001612
Tejun Heo9f13ef62012-03-05 13:15:21 -08001613 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001614 return 0;
1615}
1616
1617static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1618{
1619 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1620
Ben Blum67523c42010-03-10 15:22:11 -08001621 if (blkcg != &blkio_root_cgroup)
1622 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001623}
1624
1625static struct cgroup_subsys_state *
1626blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1627{
Li Zefan03415092010-05-07 08:57:00 +02001628 struct blkio_cgroup *blkcg;
1629 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001630
Li Zefan03415092010-05-07 08:57:00 +02001631 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001632 blkcg = &blkio_root_cgroup;
1633 goto done;
1634 }
1635
Vivek Goyal31e4c282009-12-03 12:59:42 -05001636 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1637 if (!blkcg)
1638 return ERR_PTR(-ENOMEM);
1639
1640 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1641done:
1642 spin_lock_init(&blkcg->lock);
1643 INIT_HLIST_HEAD(&blkcg->blkg_list);
1644
1645 return &blkcg->css;
1646}
1647
Tejun Heo5efd6112012-03-05 13:15:12 -08001648/**
1649 * blkcg_init_queue - initialize blkcg part of request queue
1650 * @q: request_queue to initialize
1651 *
1652 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1653 * part of new request_queue @q.
1654 *
1655 * RETURNS:
1656 * 0 on success, -errno on failure.
1657 */
1658int blkcg_init_queue(struct request_queue *q)
1659{
Tejun Heo923adde2012-03-05 13:15:13 -08001660 int ret;
1661
Tejun Heo5efd6112012-03-05 13:15:12 -08001662 might_sleep();
1663
Tejun Heo923adde2012-03-05 13:15:13 -08001664 ret = blk_throtl_init(q);
1665 if (ret)
1666 return ret;
1667
1668 mutex_lock(&all_q_mutex);
1669 INIT_LIST_HEAD(&q->all_q_node);
1670 list_add_tail(&q->all_q_node, &all_q_list);
1671 mutex_unlock(&all_q_mutex);
1672
1673 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001674}
1675
1676/**
1677 * blkcg_drain_queue - drain blkcg part of request_queue
1678 * @q: request_queue to drain
1679 *
1680 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1681 */
1682void blkcg_drain_queue(struct request_queue *q)
1683{
1684 lockdep_assert_held(q->queue_lock);
1685
1686 blk_throtl_drain(q);
1687}
1688
1689/**
1690 * blkcg_exit_queue - exit and release blkcg part of request_queue
1691 * @q: request_queue being released
1692 *
1693 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1694 */
1695void blkcg_exit_queue(struct request_queue *q)
1696{
Tejun Heo923adde2012-03-05 13:15:13 -08001697 mutex_lock(&all_q_mutex);
1698 list_del_init(&q->all_q_node);
1699 mutex_unlock(&all_q_mutex);
1700
Tejun Heoe8989fa2012-03-05 13:15:20 -08001701 blkg_destroy_all(q, true);
1702
Tejun Heo5efd6112012-03-05 13:15:12 -08001703 blk_throtl_exit(q);
1704}
1705
Vivek Goyal31e4c282009-12-03 12:59:42 -05001706/*
1707 * We cannot support shared io contexts, as we have no mean to support
1708 * two tasks with the same ioc in two different groups without major rework
1709 * of the main cic data structures. For now we allow a task to change
1710 * its cgroup only if it's the only owner of its ioc.
1711 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001712static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1713 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001714{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001715 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001716 struct io_context *ioc;
1717 int ret = 0;
1718
1719 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001720 cgroup_taskset_for_each(task, cgrp, tset) {
1721 task_lock(task);
1722 ioc = task->io_context;
1723 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1724 ret = -EINVAL;
1725 task_unlock(task);
1726 if (ret)
1727 break;
1728 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001729 return ret;
1730}
1731
Tejun Heobb9d97b2011-12-12 18:12:21 -08001732static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1733 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001734{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001735 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001736 struct io_context *ioc;
1737
Tejun Heobb9d97b2011-12-12 18:12:21 -08001738 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001739 /* we don't lose anything even if ioc allocation fails */
1740 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1741 if (ioc) {
1742 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001743 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001744 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001745 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001746}
1747
Tejun Heo923adde2012-03-05 13:15:13 -08001748static void blkcg_bypass_start(void)
1749 __acquires(&all_q_mutex)
1750{
1751 struct request_queue *q;
1752
1753 mutex_lock(&all_q_mutex);
1754
1755 list_for_each_entry(q, &all_q_list, all_q_node) {
1756 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001757 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001758 }
1759}
1760
1761static void blkcg_bypass_end(void)
1762 __releases(&all_q_mutex)
1763{
1764 struct request_queue *q;
1765
1766 list_for_each_entry(q, &all_q_list, all_q_node)
1767 blk_queue_bypass_end(q);
1768
1769 mutex_unlock(&all_q_mutex);
1770}
1771
Vivek Goyal3e252062009-12-04 10:36:42 -05001772void blkio_policy_register(struct blkio_policy_type *blkiop)
1773{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001774 struct request_queue *q;
1775
Tejun Heo923adde2012-03-05 13:15:13 -08001776 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001777 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001778
1779 BUG_ON(blkio_policy[blkiop->plid]);
1780 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001781 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001782
Vivek Goyal3e252062009-12-04 10:36:42 -05001783 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001784 list_for_each_entry(q, &all_q_list, all_q_node)
1785 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001786 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001787}
1788EXPORT_SYMBOL_GPL(blkio_policy_register);
1789
1790void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1791{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001792 struct request_queue *q;
1793
Tejun Heo923adde2012-03-05 13:15:13 -08001794 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001795 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001796
1797 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1798 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001799 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001800
Vivek Goyal3e252062009-12-04 10:36:42 -05001801 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001802 list_for_each_entry(q, &all_q_list, all_q_node)
1803 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001804 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001805}
1806EXPORT_SYMBOL_GPL(blkio_policy_unregister);