blob: 2ca9a15db0f7d2063dc7fb6cd24aae9363fa9383 [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001/*
2 * Common Block IO controller cgroup interface
3 *
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
12 */
13#include <linux/ioprio.h>
Vivek Goyal22084192009-12-03 12:59:49 -050014#include <linux/seq_file.h>
15#include <linux/kdev_t.h>
Vivek Goyal9d6a9862009-12-04 10:36:41 -050016#include <linux/module.h>
Stephen Rothwellaccee782009-12-07 19:29:39 +110017#include <linux/err.h>
Divyesh Shah91952912010-04-01 15:01:41 -070018#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Gui Jianfeng34d0f172010-04-13 16:05:49 +080020#include <linux/genhd.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080021#include <linux/delay.h>
22#include "blk-cgroup.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080023#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050024
Divyesh Shah84c124d2010-04-09 08:31:19 +020025#define MAX_KEY_LEN 100
26
Vivek Goyal3e252062009-12-04 10:36:42 -050027static DEFINE_SPINLOCK(blkio_list_lock);
28static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050029
Tejun Heo923adde2012-03-05 13:15:13 -080030static DEFINE_MUTEX(all_q_mutex);
31static LIST_HEAD(all_q_list);
32
Vivek Goyal31e4c282009-12-03 12:59:42 -050033struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050034EXPORT_SYMBOL_GPL(blkio_root_cgroup);
35
Tejun Heo035d10b2012-03-05 13:15:04 -080036static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
37
Ben Blum67523c42010-03-10 15:22:11 -080038static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
39 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080040static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
41 struct cgroup_taskset *);
42static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
43 struct cgroup_taskset *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080044static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080045static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
46static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
47
Vivek Goyal062a6442010-09-15 17:06:33 -040048/* for encoding cft->private value on file */
49#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
50/* What policy owns the file, proportional or throttle */
51#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
52#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
53
Ben Blum67523c42010-03-10 15:22:11 -080054struct cgroup_subsys blkio_subsys = {
55 .name = "blkio",
56 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080057 .can_attach = blkiocg_can_attach,
58 .attach = blkiocg_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080059 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080060 .destroy = blkiocg_destroy,
61 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080062 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080063 .module = THIS_MODULE,
64};
65EXPORT_SYMBOL_GPL(blkio_subsys);
66
Vivek Goyal31e4c282009-12-03 12:59:42 -050067struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
68{
69 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
70 struct blkio_cgroup, css);
71}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050072EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050073
Vivek Goyal70087dc2011-05-16 15:24:08 +020074struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
75{
76 return container_of(task_subsys_state(tsk, blkio_subsys_id),
77 struct blkio_cgroup, css);
78}
79EXPORT_SYMBOL_GPL(task_blkio_cgroup);
80
Tejun Heoc1768262012-03-05 13:15:17 -080081static inline void blkio_update_group_weight(struct blkio_group *blkg,
82 int plid, unsigned int weight)
Vivek Goyal062a6442010-09-15 17:06:33 -040083{
84 struct blkio_policy_type *blkiop;
85
86 list_for_each_entry(blkiop, &blkio_list, list) {
87 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -080088 if (blkiop->plid != plid)
Vivek Goyal062a6442010-09-15 17:06:33 -040089 continue;
90 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080091 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020092 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040093 }
94}
95
Tejun Heoc1768262012-03-05 13:15:17 -080096static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
97 u64 bps, int fileid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040098{
99 struct blkio_policy_type *blkiop;
100
101 list_for_each_entry(blkiop, &blkio_list, list) {
102
103 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800104 if (blkiop->plid != plid)
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400105 continue;
106
107 if (fileid == BLKIO_THROTL_read_bps_device
108 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800109 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200110 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400111
112 if (fileid == BLKIO_THROTL_write_bps_device
113 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800114 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200115 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400116 }
117}
118
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400119static inline void blkio_update_group_iops(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800120 int plid, unsigned int iops,
121 int fileid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400122{
123 struct blkio_policy_type *blkiop;
124
125 list_for_each_entry(blkiop, &blkio_list, list) {
126
127 /* If this policy does not own the blkg, do not send updates */
Tejun Heoc1768262012-03-05 13:15:17 -0800128 if (blkiop->plid != plid)
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400129 continue;
130
131 if (fileid == BLKIO_THROTL_read_iops_device
132 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800133 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200134 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400135
136 if (fileid == BLKIO_THROTL_write_iops_device
137 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800138 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200139 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400140 }
141}
142
Divyesh Shah91952912010-04-01 15:01:41 -0700143/*
144 * Add to the appropriate stat variable depending on the request type.
145 * This should be called with the blkg->stats_lock held.
146 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200147static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
148 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700149{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200150 if (direction)
151 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700152 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200153 stat[BLKIO_STAT_READ] += add;
154 if (sync)
155 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700156 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200157 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700158}
159
Divyesh Shahcdc11842010-04-08 21:15:10 -0700160/*
161 * Decrements the appropriate stat variable if non-zero depending on the
162 * request type. Panics on value being zero.
163 * This should be called with the blkg->stats_lock held.
164 */
165static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
166{
167 if (direction) {
168 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
169 stat[BLKIO_STAT_WRITE]--;
170 } else {
171 BUG_ON(stat[BLKIO_STAT_READ] == 0);
172 stat[BLKIO_STAT_READ]--;
173 }
174 if (sync) {
175 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
176 stat[BLKIO_STAT_SYNC]--;
177 } else {
178 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
179 stat[BLKIO_STAT_ASYNC]--;
180 }
181}
182
183#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700184/* This should be called with the blkg->stats_lock held. */
185static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800186 struct blkio_policy_type *pol,
187 struct blkio_group *curr_blkg)
Divyesh Shah812df482010-04-08 21:15:35 -0700188{
Tejun Heoc1768262012-03-05 13:15:17 -0800189 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800190
191 if (blkio_blkg_waiting(&pd->stats))
Divyesh Shah812df482010-04-08 21:15:35 -0700192 return;
193 if (blkg == curr_blkg)
194 return;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800195 pd->stats.start_group_wait_time = sched_clock();
196 blkio_mark_blkg_waiting(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700197}
198
199/* This should be called with the blkg->stats_lock held. */
200static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
201{
202 unsigned long long now;
203
204 if (!blkio_blkg_waiting(stats))
205 return;
206
207 now = sched_clock();
208 if (time_after64(now, stats->start_group_wait_time))
209 stats->group_wait_time += now - stats->start_group_wait_time;
210 blkio_clear_blkg_waiting(stats);
211}
212
213/* This should be called with the blkg->stats_lock held. */
214static void blkio_end_empty_time(struct blkio_group_stats *stats)
215{
216 unsigned long long now;
217
218 if (!blkio_blkg_empty(stats))
219 return;
220
221 now = sched_clock();
222 if (time_after64(now, stats->start_empty_time))
223 stats->empty_time += now - stats->start_empty_time;
224 blkio_clear_blkg_empty(stats);
225}
226
Tejun Heoc1768262012-03-05 13:15:17 -0800227void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
228 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700229{
Tejun Heoc1768262012-03-05 13:15:17 -0800230 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah812df482010-04-08 21:15:35 -0700231 unsigned long flags;
232
233 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800234 BUG_ON(blkio_blkg_idling(&pd->stats));
235 pd->stats.start_idle_time = sched_clock();
236 blkio_mark_blkg_idling(&pd->stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700237 spin_unlock_irqrestore(&blkg->stats_lock, flags);
238}
239EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
240
Tejun Heoc1768262012-03-05 13:15:17 -0800241void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
242 struct blkio_policy_type *pol)
Divyesh Shah812df482010-04-08 21:15:35 -0700243{
Tejun Heoc1768262012-03-05 13:15:17 -0800244 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah812df482010-04-08 21:15:35 -0700245 unsigned long flags;
246 unsigned long long now;
247 struct blkio_group_stats *stats;
248
249 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800250 stats = &pd->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700251 if (blkio_blkg_idling(stats)) {
252 now = sched_clock();
253 if (time_after64(now, stats->start_idle_time))
254 stats->idle_time += now - stats->start_idle_time;
255 blkio_clear_blkg_idling(stats);
256 }
257 spin_unlock_irqrestore(&blkg->stats_lock, flags);
258}
259EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
260
Tejun Heoc1768262012-03-05 13:15:17 -0800261void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
262 struct blkio_policy_type *pol)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700263{
Tejun Heoc1768262012-03-05 13:15:17 -0800264 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700265 unsigned long flags;
266 struct blkio_group_stats *stats;
267
268 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800269 stats = &pd->stats;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700270 stats->avg_queue_size_sum +=
271 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
272 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
273 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700274 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700275 spin_unlock_irqrestore(&blkg->stats_lock, flags);
276}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200277EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
278
Tejun Heoc1768262012-03-05 13:15:17 -0800279void blkiocg_set_start_empty_time(struct blkio_group *blkg,
280 struct blkio_policy_type *pol)
Divyesh Shah28baf442010-04-14 11:22:38 +0200281{
Tejun Heoc1768262012-03-05 13:15:17 -0800282 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah28baf442010-04-14 11:22:38 +0200283 unsigned long flags;
284 struct blkio_group_stats *stats;
285
286 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800287 stats = &pd->stats;
Divyesh Shah28baf442010-04-14 11:22:38 +0200288
289 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
290 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
291 spin_unlock_irqrestore(&blkg->stats_lock, flags);
292 return;
293 }
294
295 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200296 * group is already marked empty. This can happen if cfqq got new
297 * request in parent group and moved to this group while being added
298 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200299 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200300 if(blkio_blkg_empty(stats)) {
301 spin_unlock_irqrestore(&blkg->stats_lock, flags);
302 return;
303 }
304
Divyesh Shah28baf442010-04-14 11:22:38 +0200305 stats->start_empty_time = sched_clock();
306 blkio_mark_blkg_empty(stats);
307 spin_unlock_irqrestore(&blkg->stats_lock, flags);
308}
309EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
310
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200311void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800312 struct blkio_policy_type *pol,
313 unsigned long dequeue)
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200314{
Tejun Heoc1768262012-03-05 13:15:17 -0800315 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800316
317 pd->stats.dequeue += dequeue;
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200318}
319EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700320#else
321static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800322 struct blkio_policy_type *pol,
323 struct blkio_group *curr_blkg) { }
324static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700325#endif
326
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200327void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800328 struct blkio_policy_type *pol,
329 struct blkio_group *curr_blkg, bool direction,
330 bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700331{
Tejun Heoc1768262012-03-05 13:15:17 -0800332 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700333 unsigned long flags;
334
335 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800336 blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700337 sync);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800338 blkio_end_empty_time(&pd->stats);
Tejun Heoc1768262012-03-05 13:15:17 -0800339 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700340 spin_unlock_irqrestore(&blkg->stats_lock, flags);
341}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200342EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700343
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200344void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800345 struct blkio_policy_type *pol,
346 bool direction, bool sync)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700347{
Tejun Heoc1768262012-03-05 13:15:17 -0800348 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shahcdc11842010-04-08 21:15:10 -0700349 unsigned long flags;
350
351 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800352 blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED],
Divyesh Shahcdc11842010-04-08 21:15:10 -0700353 direction, sync);
354 spin_unlock_irqrestore(&blkg->stats_lock, flags);
355}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200356EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700357
Tejun Heoc1768262012-03-05 13:15:17 -0800358void blkiocg_update_timeslice_used(struct blkio_group *blkg,
359 struct blkio_policy_type *pol,
360 unsigned long time,
361 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500362{
Tejun Heoc1768262012-03-05 13:15:17 -0800363 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700364 unsigned long flags;
365
366 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800367 pd->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400368#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo549d3aa2012-03-05 13:15:16 -0800369 pd->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400370#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700371 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500372}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700373EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500374
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400375/*
376 * should be called under rcu read lock or queue lock to make sure blkg pointer
377 * is valid.
378 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200379void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800380 struct blkio_policy_type *pol,
381 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700382{
Tejun Heoc1768262012-03-05 13:15:17 -0800383 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400384 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400385 unsigned long flags;
386
387 /*
388 * Disabling interrupts to provide mutual exclusion between two
389 * writes on same cpu. It probably is not needed for 64bit. Not
390 * optimizing that case yet.
391 */
392 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700393
Tejun Heo549d3aa2012-03-05 13:15:16 -0800394 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400395
Vivek Goyal575969a2011-05-19 15:38:29 -0400396 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400397 stats_cpu->sectors += bytes >> 9;
398 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
399 1, direction, sync);
400 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
401 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400402 u64_stats_update_end(&stats_cpu->syncp);
403 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700404}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200405EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700406
Divyesh Shah84c124d2010-04-09 08:31:19 +0200407void blkiocg_update_completion_stats(struct blkio_group *blkg,
Tejun Heoc1768262012-03-05 13:15:17 -0800408 struct blkio_policy_type *pol,
409 uint64_t start_time,
410 uint64_t io_start_time, bool direction,
411 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700412{
Tejun Heoc1768262012-03-05 13:15:17 -0800413 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Divyesh Shah91952912010-04-01 15:01:41 -0700414 struct blkio_group_stats *stats;
415 unsigned long flags;
416 unsigned long long now = sched_clock();
417
418 spin_lock_irqsave(&blkg->stats_lock, flags);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800419 stats = &pd->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200420 if (time_after64(now, io_start_time))
421 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
422 now - io_start_time, direction, sync);
423 if (time_after64(io_start_time, start_time))
424 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
425 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700426 spin_unlock_irqrestore(&blkg->stats_lock, flags);
427}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200428EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700429
Vivek Goyal317389a2011-05-23 10:02:19 +0200430/* Merged stats are per cpu. */
Tejun Heoc1768262012-03-05 13:15:17 -0800431void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
432 struct blkio_policy_type *pol,
433 bool direction, bool sync)
Divyesh Shah812d4022010-04-08 21:14:23 -0700434{
Tejun Heoc1768262012-03-05 13:15:17 -0800435 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyal317389a2011-05-23 10:02:19 +0200436 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700437 unsigned long flags;
438
Vivek Goyal317389a2011-05-23 10:02:19 +0200439 /*
440 * Disabling interrupts to provide mutual exclusion between two
441 * writes on same cpu. It probably is not needed for 64bit. Not
442 * optimizing that case yet.
443 */
444 local_irq_save(flags);
445
Tejun Heo549d3aa2012-03-05 13:15:16 -0800446 stats_cpu = this_cpu_ptr(pd->stats_cpu);
Vivek Goyal317389a2011-05-23 10:02:19 +0200447
448 u64_stats_update_begin(&stats_cpu->syncp);
449 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
450 direction, sync);
451 u64_stats_update_end(&stats_cpu->syncp);
452 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700453}
454EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
455
Tejun Heo03814112012-03-05 13:15:14 -0800456/**
457 * blkg_free - free a blkg
458 * @blkg: blkg to free
459 *
460 * Free @blkg which may be partially allocated.
461 */
462static void blkg_free(struct blkio_group *blkg)
463{
Tejun Heo549d3aa2012-03-05 13:15:16 -0800464 struct blkg_policy_data *pd;
465
466 if (!blkg)
467 return;
468
469 pd = blkg->pd[blkg->plid];
470 if (pd) {
471 free_percpu(pd->stats_cpu);
472 kfree(pd);
Tejun Heo03814112012-03-05 13:15:14 -0800473 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800474 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800475}
476
477/**
478 * blkg_alloc - allocate a blkg
479 * @blkcg: block cgroup the new blkg is associated with
480 * @q: request_queue the new blkg is associated with
481 * @pol: policy the new blkg is associated with
482 *
483 * Allocate a new blkg assocating @blkcg and @q for @pol.
484 *
485 * FIXME: Should be called with queue locked but currently isn't due to
486 * percpu stat breakage.
487 */
488static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
489 struct request_queue *q,
490 struct blkio_policy_type *pol)
491{
492 struct blkio_group *blkg;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800493 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800494
495 /* alloc and init base part */
496 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
497 if (!blkg)
498 return NULL;
499
500 spin_lock_init(&blkg->stats_lock);
501 rcu_assign_pointer(blkg->q, q);
Tejun Heo4eef3042012-03-05 13:15:18 -0800502 INIT_LIST_HEAD(&blkg->q_node[0]);
503 INIT_LIST_HEAD(&blkg->q_node[1]);
Tejun Heo03814112012-03-05 13:15:14 -0800504 blkg->blkcg = blkcg;
505 blkg->plid = pol->plid;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800506 blkg->refcnt = 1;
Tejun Heo03814112012-03-05 13:15:14 -0800507 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
508
Tejun Heo549d3aa2012-03-05 13:15:16 -0800509 /* alloc per-policy data and attach it to blkg */
510 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
511 q->node);
512 if (!pd) {
Tejun Heo03814112012-03-05 13:15:14 -0800513 blkg_free(blkg);
514 return NULL;
515 }
516
Tejun Heo549d3aa2012-03-05 13:15:16 -0800517 blkg->pd[pol->plid] = pd;
518 pd->blkg = blkg;
519
Tejun Heo03814112012-03-05 13:15:14 -0800520 /* broken, read comment in the callsite */
Tejun Heo549d3aa2012-03-05 13:15:16 -0800521
522 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
523 if (!pd->stats_cpu) {
Tejun Heo03814112012-03-05 13:15:14 -0800524 blkg_free(blkg);
525 return NULL;
526 }
527
Tejun Heo549d3aa2012-03-05 13:15:16 -0800528 /* invoke per-policy init */
Tejun Heo03814112012-03-05 13:15:14 -0800529 pol->ops.blkio_init_group_fn(blkg);
530 return blkg;
531}
532
Tejun Heocd1604f2012-03-05 13:15:06 -0800533struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
534 struct request_queue *q,
535 enum blkio_policy_id plid,
536 bool for_root)
537 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400538{
Tejun Heocd1604f2012-03-05 13:15:06 -0800539 struct blkio_policy_type *pol = blkio_policy[plid];
540 struct blkio_group *blkg, *new_blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400541
Tejun Heocd1604f2012-03-05 13:15:06 -0800542 WARN_ON_ONCE(!rcu_read_lock_held());
543 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500544
Tejun Heocd1604f2012-03-05 13:15:06 -0800545 /*
546 * This could be the first entry point of blkcg implementation and
547 * we shouldn't allow anything to go through for a bypassing queue.
548 * The following can be removed if blkg lookup is guaranteed to
549 * fail on a bypassing queue.
550 */
551 if (unlikely(blk_queue_bypass(q)) && !for_root)
552 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
553
554 blkg = blkg_lookup(blkcg, q, plid);
555 if (blkg)
556 return blkg;
557
Tejun Heo7ee9c562012-03-05 13:15:11 -0800558 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800559 if (!css_tryget(&blkcg->css))
560 return ERR_PTR(-EINVAL);
561
562 /*
563 * Allocate and initialize.
564 *
565 * FIXME: The following is broken. Percpu memory allocation
566 * requires %GFP_KERNEL context and can't be performed from IO
567 * path. Allocation here should inherently be atomic and the
568 * following lock dancing can be removed once the broken percpu
569 * allocation is fixed.
570 */
571 spin_unlock_irq(q->queue_lock);
572 rcu_read_unlock();
573
Tejun Heo03814112012-03-05 13:15:14 -0800574 new_blkg = blkg_alloc(blkcg, q, pol);
Tejun Heocd1604f2012-03-05 13:15:06 -0800575
576 rcu_read_lock();
577 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800578
579 /* did bypass get turned on inbetween? */
580 if (unlikely(blk_queue_bypass(q)) && !for_root) {
581 blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
582 goto out;
583 }
584
585 /* did someone beat us to it? */
586 blkg = blkg_lookup(blkcg, q, plid);
587 if (unlikely(blkg))
588 goto out;
589
590 /* did alloc fail? */
Tejun Heo03814112012-03-05 13:15:14 -0800591 if (unlikely(!new_blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800592 blkg = ERR_PTR(-ENOMEM);
593 goto out;
594 }
595
596 /* insert */
597 spin_lock(&blkcg->lock);
598 swap(blkg, new_blkg);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800599
Vivek Goyal31e4c282009-12-03 12:59:42 -0500600 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800601 list_add(&blkg->q_node[plid], &q->blkg_list[plid]);
602 q->nr_blkgs[plid]++;
603
Tejun Heocd1604f2012-03-05 13:15:06 -0800604 spin_unlock(&blkcg->lock);
605out:
Tejun Heo03814112012-03-05 13:15:14 -0800606 blkg_free(new_blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800607 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500608}
Tejun Heocd1604f2012-03-05 13:15:06 -0800609EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500610
Vivek Goyalb1c35762009-12-03 12:59:47 -0500611static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
612{
613 hlist_del_init_rcu(&blkg->blkcg_node);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500614}
615
616/*
617 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
618 * indicating that blk_group was unhashed by the time we got to it.
619 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500620int blkiocg_del_blkio_group(struct blkio_group *blkg)
621{
Tejun Heo7ee9c562012-03-05 13:15:11 -0800622 struct blkio_cgroup *blkcg = blkg->blkcg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500623 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500624 int ret = 1;
625
Tejun Heo7ee9c562012-03-05 13:15:11 -0800626 spin_lock_irqsave(&blkcg->lock, flags);
627 if (!hlist_unhashed(&blkg->blkcg_node)) {
628 __blkiocg_del_blkio_group(blkg);
629 ret = 0;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500630 }
Tejun Heo7ee9c562012-03-05 13:15:11 -0800631 spin_unlock_irqrestore(&blkcg->lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200632
Vivek Goyalb1c35762009-12-03 12:59:47 -0500633 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500634}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500635EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500636
637/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800638struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
639 struct request_queue *q,
640 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500641{
642 struct blkio_group *blkg;
643 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500644
Tejun Heoca32aef2012-03-05 13:15:03 -0800645 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
646 if (blkg->q == q && blkg->plid == plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500647 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500648 return NULL;
649}
Tejun Heocd1604f2012-03-05 13:15:06 -0800650EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500651
Tejun Heo03aa264a2012-03-05 13:15:19 -0800652static void blkg_destroy(struct blkio_group *blkg, enum blkio_policy_id plid)
Tejun Heo72e06c22012-03-05 13:15:00 -0800653{
Tejun Heo03aa264a2012-03-05 13:15:19 -0800654 struct request_queue *q = blkg->q;
655
656 lockdep_assert_held(q->queue_lock);
657
658 /* Something wrong if we are trying to remove same group twice */
659 WARN_ON_ONCE(list_empty(&blkg->q_node[plid]));
660 list_del_init(&blkg->q_node[plid]);
661
662 WARN_ON_ONCE(q->nr_blkgs[plid] <= 0);
663 q->nr_blkgs[plid]--;
664
665 /*
666 * Put the reference taken at the time of creation so that when all
667 * queues are gone, group can be destroyed.
668 */
669 blkg_put(blkg);
670}
671
672void blkg_destroy_all(struct request_queue *q, enum blkio_policy_id plid,
673 bool destroy_root)
674{
675 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800676
677 while (true) {
678 bool done = true;
679
Tejun Heo72e06c22012-03-05 13:15:00 -0800680 spin_lock_irq(q->queue_lock);
681
Tejun Heo03aa264a2012-03-05 13:15:19 -0800682 list_for_each_entry_safe(blkg, n, &q->blkg_list[plid],
683 q_node[plid]) {
684 /* skip root? */
685 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
686 continue;
687
688 /*
689 * If cgroup removal path got to blk_group first
690 * and removed it from cgroup list, then it will
691 * take care of destroying cfqg also.
692 */
693 if (!blkiocg_del_blkio_group(blkg))
694 blkg_destroy(blkg, plid);
695 else
Tejun Heo72e06c22012-03-05 13:15:00 -0800696 done = false;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800697 }
Tejun Heo72e06c22012-03-05 13:15:00 -0800698
699 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800700
Tejun Heo03aa264a2012-03-05 13:15:19 -0800701 /*
702 * Group list may not be empty if we raced cgroup removal
703 * and lost. cgroup removal is guaranteed to make forward
704 * progress and retrying after a while is enough. This
705 * ugliness is scheduled to be removed after locking
706 * update.
707 */
Tejun Heo72e06c22012-03-05 13:15:00 -0800708 if (done)
709 break;
710
711 msleep(10); /* just some random duration I like */
712 }
713}
Tejun Heo03aa264a2012-03-05 13:15:19 -0800714EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800715
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800716static void blkg_rcu_free(struct rcu_head *rcu_head)
717{
718 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
719}
720
721void __blkg_release(struct blkio_group *blkg)
722{
723 /* release the extra blkcg reference this blkg has been holding */
724 css_put(&blkg->blkcg->css);
725
726 /*
727 * A group is freed in rcu manner. But having an rcu lock does not
728 * mean that one can access all the fields of blkg and assume these
729 * are valid. For example, don't try to follow throtl_data and
730 * request queue links.
731 *
732 * Having a reference to blkg under an rcu allows acess to only
733 * values local to groups like group stats and group rate limits
734 */
735 call_rcu(&blkg->rcu_head, blkg_rcu_free);
736}
737EXPORT_SYMBOL_GPL(__blkg_release);
738
Tejun Heoc1768262012-03-05 13:15:17 -0800739static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400740{
Tejun Heoc1768262012-03-05 13:15:17 -0800741 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400742 struct blkio_group_stats_cpu *stats_cpu;
743 int i, j, k;
744 /*
745 * Note: On 64 bit arch this should not be an issue. This has the
746 * possibility of returning some inconsistent value on 32bit arch
747 * as 64bit update on 32bit is non atomic. Taking care of this
748 * corner case makes code very complicated, like sending IPIs to
749 * cpus, taking care of stats of offline cpus etc.
750 *
751 * reset stats is anyway more of a debug feature and this sounds a
752 * corner case. So I am not complicating the code yet until and
753 * unless this becomes a real issue.
754 */
755 for_each_possible_cpu(i) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800756 stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400757 stats_cpu->sectors = 0;
758 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
759 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
760 stats_cpu->stat_arr_cpu[j][k] = 0;
761 }
762}
763
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700764static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200765blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700766{
767 struct blkio_cgroup *blkcg;
768 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700769 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700770 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700771 uint64_t queued[BLKIO_STAT_TOTAL];
772 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700773#ifdef CONFIG_DEBUG_BLK_CGROUP
774 bool idling, waiting, empty;
775 unsigned long long now = sched_clock();
776#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700777
778 blkcg = cgroup_to_blkio_cgroup(cgroup);
779 spin_lock_irq(&blkcg->lock);
780 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800781 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
782
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700783 spin_lock(&blkg->stats_lock);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800784 stats = &pd->stats;
Divyesh Shah812df482010-04-08 21:15:35 -0700785#ifdef CONFIG_DEBUG_BLK_CGROUP
786 idling = blkio_blkg_idling(stats);
787 waiting = blkio_blkg_waiting(stats);
788 empty = blkio_blkg_empty(stats);
789#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700790 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700791 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
792 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700793 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700794 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
795#ifdef CONFIG_DEBUG_BLK_CGROUP
796 if (idling) {
797 blkio_mark_blkg_idling(stats);
798 stats->start_idle_time = now;
799 }
800 if (waiting) {
801 blkio_mark_blkg_waiting(stats);
802 stats->start_group_wait_time = now;
803 }
804 if (empty) {
805 blkio_mark_blkg_empty(stats);
806 stats->start_empty_time = now;
807 }
808#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700809 spin_unlock(&blkg->stats_lock);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400810
811 /* Reset Per cpu stats which don't take blkg->stats_lock */
Tejun Heoc1768262012-03-05 13:15:17 -0800812 blkio_reset_stats_cpu(blkg, blkg->plid);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700813 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400814
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700815 spin_unlock_irq(&blkcg->lock);
816 return 0;
817}
818
Tejun Heo7a4dd282012-03-05 13:15:09 -0800819static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
820 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700821{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800822 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700823 chars_left -= strlen(str);
824 if (chars_left <= 0) {
825 printk(KERN_WARNING
826 "Possibly incorrect cgroup stat display format");
827 return;
828 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200829 if (diskname_only)
830 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700831 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200832 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700833 strlcat(str, " Read", chars_left);
834 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200835 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700836 strlcat(str, " Write", chars_left);
837 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200838 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700839 strlcat(str, " Sync", chars_left);
840 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200841 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700842 strlcat(str, " Async", chars_left);
843 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200844 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700845 strlcat(str, " Total", chars_left);
846 break;
847 default:
848 strlcat(str, " Invalid", chars_left);
849 }
850}
851
Divyesh Shah84c124d2010-04-09 08:31:19 +0200852static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800853 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200854{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800855 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200856 cb->fill(cb, str, val);
857 return val;
858}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700859
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400860
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
869 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400870 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800871 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400872
Vivek Goyal575969a2011-05-19 15:38:29 -0400873 do {
874 start = u64_stats_fetch_begin(&stats_cpu->syncp);
875 if (type == BLKIO_STAT_CPU_SECTORS)
876 tval = stats_cpu->sectors;
877 else
878 tval = stats_cpu->stat_arr_cpu[type][sub_type];
879 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
880
881 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400882 }
883
884 return val;
885}
886
Tejun Heoc1768262012-03-05 13:15:17 -0800887static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800888 struct cgroup_map_cb *cb, const char *dname,
889 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400890{
891 uint64_t disk_total, val;
892 char key_str[MAX_KEY_LEN];
893 enum stat_sub_type sub_type;
894
895 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800896 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800897 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
898 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400899 }
900
901 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
902 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800903 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
904 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800905 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400906 cb->fill(cb, key_str, val);
907 }
908
Tejun Heoc1768262012-03-05 13:15:17 -0800909 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
910 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400911
Tejun Heo7a4dd282012-03-05 13:15:09 -0800912 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
913 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400914 cb->fill(cb, key_str, disk_total);
915 return disk_total;
916}
917
Divyesh Shah84c124d2010-04-09 08:31:19 +0200918/* This should be called with blkg->stats_lock held */
Tejun Heoc1768262012-03-05 13:15:17 -0800919static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800920 struct cgroup_map_cb *cb, const char *dname,
921 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700922{
Tejun Heoc1768262012-03-05 13:15:17 -0800923 struct blkg_policy_data *pd = blkg->pd[plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700924 uint64_t disk_total;
925 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200926 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700927
Divyesh Shah84c124d2010-04-09 08:31:19 +0200928 if (type == BLKIO_STAT_TIME)
929 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800930 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100931#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100932 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
933 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800934 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700935 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800936 uint64_t sum = pd->stats.avg_queue_size_sum;
937 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700938 if (samples)
939 do_div(sum, samples);
940 else
941 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800942 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
943 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700944 }
Divyesh Shah812df482010-04-08 21:15:35 -0700945 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
946 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800947 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700948 if (type == BLKIO_STAT_IDLE_TIME)
949 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800950 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700951 if (type == BLKIO_STAT_EMPTY_TIME)
952 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800953 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200954 if (type == BLKIO_STAT_DEQUEUE)
955 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800956 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200957#endif
958
959 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
960 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800961 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
962 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800963 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700964 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800965 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
966 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800967 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
968 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700969 cb->fill(cb, key_str, disk_total);
970 return disk_total;
971}
972
Tejun Heo4bfd4822012-03-05 13:15:08 -0800973static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
974 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800975{
Tejun Heoece84242011-10-19 14:31:15 +0200976 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800977 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800978 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800979 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200980 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200981 int i = 0, ret = -EINVAL;
982 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800983 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200984 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800985
986 memset(s, 0, sizeof(s));
987
988 while ((p = strsep(&buf, " ")) != NULL) {
989 if (!*p)
990 continue;
991
992 s[i++] = p;
993
994 /* Prevent from inputing too many things */
995 if (i == 3)
996 break;
997 }
998
999 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001000 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001001
1002 p = strsep(&s[0], ":");
1003 if (p != NULL)
1004 major_s = p;
1005 else
Tejun Heoece84242011-10-19 14:31:15 +02001006 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001007
1008 minor_s = s[0];
1009 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001010 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001011
Tejun Heoece84242011-10-19 14:31:15 +02001012 if (strict_strtoul(major_s, 10, &major))
1013 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001014
Tejun Heoece84242011-10-19 14:31:15 +02001015 if (strict_strtoul(minor_s, 10, &minor))
1016 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001017
1018 dev = MKDEV(major, minor);
1019
Tejun Heoece84242011-10-19 14:31:15 +02001020 if (strict_strtoull(s[1], 10, &temp))
1021 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001022
Tejun Heoe56da7e2012-03-05 13:15:07 -08001023 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001024 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001025 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001026
1027 rcu_read_lock();
1028
Tejun Heo4bfd4822012-03-05 13:15:08 -08001029 spin_lock_irq(disk->queue->queue_lock);
1030 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1031 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001032
Tejun Heo4bfd4822012-03-05 13:15:08 -08001033 if (IS_ERR(blkg)) {
1034 ret = PTR_ERR(blkg);
1035 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001036 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001037
Tejun Heo549d3aa2012-03-05 13:15:16 -08001038 pd = blkg->pd[plid];
1039
Vivek Goyal062a6442010-09-15 17:06:33 -04001040 switch (plid) {
1041 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001042 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1043 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001044 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001045
Tejun Heo549d3aa2012-03-05 13:15:16 -08001046 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001047 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001048 break;
1049 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001050 switch(fileid) {
1051 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001052 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001053 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001054 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001055 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001056 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001057 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001058 break;
1059 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001060 if (temp > THROTL_IOPS_MAX)
1061 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001062 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001063 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001064 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001065 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001066 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001067 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001068 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001069 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001070 break;
1071 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001072 break;
1073 default:
1074 BUG();
1075 }
Tejun Heoece84242011-10-19 14:31:15 +02001076 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001077out_unlock:
1078 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001079out:
1080 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001081
1082 /*
1083 * If queue was bypassing, we should retry. Do so after a short
1084 * msleep(). It isn't strictly necessary but queue can be
1085 * bypassing for some time and it's always nice to avoid busy
1086 * looping.
1087 */
1088 if (ret == -EBUSY) {
1089 msleep(10);
1090 return restart_syscall();
1091 }
Tejun Heoece84242011-10-19 14:31:15 +02001092 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001093}
1094
Vivek Goyal062a6442010-09-15 17:06:33 -04001095static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1096 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001097{
1098 int ret = 0;
1099 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001100 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001101 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1102 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001103
1104 buf = kstrdup(buffer, GFP_KERNEL);
1105 if (!buf)
1106 return -ENOMEM;
1107
Tejun Heo4bfd4822012-03-05 13:15:08 -08001108 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001109 kfree(buf);
1110 return ret;
1111}
1112
Vivek Goyal92616b52012-03-05 13:15:10 -08001113static const char *blkg_dev_name(struct blkio_group *blkg)
1114{
1115 /* some drivers (floppy) instantiate a queue w/o disk registered */
1116 if (blkg->q->backing_dev_info.dev)
1117 return dev_name(blkg->q->backing_dev_info.dev);
1118 return NULL;
1119}
1120
Tejun Heo4bfd4822012-03-05 13:15:08 -08001121static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1122 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001123{
Tejun Heoc1768262012-03-05 13:15:17 -08001124 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001125 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001126 struct blkg_policy_data *pd = blkg->pd[plid];
1127 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001128 int rw = WRITE;
1129
Vivek Goyal92616b52012-03-05 13:15:10 -08001130 if (!dname)
1131 return;
1132
Tejun Heoc1768262012-03-05 13:15:17 -08001133 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001134 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001135 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001136 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001137 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001138 break;
1139 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001140 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001141 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001142 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001143 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001144 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001145 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001146 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001147 break;
1148 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001149 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001150 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001151 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001152 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001153 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001154 break;
1155 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001156 break;
1157 default:
1158 BUG();
1159 }
1160}
1161
1162/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001163static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1164 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001165{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001166 struct blkio_group *blkg;
1167 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001168
Tejun Heo4bfd4822012-03-05 13:15:08 -08001169 spin_lock_irq(&blkcg->lock);
1170 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
1171 if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
1172 blkio_print_group_conf(cft, blkg, m);
1173 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001174}
1175
1176static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1177 struct seq_file *m)
1178{
1179 struct blkio_cgroup *blkcg;
1180 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1181 int name = BLKIOFILE_ATTR(cft->private);
1182
1183 blkcg = cgroup_to_blkio_cgroup(cgrp);
1184
1185 switch(plid) {
1186 case BLKIO_POLICY_PROP:
1187 switch(name) {
1188 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001189 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001190 return 0;
1191 default:
1192 BUG();
1193 }
1194 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001195 case BLKIO_POLICY_THROTL:
1196 switch(name){
1197 case BLKIO_THROTL_read_bps_device:
1198 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001199 case BLKIO_THROTL_read_iops_device:
1200 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001201 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001202 return 0;
1203 default:
1204 BUG();
1205 }
1206 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001207 default:
1208 BUG();
1209 }
1210
1211 return 0;
1212}
1213
1214static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001215 struct cftype *cft, struct cgroup_map_cb *cb,
1216 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001217{
1218 struct blkio_group *blkg;
1219 struct hlist_node *n;
1220 uint64_t cgroup_total = 0;
1221
1222 rcu_read_lock();
1223 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001224 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001225 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001226
Tejun Heoc1768262012-03-05 13:15:17 -08001227 if (!dname || plid != blkg->plid)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001228 continue;
Tejun Heoc1768262012-03-05 13:15:17 -08001229 if (pcpu) {
1230 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1231 cb, dname, type);
1232 } else {
Tejun Heo7a4dd282012-03-05 13:15:09 -08001233 spin_lock_irq(&blkg->stats_lock);
Tejun Heoc1768262012-03-05 13:15:17 -08001234 cgroup_total += blkio_get_stat(blkg, plid,
1235 cb, dname, type);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001236 spin_unlock_irq(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001237 }
1238 }
1239 if (show_total)
1240 cb->fill(cb, "Total", cgroup_total);
1241 rcu_read_unlock();
1242 return 0;
1243}
1244
1245/* All map kind of cgroup file get serviced by this function */
1246static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1247 struct cgroup_map_cb *cb)
1248{
1249 struct blkio_cgroup *blkcg;
1250 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1251 int name = BLKIOFILE_ATTR(cft->private);
1252
1253 blkcg = cgroup_to_blkio_cgroup(cgrp);
1254
1255 switch(plid) {
1256 case BLKIO_POLICY_PROP:
1257 switch(name) {
1258 case BLKIO_PROP_time:
1259 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001260 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001261 case BLKIO_PROP_sectors:
1262 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001263 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001264 case BLKIO_PROP_io_service_bytes:
1265 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001266 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001267 case BLKIO_PROP_io_serviced:
1268 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001269 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001270 case BLKIO_PROP_io_service_time:
1271 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001272 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001273 case BLKIO_PROP_io_wait_time:
1274 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001275 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001276 case BLKIO_PROP_io_merged:
1277 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001278 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001279 case BLKIO_PROP_io_queued:
1280 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001281 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001282#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001283 case BLKIO_PROP_unaccounted_time:
1284 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001285 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001286 case BLKIO_PROP_dequeue:
1287 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001288 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001289 case BLKIO_PROP_avg_queue_size:
1290 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001291 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001292 case BLKIO_PROP_group_wait_time:
1293 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001294 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001295 case BLKIO_PROP_idle_time:
1296 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001297 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001298 case BLKIO_PROP_empty_time:
1299 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001300 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001301#endif
1302 default:
1303 BUG();
1304 }
1305 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001306 case BLKIO_POLICY_THROTL:
1307 switch(name){
1308 case BLKIO_THROTL_io_service_bytes:
1309 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001310 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001311 case BLKIO_THROTL_io_serviced:
1312 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001313 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001314 default:
1315 BUG();
1316 }
1317 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001318 default:
1319 BUG();
1320 }
1321
1322 return 0;
1323}
1324
Tejun Heo4bfd4822012-03-05 13:15:08 -08001325static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001326{
1327 struct blkio_group *blkg;
1328 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001329
1330 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1331 return -EINVAL;
1332
1333 spin_lock(&blkio_list_lock);
1334 spin_lock_irq(&blkcg->lock);
1335 blkcg->weight = (unsigned int)val;
1336
Tejun Heo549d3aa2012-03-05 13:15:16 -08001337 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1338 struct blkg_policy_data *pd = blkg->pd[blkg->plid];
1339
1340 if (blkg->plid == plid && !pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001341 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001342 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001343
Vivek Goyal062a6442010-09-15 17:06:33 -04001344 spin_unlock_irq(&blkcg->lock);
1345 spin_unlock(&blkio_list_lock);
1346 return 0;
1347}
1348
1349static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1350 struct blkio_cgroup *blkcg;
1351 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1352 int name = BLKIOFILE_ATTR(cft->private);
1353
1354 blkcg = cgroup_to_blkio_cgroup(cgrp);
1355
1356 switch(plid) {
1357 case BLKIO_POLICY_PROP:
1358 switch(name) {
1359 case BLKIO_PROP_weight:
1360 return (u64)blkcg->weight;
1361 }
1362 break;
1363 default:
1364 BUG();
1365 }
1366 return 0;
1367}
1368
1369static int
1370blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1371{
1372 struct blkio_cgroup *blkcg;
1373 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1374 int name = BLKIOFILE_ATTR(cft->private);
1375
1376 blkcg = cgroup_to_blkio_cgroup(cgrp);
1377
1378 switch(plid) {
1379 case BLKIO_POLICY_PROP:
1380 switch(name) {
1381 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001382 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001383 }
1384 break;
1385 default:
1386 BUG();
1387 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001388
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001389 return 0;
1390}
1391
Vivek Goyal31e4c282009-12-03 12:59:42 -05001392struct cftype blkio_files[] = {
1393 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001394 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001395 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1396 BLKIO_PROP_weight_device),
1397 .read_seq_string = blkiocg_file_read,
1398 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001399 .max_write_len = 256,
1400 },
1401 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001402 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001403 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1404 BLKIO_PROP_weight),
1405 .read_u64 = blkiocg_file_read_u64,
1406 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001407 },
Vivek Goyal22084192009-12-03 12:59:49 -05001408 {
1409 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001410 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1411 BLKIO_PROP_time),
1412 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001413 },
1414 {
1415 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001416 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1417 BLKIO_PROP_sectors),
1418 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001419 },
1420 {
1421 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001422 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1423 BLKIO_PROP_io_service_bytes),
1424 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001425 },
1426 {
1427 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001428 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1429 BLKIO_PROP_io_serviced),
1430 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001431 },
1432 {
1433 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001434 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1435 BLKIO_PROP_io_service_time),
1436 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001437 },
1438 {
1439 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001440 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1441 BLKIO_PROP_io_wait_time),
1442 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001443 },
1444 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001445 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001446 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1447 BLKIO_PROP_io_merged),
1448 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001449 },
1450 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001451 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001452 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1453 BLKIO_PROP_io_queued),
1454 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001455 },
1456 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001457 .name = "reset_stats",
1458 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001459 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001460#ifdef CONFIG_BLK_DEV_THROTTLING
1461 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001462 .name = "throttle.read_bps_device",
1463 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1464 BLKIO_THROTL_read_bps_device),
1465 .read_seq_string = blkiocg_file_read,
1466 .write_string = blkiocg_file_write,
1467 .max_write_len = 256,
1468 },
1469
1470 {
1471 .name = "throttle.write_bps_device",
1472 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1473 BLKIO_THROTL_write_bps_device),
1474 .read_seq_string = blkiocg_file_read,
1475 .write_string = blkiocg_file_write,
1476 .max_write_len = 256,
1477 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001478
1479 {
1480 .name = "throttle.read_iops_device",
1481 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1482 BLKIO_THROTL_read_iops_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_iops_device",
1490 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1491 BLKIO_THROTL_write_iops_device),
1492 .read_seq_string = blkiocg_file_read,
1493 .write_string = blkiocg_file_write,
1494 .max_write_len = 256,
1495 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001496 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001497 .name = "throttle.io_service_bytes",
1498 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1499 BLKIO_THROTL_io_service_bytes),
1500 .read_map = blkiocg_file_read_map,
1501 },
1502 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001503 .name = "throttle.io_serviced",
1504 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1505 BLKIO_THROTL_io_serviced),
1506 .read_map = blkiocg_file_read_map,
1507 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001508#endif /* CONFIG_BLK_DEV_THROTTLING */
1509
Vivek Goyal22084192009-12-03 12:59:49 -05001510#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001511 {
1512 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001513 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1514 BLKIO_PROP_avg_queue_size),
1515 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001516 },
1517 {
Divyesh Shah812df482010-04-08 21:15:35 -07001518 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001519 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1520 BLKIO_PROP_group_wait_time),
1521 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001522 },
1523 {
1524 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001525 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1526 BLKIO_PROP_idle_time),
1527 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001528 },
1529 {
1530 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001531 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1532 BLKIO_PROP_empty_time),
1533 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001534 },
1535 {
Vivek Goyal22084192009-12-03 12:59:49 -05001536 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001537 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1538 BLKIO_PROP_dequeue),
1539 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001540 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001541 {
1542 .name = "unaccounted_time",
1543 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1544 BLKIO_PROP_unaccounted_time),
1545 .read_map = blkiocg_file_read_map,
1546 },
Vivek Goyal22084192009-12-03 12:59:49 -05001547#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001548};
1549
1550static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1551{
1552 return cgroup_add_files(cgroup, subsys, blkio_files,
1553 ARRAY_SIZE(blkio_files));
1554}
1555
Tejun Heo7ee9c562012-03-05 13:15:11 -08001556static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1557 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001558{
1559 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001560 unsigned long flags;
1561 struct blkio_group *blkg;
Tejun Heoca32aef2012-03-05 13:15:03 -08001562 struct request_queue *q;
Vivek Goyal3e252062009-12-04 10:36:42 -05001563 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001564
Vivek Goyalb1c35762009-12-03 12:59:47 -05001565 rcu_read_lock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001566
Jens Axboe0f3942a2010-05-03 14:28:55 +02001567 do {
1568 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001569
Jens Axboe0f3942a2010-05-03 14:28:55 +02001570 if (hlist_empty(&blkcg->blkg_list)) {
1571 spin_unlock_irqrestore(&blkcg->lock, flags);
1572 break;
1573 }
1574
1575 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1576 blkcg_node);
Tejun Heoca32aef2012-03-05 13:15:03 -08001577 q = rcu_dereference(blkg->q);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001578 __blkiocg_del_blkio_group(blkg);
1579
Vivek Goyalb1c35762009-12-03 12:59:47 -05001580 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001581
Jens Axboe0f3942a2010-05-03 14:28:55 +02001582 /*
1583 * This blkio_group is being unlinked as associated cgroup is
1584 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001585 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001586 */
1587 spin_lock(&blkio_list_lock);
Tejun Heo03aa264a2012-03-05 13:15:19 -08001588 spin_lock_irqsave(q->queue_lock, flags);
Vivek Goyal61014e92010-10-01 14:49:44 +02001589 list_for_each_entry(blkiop, &blkio_list, list) {
1590 if (blkiop->plid != blkg->plid)
1591 continue;
Tejun Heo03aa264a2012-03-05 13:15:19 -08001592 blkg_destroy(blkg, blkiop->plid);
Vivek Goyal61014e92010-10-01 14:49:44 +02001593 }
Tejun Heo03aa264a2012-03-05 13:15:19 -08001594 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001595 spin_unlock(&blkio_list_lock);
1596 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001597
Vivek Goyalb1c35762009-12-03 12:59:47 -05001598 rcu_read_unlock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001599
1600 return 0;
1601}
1602
1603static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1604{
1605 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1606
Ben Blum67523c42010-03-10 15:22:11 -08001607 if (blkcg != &blkio_root_cgroup)
1608 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001609}
1610
1611static struct cgroup_subsys_state *
1612blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1613{
Li Zefan03415092010-05-07 08:57:00 +02001614 struct blkio_cgroup *blkcg;
1615 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001616
Li Zefan03415092010-05-07 08:57:00 +02001617 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001618 blkcg = &blkio_root_cgroup;
1619 goto done;
1620 }
1621
Vivek Goyal31e4c282009-12-03 12:59:42 -05001622 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1623 if (!blkcg)
1624 return ERR_PTR(-ENOMEM);
1625
1626 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1627done:
1628 spin_lock_init(&blkcg->lock);
1629 INIT_HLIST_HEAD(&blkcg->blkg_list);
1630
1631 return &blkcg->css;
1632}
1633
Tejun Heo5efd6112012-03-05 13:15:12 -08001634/**
1635 * blkcg_init_queue - initialize blkcg part of request queue
1636 * @q: request_queue to initialize
1637 *
1638 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1639 * part of new request_queue @q.
1640 *
1641 * RETURNS:
1642 * 0 on success, -errno on failure.
1643 */
1644int blkcg_init_queue(struct request_queue *q)
1645{
Tejun Heo923adde2012-03-05 13:15:13 -08001646 int ret;
1647
Tejun Heo5efd6112012-03-05 13:15:12 -08001648 might_sleep();
1649
Tejun Heo923adde2012-03-05 13:15:13 -08001650 ret = blk_throtl_init(q);
1651 if (ret)
1652 return ret;
1653
1654 mutex_lock(&all_q_mutex);
1655 INIT_LIST_HEAD(&q->all_q_node);
1656 list_add_tail(&q->all_q_node, &all_q_list);
1657 mutex_unlock(&all_q_mutex);
1658
1659 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001660}
1661
1662/**
1663 * blkcg_drain_queue - drain blkcg part of request_queue
1664 * @q: request_queue to drain
1665 *
1666 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1667 */
1668void blkcg_drain_queue(struct request_queue *q)
1669{
1670 lockdep_assert_held(q->queue_lock);
1671
1672 blk_throtl_drain(q);
1673}
1674
1675/**
1676 * blkcg_exit_queue - exit and release blkcg part of request_queue
1677 * @q: request_queue being released
1678 *
1679 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1680 */
1681void blkcg_exit_queue(struct request_queue *q)
1682{
Tejun Heo923adde2012-03-05 13:15:13 -08001683 mutex_lock(&all_q_mutex);
1684 list_del_init(&q->all_q_node);
1685 mutex_unlock(&all_q_mutex);
1686
Tejun Heo5efd6112012-03-05 13:15:12 -08001687 blk_throtl_exit(q);
1688}
1689
Vivek Goyal31e4c282009-12-03 12:59:42 -05001690/*
1691 * We cannot support shared io contexts, as we have no mean to support
1692 * two tasks with the same ioc in two different groups without major rework
1693 * of the main cic data structures. For now we allow a task to change
1694 * its cgroup only if it's the only owner of its ioc.
1695 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001696static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1697 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001698{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001699 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001700 struct io_context *ioc;
1701 int ret = 0;
1702
1703 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001704 cgroup_taskset_for_each(task, cgrp, tset) {
1705 task_lock(task);
1706 ioc = task->io_context;
1707 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1708 ret = -EINVAL;
1709 task_unlock(task);
1710 if (ret)
1711 break;
1712 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001713 return ret;
1714}
1715
Tejun Heobb9d97b2011-12-12 18:12:21 -08001716static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1717 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001718{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001719 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001720 struct io_context *ioc;
1721
Tejun Heobb9d97b2011-12-12 18:12:21 -08001722 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001723 /* we don't lose anything even if ioc allocation fails */
1724 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1725 if (ioc) {
1726 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001727 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001728 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001729 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001730}
1731
Tejun Heo923adde2012-03-05 13:15:13 -08001732static void blkcg_bypass_start(void)
1733 __acquires(&all_q_mutex)
1734{
1735 struct request_queue *q;
Tejun Heo03aa264a2012-03-05 13:15:19 -08001736 int i;
Tejun Heo923adde2012-03-05 13:15:13 -08001737
1738 mutex_lock(&all_q_mutex);
1739
1740 list_for_each_entry(q, &all_q_list, all_q_node) {
1741 blk_queue_bypass_start(q);
Tejun Heo03aa264a2012-03-05 13:15:19 -08001742 for (i = 0; i < BLKIO_NR_POLICIES; i++)
1743 blkg_destroy_all(q, i, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001744 }
1745}
1746
1747static void blkcg_bypass_end(void)
1748 __releases(&all_q_mutex)
1749{
1750 struct request_queue *q;
1751
1752 list_for_each_entry(q, &all_q_list, all_q_node)
1753 blk_queue_bypass_end(q);
1754
1755 mutex_unlock(&all_q_mutex);
1756}
1757
Vivek Goyal3e252062009-12-04 10:36:42 -05001758void blkio_policy_register(struct blkio_policy_type *blkiop)
1759{
Tejun Heo923adde2012-03-05 13:15:13 -08001760 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001761 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001762
1763 BUG_ON(blkio_policy[blkiop->plid]);
1764 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001765 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001766
Vivek Goyal3e252062009-12-04 10:36:42 -05001767 spin_unlock(&blkio_list_lock);
Tejun Heo923adde2012-03-05 13:15:13 -08001768 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001769}
1770EXPORT_SYMBOL_GPL(blkio_policy_register);
1771
1772void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1773{
Tejun Heo923adde2012-03-05 13:15:13 -08001774 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001775 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001776
1777 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1778 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001779 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001780
Vivek Goyal3e252062009-12-04 10:36:42 -05001781 spin_unlock(&blkio_list_lock);
Tejun Heo923adde2012-03-05 13:15:13 -08001782 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001783}
1784EXPORT_SYMBOL_GPL(blkio_policy_unregister);