blob: e9e3b038c702ee981a1cd36fb7c5ded0c29d38d1 [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 Heoe8989fa2012-03-05 13:15:20 -0800464 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800465
466 if (!blkg)
467 return;
468
Tejun Heoe8989fa2012-03-05 13:15:20 -0800469 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
470 struct blkg_policy_data *pd = blkg->pd[i];
471
472 if (pd) {
473 free_percpu(pd->stats_cpu);
474 kfree(pd);
475 }
Tejun Heo03814112012-03-05 13:15:14 -0800476 }
Tejun Heoe8989fa2012-03-05 13:15:20 -0800477
Tejun Heo549d3aa2012-03-05 13:15:16 -0800478 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -0800479}
480
481/**
482 * blkg_alloc - allocate a blkg
483 * @blkcg: block cgroup the new blkg is associated with
484 * @q: request_queue the new blkg is associated with
Tejun Heo03814112012-03-05 13:15:14 -0800485 *
Tejun Heoe8989fa2012-03-05 13:15:20 -0800486 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -0800487 *
488 * FIXME: Should be called with queue locked but currently isn't due to
489 * percpu stat breakage.
490 */
491static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800492 struct request_queue *q)
Tejun Heo03814112012-03-05 13:15:14 -0800493{
494 struct blkio_group *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800495 int i;
Tejun Heo03814112012-03-05 13:15:14 -0800496
497 /* alloc and init base part */
498 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
499 if (!blkg)
500 return NULL;
501
502 spin_lock_init(&blkg->stats_lock);
503 rcu_assign_pointer(blkg->q, q);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800504 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800505 blkg->blkcg = blkcg;
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 Heoe8989fa2012-03-05 13:15:20 -0800509 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
510 struct blkio_policy_type *pol = blkio_policy[i];
511 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800512
Tejun Heoe8989fa2012-03-05 13:15:20 -0800513 if (!pol)
514 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800515
Tejun Heoe8989fa2012-03-05 13:15:20 -0800516 /* alloc per-policy data and attach it to blkg */
517 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
518 q->node);
519 if (!pd) {
520 blkg_free(blkg);
521 return NULL;
522 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800523
Tejun Heoe8989fa2012-03-05 13:15:20 -0800524 blkg->pd[i] = pd;
525 pd->blkg = blkg;
526
527 /* broken, read comment in the callsite */
528 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
529 if (!pd->stats_cpu) {
530 blkg_free(blkg);
531 return NULL;
532 }
Tejun Heo03814112012-03-05 13:15:14 -0800533 }
534
Tejun Heo549d3aa2012-03-05 13:15:16 -0800535 /* invoke per-policy init */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800536 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
537 struct blkio_policy_type *pol = blkio_policy[i];
538
539 if (pol)
540 pol->ops.blkio_init_group_fn(blkg);
541 }
542
Tejun Heo03814112012-03-05 13:15:14 -0800543 return blkg;
544}
545
Tejun Heocd1604f2012-03-05 13:15:06 -0800546struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
547 struct request_queue *q,
548 enum blkio_policy_id plid,
549 bool for_root)
550 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400551{
Tejun Heocd1604f2012-03-05 13:15:06 -0800552 struct blkio_group *blkg, *new_blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400553
Tejun Heocd1604f2012-03-05 13:15:06 -0800554 WARN_ON_ONCE(!rcu_read_lock_held());
555 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500556
Tejun Heocd1604f2012-03-05 13:15:06 -0800557 /*
558 * This could be the first entry point of blkcg implementation and
559 * we shouldn't allow anything to go through for a bypassing queue.
560 * The following can be removed if blkg lookup is guaranteed to
561 * fail on a bypassing queue.
562 */
563 if (unlikely(blk_queue_bypass(q)) && !for_root)
564 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
565
Tejun Heoe8989fa2012-03-05 13:15:20 -0800566 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800567 if (blkg)
568 return blkg;
569
Tejun Heo7ee9c562012-03-05 13:15:11 -0800570 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800571 if (!css_tryget(&blkcg->css))
572 return ERR_PTR(-EINVAL);
573
574 /*
575 * Allocate and initialize.
576 *
577 * FIXME: The following is broken. Percpu memory allocation
578 * requires %GFP_KERNEL context and can't be performed from IO
579 * path. Allocation here should inherently be atomic and the
580 * following lock dancing can be removed once the broken percpu
581 * allocation is fixed.
582 */
583 spin_unlock_irq(q->queue_lock);
584 rcu_read_unlock();
585
Tejun Heoe8989fa2012-03-05 13:15:20 -0800586 new_blkg = blkg_alloc(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800587
588 rcu_read_lock();
589 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800590
591 /* did bypass get turned on inbetween? */
592 if (unlikely(blk_queue_bypass(q)) && !for_root) {
593 blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
594 goto out;
595 }
596
597 /* did someone beat us to it? */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800598 blkg = blkg_lookup(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800599 if (unlikely(blkg))
600 goto out;
601
602 /* did alloc fail? */
Tejun Heo03814112012-03-05 13:15:14 -0800603 if (unlikely(!new_blkg)) {
Tejun Heocd1604f2012-03-05 13:15:06 -0800604 blkg = ERR_PTR(-ENOMEM);
605 goto out;
606 }
607
608 /* insert */
609 spin_lock(&blkcg->lock);
610 swap(blkg, new_blkg);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800611
Vivek Goyal31e4c282009-12-03 12:59:42 -0500612 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800613 list_add(&blkg->q_node, &q->blkg_list);
614 q->nr_blkgs++;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800615
Tejun Heocd1604f2012-03-05 13:15:06 -0800616 spin_unlock(&blkcg->lock);
617out:
Tejun Heo03814112012-03-05 13:15:14 -0800618 blkg_free(new_blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800619 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500620}
Tejun Heocd1604f2012-03-05 13:15:06 -0800621EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500622
Vivek Goyal31e4c282009-12-03 12:59:42 -0500623/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800624struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800625 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500626{
627 struct blkio_group *blkg;
628 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500629
Tejun Heoca32aef2012-03-05 13:15:03 -0800630 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800631 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500632 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500633 return NULL;
634}
Tejun Heocd1604f2012-03-05 13:15:06 -0800635EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500636
Tejun Heoe8989fa2012-03-05 13:15:20 -0800637static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800638{
Tejun Heo03aa264a2012-03-05 13:15:19 -0800639 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800640 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800641
642 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800643 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800644
645 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800646 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800647 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800648 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800649 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa264a2012-03-05 13:15:19 -0800650
Tejun Heoe8989fa2012-03-05 13:15:20 -0800651 WARN_ON_ONCE(q->nr_blkgs <= 0);
652 q->nr_blkgs--;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800653
654 /*
655 * Put the reference taken at the time of creation so that when all
656 * queues are gone, group can be destroyed.
657 */
658 blkg_put(blkg);
659}
660
Tejun Heoe8989fa2012-03-05 13:15:20 -0800661/*
662 * XXX: This updates blkg policy data in-place for root blkg, which is
663 * necessary across elevator switch and policy registration as root blkgs
664 * aren't shot down. This broken and racy implementation is temporary.
665 * Eventually, blkg shoot down will be replaced by proper in-place update.
666 */
667void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
668{
669 struct blkio_policy_type *pol = blkio_policy[plid];
670 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
671 struct blkg_policy_data *pd;
672
673 if (!blkg)
674 return;
675
676 kfree(blkg->pd[plid]);
677 blkg->pd[plid] = NULL;
678
679 if (!pol)
680 return;
681
682 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
683 WARN_ON_ONCE(!pd);
684
685 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
686 WARN_ON_ONCE(!pd->stats_cpu);
687
688 blkg->pd[plid] = pd;
689 pd->blkg = blkg;
690 pol->ops.blkio_init_group_fn(blkg);
691}
692EXPORT_SYMBOL_GPL(update_root_blkg_pd);
693
Tejun Heo9f13ef62012-03-05 13:15:21 -0800694/**
695 * blkg_destroy_all - destroy all blkgs associated with a request_queue
696 * @q: request_queue of interest
697 * @destroy_root: whether to destroy root blkg or not
698 *
699 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
700 * destroyed; otherwise, root blkg is left alone.
701 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800702void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa264a2012-03-05 13:15:19 -0800703{
704 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800705
Tejun Heo9f13ef62012-03-05 13:15:21 -0800706 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800707
Tejun Heo9f13ef62012-03-05 13:15:21 -0800708 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
709 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800710
Tejun Heo9f13ef62012-03-05 13:15:21 -0800711 /* skip root? */
712 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
713 continue;
Tejun Heo03aa264a2012-03-05 13:15:19 -0800714
Tejun Heo9f13ef62012-03-05 13:15:21 -0800715 spin_lock(&blkcg->lock);
716 blkg_destroy(blkg);
717 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800718 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800719
720 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800721}
Tejun Heo03aa264a2012-03-05 13:15:19 -0800722EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800723
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800724static void blkg_rcu_free(struct rcu_head *rcu_head)
725{
726 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
727}
728
729void __blkg_release(struct blkio_group *blkg)
730{
731 /* release the extra blkcg reference this blkg has been holding */
732 css_put(&blkg->blkcg->css);
733
734 /*
735 * A group is freed in rcu manner. But having an rcu lock does not
736 * mean that one can access all the fields of blkg and assume these
737 * are valid. For example, don't try to follow throtl_data and
738 * request queue links.
739 *
740 * Having a reference to blkg under an rcu allows acess to only
741 * values local to groups like group stats and group rate limits
742 */
743 call_rcu(&blkg->rcu_head, blkg_rcu_free);
744}
745EXPORT_SYMBOL_GPL(__blkg_release);
746
Tejun Heoc1768262012-03-05 13:15:17 -0800747static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400748{
Tejun Heoc1768262012-03-05 13:15:17 -0800749 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400750 struct blkio_group_stats_cpu *stats_cpu;
751 int i, j, k;
752 /*
753 * Note: On 64 bit arch this should not be an issue. This has the
754 * possibility of returning some inconsistent value on 32bit arch
755 * as 64bit update on 32bit is non atomic. Taking care of this
756 * corner case makes code very complicated, like sending IPIs to
757 * cpus, taking care of stats of offline cpus etc.
758 *
759 * reset stats is anyway more of a debug feature and this sounds a
760 * corner case. So I am not complicating the code yet until and
761 * unless this becomes a real issue.
762 */
763 for_each_possible_cpu(i) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800764 stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400765 stats_cpu->sectors = 0;
766 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
767 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
768 stats_cpu->stat_arr_cpu[j][k] = 0;
769 }
770}
771
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700772static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200773blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700774{
775 struct blkio_cgroup *blkcg;
776 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700777 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700778 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700779 uint64_t queued[BLKIO_STAT_TOTAL];
780 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700781#ifdef CONFIG_DEBUG_BLK_CGROUP
782 bool idling, waiting, empty;
783 unsigned long long now = sched_clock();
784#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700785
786 blkcg = cgroup_to_blkio_cgroup(cgroup);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800787 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700788 spin_lock_irq(&blkcg->lock);
789 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800790 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800791
Tejun Heoe8989fa2012-03-05 13:15:20 -0800792 list_for_each_entry(pol, &blkio_list, list) {
793 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400794
Tejun Heoe8989fa2012-03-05 13:15:20 -0800795 spin_lock(&blkg->stats_lock);
796 stats = &pd->stats;
797#ifdef CONFIG_DEBUG_BLK_CGROUP
798 idling = blkio_blkg_idling(stats);
799 waiting = blkio_blkg_waiting(stats);
800 empty = blkio_blkg_empty(stats);
801#endif
802 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
803 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
804 memset(stats, 0, sizeof(struct blkio_group_stats));
805 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
806 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
807#ifdef CONFIG_DEBUG_BLK_CGROUP
808 if (idling) {
809 blkio_mark_blkg_idling(stats);
810 stats->start_idle_time = now;
811 }
812 if (waiting) {
813 blkio_mark_blkg_waiting(stats);
814 stats->start_group_wait_time = now;
815 }
816 if (empty) {
817 blkio_mark_blkg_empty(stats);
818 stats->start_empty_time = now;
819 }
820#endif
821 spin_unlock(&blkg->stats_lock);
822
823 /* Reset Per cpu stats which don't take blkg->stats_lock */
824 blkio_reset_stats_cpu(blkg, pol->plid);
825 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700826 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400827
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700828 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800829 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700830 return 0;
831}
832
Tejun Heo7a4dd282012-03-05 13:15:09 -0800833static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
834 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700835{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800836 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700837 chars_left -= strlen(str);
838 if (chars_left <= 0) {
839 printk(KERN_WARNING
840 "Possibly incorrect cgroup stat display format");
841 return;
842 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200843 if (diskname_only)
844 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700845 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200846 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700847 strlcat(str, " Read", chars_left);
848 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200849 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700850 strlcat(str, " Write", chars_left);
851 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200852 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700853 strlcat(str, " Sync", chars_left);
854 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200855 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700856 strlcat(str, " Async", chars_left);
857 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200858 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700859 strlcat(str, " Total", chars_left);
860 break;
861 default:
862 strlcat(str, " Invalid", chars_left);
863 }
864}
865
Divyesh Shah84c124d2010-04-09 08:31:19 +0200866static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800867 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200868{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800869 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200870 cb->fill(cb, str, val);
871 return val;
872}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700873
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400874
Tejun Heoc1768262012-03-05 13:15:17 -0800875static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400876 enum stat_type_cpu type, enum stat_sub_type sub_type)
877{
Tejun Heoc1768262012-03-05 13:15:17 -0800878 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400879 int cpu;
880 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400881 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400882
883 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400884 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800885 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400886
Vivek Goyal575969a2011-05-19 15:38:29 -0400887 do {
888 start = u64_stats_fetch_begin(&stats_cpu->syncp);
889 if (type == BLKIO_STAT_CPU_SECTORS)
890 tval = stats_cpu->sectors;
891 else
892 tval = stats_cpu->stat_arr_cpu[type][sub_type];
893 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
894
895 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400896 }
897
898 return val;
899}
900
Tejun Heoc1768262012-03-05 13:15:17 -0800901static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800902 struct cgroup_map_cb *cb, const char *dname,
903 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400904{
905 uint64_t disk_total, val;
906 char key_str[MAX_KEY_LEN];
907 enum stat_sub_type sub_type;
908
909 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800910 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800911 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
912 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400913 }
914
915 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
916 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800917 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
918 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800919 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400920 cb->fill(cb, key_str, val);
921 }
922
Tejun Heoc1768262012-03-05 13:15:17 -0800923 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
924 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400925
Tejun Heo7a4dd282012-03-05 13:15:09 -0800926 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
927 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400928 cb->fill(cb, key_str, disk_total);
929 return disk_total;
930}
931
Divyesh Shah84c124d2010-04-09 08:31:19 +0200932/* This should be called with blkg->stats_lock held */
Tejun Heoc1768262012-03-05 13:15:17 -0800933static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800934 struct cgroup_map_cb *cb, const char *dname,
935 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700936{
Tejun Heoc1768262012-03-05 13:15:17 -0800937 struct blkg_policy_data *pd = blkg->pd[plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700938 uint64_t disk_total;
939 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200940 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700941
Divyesh Shah84c124d2010-04-09 08:31:19 +0200942 if (type == BLKIO_STAT_TIME)
943 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800944 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100945#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100946 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
947 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800948 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700949 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800950 uint64_t sum = pd->stats.avg_queue_size_sum;
951 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700952 if (samples)
953 do_div(sum, samples);
954 else
955 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800956 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
957 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700958 }
Divyesh Shah812df482010-04-08 21:15:35 -0700959 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
960 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800961 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700962 if (type == BLKIO_STAT_IDLE_TIME)
963 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800964 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700965 if (type == BLKIO_STAT_EMPTY_TIME)
966 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800967 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200968 if (type == BLKIO_STAT_DEQUEUE)
969 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800970 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200971#endif
972
973 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
974 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800975 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
976 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800977 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700978 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800979 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
980 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800981 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
982 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700983 cb->fill(cb, key_str, disk_total);
984 return disk_total;
985}
986
Tejun Heo4bfd4822012-03-05 13:15:08 -0800987static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
988 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800989{
Tejun Heoece84242011-10-19 14:31:15 +0200990 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800991 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800992 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800993 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200994 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200995 int i = 0, ret = -EINVAL;
996 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800997 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200998 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800999
1000 memset(s, 0, sizeof(s));
1001
1002 while ((p = strsep(&buf, " ")) != NULL) {
1003 if (!*p)
1004 continue;
1005
1006 s[i++] = p;
1007
1008 /* Prevent from inputing too many things */
1009 if (i == 3)
1010 break;
1011 }
1012
1013 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001014 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001015
1016 p = strsep(&s[0], ":");
1017 if (p != NULL)
1018 major_s = p;
1019 else
Tejun Heoece84242011-10-19 14:31:15 +02001020 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001021
1022 minor_s = s[0];
1023 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001024 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001025
Tejun Heoece84242011-10-19 14:31:15 +02001026 if (strict_strtoul(major_s, 10, &major))
1027 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001028
Tejun Heoece84242011-10-19 14:31:15 +02001029 if (strict_strtoul(minor_s, 10, &minor))
1030 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001031
1032 dev = MKDEV(major, minor);
1033
Tejun Heoece84242011-10-19 14:31:15 +02001034 if (strict_strtoull(s[1], 10, &temp))
1035 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001036
Tejun Heoe56da7e2012-03-05 13:15:07 -08001037 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001038 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001039 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001040
1041 rcu_read_lock();
1042
Tejun Heo4bfd4822012-03-05 13:15:08 -08001043 spin_lock_irq(disk->queue->queue_lock);
1044 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1045 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001046
Tejun Heo4bfd4822012-03-05 13:15:08 -08001047 if (IS_ERR(blkg)) {
1048 ret = PTR_ERR(blkg);
1049 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001050 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001051
Tejun Heo549d3aa2012-03-05 13:15:16 -08001052 pd = blkg->pd[plid];
1053
Vivek Goyal062a6442010-09-15 17:06:33 -04001054 switch (plid) {
1055 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001056 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1057 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001058 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001059
Tejun Heo549d3aa2012-03-05 13:15:16 -08001060 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001061 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001062 break;
1063 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001064 switch(fileid) {
1065 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001066 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001067 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001068 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001069 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001070 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001071 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001072 break;
1073 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001074 if (temp > THROTL_IOPS_MAX)
1075 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001076 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001077 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001078 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001079 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001080 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001081 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001082 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001083 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001084 break;
1085 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001086 break;
1087 default:
1088 BUG();
1089 }
Tejun Heoece84242011-10-19 14:31:15 +02001090 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001091out_unlock:
1092 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001093out:
1094 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001095
1096 /*
1097 * If queue was bypassing, we should retry. Do so after a short
1098 * msleep(). It isn't strictly necessary but queue can be
1099 * bypassing for some time and it's always nice to avoid busy
1100 * looping.
1101 */
1102 if (ret == -EBUSY) {
1103 msleep(10);
1104 return restart_syscall();
1105 }
Tejun Heoece84242011-10-19 14:31:15 +02001106 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001107}
1108
Vivek Goyal062a6442010-09-15 17:06:33 -04001109static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1110 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001111{
1112 int ret = 0;
1113 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001114 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001115 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1116 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001117
1118 buf = kstrdup(buffer, GFP_KERNEL);
1119 if (!buf)
1120 return -ENOMEM;
1121
Tejun Heo4bfd4822012-03-05 13:15:08 -08001122 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001123 kfree(buf);
1124 return ret;
1125}
1126
Vivek Goyal92616b52012-03-05 13:15:10 -08001127static const char *blkg_dev_name(struct blkio_group *blkg)
1128{
1129 /* some drivers (floppy) instantiate a queue w/o disk registered */
1130 if (blkg->q->backing_dev_info.dev)
1131 return dev_name(blkg->q->backing_dev_info.dev);
1132 return NULL;
1133}
1134
Tejun Heo4bfd4822012-03-05 13:15:08 -08001135static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1136 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001137{
Tejun Heoc1768262012-03-05 13:15:17 -08001138 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001139 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001140 struct blkg_policy_data *pd = blkg->pd[plid];
1141 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001142 int rw = WRITE;
1143
Vivek Goyal92616b52012-03-05 13:15:10 -08001144 if (!dname)
1145 return;
1146
Tejun Heoc1768262012-03-05 13:15:17 -08001147 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001148 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001149 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001150 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001151 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001152 break;
1153 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001154 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001155 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001156 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001157 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001158 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001159 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001160 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001161 break;
1162 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001163 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001164 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001165 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001166 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001167 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001168 break;
1169 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001170 break;
1171 default:
1172 BUG();
1173 }
1174}
1175
1176/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001177static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1178 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001179{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001180 struct blkio_group *blkg;
1181 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001182
Tejun Heo4bfd4822012-03-05 13:15:08 -08001183 spin_lock_irq(&blkcg->lock);
1184 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001185 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001186 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001187}
1188
1189static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1190 struct seq_file *m)
1191{
1192 struct blkio_cgroup *blkcg;
1193 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1194 int name = BLKIOFILE_ATTR(cft->private);
1195
1196 blkcg = cgroup_to_blkio_cgroup(cgrp);
1197
1198 switch(plid) {
1199 case BLKIO_POLICY_PROP:
1200 switch(name) {
1201 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001202 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001203 return 0;
1204 default:
1205 BUG();
1206 }
1207 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001208 case BLKIO_POLICY_THROTL:
1209 switch(name){
1210 case BLKIO_THROTL_read_bps_device:
1211 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001212 case BLKIO_THROTL_read_iops_device:
1213 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001214 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001215 return 0;
1216 default:
1217 BUG();
1218 }
1219 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001220 default:
1221 BUG();
1222 }
1223
1224 return 0;
1225}
1226
1227static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001228 struct cftype *cft, struct cgroup_map_cb *cb,
1229 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001230{
1231 struct blkio_group *blkg;
1232 struct hlist_node *n;
1233 uint64_t cgroup_total = 0;
1234
1235 rcu_read_lock();
1236 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001237 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001238 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001239
Tejun Heoe8989fa2012-03-05 13:15:20 -08001240 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001241 continue;
Tejun Heoc1768262012-03-05 13:15:17 -08001242 if (pcpu) {
1243 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1244 cb, dname, type);
1245 } else {
Tejun Heo7a4dd282012-03-05 13:15:09 -08001246 spin_lock_irq(&blkg->stats_lock);
Tejun Heoc1768262012-03-05 13:15:17 -08001247 cgroup_total += blkio_get_stat(blkg, plid,
1248 cb, dname, type);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001249 spin_unlock_irq(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001250 }
1251 }
1252 if (show_total)
1253 cb->fill(cb, "Total", cgroup_total);
1254 rcu_read_unlock();
1255 return 0;
1256}
1257
1258/* All map kind of cgroup file get serviced by this function */
1259static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1260 struct cgroup_map_cb *cb)
1261{
1262 struct blkio_cgroup *blkcg;
1263 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1264 int name = BLKIOFILE_ATTR(cft->private);
1265
1266 blkcg = cgroup_to_blkio_cgroup(cgrp);
1267
1268 switch(plid) {
1269 case BLKIO_POLICY_PROP:
1270 switch(name) {
1271 case BLKIO_PROP_time:
1272 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001273 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001274 case BLKIO_PROP_sectors:
1275 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001276 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001277 case BLKIO_PROP_io_service_bytes:
1278 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001279 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001280 case BLKIO_PROP_io_serviced:
1281 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001282 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001283 case BLKIO_PROP_io_service_time:
1284 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001285 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001286 case BLKIO_PROP_io_wait_time:
1287 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001288 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001289 case BLKIO_PROP_io_merged:
1290 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001291 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001292 case BLKIO_PROP_io_queued:
1293 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001294 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001295#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001296 case BLKIO_PROP_unaccounted_time:
1297 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001298 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001299 case BLKIO_PROP_dequeue:
1300 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001301 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001302 case BLKIO_PROP_avg_queue_size:
1303 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001304 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001305 case BLKIO_PROP_group_wait_time:
1306 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001307 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001308 case BLKIO_PROP_idle_time:
1309 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001310 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001311 case BLKIO_PROP_empty_time:
1312 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001313 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001314#endif
1315 default:
1316 BUG();
1317 }
1318 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001319 case BLKIO_POLICY_THROTL:
1320 switch(name){
1321 case BLKIO_THROTL_io_service_bytes:
1322 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001323 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001324 case BLKIO_THROTL_io_serviced:
1325 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001326 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001327 default:
1328 BUG();
1329 }
1330 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001331 default:
1332 BUG();
1333 }
1334
1335 return 0;
1336}
1337
Tejun Heo4bfd4822012-03-05 13:15:08 -08001338static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001339{
1340 struct blkio_group *blkg;
1341 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001342
1343 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1344 return -EINVAL;
1345
1346 spin_lock(&blkio_list_lock);
1347 spin_lock_irq(&blkcg->lock);
1348 blkcg->weight = (unsigned int)val;
1349
Tejun Heo549d3aa2012-03-05 13:15:16 -08001350 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001351 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001352
Tejun Heoe8989fa2012-03-05 13:15:20 -08001353 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001354 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001355 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001356
Vivek Goyal062a6442010-09-15 17:06:33 -04001357 spin_unlock_irq(&blkcg->lock);
1358 spin_unlock(&blkio_list_lock);
1359 return 0;
1360}
1361
1362static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1363 struct blkio_cgroup *blkcg;
1364 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1365 int name = BLKIOFILE_ATTR(cft->private);
1366
1367 blkcg = cgroup_to_blkio_cgroup(cgrp);
1368
1369 switch(plid) {
1370 case BLKIO_POLICY_PROP:
1371 switch(name) {
1372 case BLKIO_PROP_weight:
1373 return (u64)blkcg->weight;
1374 }
1375 break;
1376 default:
1377 BUG();
1378 }
1379 return 0;
1380}
1381
1382static int
1383blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1384{
1385 struct blkio_cgroup *blkcg;
1386 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1387 int name = BLKIOFILE_ATTR(cft->private);
1388
1389 blkcg = cgroup_to_blkio_cgroup(cgrp);
1390
1391 switch(plid) {
1392 case BLKIO_POLICY_PROP:
1393 switch(name) {
1394 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001395 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001396 }
1397 break;
1398 default:
1399 BUG();
1400 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001401
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001402 return 0;
1403}
1404
Vivek Goyal31e4c282009-12-03 12:59:42 -05001405struct cftype blkio_files[] = {
1406 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001407 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001408 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1409 BLKIO_PROP_weight_device),
1410 .read_seq_string = blkiocg_file_read,
1411 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001412 .max_write_len = 256,
1413 },
1414 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001415 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001416 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1417 BLKIO_PROP_weight),
1418 .read_u64 = blkiocg_file_read_u64,
1419 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001420 },
Vivek Goyal22084192009-12-03 12:59:49 -05001421 {
1422 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001423 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1424 BLKIO_PROP_time),
1425 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001426 },
1427 {
1428 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001429 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1430 BLKIO_PROP_sectors),
1431 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001432 },
1433 {
1434 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001435 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1436 BLKIO_PROP_io_service_bytes),
1437 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001438 },
1439 {
1440 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001441 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1442 BLKIO_PROP_io_serviced),
1443 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001444 },
1445 {
1446 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001447 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1448 BLKIO_PROP_io_service_time),
1449 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001450 },
1451 {
1452 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001453 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1454 BLKIO_PROP_io_wait_time),
1455 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001456 },
1457 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001458 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001459 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1460 BLKIO_PROP_io_merged),
1461 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001462 },
1463 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001464 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001465 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1466 BLKIO_PROP_io_queued),
1467 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001468 },
1469 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001470 .name = "reset_stats",
1471 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001472 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001473#ifdef CONFIG_BLK_DEV_THROTTLING
1474 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001475 .name = "throttle.read_bps_device",
1476 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1477 BLKIO_THROTL_read_bps_device),
1478 .read_seq_string = blkiocg_file_read,
1479 .write_string = blkiocg_file_write,
1480 .max_write_len = 256,
1481 },
1482
1483 {
1484 .name = "throttle.write_bps_device",
1485 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1486 BLKIO_THROTL_write_bps_device),
1487 .read_seq_string = blkiocg_file_read,
1488 .write_string = blkiocg_file_write,
1489 .max_write_len = 256,
1490 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001491
1492 {
1493 .name = "throttle.read_iops_device",
1494 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1495 BLKIO_THROTL_read_iops_device),
1496 .read_seq_string = blkiocg_file_read,
1497 .write_string = blkiocg_file_write,
1498 .max_write_len = 256,
1499 },
1500
1501 {
1502 .name = "throttle.write_iops_device",
1503 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1504 BLKIO_THROTL_write_iops_device),
1505 .read_seq_string = blkiocg_file_read,
1506 .write_string = blkiocg_file_write,
1507 .max_write_len = 256,
1508 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001509 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001510 .name = "throttle.io_service_bytes",
1511 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1512 BLKIO_THROTL_io_service_bytes),
1513 .read_map = blkiocg_file_read_map,
1514 },
1515 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001516 .name = "throttle.io_serviced",
1517 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1518 BLKIO_THROTL_io_serviced),
1519 .read_map = blkiocg_file_read_map,
1520 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001521#endif /* CONFIG_BLK_DEV_THROTTLING */
1522
Vivek Goyal22084192009-12-03 12:59:49 -05001523#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001524 {
1525 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001526 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1527 BLKIO_PROP_avg_queue_size),
1528 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001529 },
1530 {
Divyesh Shah812df482010-04-08 21:15:35 -07001531 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001532 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1533 BLKIO_PROP_group_wait_time),
1534 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001535 },
1536 {
1537 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001538 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1539 BLKIO_PROP_idle_time),
1540 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001541 },
1542 {
1543 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001544 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1545 BLKIO_PROP_empty_time),
1546 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001547 },
1548 {
Vivek Goyal22084192009-12-03 12:59:49 -05001549 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001550 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1551 BLKIO_PROP_dequeue),
1552 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001553 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001554 {
1555 .name = "unaccounted_time",
1556 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1557 BLKIO_PROP_unaccounted_time),
1558 .read_map = blkiocg_file_read_map,
1559 },
Vivek Goyal22084192009-12-03 12:59:49 -05001560#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001561};
1562
1563static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1564{
1565 return cgroup_add_files(cgroup, subsys, blkio_files,
1566 ARRAY_SIZE(blkio_files));
1567}
1568
Tejun Heo9f13ef62012-03-05 13:15:21 -08001569/**
1570 * blkiocg_pre_destroy - cgroup pre_destroy callback
1571 * @subsys: cgroup subsys
1572 * @cgroup: cgroup of interest
1573 *
1574 * This function is called when @cgroup is about to go away and responsible
1575 * for shooting down all blkgs associated with @cgroup. blkgs should be
1576 * removed while holding both q and blkcg locks. As blkcg lock is nested
1577 * inside q lock, this function performs reverse double lock dancing.
1578 *
1579 * This is the blkcg counterpart of ioc_release_fn().
1580 */
Tejun Heo7ee9c562012-03-05 13:15:11 -08001581static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1582 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001583{
1584 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1585
Vivek Goyalb1c35762009-12-03 12:59:47 -05001586 rcu_read_lock();
Tejun Heo9f13ef62012-03-05 13:15:21 -08001587 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001588
Tejun Heo9f13ef62012-03-05 13:15:21 -08001589 while (!hlist_empty(&blkcg->blkg_list)) {
1590 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1591 struct blkio_group, blkcg_node);
1592 struct request_queue *q = rcu_dereference(blkg->q);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001593
Tejun Heo9f13ef62012-03-05 13:15:21 -08001594 if (spin_trylock(q->queue_lock)) {
1595 blkg_destroy(blkg);
1596 spin_unlock(q->queue_lock);
1597 } else {
1598 spin_unlock_irq(&blkcg->lock);
1599 rcu_read_unlock();
1600 cpu_relax();
1601 rcu_read_lock();
1602 spin_lock(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001603 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001604 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001605
Tejun Heo9f13ef62012-03-05 13:15:21 -08001606 spin_unlock_irq(&blkcg->lock);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001607 rcu_read_unlock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001608 return 0;
1609}
1610
1611static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1612{
1613 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1614
Ben Blum67523c42010-03-10 15:22:11 -08001615 if (blkcg != &blkio_root_cgroup)
1616 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001617}
1618
1619static struct cgroup_subsys_state *
1620blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1621{
Li Zefan03415092010-05-07 08:57:00 +02001622 struct blkio_cgroup *blkcg;
1623 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001624
Li Zefan03415092010-05-07 08:57:00 +02001625 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001626 blkcg = &blkio_root_cgroup;
1627 goto done;
1628 }
1629
Vivek Goyal31e4c282009-12-03 12:59:42 -05001630 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1631 if (!blkcg)
1632 return ERR_PTR(-ENOMEM);
1633
1634 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1635done:
1636 spin_lock_init(&blkcg->lock);
1637 INIT_HLIST_HEAD(&blkcg->blkg_list);
1638
1639 return &blkcg->css;
1640}
1641
Tejun Heo5efd6112012-03-05 13:15:12 -08001642/**
1643 * blkcg_init_queue - initialize blkcg part of request queue
1644 * @q: request_queue to initialize
1645 *
1646 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1647 * part of new request_queue @q.
1648 *
1649 * RETURNS:
1650 * 0 on success, -errno on failure.
1651 */
1652int blkcg_init_queue(struct request_queue *q)
1653{
Tejun Heo923adde2012-03-05 13:15:13 -08001654 int ret;
1655
Tejun Heo5efd6112012-03-05 13:15:12 -08001656 might_sleep();
1657
Tejun Heo923adde2012-03-05 13:15:13 -08001658 ret = blk_throtl_init(q);
1659 if (ret)
1660 return ret;
1661
1662 mutex_lock(&all_q_mutex);
1663 INIT_LIST_HEAD(&q->all_q_node);
1664 list_add_tail(&q->all_q_node, &all_q_list);
1665 mutex_unlock(&all_q_mutex);
1666
1667 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001668}
1669
1670/**
1671 * blkcg_drain_queue - drain blkcg part of request_queue
1672 * @q: request_queue to drain
1673 *
1674 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1675 */
1676void blkcg_drain_queue(struct request_queue *q)
1677{
1678 lockdep_assert_held(q->queue_lock);
1679
1680 blk_throtl_drain(q);
1681}
1682
1683/**
1684 * blkcg_exit_queue - exit and release blkcg part of request_queue
1685 * @q: request_queue being released
1686 *
1687 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1688 */
1689void blkcg_exit_queue(struct request_queue *q)
1690{
Tejun Heo923adde2012-03-05 13:15:13 -08001691 mutex_lock(&all_q_mutex);
1692 list_del_init(&q->all_q_node);
1693 mutex_unlock(&all_q_mutex);
1694
Tejun Heoe8989fa2012-03-05 13:15:20 -08001695 blkg_destroy_all(q, true);
1696
Tejun Heo5efd6112012-03-05 13:15:12 -08001697 blk_throtl_exit(q);
1698}
1699
Vivek Goyal31e4c282009-12-03 12:59:42 -05001700/*
1701 * We cannot support shared io contexts, as we have no mean to support
1702 * two tasks with the same ioc in two different groups without major rework
1703 * of the main cic data structures. For now we allow a task to change
1704 * its cgroup only if it's the only owner of its ioc.
1705 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001706static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1707 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001708{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001709 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001710 struct io_context *ioc;
1711 int ret = 0;
1712
1713 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001714 cgroup_taskset_for_each(task, cgrp, tset) {
1715 task_lock(task);
1716 ioc = task->io_context;
1717 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1718 ret = -EINVAL;
1719 task_unlock(task);
1720 if (ret)
1721 break;
1722 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001723 return ret;
1724}
1725
Tejun Heobb9d97b2011-12-12 18:12:21 -08001726static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1727 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001728{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001729 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001730 struct io_context *ioc;
1731
Tejun Heobb9d97b2011-12-12 18:12:21 -08001732 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001733 /* we don't lose anything even if ioc allocation fails */
1734 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1735 if (ioc) {
1736 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001737 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001738 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001739 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001740}
1741
Tejun Heo923adde2012-03-05 13:15:13 -08001742static void blkcg_bypass_start(void)
1743 __acquires(&all_q_mutex)
1744{
1745 struct request_queue *q;
1746
1747 mutex_lock(&all_q_mutex);
1748
1749 list_for_each_entry(q, &all_q_list, all_q_node) {
1750 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001751 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001752 }
1753}
1754
1755static void blkcg_bypass_end(void)
1756 __releases(&all_q_mutex)
1757{
1758 struct request_queue *q;
1759
1760 list_for_each_entry(q, &all_q_list, all_q_node)
1761 blk_queue_bypass_end(q);
1762
1763 mutex_unlock(&all_q_mutex);
1764}
1765
Vivek Goyal3e252062009-12-04 10:36:42 -05001766void blkio_policy_register(struct blkio_policy_type *blkiop)
1767{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001768 struct request_queue *q;
1769
Tejun Heo923adde2012-03-05 13:15:13 -08001770 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001771 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001772
1773 BUG_ON(blkio_policy[blkiop->plid]);
1774 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001775 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001776
Vivek Goyal3e252062009-12-04 10:36:42 -05001777 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001778 list_for_each_entry(q, &all_q_list, all_q_node)
1779 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001780 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001781}
1782EXPORT_SYMBOL_GPL(blkio_policy_register);
1783
1784void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1785{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001786 struct request_queue *q;
1787
Tejun Heo923adde2012-03-05 13:15:13 -08001788 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001789 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001790
1791 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1792 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001793 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001794
Vivek Goyal3e252062009-12-04 10:36:42 -05001795 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001796 list_for_each_entry(q, &all_q_list, all_q_node)
1797 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001798 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001799}
1800EXPORT_SYMBOL_GPL(blkio_policy_unregister);