blob: 27d39a810cb6efa883f7a78d464c9d3250b71dd9 [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);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800503 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 Heo03aa2642012-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);
Tejun Heo03aa2642012-03-05 13:15:19 -0800614
Tejun Heocd1604f2012-03-05 13:15:06 -0800615 spin_unlock(&blkcg->lock);
616out:
Tejun Heo03814112012-03-05 13:15:14 -0800617 blkg_free(new_blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800618 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500619}
Tejun Heocd1604f2012-03-05 13:15:06 -0800620EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500621
Vivek Goyal31e4c282009-12-03 12:59:42 -0500622/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800623struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800624 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500625{
626 struct blkio_group *blkg;
627 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500628
Tejun Heoca32aef2012-03-05 13:15:03 -0800629 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800630 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500631 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500632 return NULL;
633}
Tejun Heocd1604f2012-03-05 13:15:06 -0800634EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500635
Tejun Heoe8989fa2012-03-05 13:15:20 -0800636static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800637{
Tejun Heo03aa2642012-03-05 13:15:19 -0800638 struct request_queue *q = blkg->q;
Tejun Heo9f13ef62012-03-05 13:15:21 -0800639 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo03aa2642012-03-05 13:15:19 -0800640
641 lockdep_assert_held(q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800642 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800643
644 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800645 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800646 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoe8989fa2012-03-05 13:15:20 -0800647 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800648 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800649
Tejun Heo03aa2642012-03-05 13:15:19 -0800650 /*
651 * Put the reference taken at the time of creation so that when all
652 * queues are gone, group can be destroyed.
653 */
654 blkg_put(blkg);
655}
656
Tejun Heoe8989fa2012-03-05 13:15:20 -0800657/*
658 * XXX: This updates blkg policy data in-place for root blkg, which is
659 * necessary across elevator switch and policy registration as root blkgs
660 * aren't shot down. This broken and racy implementation is temporary.
661 * Eventually, blkg shoot down will be replaced by proper in-place update.
662 */
663void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
664{
665 struct blkio_policy_type *pol = blkio_policy[plid];
666 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
667 struct blkg_policy_data *pd;
668
669 if (!blkg)
670 return;
671
672 kfree(blkg->pd[plid]);
673 blkg->pd[plid] = NULL;
674
675 if (!pol)
676 return;
677
678 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
679 WARN_ON_ONCE(!pd);
680
681 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
682 WARN_ON_ONCE(!pd->stats_cpu);
683
684 blkg->pd[plid] = pd;
685 pd->blkg = blkg;
686 pol->ops.blkio_init_group_fn(blkg);
687}
688EXPORT_SYMBOL_GPL(update_root_blkg_pd);
689
Tejun Heo9f13ef62012-03-05 13:15:21 -0800690/**
691 * blkg_destroy_all - destroy all blkgs associated with a request_queue
692 * @q: request_queue of interest
693 * @destroy_root: whether to destroy root blkg or not
694 *
695 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
696 * destroyed; otherwise, root blkg is left alone.
697 */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800698void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800699{
700 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800701
Tejun Heo9f13ef62012-03-05 13:15:21 -0800702 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800703
Tejun Heo9f13ef62012-03-05 13:15:21 -0800704 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
705 struct blkio_cgroup *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800706
Tejun Heo9f13ef62012-03-05 13:15:21 -0800707 /* skip root? */
708 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
709 continue;
Tejun Heo03aa2642012-03-05 13:15:19 -0800710
Tejun Heo9f13ef62012-03-05 13:15:21 -0800711 spin_lock(&blkcg->lock);
712 blkg_destroy(blkg);
713 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800714 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800715
716 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800717}
Tejun Heo03aa2642012-03-05 13:15:19 -0800718EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800719
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800720static void blkg_rcu_free(struct rcu_head *rcu_head)
721{
722 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
723}
724
725void __blkg_release(struct blkio_group *blkg)
726{
727 /* release the extra blkcg reference this blkg has been holding */
728 css_put(&blkg->blkcg->css);
729
730 /*
731 * A group is freed in rcu manner. But having an rcu lock does not
732 * mean that one can access all the fields of blkg and assume these
733 * are valid. For example, don't try to follow throtl_data and
734 * request queue links.
735 *
736 * Having a reference to blkg under an rcu allows acess to only
737 * values local to groups like group stats and group rate limits
738 */
739 call_rcu(&blkg->rcu_head, blkg_rcu_free);
740}
741EXPORT_SYMBOL_GPL(__blkg_release);
742
Tejun Heoc1768262012-03-05 13:15:17 -0800743static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400744{
Tejun Heoc1768262012-03-05 13:15:17 -0800745 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400746 struct blkio_group_stats_cpu *stats_cpu;
747 int i, j, k;
748 /*
749 * Note: On 64 bit arch this should not be an issue. This has the
750 * possibility of returning some inconsistent value on 32bit arch
751 * as 64bit update on 32bit is non atomic. Taking care of this
752 * corner case makes code very complicated, like sending IPIs to
753 * cpus, taking care of stats of offline cpus etc.
754 *
755 * reset stats is anyway more of a debug feature and this sounds a
756 * corner case. So I am not complicating the code yet until and
757 * unless this becomes a real issue.
758 */
759 for_each_possible_cpu(i) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800760 stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400761 stats_cpu->sectors = 0;
762 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
763 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
764 stats_cpu->stat_arr_cpu[j][k] = 0;
765 }
766}
767
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700768static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200769blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700770{
771 struct blkio_cgroup *blkcg;
772 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700773 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700774 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700775 uint64_t queued[BLKIO_STAT_TOTAL];
776 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700777#ifdef CONFIG_DEBUG_BLK_CGROUP
778 bool idling, waiting, empty;
779 unsigned long long now = sched_clock();
780#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700781
782 blkcg = cgroup_to_blkio_cgroup(cgroup);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800783 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700784 spin_lock_irq(&blkcg->lock);
785 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800786 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800787
Tejun Heoe8989fa2012-03-05 13:15:20 -0800788 list_for_each_entry(pol, &blkio_list, list) {
789 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400790
Tejun Heoe8989fa2012-03-05 13:15:20 -0800791 spin_lock(&blkg->stats_lock);
792 stats = &pd->stats;
793#ifdef CONFIG_DEBUG_BLK_CGROUP
794 idling = blkio_blkg_idling(stats);
795 waiting = blkio_blkg_waiting(stats);
796 empty = blkio_blkg_empty(stats);
797#endif
798 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
799 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
800 memset(stats, 0, sizeof(struct blkio_group_stats));
801 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
802 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
803#ifdef CONFIG_DEBUG_BLK_CGROUP
804 if (idling) {
805 blkio_mark_blkg_idling(stats);
806 stats->start_idle_time = now;
807 }
808 if (waiting) {
809 blkio_mark_blkg_waiting(stats);
810 stats->start_group_wait_time = now;
811 }
812 if (empty) {
813 blkio_mark_blkg_empty(stats);
814 stats->start_empty_time = now;
815 }
816#endif
817 spin_unlock(&blkg->stats_lock);
818
819 /* Reset Per cpu stats which don't take blkg->stats_lock */
820 blkio_reset_stats_cpu(blkg, pol->plid);
821 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700822 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400823
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700824 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800825 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700826 return 0;
827}
828
Tejun Heo7a4dd282012-03-05 13:15:09 -0800829static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
830 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700831{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800832 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700833 chars_left -= strlen(str);
834 if (chars_left <= 0) {
835 printk(KERN_WARNING
836 "Possibly incorrect cgroup stat display format");
837 return;
838 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200839 if (diskname_only)
840 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700841 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200842 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700843 strlcat(str, " Read", chars_left);
844 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200845 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700846 strlcat(str, " Write", chars_left);
847 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200848 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700849 strlcat(str, " Sync", chars_left);
850 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200851 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700852 strlcat(str, " Async", chars_left);
853 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200854 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700855 strlcat(str, " Total", chars_left);
856 break;
857 default:
858 strlcat(str, " Invalid", chars_left);
859 }
860}
861
Divyesh Shah84c124d2010-04-09 08:31:19 +0200862static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800863 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200864{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800865 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200866 cb->fill(cb, str, val);
867 return val;
868}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700869
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400870
Tejun Heoc1768262012-03-05 13:15:17 -0800871static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400872 enum stat_type_cpu type, enum stat_sub_type sub_type)
873{
Tejun Heoc1768262012-03-05 13:15:17 -0800874 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400875 int cpu;
876 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400877 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400878
879 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400880 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800881 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400882
Vivek Goyal575969a2011-05-19 15:38:29 -0400883 do {
884 start = u64_stats_fetch_begin(&stats_cpu->syncp);
885 if (type == BLKIO_STAT_CPU_SECTORS)
886 tval = stats_cpu->sectors;
887 else
888 tval = stats_cpu->stat_arr_cpu[type][sub_type];
889 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
890
891 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400892 }
893
894 return val;
895}
896
Tejun Heoc1768262012-03-05 13:15:17 -0800897static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800898 struct cgroup_map_cb *cb, const char *dname,
899 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400900{
901 uint64_t disk_total, val;
902 char key_str[MAX_KEY_LEN];
903 enum stat_sub_type sub_type;
904
905 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800906 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800907 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
908 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400909 }
910
911 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
912 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800913 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
914 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800915 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400916 cb->fill(cb, key_str, val);
917 }
918
Tejun Heoc1768262012-03-05 13:15:17 -0800919 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
920 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400921
Tejun Heo7a4dd282012-03-05 13:15:09 -0800922 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
923 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400924 cb->fill(cb, key_str, disk_total);
925 return disk_total;
926}
927
Divyesh Shah84c124d2010-04-09 08:31:19 +0200928/* This should be called with blkg->stats_lock held */
Tejun Heoc1768262012-03-05 13:15:17 -0800929static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800930 struct cgroup_map_cb *cb, const char *dname,
931 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700932{
Tejun Heoc1768262012-03-05 13:15:17 -0800933 struct blkg_policy_data *pd = blkg->pd[plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700934 uint64_t disk_total;
935 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200936 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700937
Divyesh Shah84c124d2010-04-09 08:31:19 +0200938 if (type == BLKIO_STAT_TIME)
939 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800940 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100941#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100942 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
943 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800944 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700945 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800946 uint64_t sum = pd->stats.avg_queue_size_sum;
947 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700948 if (samples)
949 do_div(sum, samples);
950 else
951 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800952 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
953 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700954 }
Divyesh Shah812df482010-04-08 21:15:35 -0700955 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
956 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800957 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700958 if (type == BLKIO_STAT_IDLE_TIME)
959 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800960 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700961 if (type == BLKIO_STAT_EMPTY_TIME)
962 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800963 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200964 if (type == BLKIO_STAT_DEQUEUE)
965 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800966 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200967#endif
968
969 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
970 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800971 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
972 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -0800973 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700974 }
Tejun Heo549d3aa2012-03-05 13:15:16 -0800975 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
976 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800977 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
978 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700979 cb->fill(cb, key_str, disk_total);
980 return disk_total;
981}
982
Tejun Heo4bfd4822012-03-05 13:15:08 -0800983static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
984 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800985{
Tejun Heoece84242011-10-19 14:31:15 +0200986 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800987 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800988 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800989 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200990 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200991 int i = 0, ret = -EINVAL;
992 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800993 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200994 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800995
996 memset(s, 0, sizeof(s));
997
998 while ((p = strsep(&buf, " ")) != NULL) {
999 if (!*p)
1000 continue;
1001
1002 s[i++] = p;
1003
1004 /* Prevent from inputing too many things */
1005 if (i == 3)
1006 break;
1007 }
1008
1009 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001010 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001011
1012 p = strsep(&s[0], ":");
1013 if (p != NULL)
1014 major_s = p;
1015 else
Tejun Heoece84242011-10-19 14:31:15 +02001016 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001017
1018 minor_s = s[0];
1019 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001020 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001021
Tejun Heoece84242011-10-19 14:31:15 +02001022 if (strict_strtoul(major_s, 10, &major))
1023 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001024
Tejun Heoece84242011-10-19 14:31:15 +02001025 if (strict_strtoul(minor_s, 10, &minor))
1026 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001027
1028 dev = MKDEV(major, minor);
1029
Tejun Heoece84242011-10-19 14:31:15 +02001030 if (strict_strtoull(s[1], 10, &temp))
1031 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001032
Tejun Heoe56da7e2012-03-05 13:15:07 -08001033 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001034 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001035 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001036
1037 rcu_read_lock();
1038
Tejun Heo4bfd4822012-03-05 13:15:08 -08001039 spin_lock_irq(disk->queue->queue_lock);
1040 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1041 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001042
Tejun Heo4bfd4822012-03-05 13:15:08 -08001043 if (IS_ERR(blkg)) {
1044 ret = PTR_ERR(blkg);
1045 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001046 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001047
Tejun Heo549d3aa2012-03-05 13:15:16 -08001048 pd = blkg->pd[plid];
1049
Vivek Goyal062a6442010-09-15 17:06:33 -04001050 switch (plid) {
1051 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001052 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1053 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001054 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001055
Tejun Heo549d3aa2012-03-05 13:15:16 -08001056 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001057 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001058 break;
1059 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001060 switch(fileid) {
1061 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001062 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001063 blkio_update_group_bps(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_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001066 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001067 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001068 break;
1069 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001070 if (temp > THROTL_IOPS_MAX)
1071 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001072 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001073 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001074 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001075 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001076 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001077 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001078 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001079 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001080 break;
1081 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001082 break;
1083 default:
1084 BUG();
1085 }
Tejun Heoece84242011-10-19 14:31:15 +02001086 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001087out_unlock:
1088 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001089out:
1090 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001091
1092 /*
1093 * If queue was bypassing, we should retry. Do so after a short
1094 * msleep(). It isn't strictly necessary but queue can be
1095 * bypassing for some time and it's always nice to avoid busy
1096 * looping.
1097 */
1098 if (ret == -EBUSY) {
1099 msleep(10);
1100 return restart_syscall();
1101 }
Tejun Heoece84242011-10-19 14:31:15 +02001102 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001103}
1104
Vivek Goyal062a6442010-09-15 17:06:33 -04001105static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1106 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001107{
1108 int ret = 0;
1109 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001110 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001111 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1112 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001113
1114 buf = kstrdup(buffer, GFP_KERNEL);
1115 if (!buf)
1116 return -ENOMEM;
1117
Tejun Heo4bfd4822012-03-05 13:15:08 -08001118 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001119 kfree(buf);
1120 return ret;
1121}
1122
Vivek Goyal92616b52012-03-05 13:15:10 -08001123static const char *blkg_dev_name(struct blkio_group *blkg)
1124{
1125 /* some drivers (floppy) instantiate a queue w/o disk registered */
1126 if (blkg->q->backing_dev_info.dev)
1127 return dev_name(blkg->q->backing_dev_info.dev);
1128 return NULL;
1129}
1130
Tejun Heo4bfd4822012-03-05 13:15:08 -08001131static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1132 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001133{
Tejun Heoc1768262012-03-05 13:15:17 -08001134 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001135 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001136 struct blkg_policy_data *pd = blkg->pd[plid];
1137 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001138 int rw = WRITE;
1139
Vivek Goyal92616b52012-03-05 13:15:10 -08001140 if (!dname)
1141 return;
1142
Tejun Heoc1768262012-03-05 13:15:17 -08001143 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001144 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001145 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001146 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001147 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001148 break;
1149 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001150 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001151 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001152 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001153 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001154 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001155 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001156 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001157 break;
1158 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001159 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001160 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001161 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001162 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001163 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001164 break;
1165 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001166 break;
1167 default:
1168 BUG();
1169 }
1170}
1171
1172/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001173static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1174 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001175{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001176 struct blkio_group *blkg;
1177 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001178
Tejun Heo4bfd4822012-03-05 13:15:08 -08001179 spin_lock_irq(&blkcg->lock);
1180 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001181 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001182 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001183}
1184
1185static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1186 struct seq_file *m)
1187{
1188 struct blkio_cgroup *blkcg;
1189 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1190 int name = BLKIOFILE_ATTR(cft->private);
1191
1192 blkcg = cgroup_to_blkio_cgroup(cgrp);
1193
1194 switch(plid) {
1195 case BLKIO_POLICY_PROP:
1196 switch(name) {
1197 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001198 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001199 return 0;
1200 default:
1201 BUG();
1202 }
1203 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001204 case BLKIO_POLICY_THROTL:
1205 switch(name){
1206 case BLKIO_THROTL_read_bps_device:
1207 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001208 case BLKIO_THROTL_read_iops_device:
1209 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001210 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001211 return 0;
1212 default:
1213 BUG();
1214 }
1215 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001216 default:
1217 BUG();
1218 }
1219
1220 return 0;
1221}
1222
1223static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001224 struct cftype *cft, struct cgroup_map_cb *cb,
1225 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001226{
1227 struct blkio_group *blkg;
1228 struct hlist_node *n;
1229 uint64_t cgroup_total = 0;
1230
Tejun Heoc875f4d2012-03-05 13:15:22 -08001231 spin_lock_irq(&blkcg->lock);
1232
1233 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001234 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001235 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001236
Tejun Heoe8989fa2012-03-05 13:15:20 -08001237 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001238 continue;
Tejun Heoc1768262012-03-05 13:15:17 -08001239 if (pcpu) {
1240 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1241 cb, dname, type);
1242 } else {
Tejun Heoc875f4d2012-03-05 13:15:22 -08001243 spin_lock(&blkg->stats_lock);
Tejun Heoc1768262012-03-05 13:15:17 -08001244 cgroup_total += blkio_get_stat(blkg, plid,
1245 cb, dname, type);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001246 spin_unlock(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001247 }
1248 }
1249 if (show_total)
1250 cb->fill(cb, "Total", cgroup_total);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001251
1252 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001253 return 0;
1254}
1255
1256/* All map kind of cgroup file get serviced by this function */
1257static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1258 struct cgroup_map_cb *cb)
1259{
1260 struct blkio_cgroup *blkcg;
1261 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1262 int name = BLKIOFILE_ATTR(cft->private);
1263
1264 blkcg = cgroup_to_blkio_cgroup(cgrp);
1265
1266 switch(plid) {
1267 case BLKIO_POLICY_PROP:
1268 switch(name) {
1269 case BLKIO_PROP_time:
1270 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001271 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001272 case BLKIO_PROP_sectors:
1273 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001274 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001275 case BLKIO_PROP_io_service_bytes:
1276 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001277 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001278 case BLKIO_PROP_io_serviced:
1279 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001280 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001281 case BLKIO_PROP_io_service_time:
1282 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001283 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001284 case BLKIO_PROP_io_wait_time:
1285 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001286 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001287 case BLKIO_PROP_io_merged:
1288 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001289 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001290 case BLKIO_PROP_io_queued:
1291 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001292 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001293#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001294 case BLKIO_PROP_unaccounted_time:
1295 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001296 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001297 case BLKIO_PROP_dequeue:
1298 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001299 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001300 case BLKIO_PROP_avg_queue_size:
1301 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001302 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001303 case BLKIO_PROP_group_wait_time:
1304 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001305 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001306 case BLKIO_PROP_idle_time:
1307 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001308 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001309 case BLKIO_PROP_empty_time:
1310 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001311 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001312#endif
1313 default:
1314 BUG();
1315 }
1316 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001317 case BLKIO_POLICY_THROTL:
1318 switch(name){
1319 case BLKIO_THROTL_io_service_bytes:
1320 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001321 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001322 case BLKIO_THROTL_io_serviced:
1323 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001324 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001325 default:
1326 BUG();
1327 }
1328 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001329 default:
1330 BUG();
1331 }
1332
1333 return 0;
1334}
1335
Tejun Heo4bfd4822012-03-05 13:15:08 -08001336static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001337{
1338 struct blkio_group *blkg;
1339 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001340
1341 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1342 return -EINVAL;
1343
1344 spin_lock(&blkio_list_lock);
1345 spin_lock_irq(&blkcg->lock);
1346 blkcg->weight = (unsigned int)val;
1347
Tejun Heo549d3aa2012-03-05 13:15:16 -08001348 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001349 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001350
Tejun Heoe8989fa2012-03-05 13:15:20 -08001351 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001352 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001353 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001354
Vivek Goyal062a6442010-09-15 17:06:33 -04001355 spin_unlock_irq(&blkcg->lock);
1356 spin_unlock(&blkio_list_lock);
1357 return 0;
1358}
1359
1360static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1361 struct blkio_cgroup *blkcg;
1362 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1363 int name = BLKIOFILE_ATTR(cft->private);
1364
1365 blkcg = cgroup_to_blkio_cgroup(cgrp);
1366
1367 switch(plid) {
1368 case BLKIO_POLICY_PROP:
1369 switch(name) {
1370 case BLKIO_PROP_weight:
1371 return (u64)blkcg->weight;
1372 }
1373 break;
1374 default:
1375 BUG();
1376 }
1377 return 0;
1378}
1379
1380static int
1381blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1382{
1383 struct blkio_cgroup *blkcg;
1384 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1385 int name = BLKIOFILE_ATTR(cft->private);
1386
1387 blkcg = cgroup_to_blkio_cgroup(cgrp);
1388
1389 switch(plid) {
1390 case BLKIO_POLICY_PROP:
1391 switch(name) {
1392 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001393 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001394 }
1395 break;
1396 default:
1397 BUG();
1398 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001399
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001400 return 0;
1401}
1402
Vivek Goyal31e4c282009-12-03 12:59:42 -05001403struct cftype blkio_files[] = {
1404 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001405 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001406 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1407 BLKIO_PROP_weight_device),
1408 .read_seq_string = blkiocg_file_read,
1409 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001410 .max_write_len = 256,
1411 },
1412 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001413 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001414 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1415 BLKIO_PROP_weight),
1416 .read_u64 = blkiocg_file_read_u64,
1417 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001418 },
Vivek Goyal22084192009-12-03 12:59:49 -05001419 {
1420 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001421 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1422 BLKIO_PROP_time),
1423 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001424 },
1425 {
1426 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001427 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1428 BLKIO_PROP_sectors),
1429 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001430 },
1431 {
1432 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001433 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1434 BLKIO_PROP_io_service_bytes),
1435 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001436 },
1437 {
1438 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001439 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1440 BLKIO_PROP_io_serviced),
1441 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001442 },
1443 {
1444 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001445 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1446 BLKIO_PROP_io_service_time),
1447 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001448 },
1449 {
1450 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001451 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1452 BLKIO_PROP_io_wait_time),
1453 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001454 },
1455 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001456 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001457 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1458 BLKIO_PROP_io_merged),
1459 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001460 },
1461 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001462 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001463 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1464 BLKIO_PROP_io_queued),
1465 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001466 },
1467 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001468 .name = "reset_stats",
1469 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001470 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001471#ifdef CONFIG_BLK_DEV_THROTTLING
1472 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001473 .name = "throttle.read_bps_device",
1474 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1475 BLKIO_THROTL_read_bps_device),
1476 .read_seq_string = blkiocg_file_read,
1477 .write_string = blkiocg_file_write,
1478 .max_write_len = 256,
1479 },
1480
1481 {
1482 .name = "throttle.write_bps_device",
1483 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1484 BLKIO_THROTL_write_bps_device),
1485 .read_seq_string = blkiocg_file_read,
1486 .write_string = blkiocg_file_write,
1487 .max_write_len = 256,
1488 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001489
1490 {
1491 .name = "throttle.read_iops_device",
1492 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1493 BLKIO_THROTL_read_iops_device),
1494 .read_seq_string = blkiocg_file_read,
1495 .write_string = blkiocg_file_write,
1496 .max_write_len = 256,
1497 },
1498
1499 {
1500 .name = "throttle.write_iops_device",
1501 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1502 BLKIO_THROTL_write_iops_device),
1503 .read_seq_string = blkiocg_file_read,
1504 .write_string = blkiocg_file_write,
1505 .max_write_len = 256,
1506 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001507 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001508 .name = "throttle.io_service_bytes",
1509 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1510 BLKIO_THROTL_io_service_bytes),
1511 .read_map = blkiocg_file_read_map,
1512 },
1513 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001514 .name = "throttle.io_serviced",
1515 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1516 BLKIO_THROTL_io_serviced),
1517 .read_map = blkiocg_file_read_map,
1518 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001519#endif /* CONFIG_BLK_DEV_THROTTLING */
1520
Vivek Goyal22084192009-12-03 12:59:49 -05001521#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001522 {
1523 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001524 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1525 BLKIO_PROP_avg_queue_size),
1526 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001527 },
1528 {
Divyesh Shah812df482010-04-08 21:15:35 -07001529 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001530 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1531 BLKIO_PROP_group_wait_time),
1532 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001533 },
1534 {
1535 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001536 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1537 BLKIO_PROP_idle_time),
1538 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001539 },
1540 {
1541 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001542 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1543 BLKIO_PROP_empty_time),
1544 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001545 },
1546 {
Vivek Goyal22084192009-12-03 12:59:49 -05001547 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001548 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1549 BLKIO_PROP_dequeue),
1550 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001551 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001552 {
1553 .name = "unaccounted_time",
1554 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1555 BLKIO_PROP_unaccounted_time),
1556 .read_map = blkiocg_file_read_map,
1557 },
Vivek Goyal22084192009-12-03 12:59:49 -05001558#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001559};
1560
1561static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1562{
1563 return cgroup_add_files(cgroup, subsys, blkio_files,
1564 ARRAY_SIZE(blkio_files));
1565}
1566
Tejun Heo9f13ef62012-03-05 13:15:21 -08001567/**
1568 * blkiocg_pre_destroy - cgroup pre_destroy callback
1569 * @subsys: cgroup subsys
1570 * @cgroup: cgroup of interest
1571 *
1572 * This function is called when @cgroup is about to go away and responsible
1573 * for shooting down all blkgs associated with @cgroup. blkgs should be
1574 * removed while holding both q and blkcg locks. As blkcg lock is nested
1575 * inside q lock, this function performs reverse double lock dancing.
1576 *
1577 * This is the blkcg counterpart of ioc_release_fn().
1578 */
Tejun Heo7ee9c562012-03-05 13:15:11 -08001579static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1580 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001581{
1582 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1583
Tejun Heo9f13ef62012-03-05 13:15:21 -08001584 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001585
Tejun Heo9f13ef62012-03-05 13:15:21 -08001586 while (!hlist_empty(&blkcg->blkg_list)) {
1587 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1588 struct blkio_group, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -08001589 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001590
Tejun Heo9f13ef62012-03-05 13:15:21 -08001591 if (spin_trylock(q->queue_lock)) {
1592 blkg_destroy(blkg);
1593 spin_unlock(q->queue_lock);
1594 } else {
1595 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -08001596 cpu_relax();
Tejun Heo9f13ef62012-03-05 13:15:21 -08001597 spin_lock(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001598 }
Tejun Heo9f13ef62012-03-05 13:15:21 -08001599 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001600
Tejun Heo9f13ef62012-03-05 13:15:21 -08001601 spin_unlock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001602 return 0;
1603}
1604
1605static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1606{
1607 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1608
Ben Blum67523c42010-03-10 15:22:11 -08001609 if (blkcg != &blkio_root_cgroup)
1610 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001611}
1612
1613static struct cgroup_subsys_state *
1614blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1615{
Li Zefan03415092010-05-07 08:57:00 +02001616 struct blkio_cgroup *blkcg;
1617 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001618
Li Zefan03415092010-05-07 08:57:00 +02001619 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001620 blkcg = &blkio_root_cgroup;
1621 goto done;
1622 }
1623
Vivek Goyal31e4c282009-12-03 12:59:42 -05001624 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1625 if (!blkcg)
1626 return ERR_PTR(-ENOMEM);
1627
1628 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1629done:
1630 spin_lock_init(&blkcg->lock);
1631 INIT_HLIST_HEAD(&blkcg->blkg_list);
1632
1633 return &blkcg->css;
1634}
1635
Tejun Heo5efd6112012-03-05 13:15:12 -08001636/**
1637 * blkcg_init_queue - initialize blkcg part of request queue
1638 * @q: request_queue to initialize
1639 *
1640 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1641 * part of new request_queue @q.
1642 *
1643 * RETURNS:
1644 * 0 on success, -errno on failure.
1645 */
1646int blkcg_init_queue(struct request_queue *q)
1647{
Tejun Heo923adde2012-03-05 13:15:13 -08001648 int ret;
1649
Tejun Heo5efd6112012-03-05 13:15:12 -08001650 might_sleep();
1651
Tejun Heo923adde2012-03-05 13:15:13 -08001652 ret = blk_throtl_init(q);
1653 if (ret)
1654 return ret;
1655
1656 mutex_lock(&all_q_mutex);
1657 INIT_LIST_HEAD(&q->all_q_node);
1658 list_add_tail(&q->all_q_node, &all_q_list);
1659 mutex_unlock(&all_q_mutex);
1660
1661 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001662}
1663
1664/**
1665 * blkcg_drain_queue - drain blkcg part of request_queue
1666 * @q: request_queue to drain
1667 *
1668 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1669 */
1670void blkcg_drain_queue(struct request_queue *q)
1671{
1672 lockdep_assert_held(q->queue_lock);
1673
1674 blk_throtl_drain(q);
1675}
1676
1677/**
1678 * blkcg_exit_queue - exit and release blkcg part of request_queue
1679 * @q: request_queue being released
1680 *
1681 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1682 */
1683void blkcg_exit_queue(struct request_queue *q)
1684{
Tejun Heo923adde2012-03-05 13:15:13 -08001685 mutex_lock(&all_q_mutex);
1686 list_del_init(&q->all_q_node);
1687 mutex_unlock(&all_q_mutex);
1688
Tejun Heoe8989fa2012-03-05 13:15:20 -08001689 blkg_destroy_all(q, true);
1690
Tejun Heo5efd6112012-03-05 13:15:12 -08001691 blk_throtl_exit(q);
1692}
1693
Vivek Goyal31e4c282009-12-03 12:59:42 -05001694/*
1695 * We cannot support shared io contexts, as we have no mean to support
1696 * two tasks with the same ioc in two different groups without major rework
1697 * of the main cic data structures. For now we allow a task to change
1698 * its cgroup only if it's the only owner of its ioc.
1699 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001700static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1701 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001702{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001703 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001704 struct io_context *ioc;
1705 int ret = 0;
1706
1707 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001708 cgroup_taskset_for_each(task, cgrp, tset) {
1709 task_lock(task);
1710 ioc = task->io_context;
1711 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1712 ret = -EINVAL;
1713 task_unlock(task);
1714 if (ret)
1715 break;
1716 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001717 return ret;
1718}
1719
Tejun Heobb9d97b2011-12-12 18:12:21 -08001720static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1721 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001722{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001723 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001724 struct io_context *ioc;
1725
Tejun Heobb9d97b2011-12-12 18:12:21 -08001726 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001727 /* we don't lose anything even if ioc allocation fails */
1728 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1729 if (ioc) {
1730 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001731 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001732 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001733 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001734}
1735
Tejun Heo923adde2012-03-05 13:15:13 -08001736static void blkcg_bypass_start(void)
1737 __acquires(&all_q_mutex)
1738{
1739 struct request_queue *q;
1740
1741 mutex_lock(&all_q_mutex);
1742
1743 list_for_each_entry(q, &all_q_list, all_q_node) {
1744 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001745 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001746 }
1747}
1748
1749static void blkcg_bypass_end(void)
1750 __releases(&all_q_mutex)
1751{
1752 struct request_queue *q;
1753
1754 list_for_each_entry(q, &all_q_list, all_q_node)
1755 blk_queue_bypass_end(q);
1756
1757 mutex_unlock(&all_q_mutex);
1758}
1759
Vivek Goyal3e252062009-12-04 10:36:42 -05001760void blkio_policy_register(struct blkio_policy_type *blkiop)
1761{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001762 struct request_queue *q;
1763
Tejun Heo923adde2012-03-05 13:15:13 -08001764 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001765 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001766
1767 BUG_ON(blkio_policy[blkiop->plid]);
1768 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001769 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001770
Vivek Goyal3e252062009-12-04 10:36:42 -05001771 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001772 list_for_each_entry(q, &all_q_list, all_q_node)
1773 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001774 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001775}
1776EXPORT_SYMBOL_GPL(blkio_policy_register);
1777
1778void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1779{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001780 struct request_queue *q;
1781
Tejun Heo923adde2012-03-05 13:15:13 -08001782 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001783 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001784
1785 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1786 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001787 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001788
Vivek Goyal3e252062009-12-04 10:36:42 -05001789 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001790 list_for_each_entry(q, &all_q_list, all_q_node)
1791 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001792 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001793}
1794EXPORT_SYMBOL_GPL(blkio_policy_unregister);