blob: aa54c4110f5414ecfe3bf8e884cfc813b73376bf [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 *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080050static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080051static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
52static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
53
Vivek Goyal062a6442010-09-15 17:06:33 -040054/* for encoding cft->private value on file */
55#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
56/* What policy owns the file, proportional or throttle */
57#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
58#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
59
Ben Blum67523c42010-03-10 15:22:11 -080060struct cgroup_subsys blkio_subsys = {
61 .name = "blkio",
62 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080063 .can_attach = blkiocg_can_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080064 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080065 .destroy = blkiocg_destroy,
66 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080067 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080068 .module = THIS_MODULE,
69};
70EXPORT_SYMBOL_GPL(blkio_subsys);
71
Vivek Goyal31e4c282009-12-03 12:59:42 -050072struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
73{
74 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
75 struct blkio_cgroup, css);
76}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050077EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050078
Tejun Heo4f85cb92012-03-05 13:15:28 -080079static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
Vivek Goyal70087dc2011-05-16 15:24:08 +020080{
81 return container_of(task_subsys_state(tsk, blkio_subsys_id),
82 struct blkio_cgroup, css);
83}
Tejun Heo4f85cb92012-03-05 13:15:28 -080084
85struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
86{
87 if (bio && bio->bi_css)
88 return container_of(bio->bi_css, struct blkio_cgroup, css);
89 return task_blkio_cgroup(current);
90}
91EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
Vivek Goyal70087dc2011-05-16 15:24:08 +020092
Tejun Heoc1768262012-03-05 13:15:17 -080093static inline void blkio_update_group_weight(struct blkio_group *blkg,
94 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040095{
96 struct blkio_policy_type *blkiop;
97
98 list_for_each_entry(blkiop, &blkio_list, list) {
99 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800100 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -0400101 continue;
102 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800103 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200104 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -0400105 }
106}
107
Tejun Heoc1768262012-03-05 13:15:17 -0800108static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
109 u64 bps, int fileid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400110{
111 struct blkio_policy_type *blkiop;
112
113 list_for_each_entry(blkiop, &blkio_list, list) {
114
115 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800116 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400117 continue;
118
119 if (fileid == BLKIO_THROTL_read_bps_device
120 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800121 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200122 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400123
124 if (fileid == BLKIO_THROTL_write_bps_device
125 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800126 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200127 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400128 }
129}
130
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400131static inline void blkio_update_group_iops(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800132 int plid, unsigned int iops,
133 int fileid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400134{
135 struct blkio_policy_type *blkiop;
136
137 list_for_each_entry(blkiop, &blkio_list, list) {
138
139 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800140 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400141 continue;
142
143 if (fileid == BLKIO_THROTL_read_iops_device
144 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800145 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200146 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400147
148 if (fileid == BLKIO_THROTL_write_iops_device
149 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800150 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200151 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400152 }
153}
154
Divyesh Shah91952912010-04-01 15:01:41 -0700155/*
156 * Add to the appropriate stat variable depending on the request type.
Tejun Heoedf1b872012-03-08 10:54:00 -0800157 * This should be called with queue_lock held.
Divyesh Shah91952912010-04-01 15:01:41 -0700158 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200159static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
160 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700161{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200162 if (direction)
163 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700164 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200165 stat[BLKIO_STAT_READ] += add;
166 if (sync)
167 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700168 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200169 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700170}
171
Divyesh Shahcdc11842010-04-08 21:15:10 -0700172/*
173 * Decrements the appropriate stat variable if non-zero depending on the
174 * request type. Panics on value being zero.
Tejun Heoedf1b872012-03-08 10:54:00 -0800175 * This should be called with the queue_lock held.
Divyesh Shahcdc11842010-04-08 21:15:10 -0700176 */
177static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
178{
179 if (direction) {
180 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
181 stat[BLKIO_STAT_WRITE]--;
182 } else {
183 BUG_ON(stat[BLKIO_STAT_READ] == 0);
184 stat[BLKIO_STAT_READ]--;
185 }
186 if (sync) {
187 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
188 stat[BLKIO_STAT_SYNC]--;
189 } else {
190 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
191 stat[BLKIO_STAT_ASYNC]--;
192 }
193}
194
195#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800196/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700197static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800198 struct blkio_policy_type *pol,
199 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700200{
Tejun Heoc1768262012-03-05 13:15:17 -0800201 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800202
203 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700204 return;
205 if (blkg == curr_blkg)
206 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800207 pd->stats.start_group_wait_time = sched_clock();
208 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700209}
210
Tejun Heoedf1b872012-03-08 10:54:00 -0800211/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700212static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
213{
214 unsigned long long now;
215
216 if (!blkio_blkg_waiting(stats))
217 return;
218
219 now = sched_clock();
220 if (time_after64(now, stats->start_group_wait_time))
221 stats->group_wait_time += now - stats->start_group_wait_time;
222 blkio_clear_blkg_waiting(stats);
223}
224
Tejun Heoedf1b872012-03-08 10:54:00 -0800225/* This should be called with the queue_lock held. */
Divyesh Shah812df482010-04-08 21:15:35 -0700226static void blkio_end_empty_time(struct blkio_group_stats *stats)
227{
228 unsigned long long now;
229
230 if (!blkio_blkg_empty(stats))
231 return;
232
233 now = sched_clock();
234 if (time_after64(now, stats->start_empty_time))
235 stats->empty_time += now - stats->start_empty_time;
236 blkio_clear_blkg_empty(stats);
237}
238
Tejun Heoc1768262012-03-05 13:15:17 -0800239void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
240 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700241{
Tejun Heoedf1b872012-03-08 10:54:00 -0800242 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700243
Tejun Heoedf1b872012-03-08 10:54:00 -0800244 lockdep_assert_held(blkg->q->queue_lock);
245 BUG_ON(blkio_blkg_idling(stats));
246
247 stats->start_idle_time = sched_clock();
248 blkio_mark_blkg_idling(stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700249}
250EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
251
Tejun Heoc1768262012-03-05 13:15:17 -0800252void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
253 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700254{
Tejun Heoedf1b872012-03-08 10:54:00 -0800255 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700256
Tejun Heoedf1b872012-03-08 10:54:00 -0800257 lockdep_assert_held(blkg->q->queue_lock);
258
Divyesh Shah812df482010-04-08 21:15:35 -0700259 if (blkio_blkg_idling(stats)) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800260 unsigned long long now = sched_clock();
261
262 if (time_after64(now, stats->start_idle_time)) {
263 u64_stats_update_begin(&stats->syncp);
Divyesh Shah812df482010-04-08 21:15:35 -0700264 stats->idle_time += now - stats->start_idle_time;
Tejun Heoedf1b872012-03-08 10:54:00 -0800265 u64_stats_update_end(&stats->syncp);
266 }
Divyesh Shah812df482010-04-08 21:15:35 -0700267 blkio_clear_blkg_idling(stats);
268 }
Divyesh Shah812df482010-04-08 21:15:35 -0700269}
270EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
271
Tejun Heoc1768262012-03-05 13:15:17 -0800272void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
273 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700274{
Tejun Heoedf1b872012-03-08 10:54:00 -0800275 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700276
Tejun Heoedf1b872012-03-08 10:54:00 -0800277 lockdep_assert_held(blkg->q->queue_lock);
278
279 u64_stats_update_begin(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700280 stats->avg_queue_size_sum +=
281 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
282 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
283 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700284 blkio_update_group_wait_time(stats);
Tejun Heoedf1b872012-03-08 10:54:00 -0800285 u64_stats_update_end(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700286}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200287EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
288
Tejun Heoc1768262012-03-05 13:15:17 -0800289void blkiocg_set_start_empty_time(struct blkio_group *blkg,
290 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200291{
Tejun Heoedf1b872012-03-08 10:54:00 -0800292 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200293
Tejun Heoedf1b872012-03-08 10:54:00 -0800294 lockdep_assert_held(blkg->q->queue_lock);
Divyesh Shah28baf442010-04-14 11:22:38 +0200295
296 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
Tejun Heoedf1b872012-03-08 10:54:00 -0800297 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE])
Divyesh Shah28baf442010-04-14 11:22:38 +0200298 return;
Divyesh Shah28baf442010-04-14 11:22:38 +0200299
300 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200301 * group is already marked empty. This can happen if cfqq got new
302 * request in parent group and moved to this group while being added
303 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200304 */
Tejun Heoedf1b872012-03-08 10:54:00 -0800305 if (blkio_blkg_empty(stats))
Vivek Goyale5ff0822010-04-26 19:25:11 +0200306 return;
Vivek Goyale5ff0822010-04-26 19:25:11 +0200307
Divyesh Shah28baf442010-04-14 11:22:38 +0200308 stats->start_empty_time = sched_clock();
309 blkio_mark_blkg_empty(stats);
Divyesh Shah28baf442010-04-14 11:22:38 +0200310}
311EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
312
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200313void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800314 struct blkio_policy_type *pol,
315 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200316{
Tejun Heoc1768262012-03-05 13:15:17 -0800317 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800318
Tejun Heoedf1b872012-03-08 10:54:00 -0800319 lockdep_assert_held(blkg->q->queue_lock);
320
Tejun Heo549d3aa2012-03-05 13:15:16 -0800321 pd->stats.dequeue += dequeue;
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200322}
323EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700324#else
325static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800326 struct blkio_policy_type *pol,
327 struct blkio_group *curr_blkg) { }
328static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700329#endif
330
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200331void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800332 struct blkio_policy_type *pol,
333 struct blkio_group *curr_blkg, bool direction,
334 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700335{
Tejun Heoedf1b872012-03-08 10:54:00 -0800336 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700337
Tejun Heoedf1b872012-03-08 10:54:00 -0800338 lockdep_assert_held(blkg->q->queue_lock);
339
340 u64_stats_update_begin(&stats->syncp);
341 blkio_add_stat(stats->stat_arr[BLKIO_STAT_QUEUED], 1, direction, sync);
342 blkio_end_empty_time(stats);
343 u64_stats_update_end(&stats->syncp);
344
Tejun Heoc1768262012-03-05 13:15:17 -0800345 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700346}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200347EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700348
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200349void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800350 struct blkio_policy_type *pol,
351 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700352{
Tejun Heoedf1b872012-03-08 10:54:00 -0800353 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700354
Tejun Heoedf1b872012-03-08 10:54:00 -0800355 lockdep_assert_held(blkg->q->queue_lock);
356
357 u64_stats_update_begin(&stats->syncp);
358 blkio_check_and_dec_stat(stats->stat_arr[BLKIO_STAT_QUEUED], direction,
359 sync);
360 u64_stats_update_end(&stats->syncp);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700361}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200362EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700363
Tejun Heoc1768262012-03-05 13:15:17 -0800364void blkiocg_update_timeslice_used(struct blkio_group *blkg,
365 struct blkio_policy_type *pol,
366 unsigned long time,
367 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500368{
Tejun Heoedf1b872012-03-08 10:54:00 -0800369 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700370
Tejun Heoedf1b872012-03-08 10:54:00 -0800371 lockdep_assert_held(blkg->q->queue_lock);
372
373 u64_stats_update_begin(&stats->syncp);
374 stats->time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400375#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800376 stats->unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400377#endif
Tejun Heoedf1b872012-03-08 10:54:00 -0800378 u64_stats_update_end(&stats->syncp);
Vivek Goyal22084192009-12-03 12:59:49 -0500379}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700380EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500381
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400382/*
383 * should be called under rcu read lock or queue lock to make sure blkg pointer
384 * is valid.
385 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200386void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800387 struct blkio_policy_type *pol,
388 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700389{
Tejun Heoc1768262012-03-05 13:15:17 -0800390 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400391 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400392 unsigned long flags;
393
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800394 /* If per cpu stats are not allocated yet, don't do any accounting. */
395 if (pd->stats_cpu == NULL)
396 return;
397
Vivek Goyal575969a2011-05-19 15:38:29 -0400398 /*
399 * Disabling interrupts to provide mutual exclusion between two
400 * writes on same cpu. It probably is not needed for 64bit. Not
401 * optimizing that case yet.
402 */
403 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700404
Tejun Heo549d3aa2012-03-05 13:15:16 -0800405 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400406
Vivek Goyal575969a2011-05-19 15:38:29 -0400407 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400408 stats_cpu->sectors += bytes >> 9;
409 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
410 1, direction, sync);
411 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
412 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400413 u64_stats_update_end(&stats_cpu->syncp);
414 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700415}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200416EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700417
Divyesh Shah84c124d2010-04-09 08:31:19 +0200418void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800419 struct blkio_policy_type *pol,
420 uint64_t start_time,
421 uint64_t io_start_time, bool direction,
422 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700423{
Tejun Heoedf1b872012-03-08 10:54:00 -0800424 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah91952912010-04-01 15:01:41 -0700425 unsigned long long now = sched_clock();
426
Tejun Heoedf1b872012-03-08 10:54:00 -0800427 lockdep_assert_held(blkg->q->queue_lock);
428
429 u64_stats_update_begin(&stats->syncp);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200430 if (time_after64(now, io_start_time))
431 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
432 now - io_start_time, direction, sync);
433 if (time_after64(io_start_time, start_time))
434 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
435 io_start_time - start_time, direction, sync);
Tejun Heoedf1b872012-03-08 10:54:00 -0800436 u64_stats_update_end(&stats->syncp);
Divyesh Shah91952912010-04-01 15:01:41 -0700437}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200438EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700439
Vivek Goyal317389a2011-05-23 10:02:19 +0200440/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800441void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
442 struct blkio_policy_type *pol,
443 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700444{
Tejun Heoedf1b872012-03-08 10:54:00 -0800445 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Divyesh Shah812d4022010-04-08 21:14:23 -0700446
Tejun Heoedf1b872012-03-08 10:54:00 -0800447 lockdep_assert_held(blkg->q->queue_lock);
448
449 u64_stats_update_begin(&stats->syncp);
Tejun Heo5fe224d2012-03-08 10:53:57 -0800450 blkio_add_stat(stats->stat_arr[BLKIO_STAT_MERGED], 1, direction, sync);
Tejun Heoedf1b872012-03-08 10:54:00 -0800451 u64_stats_update_end(&stats->syncp);
Divyesh Shah812d4022010-04-08 21:14:23 -0700452}
453EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
454
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800455/*
456 * Worker for allocating per cpu stat for blk groups. This is scheduled on
457 * the system_nrt_wq once there are some groups on the alloc_list waiting
458 * for allocation.
459 */
460static void blkio_stat_alloc_fn(struct work_struct *work)
461{
462 static void *pcpu_stats[BLKIO_NR_POLICIES];
463 struct delayed_work *dwork = to_delayed_work(work);
464 struct blkio_group *blkg;
465 int i;
466 bool empty = false;
467
468alloc_stats:
469 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
470 if (pcpu_stats[i] != NULL)
471 continue;
472
473 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
474
475 /* Allocation failed. Try again after some time. */
476 if (pcpu_stats[i] == NULL) {
477 queue_delayed_work(system_nrt_wq, dwork,
478 msecs_to_jiffies(10));
479 return;
480 }
481 }
482
483 spin_lock_irq(&blkio_list_lock);
484 spin_lock(&alloc_list_lock);
485
486 /* cgroup got deleted or queue exited. */
487 if (!list_empty(&alloc_list)) {
488 blkg = list_first_entry(&alloc_list, struct blkio_group,
489 alloc_node);
490 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
491 struct blkg_policy_data *pd = blkg->pd[i];
492
493 if (blkio_policy[i] && pd && !pd->stats_cpu)
494 swap(pd->stats_cpu, pcpu_stats[i]);
495 }
496
497 list_del_init(&blkg->alloc_node);
498 }
499
500 empty = list_empty(&alloc_list);
501
502 spin_unlock(&alloc_list_lock);
503 spin_unlock_irq(&blkio_list_lock);
504
505 if (!empty)
506 goto alloc_stats;
507}
508
Tejun Heo03814112012-03-05 13:15:14 -0800509/**
510 * blkg_free - free a blkg
511 * @blkg: blkg to free
512 *
513 * Free @blkg which may be partially allocated.
514 */
515static void blkg_free(struct blkio_group *blkg)
516{
Tejun Heoe8989fa2012-03-05 13:15:20 -0800517 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800518
519 if (!blkg)
520 return;
521
Tejun Heoe8989fa2012-03-05 13:15:20 -0800522 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
523 struct blkg_policy_data *pd = blkg->pd[i];
524
525 if (pd) {
526 free_percpu(pd->stats_cpu);
527 kfree(pd);
528 }
Tejun Heo03814112012-03-05 13:15:14 -0800529 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800530
Tejun Heo549d3aa2012-03-05 13:15:16 -0800531 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800532}
533
534/**
535 * blkg_alloc - allocate a blkg
536 * @blkcg: block cgroup the new blkg is associated with
537 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800538 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800539 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800540 */
541static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800542 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800543{
544 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800545 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800546
547 /* alloc and init base part */
548 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
549 if (!blkg)
550 return NULL;
551
Tejun Heoc875f4d2012-03-05 13:15:22 -0800552 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800553 INIT_LIST_HEAD(&blkg->q_node);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800554 INIT_LIST_HEAD(&blkg->alloc_node);
Tejun Heo03814112012-03-05 13:15:14 -0800555 blkg->blkcg = blkcg;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800556 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800557 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
558
Tejun Heoe8989fa2012-03-05 13:15:20 -0800559 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
560 struct blkio_policy_type *pol = blkio_policy[i];
561 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800562
Tejun Heoe8989fa2012-03-05 13:15:20 -0800563 if (!pol)
564 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800565
Tejun Heoe8989fa2012-03-05 13:15:20 -0800566 /* alloc per-policy data and attach it to blkg */
567 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
568 q->node);
569 if (!pd) {
570 blkg_free(blkg);
571 return NULL;
572 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800573
Tejun Heoe8989fa2012-03-05 13:15:20 -0800574 blkg->pd[i] = pd;
575 pd->blkg = blkg;
Tejun Heo03814112012-03-05 13:15:14 -0800576 }
577
Tejun Heo549d3aa2012-03-05 13:15:16 -0800578 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800579 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
580 struct blkio_policy_type *pol = blkio_policy[i];
581
582 if (pol)
583 pol->ops.blkio_init_group_fn(blkg);
584 }
585
Tejun Heo03814112012-03-05 13:15:14 -0800586 return blkg;
587}
588
Tejun Heocd1604f2012-03-05 13:15:06 -0800589struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
590 struct request_queue *q,
591 enum blkio_policy_id plid,
592 bool for_root)
593 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400594{
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800595 struct blkio_group *blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400596
Tejun Heocd1604f2012-03-05 13:15:06 -0800597 WARN_ON_ONCE(!rcu_read_lock_held());
598 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500599
Tejun Heocd1604f2012-03-05 13:15:06 -0800600 /*
601 * This could be the first entry point of blkcg implementation and
602 * we shouldn't allow anything to go through for a bypassing queue.
603 * The following can be removed if blkg lookup is guaranteed to
604 * fail on a bypassing queue.
605 */
606 if (unlikely(blk_queue_bypass(q)) && !for_root)
607 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
608
Tejun Heoe8989fa2012-03-05 13:15:20 -0800609 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800610 if (blkg)
611 return blkg;
612
Tejun Heo7ee9c562012-03-05 13:15:11 -0800613 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800614 if (!css_tryget(&blkcg->css))
615 return ERR_PTR(-EINVAL);
616
617 /*
618 * Allocate and initialize.
Tejun Heocd1604f2012-03-05 13:15:06 -0800619 */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800620 blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800621
622 /* did alloc fail? */
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800623 if (unlikely(!blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800624 blkg = ERR_PTR(-ENOMEM);
625 goto out;
626 }
627
628 /* insert */
629 spin_lock(&blkcg->lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500630 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800631 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800632 spin_unlock(&blkcg->lock);
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800633
634 spin_lock(&alloc_list_lock);
635 list_add(&blkg->alloc_node, &alloc_list);
636 /* Queue per cpu stat allocation from worker thread. */
637 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
638 spin_unlock(&alloc_list_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800639out:
Tejun Heocd1604f2012-03-05 13:15:06 -0800640 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500641}
Tejun Heocd1604f2012-03-05 13:15:06 -0800642EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500643
Vivek Goyal31e4c282009-12-03 12:59:42 -0500644/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800645struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800646 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500647{
648 struct blkio_group *blkg;
649 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500650
Tejun Heoca32aef2012-03-05 13:15:03 -0800651 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800652 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500653 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500654 return NULL;
655}
Tejun Heocd1604f2012-03-05 13:15:06 -0800656EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500657
Tejun Heoe8989fa2012-03-05 13:15:20 -0800658static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800659{
Tejun Heo03aa264a2012-03-05 13:15:19 -0800660 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800661 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800662
663 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800664 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800665
666 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800667 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800668 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800669 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800670 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800671
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800672 spin_lock(&alloc_list_lock);
673 list_del_init(&blkg->alloc_node);
674 spin_unlock(&alloc_list_lock);
675
Tejun Heo03aa264a2012-03-05 13:15:19 -0800676 /*
677 * Put the reference taken at the time of creation so that when all
678 * queues are gone, group can be destroyed.
679 */
680 blkg_put(blkg);
681}
682
Tejun Heoe8989fa2012-03-05 13:15:20 -0800683/*
684 * XXX: This updates blkg policy data in-place for root blkg, which is
685 * necessary across elevator switch and policy registration as root blkgs
686 * aren't shot down. This broken and racy implementation is temporary.
687 * Eventually, blkg shoot down will be replaced by proper in-place update.
688 */
689void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
690{
691 struct blkio_policy_type *pol = blkio_policy[plid];
692 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
693 struct blkg_policy_data *pd;
694
695 if (!blkg)
696 return;
697
698 kfree(blkg->pd[plid]);
699 blkg->pd[plid] = NULL;
700
701 if (!pol)
702 return;
703
704 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
705 WARN_ON_ONCE(!pd);
706
707 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
708 WARN_ON_ONCE(!pd->stats_cpu);
709
710 blkg->pd[plid] = pd;
711 pd->blkg = blkg;
712 pol->ops.blkio_init_group_fn(blkg);
713}
714EXPORT_SYMBOL_GPL(update_root_blkg_pd);
715
Tejun Heo9f13ef62012-03-05 13:15:21 -0800716/**
717 * blkg_destroy_all - destroy all blkgs associated with a request_queue
718 * @q: request_queue of interest
719 * @destroy_root: whether to destroy root blkg or not
720 *
721 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
722 * destroyed; otherwise, root blkg is left alone.
723 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800724void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa264a2012-03-05 13:15:19 -0800725{
726 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800727
Tejun Heo9f13ef62012-03-05 13:15:21 -0800728 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800729
Tejun Heo9f13ef62012-03-05 13:15:21 -0800730 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
731 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800732
Tejun Heo9f13ef62012-03-05 13:15:21 -0800733 /* skip root? */
734 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
735 continue;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800736
Tejun Heo9f13ef62012-03-05 13:15:21 -0800737 spin_lock(&blkcg->lock);
738 blkg_destroy(blkg);
739 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800740 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800741
742 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800743}
Tejun Heo03aa264a2012-03-05 13:15:19 -0800744EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800745
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800746static void blkg_rcu_free(struct rcu_head *rcu_head)
747{
748 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
749}
750
751void __blkg_release(struct blkio_group *blkg)
752{
753 /* release the extra blkcg reference this blkg has been holding */
754 css_put(&blkg->blkcg->css);
755
756 /*
757 * A group is freed in rcu manner. But having an rcu lock does not
758 * mean that one can access all the fields of blkg and assume these
759 * are valid. For example, don't try to follow throtl_data and
760 * request queue links.
761 *
762 * Having a reference to blkg under an rcu allows acess to only
763 * values local to groups like group stats and group rate limits
764 */
765 call_rcu(&blkg->rcu_head, blkg_rcu_free);
766}
767EXPORT_SYMBOL_GPL(__blkg_release);
768
Tejun Heoc1768262012-03-05 13:15:17 -0800769static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400770{
Tejun Heoc1768262012-03-05 13:15:17 -0800771 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800772 int cpu;
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800773
774 if (pd->stats_cpu == NULL)
775 return;
Tejun Heo997a0262012-03-08 10:53:58 -0800776
777 for_each_possible_cpu(cpu) {
778 struct blkio_group_stats_cpu *sc =
779 per_cpu_ptr(pd->stats_cpu, cpu);
780
781 sc->sectors = 0;
782 memset(sc->stat_arr_cpu, 0, sizeof(sc->stat_arr_cpu));
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400783 }
784}
785
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700786static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200787blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700788{
Tejun Heo997a0262012-03-08 10:53:58 -0800789 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700790 struct blkio_group *blkg;
791 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700792 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700793
Tejun Heoe8989fa2012-03-05 13:15:20 -0800794 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700795 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800796
797 /*
798 * Note that stat reset is racy - it doesn't synchronize against
799 * stat updates. This is a debug feature which shouldn't exist
800 * anyway. If you get hit by a race, retry.
801 */
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700802 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800803 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800804
Tejun Heoe8989fa2012-03-05 13:15:20 -0800805 list_for_each_entry(pol, &blkio_list, list) {
806 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo997a0262012-03-08 10:53:58 -0800807 struct blkio_group_stats *stats = &pd->stats;
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400808
Tejun Heo997a0262012-03-08 10:53:58 -0800809 /* queued stats shouldn't be cleared */
810 for (i = 0; i < ARRAY_SIZE(stats->stat_arr); i++)
811 if (i != BLKIO_STAT_QUEUED)
812 memset(stats->stat_arr[i], 0,
813 sizeof(stats->stat_arr[i]));
814 stats->time = 0;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800815#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo997a0262012-03-08 10:53:58 -0800816 memset((void *)stats + BLKG_STATS_DEBUG_CLEAR_START, 0,
817 BLKG_STATS_DEBUG_CLEAR_SIZE);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800818#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -0800819 blkio_reset_stats_cpu(blkg, pol->plid);
820 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700821 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400822
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700823 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800824 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700825 return 0;
826}
827
Tejun Heo7a4dd282012-03-05 13:15:09 -0800828static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
829 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700830{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800831 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700832 chars_left -= strlen(str);
833 if (chars_left <= 0) {
834 printk(KERN_WARNING
835 "Possibly incorrect cgroup stat display format");
836 return;
837 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200838 if (diskname_only)
839 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700840 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200841 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700842 strlcat(str, " Read", chars_left);
843 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200844 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700845 strlcat(str, " Write", chars_left);
846 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200847 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700848 strlcat(str, " Sync", chars_left);
849 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200850 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700851 strlcat(str, " Async", chars_left);
852 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200853 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700854 strlcat(str, " Total", chars_left);
855 break;
856 default:
857 strlcat(str, " Invalid", chars_left);
858 }
859}
860
Tejun Heoc1768262012-03-05 13:15:17 -0800861static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400862 enum stat_type_cpu type, enum stat_sub_type sub_type)
863{
Tejun Heoc1768262012-03-05 13:15:17 -0800864 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400865 int cpu;
866 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400867 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400868
Vivek Goyal1cd9e032012-03-08 10:53:56 -0800869 if (pd->stats_cpu == NULL)
870 return val;
871
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400872 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400873 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800874 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400875
Vivek Goyal575969a2011-05-19 15:38:29 -0400876 do {
877 start = u64_stats_fetch_begin(&stats_cpu->syncp);
878 if (type == BLKIO_STAT_CPU_SECTORS)
879 tval = stats_cpu->sectors;
880 else
881 tval = stats_cpu->stat_arr_cpu[type][sub_type];
882 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
883
884 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400885 }
886
887 return val;
888}
889
Tejun Heoc1768262012-03-05 13:15:17 -0800890static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800891 struct cgroup_map_cb *cb, const char *dname,
892 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400893{
894 uint64_t disk_total, val;
895 char key_str[MAX_KEY_LEN];
896 enum stat_sub_type sub_type;
897
898 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800899 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heoc4c76a02012-03-08 10:53:59 -0800900 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
901 cb->fill(cb, key_str, val);
902 return val;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400903 }
904
905 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
906 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800907 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
908 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800909 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400910 cb->fill(cb, key_str, val);
911 }
912
Tejun Heoc1768262012-03-05 13:15:17 -0800913 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
914 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400915
Tejun Heo7a4dd282012-03-05 13:15:09 -0800916 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
917 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400918 cb->fill(cb, key_str, disk_total);
919 return disk_total;
920}
921
Tejun Heoc1768262012-03-05 13:15:17 -0800922static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800923 struct cgroup_map_cb *cb, const char *dname,
924 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700925{
Tejun Heoc4c76a02012-03-08 10:53:59 -0800926 struct blkio_group_stats *stats = &blkg->pd[plid]->stats;
927 uint64_t v = 0, disk_total = 0;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700928 char key_str[MAX_KEY_LEN];
Tejun Heoedf1b872012-03-08 10:54:00 -0800929 unsigned int sync_start;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800930 int st;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700931
Tejun Heoc4c76a02012-03-08 10:53:59 -0800932 if (type >= BLKIO_STAT_ARR_NR) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800933 do {
934 sync_start = u64_stats_fetch_begin(&stats->syncp);
935 switch (type) {
936 case BLKIO_STAT_TIME:
937 v = stats->time;
938 break;
Justin TerAvest9026e522011-03-22 21:26:54 +0100939#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoedf1b872012-03-08 10:54:00 -0800940 case BLKIO_STAT_UNACCOUNTED_TIME:
941 v = stats->unaccounted_time;
942 break;
943 case BLKIO_STAT_AVG_QUEUE_SIZE: {
944 uint64_t samples = stats->avg_queue_size_samples;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200945
Tejun Heoedf1b872012-03-08 10:54:00 -0800946 if (samples) {
947 v = stats->avg_queue_size_sum;
948 do_div(v, samples);
949 }
950 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800951 }
Tejun Heoedf1b872012-03-08 10:54:00 -0800952 case BLKIO_STAT_IDLE_TIME:
953 v = stats->idle_time;
954 break;
955 case BLKIO_STAT_EMPTY_TIME:
956 v = stats->empty_time;
957 break;
958 case BLKIO_STAT_DEQUEUE:
959 v = stats->dequeue;
960 break;
961 case BLKIO_STAT_GROUP_WAIT_TIME:
962 v = stats->group_wait_time;
963 break;
Tejun Heoc4c76a02012-03-08 10:53:59 -0800964#endif
Tejun Heoedf1b872012-03-08 10:54:00 -0800965 default:
966 WARN_ON_ONCE(1);
967 }
968 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
Tejun Heoc4c76a02012-03-08 10:53:59 -0800969
970 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
971 cb->fill(cb, key_str, v);
972 return v;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700973 }
Tejun Heoc4c76a02012-03-08 10:53:59 -0800974
975 for (st = BLKIO_STAT_READ; st < BLKIO_STAT_TOTAL; st++) {
Tejun Heoedf1b872012-03-08 10:54:00 -0800976 do {
977 sync_start = u64_stats_fetch_begin(&stats->syncp);
978 v = stats->stat_arr[type][st];
979 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
Tejun Heoc4c76a02012-03-08 10:53:59 -0800980
981 blkio_get_key_name(st, dname, key_str, MAX_KEY_LEN, false);
982 cb->fill(cb, key_str, v);
983 if (st == BLKIO_STAT_READ || st == BLKIO_STAT_WRITE)
984 disk_total += v;
985 }
986
Tejun Heo7a4dd282012-03-05 13:15:09 -0800987 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
988 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700989 cb->fill(cb, key_str, disk_total);
990 return disk_total;
991}
992
Tejun Heo4bfd4822012-03-05 13:15:08 -0800993static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
994 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800995{
Tejun Heoece84242011-10-19 14:31:15 +0200996 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800997 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800998 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800999 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001000 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +02001001 int i = 0, ret = -EINVAL;
1002 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001003 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001004 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001005
1006 memset(s, 0, sizeof(s));
1007
1008 while ((p = strsep(&buf, " ")) != NULL) {
1009 if (!*p)
1010 continue;
1011
1012 s[i++] = p;
1013
1014 /* Prevent from inputing too many things */
1015 if (i == 3)
1016 break;
1017 }
1018
1019 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001020 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001021
1022 p = strsep(&s[0], ":");
1023 if (p != NULL)
1024 major_s = p;
1025 else
Tejun Heoece84242011-10-19 14:31:15 +02001026 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001027
1028 minor_s = s[0];
1029 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001030 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001031
Tejun Heoece84242011-10-19 14:31:15 +02001032 if (strict_strtoul(major_s, 10, &major))
1033 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001034
Tejun Heoece84242011-10-19 14:31:15 +02001035 if (strict_strtoul(minor_s, 10, &minor))
1036 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001037
1038 dev = MKDEV(major, minor);
1039
Tejun Heoece84242011-10-19 14:31:15 +02001040 if (strict_strtoull(s[1], 10, &temp))
1041 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001042
Tejun Heoe56da7e2012-03-05 13:15:07 -08001043 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001044 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001045 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001046
1047 rcu_read_lock();
1048
Tejun Heo4bfd4822012-03-05 13:15:08 -08001049 spin_lock_irq(disk->queue->queue_lock);
1050 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1051 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001052
Tejun Heo4bfd4822012-03-05 13:15:08 -08001053 if (IS_ERR(blkg)) {
1054 ret = PTR_ERR(blkg);
1055 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001056 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001057
Tejun Heo549d3aa2012-03-05 13:15:16 -08001058 pd = blkg->pd[plid];
1059
Vivek Goyal062a6442010-09-15 17:06:33 -04001060 switch (plid) {
1061 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001062 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1063 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001064 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001065
Tejun Heo549d3aa2012-03-05 13:15:16 -08001066 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001067 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001068 break;
1069 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001070 switch(fileid) {
1071 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001072 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001073 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001074 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001075 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001076 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001077 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001078 break;
1079 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001080 if (temp > THROTL_IOPS_MAX)
1081 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001082 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001083 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001084 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001085 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001086 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001087 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001088 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001089 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001090 break;
1091 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001092 break;
1093 default:
1094 BUG();
1095 }
Tejun Heoece84242011-10-19 14:31:15 +02001096 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001097out_unlock:
1098 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001099out:
1100 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001101
1102 /*
1103 * If queue was bypassing, we should retry. Do so after a short
1104 * msleep(). It isn't strictly necessary but queue can be
1105 * bypassing for some time and it's always nice to avoid busy
1106 * looping.
1107 */
1108 if (ret == -EBUSY) {
1109 msleep(10);
1110 return restart_syscall();
1111 }
Tejun Heoece84242011-10-19 14:31:15 +02001112 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001113}
1114
Vivek Goyal062a6442010-09-15 17:06:33 -04001115static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1116 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001117{
1118 int ret = 0;
1119 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001120 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001121 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1122 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001123
1124 buf = kstrdup(buffer, GFP_KERNEL);
1125 if (!buf)
1126 return -ENOMEM;
1127
Tejun Heo4bfd4822012-03-05 13:15:08 -08001128 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001129 kfree(buf);
1130 return ret;
1131}
1132
Vivek Goyal92616b52012-03-05 13:15:10 -08001133static const char *blkg_dev_name(struct blkio_group *blkg)
1134{
1135 /* some drivers (floppy) instantiate a queue w/o disk registered */
1136 if (blkg->q->backing_dev_info.dev)
1137 return dev_name(blkg->q->backing_dev_info.dev);
1138 return NULL;
1139}
1140
Tejun Heo4bfd4822012-03-05 13:15:08 -08001141static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1142 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001143{
Tejun Heoc1768262012-03-05 13:15:17 -08001144 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001145 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001146 struct blkg_policy_data *pd = blkg->pd[plid];
1147 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001148 int rw = WRITE;
1149
Vivek Goyal92616b52012-03-05 13:15:10 -08001150 if (!dname)
1151 return;
1152
Tejun Heoc1768262012-03-05 13:15:17 -08001153 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001154 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001155 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001156 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001157 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001158 break;
1159 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001160 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001161 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001162 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001163 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001164 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001165 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001166 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001167 break;
1168 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001169 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001170 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001171 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001172 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001173 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001174 break;
1175 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001176 break;
1177 default:
1178 BUG();
1179 }
1180}
1181
1182/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001183static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1184 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001185{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001186 struct blkio_group *blkg;
1187 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001188
Tejun Heo4bfd4822012-03-05 13:15:08 -08001189 spin_lock_irq(&blkcg->lock);
1190 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001191 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001192 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001193}
1194
1195static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1196 struct seq_file *m)
1197{
1198 struct blkio_cgroup *blkcg;
1199 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1200 int name = BLKIOFILE_ATTR(cft->private);
1201
1202 blkcg = cgroup_to_blkio_cgroup(cgrp);
1203
1204 switch(plid) {
1205 case BLKIO_POLICY_PROP:
1206 switch(name) {
1207 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001208 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001209 return 0;
1210 default:
1211 BUG();
1212 }
1213 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001214 case BLKIO_POLICY_THROTL:
1215 switch(name){
1216 case BLKIO_THROTL_read_bps_device:
1217 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001218 case BLKIO_THROTL_read_iops_device:
1219 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001220 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001221 return 0;
1222 default:
1223 BUG();
1224 }
1225 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001226 default:
1227 BUG();
1228 }
1229
1230 return 0;
1231}
1232
1233static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001234 struct cftype *cft, struct cgroup_map_cb *cb,
1235 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001236{
1237 struct blkio_group *blkg;
1238 struct hlist_node *n;
1239 uint64_t cgroup_total = 0;
1240
Tejun Heoc875f4d2012-03-05 13:15:22 -08001241 spin_lock_irq(&blkcg->lock);
1242
1243 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001244 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001245 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001246
Tejun Heoe8989fa2012-03-05 13:15:20 -08001247 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001248 continue;
Tejun Heoedf1b872012-03-08 10:54:00 -08001249 if (pcpu)
Tejun Heoc1768262012-03-05 13:15:17 -08001250 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1251 cb, dname, type);
Tejun Heoedf1b872012-03-08 10:54:00 -08001252 else
Tejun Heoc1768262012-03-05 13:15:17 -08001253 cgroup_total += blkio_get_stat(blkg, plid,
1254 cb, dname, type);
Vivek Goyal062a6442010-09-15 17:06:33 -04001255 }
1256 if (show_total)
1257 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001258
1259 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001260 return 0;
1261}
1262
1263/* All map kind of cgroup file get serviced by this function */
1264static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1265 struct cgroup_map_cb *cb)
1266{
1267 struct blkio_cgroup *blkcg;
1268 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1269 int name = BLKIOFILE_ATTR(cft->private);
1270
1271 blkcg = cgroup_to_blkio_cgroup(cgrp);
1272
1273 switch(plid) {
1274 case BLKIO_POLICY_PROP:
1275 switch(name) {
1276 case BLKIO_PROP_time:
1277 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001278 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001279 case BLKIO_PROP_sectors:
1280 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001281 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001282 case BLKIO_PROP_io_service_bytes:
1283 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001284 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001285 case BLKIO_PROP_io_serviced:
1286 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001287 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001288 case BLKIO_PROP_io_service_time:
1289 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001290 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001291 case BLKIO_PROP_io_wait_time:
1292 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001293 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001294 case BLKIO_PROP_io_merged:
1295 return blkio_read_blkg_stats(blkcg, cft, cb,
Tejun Heo5fe224d2012-03-08 10:53:57 -08001296 BLKIO_STAT_MERGED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001297 case BLKIO_PROP_io_queued:
1298 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001299 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001300#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001301 case BLKIO_PROP_unaccounted_time:
1302 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001303 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001304 case BLKIO_PROP_dequeue:
1305 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001306 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001307 case BLKIO_PROP_avg_queue_size:
1308 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001309 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001310 case BLKIO_PROP_group_wait_time:
1311 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001312 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001313 case BLKIO_PROP_idle_time:
1314 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001315 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001316 case BLKIO_PROP_empty_time:
1317 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001318 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001319#endif
1320 default:
1321 BUG();
1322 }
1323 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001324 case BLKIO_POLICY_THROTL:
1325 switch(name){
1326 case BLKIO_THROTL_io_service_bytes:
1327 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001328 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001329 case BLKIO_THROTL_io_serviced:
1330 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001331 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001332 default:
1333 BUG();
1334 }
1335 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001336 default:
1337 BUG();
1338 }
1339
1340 return 0;
1341}
1342
Tejun Heo4bfd4822012-03-05 13:15:08 -08001343static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001344{
1345 struct blkio_group *blkg;
1346 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001347
1348 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1349 return -EINVAL;
1350
1351 spin_lock(&blkio_list_lock);
1352 spin_lock_irq(&blkcg->lock);
1353 blkcg->weight = (unsigned int)val;
1354
Tejun Heo549d3aa2012-03-05 13:15:16 -08001355 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001356 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001357
Tejun Heoe8989fa2012-03-05 13:15:20 -08001358 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001359 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001360 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001361
Vivek Goyal062a6442010-09-15 17:06:33 -04001362 spin_unlock_irq(&blkcg->lock);
1363 spin_unlock(&blkio_list_lock);
1364 return 0;
1365}
1366
1367static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1368 struct blkio_cgroup *blkcg;
1369 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1370 int name = BLKIOFILE_ATTR(cft->private);
1371
1372 blkcg = cgroup_to_blkio_cgroup(cgrp);
1373
1374 switch(plid) {
1375 case BLKIO_POLICY_PROP:
1376 switch(name) {
1377 case BLKIO_PROP_weight:
1378 return (u64)blkcg->weight;
1379 }
1380 break;
1381 default:
1382 BUG();
1383 }
1384 return 0;
1385}
1386
1387static int
1388blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1389{
1390 struct blkio_cgroup *blkcg;
1391 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1392 int name = BLKIOFILE_ATTR(cft->private);
1393
1394 blkcg = cgroup_to_blkio_cgroup(cgrp);
1395
1396 switch(plid) {
1397 case BLKIO_POLICY_PROP:
1398 switch(name) {
1399 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001400 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001401 }
1402 break;
1403 default:
1404 BUG();
1405 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001406
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001407 return 0;
1408}
1409
Vivek Goyal31e4c282009-12-03 12:59:42 -05001410struct cftype blkio_files[] = {
1411 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001412 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001413 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1414 BLKIO_PROP_weight_device),
1415 .read_seq_string = blkiocg_file_read,
1416 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001417 .max_write_len = 256,
1418 },
1419 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001420 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001421 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1422 BLKIO_PROP_weight),
1423 .read_u64 = blkiocg_file_read_u64,
1424 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001425 },
Vivek Goyal22084192009-12-03 12:59:49 -05001426 {
1427 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001428 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1429 BLKIO_PROP_time),
1430 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001431 },
1432 {
1433 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001434 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1435 BLKIO_PROP_sectors),
1436 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001437 },
1438 {
1439 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001440 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1441 BLKIO_PROP_io_service_bytes),
1442 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001443 },
1444 {
1445 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001446 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1447 BLKIO_PROP_io_serviced),
1448 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001449 },
1450 {
1451 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001452 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1453 BLKIO_PROP_io_service_time),
1454 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001455 },
1456 {
1457 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001458 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1459 BLKIO_PROP_io_wait_time),
1460 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001461 },
1462 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001463 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001464 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1465 BLKIO_PROP_io_merged),
1466 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001467 },
1468 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001469 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001470 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1471 BLKIO_PROP_io_queued),
1472 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001473 },
1474 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001475 .name = "reset_stats",
1476 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001477 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001478#ifdef CONFIG_BLK_DEV_THROTTLING
1479 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001480 .name = "throttle.read_bps_device",
1481 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1482 BLKIO_THROTL_read_bps_device),
1483 .read_seq_string = blkiocg_file_read,
1484 .write_string = blkiocg_file_write,
1485 .max_write_len = 256,
1486 },
1487
1488 {
1489 .name = "throttle.write_bps_device",
1490 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1491 BLKIO_THROTL_write_bps_device),
1492 .read_seq_string = blkiocg_file_read,
1493 .write_string = blkiocg_file_write,
1494 .max_write_len = 256,
1495 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001496
1497 {
1498 .name = "throttle.read_iops_device",
1499 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1500 BLKIO_THROTL_read_iops_device),
1501 .read_seq_string = blkiocg_file_read,
1502 .write_string = blkiocg_file_write,
1503 .max_write_len = 256,
1504 },
1505
1506 {
1507 .name = "throttle.write_iops_device",
1508 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1509 BLKIO_THROTL_write_iops_device),
1510 .read_seq_string = blkiocg_file_read,
1511 .write_string = blkiocg_file_write,
1512 .max_write_len = 256,
1513 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001514 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001515 .name = "throttle.io_service_bytes",
1516 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1517 BLKIO_THROTL_io_service_bytes),
1518 .read_map = blkiocg_file_read_map,
1519 },
1520 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001521 .name = "throttle.io_serviced",
1522 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1523 BLKIO_THROTL_io_serviced),
1524 .read_map = blkiocg_file_read_map,
1525 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001526#endif /* CONFIG_BLK_DEV_THROTTLING */
1527
Vivek Goyal22084192009-12-03 12:59:49 -05001528#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001529 {
1530 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001531 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1532 BLKIO_PROP_avg_queue_size),
1533 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001534 },
1535 {
Divyesh Shah812df482010-04-08 21:15:35 -07001536 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001537 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1538 BLKIO_PROP_group_wait_time),
1539 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001540 },
1541 {
1542 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001543 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1544 BLKIO_PROP_idle_time),
1545 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001546 },
1547 {
1548 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001549 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1550 BLKIO_PROP_empty_time),
1551 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001552 },
1553 {
Vivek Goyal22084192009-12-03 12:59:49 -05001554 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001555 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1556 BLKIO_PROP_dequeue),
1557 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001558 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001559 {
1560 .name = "unaccounted_time",
1561 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1562 BLKIO_PROP_unaccounted_time),
1563 .read_map = blkiocg_file_read_map,
1564 },
Vivek Goyal22084192009-12-03 12:59:49 -05001565#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001566};
1567
1568static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1569{
1570 return cgroup_add_files(cgroup, subsys, blkio_files,
1571 ARRAY_SIZE(blkio_files));
1572}
1573
Tejun Heo9f13ef62012-03-05 13:15:21 -08001574/**
1575 * blkiocg_pre_destroy - cgroup pre_destroy callback
1576 * @subsys: cgroup subsys
1577 * @cgroup: cgroup of interest
1578 *
1579 * This function is called when @cgroup is about to go away and responsible
1580 * for shooting down all blkgs associated with @cgroup. blkgs should be
1581 * removed while holding both q and blkcg locks. As blkcg lock is nested
1582 * inside q lock, this function performs reverse double lock dancing.
1583 *
1584 * This is the blkcg counterpart of ioc_release_fn().
1585 */
Tejun Heo7ee9c562012-03-05 13:15:11 -08001586static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1587 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001588{
1589 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1590
Tejun Heo9f13ef62012-03-05 13:15:21 -08001591 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001592
Tejun Heo9f13ef62012-03-05 13:15:21 -08001593 while (!hlist_empty(&blkcg->blkg_list)) {
1594 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1595 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001596 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001597
Tejun Heo9f13ef62012-03-05 13:15:21 -08001598 if (spin_trylock(q->queue_lock)) {
1599 blkg_destroy(blkg);
1600 spin_unlock(q->queue_lock);
1601 } else {
1602 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001603 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +02001604 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001605 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001606 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001607
Tejun Heo9f13ef62012-03-05 13:15:21 -08001608 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001609 return 0;
1610}
1611
1612static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1613{
1614 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1615
Ben Blum67523c42010-03-10 15:22:11 -08001616 if (blkcg != &blkio_root_cgroup)
1617 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001618}
1619
1620static struct cgroup_subsys_state *
1621blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1622{
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001623 static atomic64_t id_seq = ATOMIC64_INIT(0);
Li Zefan03415092010-05-07 08:57:00 +02001624 struct blkio_cgroup *blkcg;
1625 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001626
Li Zefan03415092010-05-07 08:57:00 +02001627 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001628 blkcg = &blkio_root_cgroup;
1629 goto done;
1630 }
1631
Vivek Goyal31e4c282009-12-03 12:59:42 -05001632 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1633 if (!blkcg)
1634 return ERR_PTR(-ENOMEM);
1635
1636 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
Tejun Heo9a9e8a22012-03-19 15:10:56 -07001637 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05001638done:
1639 spin_lock_init(&blkcg->lock);
1640 INIT_HLIST_HEAD(&blkcg->blkg_list);
1641
1642 return &blkcg->css;
1643}
1644
Tejun Heo5efd6112012-03-05 13:15:12 -08001645/**
1646 * blkcg_init_queue - initialize blkcg part of request queue
1647 * @q: request_queue to initialize
1648 *
1649 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1650 * part of new request_queue @q.
1651 *
1652 * RETURNS:
1653 * 0 on success, -errno on failure.
1654 */
1655int blkcg_init_queue(struct request_queue *q)
1656{
Tejun Heo923adde2012-03-05 13:15:13 -08001657 int ret;
1658
Tejun Heo5efd6112012-03-05 13:15:12 -08001659 might_sleep();
1660
Tejun Heo923adde2012-03-05 13:15:13 -08001661 ret = blk_throtl_init(q);
1662 if (ret)
1663 return ret;
1664
1665 mutex_lock(&all_q_mutex);
1666 INIT_LIST_HEAD(&q->all_q_node);
1667 list_add_tail(&q->all_q_node, &all_q_list);
1668 mutex_unlock(&all_q_mutex);
1669
1670 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001671}
1672
1673/**
1674 * blkcg_drain_queue - drain blkcg part of request_queue
1675 * @q: request_queue to drain
1676 *
1677 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1678 */
1679void blkcg_drain_queue(struct request_queue *q)
1680{
1681 lockdep_assert_held(q->queue_lock);
1682
1683 blk_throtl_drain(q);
1684}
1685
1686/**
1687 * blkcg_exit_queue - exit and release blkcg part of request_queue
1688 * @q: request_queue being released
1689 *
1690 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1691 */
1692void blkcg_exit_queue(struct request_queue *q)
1693{
Tejun Heo923adde2012-03-05 13:15:13 -08001694 mutex_lock(&all_q_mutex);
1695 list_del_init(&q->all_q_node);
1696 mutex_unlock(&all_q_mutex);
1697
Tejun Heoe8989fa2012-03-05 13:15:20 -08001698 blkg_destroy_all(q, true);
1699
Tejun Heo5efd6112012-03-05 13:15:12 -08001700 blk_throtl_exit(q);
1701}
1702
Vivek Goyal31e4c282009-12-03 12:59:42 -05001703/*
1704 * We cannot support shared io contexts, as we have no mean to support
1705 * two tasks with the same ioc in two different groups without major rework
1706 * of the main cic data structures. For now we allow a task to change
1707 * its cgroup only if it's the only owner of its ioc.
1708 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001709static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1710 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001711{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001712 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001713 struct io_context *ioc;
1714 int ret = 0;
1715
1716 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001717 cgroup_taskset_for_each(task, cgrp, tset) {
1718 task_lock(task);
1719 ioc = task->io_context;
1720 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1721 ret = -EINVAL;
1722 task_unlock(task);
1723 if (ret)
1724 break;
1725 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001726 return ret;
1727}
1728
Tejun Heo923adde2012-03-05 13:15:13 -08001729static void blkcg_bypass_start(void)
1730 __acquires(&all_q_mutex)
1731{
1732 struct request_queue *q;
1733
1734 mutex_lock(&all_q_mutex);
1735
1736 list_for_each_entry(q, &all_q_list, all_q_node) {
1737 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001738 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001739 }
1740}
1741
1742static void blkcg_bypass_end(void)
1743 __releases(&all_q_mutex)
1744{
1745 struct request_queue *q;
1746
1747 list_for_each_entry(q, &all_q_list, all_q_node)
1748 blk_queue_bypass_end(q);
1749
1750 mutex_unlock(&all_q_mutex);
1751}
1752
Vivek Goyal3e252062009-12-04 10:36:42 -05001753void blkio_policy_register(struct blkio_policy_type *blkiop)
1754{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001755 struct request_queue *q;
1756
Tejun Heo923adde2012-03-05 13:15:13 -08001757 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001758 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001759
1760 BUG_ON(blkio_policy[blkiop->plid]);
1761 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001762 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001763
Vivek Goyal3e252062009-12-04 10:36:42 -05001764 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001765 list_for_each_entry(q, &all_q_list, all_q_node)
1766 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001767 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001768}
1769EXPORT_SYMBOL_GPL(blkio_policy_register);
1770
1771void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1772{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001773 struct request_queue *q;
1774
Tejun Heo923adde2012-03-05 13:15:13 -08001775 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001776 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001777
1778 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1779 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001780 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001781
Vivek Goyal3e252062009-12-04 10:36:42 -05001782 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001783 list_for_each_entry(q, &all_q_list, all_q_node)
1784 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001785 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001786}
1787EXPORT_SYMBOL_GPL(blkio_policy_unregister);