blob: 622fb41432264e32007ae6ad30a9bfbab39145f3 [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];
Vivek Goyal317389a2011-05-23 10:02:19 +0200454 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700455 unsigned long flags;
456
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800457 /* If per cpu stats are not allocated yet, don't do any accounting. */
458 if (pd->stats_cpu == NULL)
459 return;
460
Vivek Goyal317389a2011-05-23 10:02:19 +0200461 /*
462 * Disabling interrupts to provide mutual exclusion between two
463 * writes on same cpu. It probably is not needed for 64bit. Not
464 * optimizing that case yet.
465 */
466 local_irq_save(flags);
467
Tejun Heo549d3aa2012-03-05 13:15:16 -0800468 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal317389a2011-05-23 10:02:19 +0200469
470 u64_stats_update_begin(&stats_cpu->syncp);
471 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
472 direction, sync);
473 u64_stats_update_end(&stats_cpu->syncp);
474 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700475}
476EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
477
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800478/*
479 * Worker for allocating per cpu stat for blk groups. This is scheduled on
480 * the system_nrt_wq once there are some groups on the alloc_list waiting
481 * for allocation.
482 */
483static void blkio_stat_alloc_fn(struct work_struct *work)
484{
485 static void *pcpu_stats[BLKIO_NR_POLICIES];
486 struct delayed_work *dwork = to_delayed_work(work);
487 struct blkio_group *blkg;
488 int i;
489 bool empty = false;
490
491alloc_stats:
492 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
493 if (pcpu_stats[i] != NULL)
494 continue;
495
496 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
497
498 /* Allocation failed. Try again after some time. */
499 if (pcpu_stats[i] == NULL) {
500 queue_delayed_work(system_nrt_wq, dwork,
501 msecs_to_jiffies(10));
502 return;
503 }
504 }
505
506 spin_lock_irq(&blkio_list_lock);
507 spin_lock(&alloc_list_lock);
508
509 /* cgroup got deleted or queue exited. */
510 if (!list_empty(&alloc_list)) {
511 blkg = list_first_entry(&alloc_list, struct blkio_group,
512 alloc_node);
513 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
514 struct blkg_policy_data *pd = blkg->pd[i];
515
516 if (blkio_policy[i] && pd && !pd->stats_cpu)
517 swap(pd->stats_cpu, pcpu_stats[i]);
518 }
519
520 list_del_init(&blkg->alloc_node);
521 }
522
523 empty = list_empty(&alloc_list);
524
525 spin_unlock(&alloc_list_lock);
526 spin_unlock_irq(&blkio_list_lock);
527
528 if (!empty)
529 goto alloc_stats;
530}
531
Tejun Heo03814112012-03-05 13:15:14 -0800532/**
533 * blkg_free - free a blkg
534 * @blkg: blkg to free
535 *
536 * Free @blkg which may be partially allocated.
537 */
538static void blkg_free(struct blkio_group *blkg)
539{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800540 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800541
542 if (!blkg)
543 return;
544
Tejun Heoe8989fa2012-03-05 13:15:20 -0800545 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
546 struct blkg_policy_data *pd = blkg->pd[i];
547
548 if (pd) {
549 free_percpu(pd->stats_cpu);
550 kfree(pd);
551 }
Tejun Heo03814112012-03-05 13:15:14 -0800552 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800553
Tejun Heo549d3aa2012-03-05 13:15:16 -0800554 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800555}
556
557/**
558 * blkg_alloc - allocate a blkg
559 * @blkcg: block cgroup the new blkg is associated with
560 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800561 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800562 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800563 */
564static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800565 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800566{
567 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800568 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800569
570 /* alloc and init base part */
571 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
572 if (!blkg)
573 return NULL;
574
575 spin_lock_init(&blkg->stats_lock);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800576 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800577 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800578 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800579 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800580 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800581 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
582
Tejun Heoe8989fa2012-03-05 13:15:20 -0800583 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
584 struct blkio_policy_type *pol = blkio_policy[i];
585 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800586
Tejun Heoe8989fa2012-03-05 13:15:20 -0800587 if (!pol)
588 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800589
Tejun Heoe8989fa2012-03-05 13:15:20 -0800590 /* alloc per-policy data and attach it to blkg */
591 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
592 q->node);
593 if (!pd) {
594 blkg_free(blkg);
595 return NULL;
596 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800597
Tejun Heoe8989fa2012-03-05 13:15:20 -0800598 blkg->pd[i] = pd;
599 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800600 }
601
Tejun Heo549d3aa2012-03-05 13:15:16 -0800602 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800603 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
604 struct blkio_policy_type *pol = blkio_policy[i];
605
606 if (pol)
607 pol->ops.blkio_init_group_fn(blkg);
608 }
609
Tejun Heo03814112012-03-05 13:15:14 -0800610 return blkg;
611}
612
Tejun Heocd1604f2012-03-05 13:15:06 -0800613struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
614 struct request_queue *q,
615 enum blkio_policy_id plid,
616 bool for_root)
617 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400618{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800619 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400620
Tejun Heocd1604f2012-03-05 13:15:06 -0800621 WARN_ON_ONCE(!rcu_read_lock_held());
622 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500623
Tejun Heocd1604f2012-03-05 13:15:06 -0800624 /*
625 * This could be the first entry point of blkcg implementation and
626 * we shouldn't allow anything to go through for a bypassing queue.
627 * The following can be removed if blkg lookup is guaranteed to
628 * fail on a bypassing queue.
629 */
630 if (unlikely(blk_queue_bypass(q)) && !for_root)
631 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
632
Tejun Heoe8989fa2012-03-05 13:15:20 -0800633 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800634 if (blkg)
635 return blkg;
636
Tejun Heo7ee9c562012-03-05 13:15:11 -0800637 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800638 if (!css_tryget(&blkcg->css))
639 return ERR_PTR(-EINVAL);
640
641 /*
642 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800643 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800644 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800645
646 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800647 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800648 blkg = ERR_PTR(-ENOMEM);
649 goto out;
650 }
651
652 /* insert */
653 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500654 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800655 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800656 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800657
658 spin_lock(&alloc_list_lock);
659 list_add(&blkg->alloc_node, &alloc_list);
660 /* Queue per cpu stat allocation from worker thread. */
661 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
662 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800663out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800664 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500665}
Tejun Heocd1604f2012-03-05 13:15:06 -0800666EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500667
Vivek Goyal31e4c282009-12-03 12:59:42 -0500668/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800669struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800670 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500671{
672 struct blkio_group *blkg;
673 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500674
Tejun Heoca32aef2012-03-05 13:15:03 -0800675 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800676 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500677 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500678 return NULL;
679}
Tejun Heocd1604f2012-03-05 13:15:06 -0800680EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500681
Tejun Heoe8989fa2012-03-05 13:15:20 -0800682static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800683{
Tejun Heo03aa2642012-03-05 13:15:19 -0800684 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800685 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800686
687 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800688 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800689
690 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800691 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800692 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800693 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800694 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800695
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800696 spin_lock(&alloc_list_lock);
697 list_del_init(&blkg->alloc_node);
698 spin_unlock(&alloc_list_lock);
699
Tejun Heo03aa2642012-03-05 13:15:19 -0800700 /*
701 * Put the reference taken at the time of creation so that when all
702 * queues are gone, group can be destroyed.
703 */
704 blkg_put(blkg);
705}
706
Tejun Heoe8989fa2012-03-05 13:15:20 -0800707/*
708 * XXX: This updates blkg policy data in-place for root blkg, which is
709 * necessary across elevator switch and policy registration as root blkgs
710 * aren't shot down. This broken and racy implementation is temporary.
711 * Eventually, blkg shoot down will be replaced by proper in-place update.
712 */
713void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
714{
715 struct blkio_policy_type *pol = blkio_policy[plid];
716 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
717 struct blkg_policy_data *pd;
718
719 if (!blkg)
720 return;
721
722 kfree(blkg->pd[plid]);
723 blkg->pd[plid] = NULL;
724
725 if (!pol)
726 return;
727
728 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
729 WARN_ON_ONCE(!pd);
730
731 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
732 WARN_ON_ONCE(!pd->stats_cpu);
733
734 blkg->pd[plid] = pd;
735 pd->blkg = blkg;
736 pol->ops.blkio_init_group_fn(blkg);
737}
738EXPORT_SYMBOL_GPL(update_root_blkg_pd);
739
Tejun Heo9f13ef62012-03-05 13:15:21 -0800740/**
741 * blkg_destroy_all - destroy all blkgs associated with a request_queue
742 * @q: request_queue of interest
743 * @destroy_root: whether to destroy root blkg or not
744 *
745 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
746 * destroyed; otherwise, root blkg is left alone.
747 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800748void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800749{
750 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800751
Tejun Heo9f13ef62012-03-05 13:15:21 -0800752 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800753
Tejun Heo9f13ef62012-03-05 13:15:21 -0800754 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
755 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800756
Tejun Heo9f13ef62012-03-05 13:15:21 -0800757 /* skip root? */
758 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
759 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800760
Tejun Heo9f13ef62012-03-05 13:15:21 -0800761 spin_lock(&blkcg->lock);
762 blkg_destroy(blkg);
763 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800764 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800765
766 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800767}
Tejun Heo03aa2642012-03-05 13:15:19 -0800768EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800769
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800770static void blkg_rcu_free(struct rcu_head *rcu_head)
771{
772 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
773}
774
775void __blkg_release(struct blkio_group *blkg)
776{
777 /* release the extra blkcg reference this blkg has been holding */
778 css_put(&blkg->blkcg->css);
779
780 /*
781 * A group is freed in rcu manner. But having an rcu lock does not
782 * mean that one can access all the fields of blkg and assume these
783 * are valid. For example, don't try to follow throtl_data and
784 * request queue links.
785 *
786 * Having a reference to blkg under an rcu allows acess to only
787 * values local to groups like group stats and group rate limits
788 */
789 call_rcu(&blkg->rcu_head, blkg_rcu_free);
790}
791EXPORT_SYMBOL_GPL(__blkg_release);
792
Tejun Heoc1768262012-03-05 13:15:17 -0800793static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400794{
Tejun Heoc1768262012-03-05 13:15:17 -0800795 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400796 struct blkio_group_stats_cpu *stats_cpu;
797 int i, j, k;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800798
799 if (pd->stats_cpu == NULL)
800 return;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400801 /*
802 * Note: On 64 bit arch this should not be an issue. This has the
803 * possibility of returning some inconsistent value on 32bit arch
804 * as 64bit update on 32bit is non atomic. Taking care of this
805 * corner case makes code very complicated, like sending IPIs to
806 * cpus, taking care of stats of offline cpus etc.
807 *
808 * reset stats is anyway more of a debug feature and this sounds a
809 * corner case. So I am not complicating the code yet until and
810 * unless this becomes a real issue.
811 */
812 for_each_possible_cpu(i) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800813 stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400814 stats_cpu->sectors = 0;
815 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
816 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
817 stats_cpu->stat_arr_cpu[j][k] = 0;
818 }
819}
820
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700821static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200822blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700823{
824 struct blkio_cgroup *blkcg;
825 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700826 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700827 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700828 uint64_t queued[BLKIO_STAT_TOTAL];
829 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700830#ifdef CONFIG_DEBUG_BLK_CGROUP
831 bool idling, waiting, empty;
832 unsigned long long now = sched_clock();
833#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700834
835 blkcg = cgroup_to_blkio_cgroup(cgroup);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800836 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700837 spin_lock_irq(&blkcg->lock);
838 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800839 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800840
Tejun Heoe8989fa2012-03-05 13:15:20 -0800841 list_for_each_entry(pol, &blkio_list, list) {
842 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400843
Tejun Heoe8989fa2012-03-05 13:15:20 -0800844 spin_lock(&blkg->stats_lock);
845 stats = &pd->stats;
846#ifdef CONFIG_DEBUG_BLK_CGROUP
847 idling = blkio_blkg_idling(stats);
848 waiting = blkio_blkg_waiting(stats);
849 empty = blkio_blkg_empty(stats);
850#endif
851 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
852 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
853 memset(stats, 0, sizeof(struct blkio_group_stats));
854 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
855 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
856#ifdef CONFIG_DEBUG_BLK_CGROUP
857 if (idling) {
858 blkio_mark_blkg_idling(stats);
859 stats->start_idle_time = now;
860 }
861 if (waiting) {
862 blkio_mark_blkg_waiting(stats);
863 stats->start_group_wait_time = now;
864 }
865 if (empty) {
866 blkio_mark_blkg_empty(stats);
867 stats->start_empty_time = now;
868 }
869#endif
870 spin_unlock(&blkg->stats_lock);
871
872 /* Reset Per cpu stats which don't take blkg->stats_lock */
873 blkio_reset_stats_cpu(blkg, pol->plid);
874 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700875 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400876
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700877 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800878 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700879 return 0;
880}
881
Tejun Heo7a4dd282012-03-05 13:15:09 -0800882static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
883 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700884{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800885 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700886 chars_left -= strlen(str);
887 if (chars_left <= 0) {
888 printk(KERN_WARNING
889 "Possibly incorrect cgroup stat display format");
890 return;
891 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200892 if (diskname_only)
893 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700894 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200895 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700896 strlcat(str, " Read", chars_left);
897 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200898 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700899 strlcat(str, " Write", chars_left);
900 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200901 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700902 strlcat(str, " Sync", chars_left);
903 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200904 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700905 strlcat(str, " Async", chars_left);
906 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200907 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700908 strlcat(str, " Total", chars_left);
909 break;
910 default:
911 strlcat(str, " Invalid", chars_left);
912 }
913}
914
Divyesh Shah84c124d2010-04-09 08:31:19 +0200915static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800916 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200917{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800918 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200919 cb->fill(cb, str, val);
920 return val;
921}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700922
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400923
Tejun Heoc1768262012-03-05 13:15:17 -0800924static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400925 enum stat_type_cpu type, enum stat_sub_type sub_type)
926{
Tejun Heoc1768262012-03-05 13:15:17 -0800927 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400928 int cpu;
929 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400930 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400931
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800932 if (pd->stats_cpu == NULL)
933 return val;
934
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400935 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400936 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800937 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400938
Vivek Goyal575969a2011-05-19 15:38:29 -0400939 do {
940 start = u64_stats_fetch_begin(&stats_cpu->syncp);
941 if (type == BLKIO_STAT_CPU_SECTORS)
942 tval = stats_cpu->sectors;
943 else
944 tval = stats_cpu->stat_arr_cpu[type][sub_type];
945 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
946
947 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400948 }
949
950 return val;
951}
952
Tejun Heoc1768262012-03-05 13:15:17 -0800953static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800954 struct cgroup_map_cb *cb, const char *dname,
955 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400956{
957 uint64_t disk_total, val;
958 char key_str[MAX_KEY_LEN];
959 enum stat_sub_type sub_type;
960
961 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800962 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800963 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
964 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400965 }
966
967 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
968 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800969 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
970 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800971 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400972 cb->fill(cb, key_str, val);
973 }
974
Tejun Heoc1768262012-03-05 13:15:17 -0800975 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
976 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400977
Tejun Heo7a4dd282012-03-05 13:15:09 -0800978 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
979 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400980 cb->fill(cb, key_str, disk_total);
981 return disk_total;
982}
983
Divyesh Shah84c124d2010-04-09 08:31:19 +0200984/* This should be called with blkg->stats_lock held */
Tejun Heoc1768262012-03-05 13:15:17 -0800985static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800986 struct cgroup_map_cb *cb, const char *dname,
987 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700988{
Tejun Heoc1768262012-03-05 13:15:17 -0800989 struct blkg_policy_data *pd = blkg->pd[plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700990 uint64_t disk_total;
991 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200992 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700993
Divyesh Shah84c124d2010-04-09 08:31:19 +0200994 if (type == BLKIO_STAT_TIME)
995 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800996 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100997#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100998 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
999 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001000 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -07001001 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -08001002 uint64_t sum = pd->stats.avg_queue_size_sum;
1003 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -07001004 if (samples)
1005 do_div(sum, samples);
1006 else
1007 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -08001008 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
1009 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -07001010 }
Divyesh Shah812df482010-04-08 21:15:35 -07001011 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
1012 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001013 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -07001014 if (type == BLKIO_STAT_IDLE_TIME)
1015 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001016 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -07001017 if (type == BLKIO_STAT_EMPTY_TIME)
1018 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001019 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +02001020 if (type == BLKIO_STAT_DEQUEUE)
1021 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001022 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +02001023#endif
1024
1025 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
1026 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -08001027 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
1028 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001029 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001030 }
Tejun Heo549d3aa2012-03-05 13:15:16 -08001031 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
1032 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -08001033 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
1034 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001035 cb->fill(cb, key_str, disk_total);
1036 return disk_total;
1037}
1038
Tejun Heo4bfd4822012-03-05 13:15:08 -08001039static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
1040 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001041{
Tejun Heoece84242011-10-19 14:31:15 +02001042 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001043 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001044 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001045 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001046 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +02001047 int i = 0, ret = -EINVAL;
1048 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001049 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001050 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001051
1052 memset(s, 0, sizeof(s));
1053
1054 while ((p = strsep(&buf, " ")) != NULL) {
1055 if (!*p)
1056 continue;
1057
1058 s[i++] = p;
1059
1060 /* Prevent from inputing too many things */
1061 if (i == 3)
1062 break;
1063 }
1064
1065 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001066 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001067
1068 p = strsep(&s[0], ":");
1069 if (p != NULL)
1070 major_s = p;
1071 else
Tejun Heoece84242011-10-19 14:31:15 +02001072 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001073
1074 minor_s = s[0];
1075 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001076 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001077
Tejun Heoece84242011-10-19 14:31:15 +02001078 if (strict_strtoul(major_s, 10, &major))
1079 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001080
Tejun Heoece84242011-10-19 14:31:15 +02001081 if (strict_strtoul(minor_s, 10, &minor))
1082 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001083
1084 dev = MKDEV(major, minor);
1085
Tejun Heoece84242011-10-19 14:31:15 +02001086 if (strict_strtoull(s[1], 10, &temp))
1087 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001088
Tejun Heoe56da7e2012-03-05 13:15:07 -08001089 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001090 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001091 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001092
1093 rcu_read_lock();
1094
Tejun Heo4bfd4822012-03-05 13:15:08 -08001095 spin_lock_irq(disk->queue->queue_lock);
1096 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1097 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001098
Tejun Heo4bfd4822012-03-05 13:15:08 -08001099 if (IS_ERR(blkg)) {
1100 ret = PTR_ERR(blkg);
1101 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001102 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001103
Tejun Heo549d3aa2012-03-05 13:15:16 -08001104 pd = blkg->pd[plid];
1105
Vivek Goyal062a6442010-09-15 17:06:33 -04001106 switch (plid) {
1107 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001108 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1109 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001110 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001111
Tejun Heo549d3aa2012-03-05 13:15:16 -08001112 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001113 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001114 break;
1115 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001116 switch(fileid) {
1117 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001118 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001119 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001120 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001121 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001122 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001123 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001124 break;
1125 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001126 if (temp > THROTL_IOPS_MAX)
1127 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001128 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001129 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001130 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001131 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001132 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001133 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001134 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001135 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001136 break;
1137 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001138 break;
1139 default:
1140 BUG();
1141 }
Tejun Heoece84242011-10-19 14:31:15 +02001142 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001143out_unlock:
1144 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001145out:
1146 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001147
1148 /*
1149 * If queue was bypassing, we should retry. Do so after a short
1150 * msleep(). It isn't strictly necessary but queue can be
1151 * bypassing for some time and it's always nice to avoid busy
1152 * looping.
1153 */
1154 if (ret == -EBUSY) {
1155 msleep(10);
1156 return restart_syscall();
1157 }
Tejun Heoece84242011-10-19 14:31:15 +02001158 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001159}
1160
Vivek Goyal062a6442010-09-15 17:06:33 -04001161static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1162 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001163{
1164 int ret = 0;
1165 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001166 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001167 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1168 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001169
1170 buf = kstrdup(buffer, GFP_KERNEL);
1171 if (!buf)
1172 return -ENOMEM;
1173
Tejun Heo4bfd4822012-03-05 13:15:08 -08001174 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001175 kfree(buf);
1176 return ret;
1177}
1178
Vivek Goyal92616b52012-03-05 13:15:10 -08001179static const char *blkg_dev_name(struct blkio_group *blkg)
1180{
1181 /* some drivers (floppy) instantiate a queue w/o disk registered */
1182 if (blkg->q->backing_dev_info.dev)
1183 return dev_name(blkg->q->backing_dev_info.dev);
1184 return NULL;
1185}
1186
Tejun Heo4bfd4822012-03-05 13:15:08 -08001187static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1188 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001189{
Tejun Heoc1768262012-03-05 13:15:17 -08001190 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001191 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001192 struct blkg_policy_data *pd = blkg->pd[plid];
1193 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001194 int rw = WRITE;
1195
Vivek Goyal92616b52012-03-05 13:15:10 -08001196 if (!dname)
1197 return;
1198
Tejun Heoc1768262012-03-05 13:15:17 -08001199 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001200 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001201 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001202 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001203 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001204 break;
1205 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001206 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001207 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001208 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001209 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001210 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001211 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001212 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001213 break;
1214 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001215 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001216 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001217 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001218 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001219 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001220 break;
1221 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001222 break;
1223 default:
1224 BUG();
1225 }
1226}
1227
1228/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001229static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1230 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001231{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001232 struct blkio_group *blkg;
1233 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001234
Tejun Heo4bfd4822012-03-05 13:15:08 -08001235 spin_lock_irq(&blkcg->lock);
1236 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001237 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001238 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001239}
1240
1241static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1242 struct seq_file *m)
1243{
1244 struct blkio_cgroup *blkcg;
1245 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1246 int name = BLKIOFILE_ATTR(cft->private);
1247
1248 blkcg = cgroup_to_blkio_cgroup(cgrp);
1249
1250 switch(plid) {
1251 case BLKIO_POLICY_PROP:
1252 switch(name) {
1253 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001254 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001255 return 0;
1256 default:
1257 BUG();
1258 }
1259 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001260 case BLKIO_POLICY_THROTL:
1261 switch(name){
1262 case BLKIO_THROTL_read_bps_device:
1263 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001264 case BLKIO_THROTL_read_iops_device:
1265 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001266 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001267 return 0;
1268 default:
1269 BUG();
1270 }
1271 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001272 default:
1273 BUG();
1274 }
1275
1276 return 0;
1277}
1278
1279static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001280 struct cftype *cft, struct cgroup_map_cb *cb,
1281 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001282{
1283 struct blkio_group *blkg;
1284 struct hlist_node *n;
1285 uint64_t cgroup_total = 0;
1286
Tejun Heoc875f4d2012-03-05 13:15:22 -08001287 spin_lock_irq(&blkcg->lock);
1288
1289 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001290 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001291 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001292
Tejun Heoe8989fa2012-03-05 13:15:20 -08001293 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001294 continue;
Tejun Heoc1768262012-03-05 13:15:17 -08001295 if (pcpu) {
1296 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1297 cb, dname, type);
1298 } else {
Tejun Heoc875f4d2012-03-05 13:15:22 -08001299 spin_lock(&blkg->stats_lock);
Tejun Heoc1768262012-03-05 13:15:17 -08001300 cgroup_total += blkio_get_stat(blkg, plid,
1301 cb, dname, type);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001302 spin_unlock(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001303 }
1304 }
1305 if (show_total)
1306 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001307
1308 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001309 return 0;
1310}
1311
1312/* All map kind of cgroup file get serviced by this function */
1313static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1314 struct cgroup_map_cb *cb)
1315{
1316 struct blkio_cgroup *blkcg;
1317 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1318 int name = BLKIOFILE_ATTR(cft->private);
1319
1320 blkcg = cgroup_to_blkio_cgroup(cgrp);
1321
1322 switch(plid) {
1323 case BLKIO_POLICY_PROP:
1324 switch(name) {
1325 case BLKIO_PROP_time:
1326 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001327 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001328 case BLKIO_PROP_sectors:
1329 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001330 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001331 case BLKIO_PROP_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 Goyal062a6442010-09-15 17:06:33 -04001334 case BLKIO_PROP_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 Goyal062a6442010-09-15 17:06:33 -04001337 case BLKIO_PROP_io_service_time:
1338 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001339 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001340 case BLKIO_PROP_io_wait_time:
1341 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001342 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001343 case BLKIO_PROP_io_merged:
1344 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001345 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001346 case BLKIO_PROP_io_queued:
1347 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001348 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001349#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001350 case BLKIO_PROP_unaccounted_time:
1351 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001352 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001353 case BLKIO_PROP_dequeue:
1354 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001355 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001356 case BLKIO_PROP_avg_queue_size:
1357 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001358 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001359 case BLKIO_PROP_group_wait_time:
1360 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001361 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001362 case BLKIO_PROP_idle_time:
1363 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001364 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001365 case BLKIO_PROP_empty_time:
1366 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001367 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001368#endif
1369 default:
1370 BUG();
1371 }
1372 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001373 case BLKIO_POLICY_THROTL:
1374 switch(name){
1375 case BLKIO_THROTL_io_service_bytes:
1376 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001377 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001378 case BLKIO_THROTL_io_serviced:
1379 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001380 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001381 default:
1382 BUG();
1383 }
1384 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001385 default:
1386 BUG();
1387 }
1388
1389 return 0;
1390}
1391
Tejun Heo4bfd4822012-03-05 13:15:08 -08001392static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001393{
1394 struct blkio_group *blkg;
1395 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001396
1397 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1398 return -EINVAL;
1399
1400 spin_lock(&blkio_list_lock);
1401 spin_lock_irq(&blkcg->lock);
1402 blkcg->weight = (unsigned int)val;
1403
Tejun Heo549d3aa2012-03-05 13:15:16 -08001404 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001405 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001406
Tejun Heoe8989fa2012-03-05 13:15:20 -08001407 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001408 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001409 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001410
Vivek Goyal062a6442010-09-15 17:06:33 -04001411 spin_unlock_irq(&blkcg->lock);
1412 spin_unlock(&blkio_list_lock);
1413 return 0;
1414}
1415
1416static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1417 struct blkio_cgroup *blkcg;
1418 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1419 int name = BLKIOFILE_ATTR(cft->private);
1420
1421 blkcg = cgroup_to_blkio_cgroup(cgrp);
1422
1423 switch(plid) {
1424 case BLKIO_POLICY_PROP:
1425 switch(name) {
1426 case BLKIO_PROP_weight:
1427 return (u64)blkcg->weight;
1428 }
1429 break;
1430 default:
1431 BUG();
1432 }
1433 return 0;
1434}
1435
1436static int
1437blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1438{
1439 struct blkio_cgroup *blkcg;
1440 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1441 int name = BLKIOFILE_ATTR(cft->private);
1442
1443 blkcg = cgroup_to_blkio_cgroup(cgrp);
1444
1445 switch(plid) {
1446 case BLKIO_POLICY_PROP:
1447 switch(name) {
1448 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001449 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001450 }
1451 break;
1452 default:
1453 BUG();
1454 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001455
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001456 return 0;
1457}
1458
Vivek Goyal31e4c282009-12-03 12:59:42 -05001459struct cftype blkio_files[] = {
1460 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001461 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001462 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1463 BLKIO_PROP_weight_device),
1464 .read_seq_string = blkiocg_file_read,
1465 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001466 .max_write_len = 256,
1467 },
1468 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001469 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001470 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1471 BLKIO_PROP_weight),
1472 .read_u64 = blkiocg_file_read_u64,
1473 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001474 },
Vivek Goyal22084192009-12-03 12:59:49 -05001475 {
1476 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001477 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1478 BLKIO_PROP_time),
1479 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001480 },
1481 {
1482 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001483 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1484 BLKIO_PROP_sectors),
1485 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001486 },
1487 {
1488 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001489 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1490 BLKIO_PROP_io_service_bytes),
1491 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001492 },
1493 {
1494 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001495 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1496 BLKIO_PROP_io_serviced),
1497 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001498 },
1499 {
1500 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001501 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1502 BLKIO_PROP_io_service_time),
1503 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001504 },
1505 {
1506 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001507 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1508 BLKIO_PROP_io_wait_time),
1509 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001510 },
1511 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001512 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001513 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1514 BLKIO_PROP_io_merged),
1515 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001516 },
1517 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001518 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001519 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1520 BLKIO_PROP_io_queued),
1521 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001522 },
1523 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001524 .name = "reset_stats",
1525 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001526 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001527#ifdef CONFIG_BLK_DEV_THROTTLING
1528 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001529 .name = "throttle.read_bps_device",
1530 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1531 BLKIO_THROTL_read_bps_device),
1532 .read_seq_string = blkiocg_file_read,
1533 .write_string = blkiocg_file_write,
1534 .max_write_len = 256,
1535 },
1536
1537 {
1538 .name = "throttle.write_bps_device",
1539 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1540 BLKIO_THROTL_write_bps_device),
1541 .read_seq_string = blkiocg_file_read,
1542 .write_string = blkiocg_file_write,
1543 .max_write_len = 256,
1544 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001545
1546 {
1547 .name = "throttle.read_iops_device",
1548 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1549 BLKIO_THROTL_read_iops_device),
1550 .read_seq_string = blkiocg_file_read,
1551 .write_string = blkiocg_file_write,
1552 .max_write_len = 256,
1553 },
1554
1555 {
1556 .name = "throttle.write_iops_device",
1557 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1558 BLKIO_THROTL_write_iops_device),
1559 .read_seq_string = blkiocg_file_read,
1560 .write_string = blkiocg_file_write,
1561 .max_write_len = 256,
1562 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001563 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001564 .name = "throttle.io_service_bytes",
1565 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1566 BLKIO_THROTL_io_service_bytes),
1567 .read_map = blkiocg_file_read_map,
1568 },
1569 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001570 .name = "throttle.io_serviced",
1571 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1572 BLKIO_THROTL_io_serviced),
1573 .read_map = blkiocg_file_read_map,
1574 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001575#endif /* CONFIG_BLK_DEV_THROTTLING */
1576
Vivek Goyal22084192009-12-03 12:59:49 -05001577#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001578 {
1579 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001580 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1581 BLKIO_PROP_avg_queue_size),
1582 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001583 },
1584 {
Divyesh Shah812df482010-04-08 21:15:35 -07001585 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001586 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1587 BLKIO_PROP_group_wait_time),
1588 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001589 },
1590 {
1591 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001592 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1593 BLKIO_PROP_idle_time),
1594 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001595 },
1596 {
1597 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001598 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1599 BLKIO_PROP_empty_time),
1600 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001601 },
1602 {
Vivek Goyal22084192009-12-03 12:59:49 -05001603 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001604 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1605 BLKIO_PROP_dequeue),
1606 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001607 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001608 {
1609 .name = "unaccounted_time",
1610 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1611 BLKIO_PROP_unaccounted_time),
1612 .read_map = blkiocg_file_read_map,
1613 },
Vivek Goyal22084192009-12-03 12:59:49 -05001614#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001615};
1616
1617static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1618{
1619 return cgroup_add_files(cgroup, subsys, blkio_files,
1620 ARRAY_SIZE(blkio_files));
1621}
1622
Tejun Heo9f13ef62012-03-05 13:15:21 -08001623/**
1624 * blkiocg_pre_destroy - cgroup pre_destroy callback
1625 * @subsys: cgroup subsys
1626 * @cgroup: cgroup of interest
1627 *
1628 * This function is called when @cgroup is about to go away and responsible
1629 * for shooting down all blkgs associated with @cgroup. blkgs should be
1630 * removed while holding both q and blkcg locks. As blkcg lock is nested
1631 * inside q lock, this function performs reverse double lock dancing.
1632 *
1633 * This is the blkcg counterpart of ioc_release_fn().
1634 */
Tejun Heo7ee9c562012-03-05 13:15:11 -08001635static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1636 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001637{
1638 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1639
Tejun Heo9f13ef62012-03-05 13:15:21 -08001640 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001641
Tejun Heo9f13ef62012-03-05 13:15:21 -08001642 while (!hlist_empty(&blkcg->blkg_list)) {
1643 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1644 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001645 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001646
Tejun Heo9f13ef62012-03-05 13:15:21 -08001647 if (spin_trylock(q->queue_lock)) {
1648 blkg_destroy(blkg);
1649 spin_unlock(q->queue_lock);
1650 } else {
1651 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001652 cpu_relax();
Tejun Heo9f13ef62012-03-05 13:15:21 -08001653 spin_lock(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001654 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001655 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001656
Tejun Heo9f13ef62012-03-05 13:15:21 -08001657 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001658 return 0;
1659}
1660
1661static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1662{
1663 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1664
Ben Blum67523c42010-03-10 15:22:11 -08001665 if (blkcg != &blkio_root_cgroup)
1666 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001667}
1668
1669static struct cgroup_subsys_state *
1670blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1671{
Li Zefan03415092010-05-07 08:57:00 +02001672 struct blkio_cgroup *blkcg;
1673 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001674
Li Zefan03415092010-05-07 08:57:00 +02001675 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001676 blkcg = &blkio_root_cgroup;
1677 goto done;
1678 }
1679
Vivek Goyal31e4c282009-12-03 12:59:42 -05001680 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1681 if (!blkcg)
1682 return ERR_PTR(-ENOMEM);
1683
1684 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1685done:
1686 spin_lock_init(&blkcg->lock);
1687 INIT_HLIST_HEAD(&blkcg->blkg_list);
1688
1689 return &blkcg->css;
1690}
1691
Tejun Heo5efd6112012-03-05 13:15:12 -08001692/**
1693 * blkcg_init_queue - initialize blkcg part of request queue
1694 * @q: request_queue to initialize
1695 *
1696 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1697 * part of new request_queue @q.
1698 *
1699 * RETURNS:
1700 * 0 on success, -errno on failure.
1701 */
1702int blkcg_init_queue(struct request_queue *q)
1703{
Tejun Heo923adde2012-03-05 13:15:13 -08001704 int ret;
1705
Tejun Heo5efd6112012-03-05 13:15:12 -08001706 might_sleep();
1707
Tejun Heo923adde2012-03-05 13:15:13 -08001708 ret = blk_throtl_init(q);
1709 if (ret)
1710 return ret;
1711
1712 mutex_lock(&all_q_mutex);
1713 INIT_LIST_HEAD(&q->all_q_node);
1714 list_add_tail(&q->all_q_node, &all_q_list);
1715 mutex_unlock(&all_q_mutex);
1716
1717 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001718}
1719
1720/**
1721 * blkcg_drain_queue - drain blkcg part of request_queue
1722 * @q: request_queue to drain
1723 *
1724 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1725 */
1726void blkcg_drain_queue(struct request_queue *q)
1727{
1728 lockdep_assert_held(q->queue_lock);
1729
1730 blk_throtl_drain(q);
1731}
1732
1733/**
1734 * blkcg_exit_queue - exit and release blkcg part of request_queue
1735 * @q: request_queue being released
1736 *
1737 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1738 */
1739void blkcg_exit_queue(struct request_queue *q)
1740{
Tejun Heo923adde2012-03-05 13:15:13 -08001741 mutex_lock(&all_q_mutex);
1742 list_del_init(&q->all_q_node);
1743 mutex_unlock(&all_q_mutex);
1744
Tejun Heoe8989fa2012-03-05 13:15:20 -08001745 blkg_destroy_all(q, true);
1746
Tejun Heo5efd6112012-03-05 13:15:12 -08001747 blk_throtl_exit(q);
1748}
1749
Vivek Goyal31e4c282009-12-03 12:59:42 -05001750/*
1751 * We cannot support shared io contexts, as we have no mean to support
1752 * two tasks with the same ioc in two different groups without major rework
1753 * of the main cic data structures. For now we allow a task to change
1754 * its cgroup only if it's the only owner of its ioc.
1755 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001756static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1757 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001758{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001759 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001760 struct io_context *ioc;
1761 int ret = 0;
1762
1763 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001764 cgroup_taskset_for_each(task, cgrp, tset) {
1765 task_lock(task);
1766 ioc = task->io_context;
1767 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1768 ret = -EINVAL;
1769 task_unlock(task);
1770 if (ret)
1771 break;
1772 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001773 return ret;
1774}
1775
Tejun Heobb9d97b2011-12-12 18:12:21 -08001776static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1777 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001778{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001779 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001780 struct io_context *ioc;
1781
Tejun Heobb9d97b2011-12-12 18:12:21 -08001782 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001783 /* we don't lose anything even if ioc allocation fails */
1784 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1785 if (ioc) {
1786 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001787 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001788 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001789 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001790}
1791
Tejun Heo923adde2012-03-05 13:15:13 -08001792static void blkcg_bypass_start(void)
1793 __acquires(&all_q_mutex)
1794{
1795 struct request_queue *q;
1796
1797 mutex_lock(&all_q_mutex);
1798
1799 list_for_each_entry(q, &all_q_list, all_q_node) {
1800 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001801 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001802 }
1803}
1804
1805static void blkcg_bypass_end(void)
1806 __releases(&all_q_mutex)
1807{
1808 struct request_queue *q;
1809
1810 list_for_each_entry(q, &all_q_list, all_q_node)
1811 blk_queue_bypass_end(q);
1812
1813 mutex_unlock(&all_q_mutex);
1814}
1815
Vivek Goyal3e252062009-12-04 10:36:42 -05001816void blkio_policy_register(struct blkio_policy_type *blkiop)
1817{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001818 struct request_queue *q;
1819
Tejun Heo923adde2012-03-05 13:15:13 -08001820 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001821 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001822
1823 BUG_ON(blkio_policy[blkiop->plid]);
1824 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001825 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001826
Vivek Goyal3e252062009-12-04 10:36:42 -05001827 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001828 list_for_each_entry(q, &all_q_list, all_q_node)
1829 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001830 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001831}
1832EXPORT_SYMBOL_GPL(blkio_policy_register);
1833
1834void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1835{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001836 struct request_queue *q;
1837
Tejun Heo923adde2012-03-05 13:15:13 -08001838 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001839 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001840
1841 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1842 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001843 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001844
Vivek Goyal3e252062009-12-04 10:36:42 -05001845 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001846 list_for_each_entry(q, &all_q_list, all_q_node)
1847 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001848 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001849}
1850EXPORT_SYMBOL_GPL(blkio_policy_unregister);