blob: 30e07308db24a79d19b8b26b12d64bf588dd19f2 [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001/*
2 * Common Block IO controller cgroup interface
3 *
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
12 */
13#include <linux/ioprio.h>
Vivek Goyal22084192009-12-03 12:59:49 -050014#include <linux/seq_file.h>
15#include <linux/kdev_t.h>
Vivek Goyal9d6a9862009-12-04 10:36:41 -050016#include <linux/module.h>
Stephen Rothwellaccee782009-12-07 19:29:39 +110017#include <linux/err.h>
Divyesh Shah91952912010-04-01 15:01:41 -070018#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Gui Jianfeng34d0f172010-04-13 16:05:49 +080020#include <linux/genhd.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080021#include <linux/delay.h>
Tejun Heo9a9e8a22012-03-19 15:10:56 -070022#include <linux/atomic.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080023#include "blk-cgroup.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080024#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050025
Divyesh Shah84c124d2010-04-09 08:31:19 +020026#define MAX_KEY_LEN 100
27
Vivek Goyal3e252062009-12-04 10:36:42 -050028static DEFINE_SPINLOCK(blkio_list_lock);
29static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050030
Tejun Heo923adde2012-03-05 13:15:13 -080031static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
Vivek Goyal1cd9e032012-03-08 10:53:56 -080034/* List of groups pending per cpu stats allocation */
35static DEFINE_SPINLOCK(alloc_list_lock);
36static LIST_HEAD(alloc_list);
37
38static void blkio_stat_alloc_fn(struct work_struct *);
39static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
40
Vivek Goyal31e4c282009-12-03 12:59:42 -050041struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050042EXPORT_SYMBOL_GPL(blkio_root_cgroup);
43
Tejun Heo035d10b2012-03-05 13:15:04 -080044static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
45
Ben Blum67523c42010-03-10 15:22:11 -080046static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
47 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080048static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
49 struct cgroup_taskset *);
50static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
51 struct cgroup_taskset *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080052static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080053static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
54static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
55
Vivek Goyal062a6442010-09-15 17:06:33 -040056/* for encoding cft->private value on file */
57#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
58/* What policy owns the file, proportional or throttle */
59#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
60#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
61
Ben Blum67523c42010-03-10 15:22:11 -080062struct cgroup_subsys blkio_subsys = {
63 .name = "blkio",
64 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080065 .can_attach = blkiocg_can_attach,
66 .attach = blkiocg_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080067 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080068 .destroy = blkiocg_destroy,
69 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080070 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080071 .module = THIS_MODULE,
72};
73EXPORT_SYMBOL_GPL(blkio_subsys);
74
Vivek Goyal31e4c282009-12-03 12:59:42 -050075struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
76{
77 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
78 struct blkio_cgroup, css);
79}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050080EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050081
Tejun Heo4f85cb92012-03-05 13:15:28 -080082static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020083{
84 return container_of(task_subsys_state(tsk, blkio_subsys_id),
85 struct blkio_cgroup, css);
86}
Tejun Heo4f85cb92012-03-05 13:15:28 -080087
88struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
89{
90 if (bio && bio->bi_css)
91 return container_of(bio->bi_css, struct blkio_cgroup, css);
92 return task_blkio_cgroup(current);
93}
94EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020095
Tejun Heoc1768262012-03-05 13:15:17 -080096static inline void blkio_update_group_weight(struct blkio_group *blkg,
97 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040098{
99 struct blkio_policy_type *blkiop;
100
101 list_for_each_entry(blkiop, &blkio_list, list) {
102 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800103 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -0400104 continue;
105 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800106 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200107 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -0400108 }
109}
110
Tejun Heoc1768262012-03-05 13:15:17 -0800111static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
112 u64 bps, int fileid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400113{
114 struct blkio_policy_type *blkiop;
115
116 list_for_each_entry(blkiop, &blkio_list, list) {
117
118 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800119 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400120 continue;
121
122 if (fileid == BLKIO_THROTL_read_bps_device
123 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800124 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200125 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400126
127 if (fileid == BLKIO_THROTL_write_bps_device
128 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800129 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200130 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400131 }
132}
133
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400134static inline void blkio_update_group_iops(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800135 int plid, unsigned int iops,
136 int fileid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400137{
138 struct blkio_policy_type *blkiop;
139
140 list_for_each_entry(blkiop, &blkio_list, list) {
141
142 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800143 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400144 continue;
145
146 if (fileid == BLKIO_THROTL_read_iops_device
147 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800148 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200149 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400150
151 if (fileid == BLKIO_THROTL_write_iops_device
152 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800153 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200154 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400155 }
156}
157
Divyesh Shah91952912010-04-01 15:01:41 -0700158/*
159 * Add to the appropriate stat variable depending on the request type.
Tejun Heoedf1b872012-03-08 10:54:00 -0800160 * This should be called with queue_lock held.
Divyesh Shah91952912010-04-01 15:01:41 -0700161 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200162static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
163 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700164{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200165 if (direction)
166 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700167 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200168 stat[BLKIO_STAT_READ] += add;
169 if (sync)
170 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700171 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200172 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700173}
174
Divyesh Shahcdc11842010-04-08 21:15:10 -0700175/*
176 * Decrements the appropriate stat variable if non-zero depending on the
177 * request type. Panics on value being zero.
Tejun Heoedf1b872012-03-08 10:54:00 -0800178 * This should be called with the queue_lock held.
Divyesh Shahcdc11842010-04-08 21:15:10 -0700179 */
180static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
181{
182 if (direction) {
183 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
184 stat[BLKIO_STAT_WRITE]--;
185 } else {
186 BUG_ON(stat[BLKIO_STAT_READ] == 0);
187 stat[BLKIO_STAT_READ]--;
188 }
189 if (sync) {
190 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
191 stat[BLKIO_STAT_SYNC]--;
192 } else {
193 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
194 stat[BLKIO_STAT_ASYNC]--;
195 }
196}
197
198#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800199/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700200static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800201 struct blkio_policy_type *pol,
202 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700203{
Tejun Heoc1768262012-03-05 13:15:17 -0800204 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800205
206 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700207 return;
208 if (blkg == curr_blkg)
209 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800210 pd->stats.start_group_wait_time = sched_clock();
211 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700212}
213
Tejun Heoedf1b872012-03-08 10:54:00 -0800214/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700215static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
216{
217 unsigned long long now;
218
219 if (!blkio_blkg_waiting(stats))
220 return;
221
222 now = sched_clock();
223 if (time_after64(now, stats->start_group_wait_time))
224 stats->group_wait_time += now - stats->start_group_wait_time;
225 blkio_clear_blkg_waiting(stats);
226}
227
Tejun Heoedf1b872012-03-08 10:54:00 -0800228/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700229static void blkio_end_empty_time(struct blkio_group_stats *stats)
230{
231 unsigned long long now;
232
233 if (!blkio_blkg_empty(stats))
234 return;
235
236 now = sched_clock();
237 if (time_after64(now, stats->start_empty_time))
238 stats->empty_time += now - stats->start_empty_time;
239 blkio_clear_blkg_empty(stats);
240}
241
Tejun Heoc1768262012-03-05 13:15:17 -0800242void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
243 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700244{
Tejun Heoedf1b872012-03-08 10:54:00 -0800245 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700246
Tejun Heoedf1b872012-03-08 10:54:00 -0800247 lockdep_assert_held(blkg->q->queue_lock);
248 BUG_ON(blkio_blkg_idling(stats));
249
250 stats->start_idle_time = sched_clock();
251 blkio_mark_blkg_idling(stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700252}
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 Heoedf1b872012-03-08 10:54:00 -0800258 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700259
Tejun Heoedf1b872012-03-08 10:54:00 -0800260 lockdep_assert_held(blkg->q->queue_lock);
261
Divyesh Shah812df482010-04-08 21:15:35 -0700262 if (blkio_blkg_idling(stats)) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800263 unsigned long long now = sched_clock();
264
265 if (time_after64(now, stats->start_idle_time)) {
266 u64_stats_update_begin(&stats->syncp);
Divyesh Shah812df482010-04-08 21:15:35 -0700267 stats->idle_time += now - stats->start_idle_time;
Tejun Heoedf1b872012-03-08 10:54:00 -0800268 u64_stats_update_end(&stats->syncp);
269 }
Divyesh Shah812df482010-04-08 21:15:35 -0700270 blkio_clear_blkg_idling(stats);
271 }
Divyesh Shah812df482010-04-08 21:15:35 -0700272}
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 Heoedf1b872012-03-08 10:54:00 -0800278 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700279
Tejun Heoedf1b872012-03-08 10:54:00 -0800280 lockdep_assert_held(blkg->q->queue_lock);
281
282 u64_stats_update_begin(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700283 stats->avg_queue_size_sum +=
284 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
285 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
286 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700287 blkio_update_group_wait_time(stats);
Tejun Heoedf1b872012-03-08 10:54:00 -0800288 u64_stats_update_end(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700289}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200290EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
291
Tejun Heoc1768262012-03-05 13:15:17 -0800292void blkiocg_set_start_empty_time(struct blkio_group *blkg,
293 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200294{
Tejun Heoedf1b872012-03-08 10:54:00 -0800295 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200296
Tejun Heoedf1b872012-03-08 10:54:00 -0800297 lockdep_assert_held(blkg->q->queue_lock);
Divyesh Shah28baf442010-04-14 11:22:38 +0200298
299 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
Tejun Heoedf1b872012-03-08 10:54:00 -0800300 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE])
Divyesh Shah28baf442010-04-14 11:22:38 +0200301 return;
Divyesh Shah28baf442010-04-14 11:22:38 +0200302
303 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200304 * group is already marked empty. This can happen if cfqq got new
305 * request in parent group and moved to this group while being added
306 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200307 */
Tejun Heoedf1b872012-03-08 10:54:00 -0800308 if (blkio_blkg_empty(stats))
Vivek Goyale5ff0822010-04-26 19:25:11 +0200309 return;
Vivek Goyale5ff0822010-04-26 19:25:11 +0200310
Divyesh Shah28baf442010-04-14 11:22:38 +0200311 stats->start_empty_time = sched_clock();
312 blkio_mark_blkg_empty(stats);
Divyesh Shah28baf442010-04-14 11:22:38 +0200313}
314EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
315
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200316void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800317 struct blkio_policy_type *pol,
318 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200319{
Tejun Heoc1768262012-03-05 13:15:17 -0800320 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800321
Tejun Heoedf1b872012-03-08 10:54:00 -0800322 lockdep_assert_held(blkg->q->queue_lock);
323
Tejun Heo549d3aa2012-03-05 13:15:16 -0800324 pd->stats.dequeue += dequeue;
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200325}
326EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700327#else
328static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800329 struct blkio_policy_type *pol,
330 struct blkio_group *curr_blkg) { }
331static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700332#endif
333
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200334void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800335 struct blkio_policy_type *pol,
336 struct blkio_group *curr_blkg, bool direction,
337 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700338{
Tejun Heoedf1b872012-03-08 10:54:00 -0800339 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700340
Tejun Heoedf1b872012-03-08 10:54:00 -0800341 lockdep_assert_held(blkg->q->queue_lock);
342
343 u64_stats_update_begin(&stats->syncp);
344 blkio_add_stat(stats->stat_arr[BLKIO_STAT_QUEUED], 1, direction, sync);
345 blkio_end_empty_time(stats);
346 u64_stats_update_end(&stats->syncp);
347
Tejun Heoc1768262012-03-05 13:15:17 -0800348 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700349}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200350EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700351
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200352void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800353 struct blkio_policy_type *pol,
354 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700355{
Tejun Heoedf1b872012-03-08 10:54:00 -0800356 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700357
Tejun Heoedf1b872012-03-08 10:54:00 -0800358 lockdep_assert_held(blkg->q->queue_lock);
359
360 u64_stats_update_begin(&stats->syncp);
361 blkio_check_and_dec_stat(stats->stat_arr[BLKIO_STAT_QUEUED], direction,
362 sync);
363 u64_stats_update_end(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700364}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200365EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700366
Tejun Heoc1768262012-03-05 13:15:17 -0800367void blkiocg_update_timeslice_used(struct blkio_group *blkg,
368 struct blkio_policy_type *pol,
369 unsigned long time,
370 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500371{
Tejun Heoedf1b872012-03-08 10:54:00 -0800372 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700373
Tejun Heoedf1b872012-03-08 10:54:00 -0800374 lockdep_assert_held(blkg->q->queue_lock);
375
376 u64_stats_update_begin(&stats->syncp);
377 stats->time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400378#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800379 stats->unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400380#endif
Tejun Heoedf1b872012-03-08 10:54:00 -0800381 u64_stats_update_end(&stats->syncp);
Vivek Goyal22084192009-12-03 12:59:49 -0500382}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700383EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500384
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400385/*
386 * should be called under rcu read lock or queue lock to make sure blkg pointer
387 * is valid.
388 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200389void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800390 struct blkio_policy_type *pol,
391 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700392{
Tejun Heoc1768262012-03-05 13:15:17 -0800393 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400394 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400395 unsigned long flags;
396
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800397 /* If per cpu stats are not allocated yet, don't do any accounting. */
398 if (pd->stats_cpu == NULL)
399 return;
400
Vivek Goyal575969a2011-05-19 15:38:29 -0400401 /*
402 * Disabling interrupts to provide mutual exclusion between two
403 * writes on same cpu. It probably is not needed for 64bit. Not
404 * optimizing that case yet.
405 */
406 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700407
Tejun Heo549d3aa2012-03-05 13:15:16 -0800408 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400409
Vivek Goyal575969a2011-05-19 15:38:29 -0400410 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400411 stats_cpu->sectors += bytes >> 9;
412 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
413 1, direction, sync);
414 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
415 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400416 u64_stats_update_end(&stats_cpu->syncp);
417 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700418}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200419EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700420
Divyesh Shah84c124d2010-04-09 08:31:19 +0200421void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800422 struct blkio_policy_type *pol,
423 uint64_t start_time,
424 uint64_t io_start_time, bool direction,
425 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700426{
Tejun Heoedf1b872012-03-08 10:54:00 -0800427 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah91952912010-04-01 15:01:41 -0700428 unsigned long long now = sched_clock();
429
Tejun Heoedf1b872012-03-08 10:54:00 -0800430 lockdep_assert_held(blkg->q->queue_lock);
431
432 u64_stats_update_begin(&stats->syncp);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200433 if (time_after64(now, io_start_time))
434 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
435 now - io_start_time, direction, sync);
436 if (time_after64(io_start_time, start_time))
437 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
438 io_start_time - start_time, direction, sync);
Tejun Heoedf1b872012-03-08 10:54:00 -0800439 u64_stats_update_end(&stats->syncp);
Divyesh Shah91952912010-04-01 15:01:41 -0700440}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200441EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700442
Vivek Goyal317389a2011-05-23 10:02:19 +0200443/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800444void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
445 struct blkio_policy_type *pol,
446 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700447{
Tejun Heoedf1b872012-03-08 10:54:00 -0800448 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812d4022010-04-08 21:14:23 -0700449
Tejun Heoedf1b872012-03-08 10:54:00 -0800450 lockdep_assert_held(blkg->q->queue_lock);
451
452 u64_stats_update_begin(&stats->syncp);
Tejun Heo5fe224d2012-03-08 10:53:57 -0800453 blkio_add_stat(stats->stat_arr[BLKIO_STAT_MERGED], 1, direction, sync);
Tejun Heoedf1b872012-03-08 10:54:00 -0800454 u64_stats_update_end(&stats->syncp);
Divyesh Shah812d4022010-04-08 21:14:23 -0700455}
456EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
457
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800458/*
459 * Worker for allocating per cpu stat for blk groups. This is scheduled on
460 * the system_nrt_wq once there are some groups on the alloc_list waiting
461 * for allocation.
462 */
463static void blkio_stat_alloc_fn(struct work_struct *work)
464{
465 static void *pcpu_stats[BLKIO_NR_POLICIES];
466 struct delayed_work *dwork = to_delayed_work(work);
467 struct blkio_group *blkg;
468 int i;
469 bool empty = false;
470
471alloc_stats:
472 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
473 if (pcpu_stats[i] != NULL)
474 continue;
475
476 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
477
478 /* Allocation failed. Try again after some time. */
479 if (pcpu_stats[i] == NULL) {
480 queue_delayed_work(system_nrt_wq, dwork,
481 msecs_to_jiffies(10));
482 return;
483 }
484 }
485
486 spin_lock_irq(&blkio_list_lock);
487 spin_lock(&alloc_list_lock);
488
489 /* cgroup got deleted or queue exited. */
490 if (!list_empty(&alloc_list)) {
491 blkg = list_first_entry(&alloc_list, struct blkio_group,
492 alloc_node);
493 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
494 struct blkg_policy_data *pd = blkg->pd[i];
495
496 if (blkio_policy[i] && pd && !pd->stats_cpu)
497 swap(pd->stats_cpu, pcpu_stats[i]);
498 }
499
500 list_del_init(&blkg->alloc_node);
501 }
502
503 empty = list_empty(&alloc_list);
504
505 spin_unlock(&alloc_list_lock);
506 spin_unlock_irq(&blkio_list_lock);
507
508 if (!empty)
509 goto alloc_stats;
510}
511
Tejun Heo03814112012-03-05 13:15:14 -0800512/**
513 * blkg_free - free a blkg
514 * @blkg: blkg to free
515 *
516 * Free @blkg which may be partially allocated.
517 */
518static void blkg_free(struct blkio_group *blkg)
519{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800520 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800521
522 if (!blkg)
523 return;
524
Tejun Heoe8989fa2012-03-05 13:15:20 -0800525 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
526 struct blkg_policy_data *pd = blkg->pd[i];
527
528 if (pd) {
529 free_percpu(pd->stats_cpu);
530 kfree(pd);
531 }
Tejun Heo03814112012-03-05 13:15:14 -0800532 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800533
Tejun Heo549d3aa2012-03-05 13:15:16 -0800534 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800535}
536
537/**
538 * blkg_alloc - allocate a blkg
539 * @blkcg: block cgroup the new blkg is associated with
540 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800541 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800542 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800543 */
544static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800545 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800546{
547 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800548 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800549
550 /* alloc and init base part */
551 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
552 if (!blkg)
553 return NULL;
554
Tejun Heoc875f4d2012-03-05 13:15:22 -0800555 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800556 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800557 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800558 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800559 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800560 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
561
Tejun Heoe8989fa2012-03-05 13:15:20 -0800562 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
563 struct blkio_policy_type *pol = blkio_policy[i];
564 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800565
Tejun Heoe8989fa2012-03-05 13:15:20 -0800566 if (!pol)
567 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800568
Tejun Heoe8989fa2012-03-05 13:15:20 -0800569 /* alloc per-policy data and attach it to blkg */
570 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
571 q->node);
572 if (!pd) {
573 blkg_free(blkg);
574 return NULL;
575 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800576
Tejun Heoe8989fa2012-03-05 13:15:20 -0800577 blkg->pd[i] = pd;
578 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800579 }
580
Tejun Heo549d3aa2012-03-05 13:15:16 -0800581 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800582 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
583 struct blkio_policy_type *pol = blkio_policy[i];
584
585 if (pol)
586 pol->ops.blkio_init_group_fn(blkg);
587 }
588
Tejun Heo03814112012-03-05 13:15:14 -0800589 return blkg;
590}
591
Tejun Heocd1604f2012-03-05 13:15:06 -0800592struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
593 struct request_queue *q,
594 enum blkio_policy_id plid,
595 bool for_root)
596 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400597{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800598 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400599
Tejun Heocd1604f2012-03-05 13:15:06 -0800600 WARN_ON_ONCE(!rcu_read_lock_held());
601 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500602
Tejun Heocd1604f2012-03-05 13:15:06 -0800603 /*
604 * This could be the first entry point of blkcg implementation and
605 * we shouldn't allow anything to go through for a bypassing queue.
606 * The following can be removed if blkg lookup is guaranteed to
607 * fail on a bypassing queue.
608 */
609 if (unlikely(blk_queue_bypass(q)) && !for_root)
610 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
611
Tejun Heoe8989fa2012-03-05 13:15:20 -0800612 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800613 if (blkg)
614 return blkg;
615
Tejun Heo7ee9c562012-03-05 13:15:11 -0800616 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800617 if (!css_tryget(&blkcg->css))
618 return ERR_PTR(-EINVAL);
619
620 /*
621 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800622 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800623 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800624
625 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800626 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800627 blkg = ERR_PTR(-ENOMEM);
628 goto out;
629 }
630
631 /* insert */
632 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500633 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800634 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800635 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800636
637 spin_lock(&alloc_list_lock);
638 list_add(&blkg->alloc_node, &alloc_list);
639 /* Queue per cpu stat allocation from worker thread. */
640 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
641 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800642out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800643 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500644}
Tejun Heocd1604f2012-03-05 13:15:06 -0800645EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500646
Vivek Goyal31e4c282009-12-03 12:59:42 -0500647/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800648struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800649 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500650{
651 struct blkio_group *blkg;
652 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500653
Tejun Heoca32aef2012-03-05 13:15:03 -0800654 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800655 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500656 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500657 return NULL;
658}
Tejun Heocd1604f2012-03-05 13:15:06 -0800659EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500660
Tejun Heoe8989fa2012-03-05 13:15:20 -0800661static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800662{
Tejun Heo03aa2642012-03-05 13:15:19 -0800663 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800664 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800665
666 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800667 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800668
669 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800670 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800671 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800672 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800673 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800674
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800675 spin_lock(&alloc_list_lock);
676 list_del_init(&blkg->alloc_node);
677 spin_unlock(&alloc_list_lock);
678
Tejun Heo03aa2642012-03-05 13:15:19 -0800679 /*
680 * Put the reference taken at the time of creation so that when all
681 * queues are gone, group can be destroyed.
682 */
683 blkg_put(blkg);
684}
685
Tejun Heoe8989fa2012-03-05 13:15:20 -0800686/*
687 * XXX: This updates blkg policy data in-place for root blkg, which is
688 * necessary across elevator switch and policy registration as root blkgs
689 * aren't shot down. This broken and racy implementation is temporary.
690 * Eventually, blkg shoot down will be replaced by proper in-place update.
691 */
692void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
693{
694 struct blkio_policy_type *pol = blkio_policy[plid];
695 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
696 struct blkg_policy_data *pd;
697
698 if (!blkg)
699 return;
700
701 kfree(blkg->pd[plid]);
702 blkg->pd[plid] = NULL;
703
704 if (!pol)
705 return;
706
707 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
708 WARN_ON_ONCE(!pd);
709
710 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
711 WARN_ON_ONCE(!pd->stats_cpu);
712
713 blkg->pd[plid] = pd;
714 pd->blkg = blkg;
715 pol->ops.blkio_init_group_fn(blkg);
716}
717EXPORT_SYMBOL_GPL(update_root_blkg_pd);
718
Tejun Heo9f13ef62012-03-05 13:15:21 -0800719/**
720 * blkg_destroy_all - destroy all blkgs associated with a request_queue
721 * @q: request_queue of interest
722 * @destroy_root: whether to destroy root blkg or not
723 *
724 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
725 * destroyed; otherwise, root blkg is left alone.
726 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800727void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800728{
729 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800730
Tejun Heo9f13ef62012-03-05 13:15:21 -0800731 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800732
Tejun Heo9f13ef62012-03-05 13:15:21 -0800733 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
734 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800735
Tejun Heo9f13ef62012-03-05 13:15:21 -0800736 /* skip root? */
737 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
738 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800739
Tejun Heo9f13ef62012-03-05 13:15:21 -0800740 spin_lock(&blkcg->lock);
741 blkg_destroy(blkg);
742 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800743 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800744
745 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800746}
Tejun Heo03aa2642012-03-05 13:15:19 -0800747EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800748
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800749static void blkg_rcu_free(struct rcu_head *rcu_head)
750{
751 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
752}
753
754void __blkg_release(struct blkio_group *blkg)
755{
756 /* release the extra blkcg reference this blkg has been holding */
757 css_put(&blkg->blkcg->css);
758
759 /*
760 * A group is freed in rcu manner. But having an rcu lock does not
761 * mean that one can access all the fields of blkg and assume these
762 * are valid. For example, don't try to follow throtl_data and
763 * request queue links.
764 *
765 * Having a reference to blkg under an rcu allows acess to only
766 * values local to groups like group stats and group rate limits
767 */
768 call_rcu(&blkg->rcu_head, blkg_rcu_free);
769}
770EXPORT_SYMBOL_GPL(__blkg_release);
771
Tejun Heoc1768262012-03-05 13:15:17 -0800772static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400773{
Tejun Heoc1768262012-03-05 13:15:17 -0800774 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800775 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800776
777 if (pd->stats_cpu == NULL)
778 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800779
780 for_each_possible_cpu(cpu) {
781 struct blkio_group_stats_cpu *sc =
782 per_cpu_ptr(pd->stats_cpu, cpu);
783
784 sc->sectors = 0;
785 memset(sc->stat_arr_cpu, 0, sizeof(sc->stat_arr_cpu));
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400786 }
787}
788
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700789static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200790blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700791{
Tejun Heo997a0262012-03-08 10:53:58 -0800792 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700793 struct blkio_group *blkg;
794 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700795 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700796
Tejun Heoe8989fa2012-03-05 13:15:20 -0800797 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700798 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800799
800 /*
801 * Note that stat reset is racy - it doesn't synchronize against
802 * stat updates. This is a debug feature which shouldn't exist
803 * anyway. If you get hit by a race, retry.
804 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700805 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800806 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800807
Tejun Heoe8989fa2012-03-05 13:15:20 -0800808 list_for_each_entry(pol, &blkio_list, list) {
809 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800810 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400811
Tejun Heo997a0262012-03-08 10:53:58 -0800812 /* queued stats shouldn't be cleared */
813 for (i = 0; i < ARRAY_SIZE(stats->stat_arr); i++)
814 if (i != BLKIO_STAT_QUEUED)
815 memset(stats->stat_arr[i], 0,
816 sizeof(stats->stat_arr[i]));
817 stats->time = 0;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800818#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo997a0262012-03-08 10:53:58 -0800819 memset((void *)stats + BLKG_STATS_DEBUG_CLEAR_START, 0,
820 BLKG_STATS_DEBUG_CLEAR_SIZE);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800821#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800822 blkio_reset_stats_cpu(blkg, pol->plid);
823 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700824 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400825
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700826 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800827 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700828 return 0;
829}
830
Tejun Heo7a4dd282012-03-05 13:15:09 -0800831static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
832 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700833{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800834 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700835 chars_left -= strlen(str);
836 if (chars_left <= 0) {
837 printk(KERN_WARNING
838 "Possibly incorrect cgroup stat display format");
839 return;
840 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200841 if (diskname_only)
842 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700843 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200844 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700845 strlcat(str, " Read", chars_left);
846 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200847 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700848 strlcat(str, " Write", chars_left);
849 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200850 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700851 strlcat(str, " Sync", chars_left);
852 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200853 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700854 strlcat(str, " Async", chars_left);
855 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200856 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700857 strlcat(str, " Total", chars_left);
858 break;
859 default:
860 strlcat(str, " Invalid", chars_left);
861 }
862}
863
Tejun Heoc1768262012-03-05 13:15:17 -0800864static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400865 enum stat_type_cpu type, enum stat_sub_type sub_type)
866{
Tejun Heoc1768262012-03-05 13:15:17 -0800867 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400868 int cpu;
869 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400870 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400871
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800872 if (pd->stats_cpu == NULL)
873 return val;
874
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400875 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400876 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800877 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400878
Vivek Goyal575969a2011-05-19 15:38:29 -0400879 do {
880 start = u64_stats_fetch_begin(&stats_cpu->syncp);
881 if (type == BLKIO_STAT_CPU_SECTORS)
882 tval = stats_cpu->sectors;
883 else
884 tval = stats_cpu->stat_arr_cpu[type][sub_type];
885 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
886
887 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400888 }
889
890 return val;
891}
892
Tejun Heoc1768262012-03-05 13:15:17 -0800893static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800894 struct cgroup_map_cb *cb, const char *dname,
895 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400896{
897 uint64_t disk_total, val;
898 char key_str[MAX_KEY_LEN];
899 enum stat_sub_type sub_type;
900
901 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800902 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heoc4c76a02012-03-08 10:53:59 -0800903 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
904 cb->fill(cb, key_str, val);
905 return val;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400906 }
907
908 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
909 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800910 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
911 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800912 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400913 cb->fill(cb, key_str, val);
914 }
915
Tejun Heoc1768262012-03-05 13:15:17 -0800916 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
917 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400918
Tejun Heo7a4dd282012-03-05 13:15:09 -0800919 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
920 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400921 cb->fill(cb, key_str, disk_total);
922 return disk_total;
923}
924
Tejun Heoc1768262012-03-05 13:15:17 -0800925static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800926 struct cgroup_map_cb *cb, const char *dname,
927 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700928{
Tejun Heoc4c76a02012-03-08 10:53:59 -0800929 struct blkio_group_stats *stats = &blkg->pd[plid]->stats;
930 uint64_t v = 0, disk_total = 0;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700931 char key_str[MAX_KEY_LEN];
Tejun Heoedf1b872012-03-08 10:54:00 -0800932 unsigned int sync_start;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800933 int st;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700934
Tejun Heoc4c76a02012-03-08 10:53:59 -0800935 if (type >= BLKIO_STAT_ARR_NR) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800936 do {
937 sync_start = u64_stats_fetch_begin(&stats->syncp);
938 switch (type) {
939 case BLKIO_STAT_TIME:
940 v = stats->time;
941 break;
Justin TerAvest9026e522011-03-22 21:26:54 +0100942#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800943 case BLKIO_STAT_UNACCOUNTED_TIME:
944 v = stats->unaccounted_time;
945 break;
946 case BLKIO_STAT_AVG_QUEUE_SIZE: {
947 uint64_t samples = stats->avg_queue_size_samples;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200948
Tejun Heoedf1b872012-03-08 10:54:00 -0800949 if (samples) {
950 v = stats->avg_queue_size_sum;
951 do_div(v, samples);
952 }
953 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800954 }
Tejun Heoedf1b872012-03-08 10:54:00 -0800955 case BLKIO_STAT_IDLE_TIME:
956 v = stats->idle_time;
957 break;
958 case BLKIO_STAT_EMPTY_TIME:
959 v = stats->empty_time;
960 break;
961 case BLKIO_STAT_DEQUEUE:
962 v = stats->dequeue;
963 break;
964 case BLKIO_STAT_GROUP_WAIT_TIME:
965 v = stats->group_wait_time;
966 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800967#endif
Tejun Heoedf1b872012-03-08 10:54:00 -0800968 default:
969 WARN_ON_ONCE(1);
970 }
971 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
Tejun Heoc4c76a02012-03-08 10:53:59 -0800972
973 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
974 cb->fill(cb, key_str, v);
975 return v;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700976 }
Tejun Heoc4c76a02012-03-08 10:53:59 -0800977
978 for (st = BLKIO_STAT_READ; st < BLKIO_STAT_TOTAL; st++) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800979 do {
980 sync_start = u64_stats_fetch_begin(&stats->syncp);
981 v = stats->stat_arr[type][st];
982 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
Tejun Heoc4c76a02012-03-08 10:53:59 -0800983
984 blkio_get_key_name(st, dname, key_str, MAX_KEY_LEN, false);
985 cb->fill(cb, key_str, v);
986 if (st == BLKIO_STAT_READ || st == BLKIO_STAT_WRITE)
987 disk_total += v;
988 }
989
Tejun Heo7a4dd282012-03-05 13:15:09 -0800990 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
991 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700992 cb->fill(cb, key_str, disk_total);
993 return disk_total;
994}
995
Tejun Heo4bfd4822012-03-05 13:15:08 -0800996static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
997 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800998{
Tejun Heoece84242011-10-19 14:31:15 +0200999 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001000 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001001 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001002 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001003 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +02001004 int i = 0, ret = -EINVAL;
1005 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001006 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001007 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001008
1009 memset(s, 0, sizeof(s));
1010
1011 while ((p = strsep(&buf, " ")) != NULL) {
1012 if (!*p)
1013 continue;
1014
1015 s[i++] = p;
1016
1017 /* Prevent from inputing too many things */
1018 if (i == 3)
1019 break;
1020 }
1021
1022 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001023 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001024
1025 p = strsep(&s[0], ":");
1026 if (p != NULL)
1027 major_s = p;
1028 else
Tejun Heoece84242011-10-19 14:31:15 +02001029 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001030
1031 minor_s = s[0];
1032 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001033 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001034
Tejun Heoece84242011-10-19 14:31:15 +02001035 if (strict_strtoul(major_s, 10, &major))
1036 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001037
Tejun Heoece84242011-10-19 14:31:15 +02001038 if (strict_strtoul(minor_s, 10, &minor))
1039 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001040
1041 dev = MKDEV(major, minor);
1042
Tejun Heoece84242011-10-19 14:31:15 +02001043 if (strict_strtoull(s[1], 10, &temp))
1044 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001045
Tejun Heoe56da7e2012-03-05 13:15:07 -08001046 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001047 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001048 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001049
1050 rcu_read_lock();
1051
Tejun Heo4bfd4822012-03-05 13:15:08 -08001052 spin_lock_irq(disk->queue->queue_lock);
1053 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1054 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001055
Tejun Heo4bfd4822012-03-05 13:15:08 -08001056 if (IS_ERR(blkg)) {
1057 ret = PTR_ERR(blkg);
1058 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001059 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001060
Tejun Heo549d3aa2012-03-05 13:15:16 -08001061 pd = blkg->pd[plid];
1062
Vivek Goyal062a6442010-09-15 17:06:33 -04001063 switch (plid) {
1064 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001065 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1066 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001067 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001068
Tejun Heo549d3aa2012-03-05 13:15:16 -08001069 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001070 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001071 break;
1072 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001073 switch(fileid) {
1074 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001075 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001076 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001077 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001078 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001079 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001080 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001081 break;
1082 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001083 if (temp > THROTL_IOPS_MAX)
1084 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001085 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001086 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001087 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001088 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001089 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001090 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001091 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001092 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001093 break;
1094 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001095 break;
1096 default:
1097 BUG();
1098 }
Tejun Heoece84242011-10-19 14:31:15 +02001099 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001100out_unlock:
1101 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001102out:
1103 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001104
1105 /*
1106 * If queue was bypassing, we should retry. Do so after a short
1107 * msleep(). It isn't strictly necessary but queue can be
1108 * bypassing for some time and it's always nice to avoid busy
1109 * looping.
1110 */
1111 if (ret == -EBUSY) {
1112 msleep(10);
1113 return restart_syscall();
1114 }
Tejun Heoece84242011-10-19 14:31:15 +02001115 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001116}
1117
Vivek Goyal062a6442010-09-15 17:06:33 -04001118static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1119 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001120{
1121 int ret = 0;
1122 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001123 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001124 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1125 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001126
1127 buf = kstrdup(buffer, GFP_KERNEL);
1128 if (!buf)
1129 return -ENOMEM;
1130
Tejun Heo4bfd4822012-03-05 13:15:08 -08001131 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001132 kfree(buf);
1133 return ret;
1134}
1135
Vivek Goyal92616b52012-03-05 13:15:10 -08001136static const char *blkg_dev_name(struct blkio_group *blkg)
1137{
1138 /* some drivers (floppy) instantiate a queue w/o disk registered */
1139 if (blkg->q->backing_dev_info.dev)
1140 return dev_name(blkg->q->backing_dev_info.dev);
1141 return NULL;
1142}
1143
Tejun Heo4bfd4822012-03-05 13:15:08 -08001144static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1145 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001146{
Tejun Heoc1768262012-03-05 13:15:17 -08001147 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001148 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001149 struct blkg_policy_data *pd = blkg->pd[plid];
1150 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001151 int rw = WRITE;
1152
Vivek Goyal92616b52012-03-05 13:15:10 -08001153 if (!dname)
1154 return;
1155
Tejun Heoc1768262012-03-05 13:15:17 -08001156 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001157 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001158 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001159 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001160 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001161 break;
1162 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001163 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001164 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001165 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001166 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001167 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001168 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001169 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001170 break;
1171 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001172 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001173 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001174 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001175 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001176 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001177 break;
1178 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001179 break;
1180 default:
1181 BUG();
1182 }
1183}
1184
1185/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001186static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1187 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001188{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001189 struct blkio_group *blkg;
1190 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001191
Tejun Heo4bfd4822012-03-05 13:15:08 -08001192 spin_lock_irq(&blkcg->lock);
1193 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001194 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001195 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001196}
1197
1198static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1199 struct seq_file *m)
1200{
1201 struct blkio_cgroup *blkcg;
1202 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1203 int name = BLKIOFILE_ATTR(cft->private);
1204
1205 blkcg = cgroup_to_blkio_cgroup(cgrp);
1206
1207 switch(plid) {
1208 case BLKIO_POLICY_PROP:
1209 switch(name) {
1210 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001211 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001212 return 0;
1213 default:
1214 BUG();
1215 }
1216 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001217 case BLKIO_POLICY_THROTL:
1218 switch(name){
1219 case BLKIO_THROTL_read_bps_device:
1220 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001221 case BLKIO_THROTL_read_iops_device:
1222 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001223 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001224 return 0;
1225 default:
1226 BUG();
1227 }
1228 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001229 default:
1230 BUG();
1231 }
1232
1233 return 0;
1234}
1235
1236static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001237 struct cftype *cft, struct cgroup_map_cb *cb,
1238 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001239{
1240 struct blkio_group *blkg;
1241 struct hlist_node *n;
1242 uint64_t cgroup_total = 0;
1243
Tejun Heoc875f4d2012-03-05 13:15:22 -08001244 spin_lock_irq(&blkcg->lock);
1245
1246 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001247 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001248 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001249
Tejun Heoe8989fa2012-03-05 13:15:20 -08001250 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001251 continue;
Tejun Heoedf1b872012-03-08 10:54:00 -08001252 if (pcpu)
Tejun Heoc1768262012-03-05 13:15:17 -08001253 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1254 cb, dname, type);
Tejun Heoedf1b872012-03-08 10:54:00 -08001255 else
Tejun Heoc1768262012-03-05 13:15:17 -08001256 cgroup_total += blkio_get_stat(blkg, plid,
1257 cb, dname, type);
Vivek Goyal062a6442010-09-15 17:06:33 -04001258 }
1259 if (show_total)
1260 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001261
1262 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001263 return 0;
1264}
1265
1266/* All map kind of cgroup file get serviced by this function */
1267static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1268 struct cgroup_map_cb *cb)
1269{
1270 struct blkio_cgroup *blkcg;
1271 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1272 int name = BLKIOFILE_ATTR(cft->private);
1273
1274 blkcg = cgroup_to_blkio_cgroup(cgrp);
1275
1276 switch(plid) {
1277 case BLKIO_POLICY_PROP:
1278 switch(name) {
1279 case BLKIO_PROP_time:
1280 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001281 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001282 case BLKIO_PROP_sectors:
1283 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001284 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001285 case BLKIO_PROP_io_service_bytes:
1286 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001287 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001288 case BLKIO_PROP_io_serviced:
1289 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001290 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001291 case BLKIO_PROP_io_service_time:
1292 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001293 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001294 case BLKIO_PROP_io_wait_time:
1295 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001296 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001297 case BLKIO_PROP_io_merged:
1298 return blkio_read_blkg_stats(blkcg, cft, cb,
Tejun Heo5fe224d2012-03-08 10:53:57 -08001299 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001300 case BLKIO_PROP_io_queued:
1301 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001302 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001303#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001304 case BLKIO_PROP_unaccounted_time:
1305 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001306 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001307 case BLKIO_PROP_dequeue:
1308 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001309 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001310 case BLKIO_PROP_avg_queue_size:
1311 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001312 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001313 case BLKIO_PROP_group_wait_time:
1314 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001315 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001316 case BLKIO_PROP_idle_time:
1317 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001318 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001319 case BLKIO_PROP_empty_time:
1320 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001321 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001322#endif
1323 default:
1324 BUG();
1325 }
1326 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001327 case BLKIO_POLICY_THROTL:
1328 switch(name){
1329 case BLKIO_THROTL_io_service_bytes:
1330 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001331 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001332 case BLKIO_THROTL_io_serviced:
1333 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001334 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001335 default:
1336 BUG();
1337 }
1338 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001339 default:
1340 BUG();
1341 }
1342
1343 return 0;
1344}
1345
Tejun Heo4bfd4822012-03-05 13:15:08 -08001346static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001347{
1348 struct blkio_group *blkg;
1349 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001350
1351 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1352 return -EINVAL;
1353
1354 spin_lock(&blkio_list_lock);
1355 spin_lock_irq(&blkcg->lock);
1356 blkcg->weight = (unsigned int)val;
1357
Tejun Heo549d3aa2012-03-05 13:15:16 -08001358 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001359 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001360
Tejun Heoe8989fa2012-03-05 13:15:20 -08001361 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001362 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001363 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001364
Vivek Goyal062a6442010-09-15 17:06:33 -04001365 spin_unlock_irq(&blkcg->lock);
1366 spin_unlock(&blkio_list_lock);
1367 return 0;
1368}
1369
1370static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1371 struct blkio_cgroup *blkcg;
1372 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1373 int name = BLKIOFILE_ATTR(cft->private);
1374
1375 blkcg = cgroup_to_blkio_cgroup(cgrp);
1376
1377 switch(plid) {
1378 case BLKIO_POLICY_PROP:
1379 switch(name) {
1380 case BLKIO_PROP_weight:
1381 return (u64)blkcg->weight;
1382 }
1383 break;
1384 default:
1385 BUG();
1386 }
1387 return 0;
1388}
1389
1390static int
1391blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1392{
1393 struct blkio_cgroup *blkcg;
1394 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1395 int name = BLKIOFILE_ATTR(cft->private);
1396
1397 blkcg = cgroup_to_blkio_cgroup(cgrp);
1398
1399 switch(plid) {
1400 case BLKIO_POLICY_PROP:
1401 switch(name) {
1402 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001403 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001404 }
1405 break;
1406 default:
1407 BUG();
1408 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001409
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001410 return 0;
1411}
1412
Vivek Goyal31e4c282009-12-03 12:59:42 -05001413struct cftype blkio_files[] = {
1414 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001415 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001416 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1417 BLKIO_PROP_weight_device),
1418 .read_seq_string = blkiocg_file_read,
1419 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001420 .max_write_len = 256,
1421 },
1422 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001423 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001424 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1425 BLKIO_PROP_weight),
1426 .read_u64 = blkiocg_file_read_u64,
1427 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001428 },
Vivek Goyal22084192009-12-03 12:59:49 -05001429 {
1430 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001431 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1432 BLKIO_PROP_time),
1433 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001434 },
1435 {
1436 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001437 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1438 BLKIO_PROP_sectors),
1439 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001440 },
1441 {
1442 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001443 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1444 BLKIO_PROP_io_service_bytes),
1445 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001446 },
1447 {
1448 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001449 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1450 BLKIO_PROP_io_serviced),
1451 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001452 },
1453 {
1454 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001455 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1456 BLKIO_PROP_io_service_time),
1457 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001458 },
1459 {
1460 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001461 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1462 BLKIO_PROP_io_wait_time),
1463 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001464 },
1465 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001466 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001467 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1468 BLKIO_PROP_io_merged),
1469 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001470 },
1471 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001472 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001473 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1474 BLKIO_PROP_io_queued),
1475 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001476 },
1477 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001478 .name = "reset_stats",
1479 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001480 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001481#ifdef CONFIG_BLK_DEV_THROTTLING
1482 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001483 .name = "throttle.read_bps_device",
1484 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1485 BLKIO_THROTL_read_bps_device),
1486 .read_seq_string = blkiocg_file_read,
1487 .write_string = blkiocg_file_write,
1488 .max_write_len = 256,
1489 },
1490
1491 {
1492 .name = "throttle.write_bps_device",
1493 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1494 BLKIO_THROTL_write_bps_device),
1495 .read_seq_string = blkiocg_file_read,
1496 .write_string = blkiocg_file_write,
1497 .max_write_len = 256,
1498 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001499
1500 {
1501 .name = "throttle.read_iops_device",
1502 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1503 BLKIO_THROTL_read_iops_device),
1504 .read_seq_string = blkiocg_file_read,
1505 .write_string = blkiocg_file_write,
1506 .max_write_len = 256,
1507 },
1508
1509 {
1510 .name = "throttle.write_iops_device",
1511 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1512 BLKIO_THROTL_write_iops_device),
1513 .read_seq_string = blkiocg_file_read,
1514 .write_string = blkiocg_file_write,
1515 .max_write_len = 256,
1516 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001517 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001518 .name = "throttle.io_service_bytes",
1519 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1520 BLKIO_THROTL_io_service_bytes),
1521 .read_map = blkiocg_file_read_map,
1522 },
1523 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001524 .name = "throttle.io_serviced",
1525 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1526 BLKIO_THROTL_io_serviced),
1527 .read_map = blkiocg_file_read_map,
1528 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001529#endif /* CONFIG_BLK_DEV_THROTTLING */
1530
Vivek Goyal22084192009-12-03 12:59:49 -05001531#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001532 {
1533 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001534 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1535 BLKIO_PROP_avg_queue_size),
1536 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001537 },
1538 {
Divyesh Shah812df482010-04-08 21:15:35 -07001539 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001540 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1541 BLKIO_PROP_group_wait_time),
1542 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001543 },
1544 {
1545 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001546 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1547 BLKIO_PROP_idle_time),
1548 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001549 },
1550 {
1551 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001552 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1553 BLKIO_PROP_empty_time),
1554 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001555 },
1556 {
Vivek Goyal22084192009-12-03 12:59:49 -05001557 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001558 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1559 BLKIO_PROP_dequeue),
1560 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001561 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001562 {
1563 .name = "unaccounted_time",
1564 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1565 BLKIO_PROP_unaccounted_time),
1566 .read_map = blkiocg_file_read_map,
1567 },
Vivek Goyal22084192009-12-03 12:59:49 -05001568#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001569};
1570
1571static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1572{
1573 return cgroup_add_files(cgroup, subsys, blkio_files,
1574 ARRAY_SIZE(blkio_files));
1575}
1576
Tejun Heo9f13ef62012-03-05 13:15:21 -08001577/**
1578 * blkiocg_pre_destroy - cgroup pre_destroy callback
1579 * @subsys: cgroup subsys
1580 * @cgroup: cgroup of interest
1581 *
1582 * This function is called when @cgroup is about to go away and responsible
1583 * for shooting down all blkgs associated with @cgroup. blkgs should be
1584 * removed while holding both q and blkcg locks. As blkcg lock is nested
1585 * inside q lock, this function performs reverse double lock dancing.
1586 *
1587 * This is the blkcg counterpart of ioc_release_fn().
1588 */
Tejun Heo7ee9c562012-03-05 13:15:11 -08001589static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1590 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001591{
1592 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1593
Tejun Heo9f13ef62012-03-05 13:15:21 -08001594 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001595
Tejun Heo9f13ef62012-03-05 13:15:21 -08001596 while (!hlist_empty(&blkcg->blkg_list)) {
1597 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1598 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001599 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001600
Tejun Heo9f13ef62012-03-05 13:15:21 -08001601 if (spin_trylock(q->queue_lock)) {
1602 blkg_destroy(blkg);
1603 spin_unlock(q->queue_lock);
1604 } else {
1605 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001606 cpu_relax();
Tejun Heo9f13ef62012-03-05 13:15:21 -08001607 spin_lock(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001608 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001609 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001610
Tejun Heo9f13ef62012-03-05 13:15:21 -08001611 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001612 return 0;
1613}
1614
1615static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1616{
1617 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1618
Ben Blum67523c42010-03-10 15:22:11 -08001619 if (blkcg != &blkio_root_cgroup)
1620 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001621}
1622
1623static struct cgroup_subsys_state *
1624blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1625{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001626 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001627 struct blkio_cgroup *blkcg;
1628 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001629
Li Zefan03415092010-05-07 08:57:00 +02001630 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001631 blkcg = &blkio_root_cgroup;
1632 goto done;
1633 }
1634
Vivek Goyal31e4c282009-12-03 12:59:42 -05001635 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1636 if (!blkcg)
1637 return ERR_PTR(-ENOMEM);
1638
1639 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001640 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001641done:
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);