blob: cad5f15cf49bd5d0fd4d057f06f41cdeef5260ae [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 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);
614 q->nr_blkgs++;
Tejun Heo03aa2642012-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 Goyalb1c35762009-12-03 12:59:47 -0500623static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
624{
625 hlist_del_init_rcu(&blkg->blkcg_node);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500626}
627
628/*
629 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
630 * indicating that blk_group was unhashed by the time we got to it.
631 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500632int blkiocg_del_blkio_group(struct blkio_group *blkg)
633{
Tejun Heo7ee9c562012-03-05 13:15:11 -0800634 struct blkio_cgroup *blkcg = blkg->blkcg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500635 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500636 int ret = 1;
637
Tejun Heo7ee9c562012-03-05 13:15:11 -0800638 spin_lock_irqsave(&blkcg->lock, flags);
639 if (!hlist_unhashed(&blkg->blkcg_node)) {
640 __blkiocg_del_blkio_group(blkg);
641 ret = 0;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500642 }
Tejun Heo7ee9c562012-03-05 13:15:11 -0800643 spin_unlock_irqrestore(&blkcg->lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200644
Vivek Goyalb1c35762009-12-03 12:59:47 -0500645 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500646}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500647EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500648
649/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800650struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
Tejun Heoe8989fa2012-03-05 13:15:20 -0800651 struct request_queue *q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500652{
653 struct blkio_group *blkg;
654 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500655
Tejun Heoca32aef2012-03-05 13:15:03 -0800656 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -0800657 if (blkg->q == q)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500658 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500659 return NULL;
660}
Tejun Heocd1604f2012-03-05 13:15:06 -0800661EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500662
Tejun Heoe8989fa2012-03-05 13:15:20 -0800663static void blkg_destroy(struct blkio_group *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800664{
Tejun Heo03aa2642012-03-05 13:15:19 -0800665 struct request_queue *q = blkg->q;
666
667 lockdep_assert_held(q->queue_lock);
668
669 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800670 WARN_ON_ONCE(list_empty(&blkg->q_node));
671 list_del_init(&blkg->q_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800672
Tejun Heoe8989fa2012-03-05 13:15:20 -0800673 WARN_ON_ONCE(q->nr_blkgs <= 0);
674 q->nr_blkgs--;
Tejun Heo03aa2642012-03-05 13:15:19 -0800675
676 /*
677 * Put the reference taken at the time of creation so that when all
678 * queues are gone, group can be destroyed.
679 */
680 blkg_put(blkg);
681}
682
Tejun Heoe8989fa2012-03-05 13:15:20 -0800683/*
684 * XXX: This updates blkg policy data in-place for root blkg, which is
685 * necessary across elevator switch and policy registration as root blkgs
686 * aren't shot down. This broken and racy implementation is temporary.
687 * Eventually, blkg shoot down will be replaced by proper in-place update.
688 */
689void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
690{
691 struct blkio_policy_type *pol = blkio_policy[plid];
692 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
693 struct blkg_policy_data *pd;
694
695 if (!blkg)
696 return;
697
698 kfree(blkg->pd[plid]);
699 blkg->pd[plid] = NULL;
700
701 if (!pol)
702 return;
703
704 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
705 WARN_ON_ONCE(!pd);
706
707 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
708 WARN_ON_ONCE(!pd->stats_cpu);
709
710 blkg->pd[plid] = pd;
711 pd->blkg = blkg;
712 pol->ops.blkio_init_group_fn(blkg);
713}
714EXPORT_SYMBOL_GPL(update_root_blkg_pd);
715
716void blkg_destroy_all(struct request_queue *q, bool destroy_root)
Tejun Heo03aa2642012-03-05 13:15:19 -0800717{
718 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800719
720 while (true) {
721 bool done = true;
722
Tejun Heo72e06c22012-03-05 13:15:00 -0800723 spin_lock_irq(q->queue_lock);
724
Tejun Heoe8989fa2012-03-05 13:15:20 -0800725 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo03aa2642012-03-05 13:15:19 -0800726 /* skip root? */
727 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
728 continue;
729
730 /*
731 * If cgroup removal path got to blk_group first
732 * and removed it from cgroup list, then it will
733 * take care of destroying cfqg also.
734 */
735 if (!blkiocg_del_blkio_group(blkg))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800736 blkg_destroy(blkg);
Tejun Heo03aa2642012-03-05 13:15:19 -0800737 else
Tejun Heo72e06c22012-03-05 13:15:00 -0800738 done = false;
Tejun Heo03aa2642012-03-05 13:15:19 -0800739 }
Tejun Heo72e06c22012-03-05 13:15:00 -0800740
741 spin_unlock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800742
Tejun Heo03aa2642012-03-05 13:15:19 -0800743 /*
744 * Group list may not be empty if we raced cgroup removal
745 * and lost. cgroup removal is guaranteed to make forward
746 * progress and retrying after a while is enough. This
747 * ugliness is scheduled to be removed after locking
748 * update.
749 */
Tejun Heo72e06c22012-03-05 13:15:00 -0800750 if (done)
751 break;
752
753 msleep(10); /* just some random duration I like */
754 }
755}
Tejun Heo03aa2642012-03-05 13:15:19 -0800756EXPORT_SYMBOL_GPL(blkg_destroy_all);
Tejun Heo72e06c22012-03-05 13:15:00 -0800757
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800758static void blkg_rcu_free(struct rcu_head *rcu_head)
759{
760 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
761}
762
763void __blkg_release(struct blkio_group *blkg)
764{
765 /* release the extra blkcg reference this blkg has been holding */
766 css_put(&blkg->blkcg->css);
767
768 /*
769 * A group is freed in rcu manner. But having an rcu lock does not
770 * mean that one can access all the fields of blkg and assume these
771 * are valid. For example, don't try to follow throtl_data and
772 * request queue links.
773 *
774 * Having a reference to blkg under an rcu allows acess to only
775 * values local to groups like group stats and group rate limits
776 */
777 call_rcu(&blkg->rcu_head, blkg_rcu_free);
778}
779EXPORT_SYMBOL_GPL(__blkg_release);
780
Tejun Heoc1768262012-03-05 13:15:17 -0800781static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400782{
Tejun Heoc1768262012-03-05 13:15:17 -0800783 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400784 struct blkio_group_stats_cpu *stats_cpu;
785 int i, j, k;
786 /*
787 * Note: On 64 bit arch this should not be an issue. This has the
788 * possibility of returning some inconsistent value on 32bit arch
789 * as 64bit update on 32bit is non atomic. Taking care of this
790 * corner case makes code very complicated, like sending IPIs to
791 * cpus, taking care of stats of offline cpus etc.
792 *
793 * reset stats is anyway more of a debug feature and this sounds a
794 * corner case. So I am not complicating the code yet until and
795 * unless this becomes a real issue.
796 */
797 for_each_possible_cpu(i) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800798 stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400799 stats_cpu->sectors = 0;
800 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
801 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
802 stats_cpu->stat_arr_cpu[j][k] = 0;
803 }
804}
805
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700806static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200807blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700808{
809 struct blkio_cgroup *blkcg;
810 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700811 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700812 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700813 uint64_t queued[BLKIO_STAT_TOTAL];
814 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700815#ifdef CONFIG_DEBUG_BLK_CGROUP
816 bool idling, waiting, empty;
817 unsigned long long now = sched_clock();
818#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700819
820 blkcg = cgroup_to_blkio_cgroup(cgroup);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800821 spin_lock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700822 spin_lock_irq(&blkcg->lock);
823 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -0800824 struct blkio_policy_type *pol;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800825
Tejun Heoe8989fa2012-03-05 13:15:20 -0800826 list_for_each_entry(pol, &blkio_list, list) {
827 struct blkg_policy_data *pd = blkg->pd[pol->plid];
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400828
Tejun Heoe8989fa2012-03-05 13:15:20 -0800829 spin_lock(&blkg->stats_lock);
830 stats = &pd->stats;
831#ifdef CONFIG_DEBUG_BLK_CGROUP
832 idling = blkio_blkg_idling(stats);
833 waiting = blkio_blkg_waiting(stats);
834 empty = blkio_blkg_empty(stats);
835#endif
836 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
837 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
838 memset(stats, 0, sizeof(struct blkio_group_stats));
839 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
840 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
841#ifdef CONFIG_DEBUG_BLK_CGROUP
842 if (idling) {
843 blkio_mark_blkg_idling(stats);
844 stats->start_idle_time = now;
845 }
846 if (waiting) {
847 blkio_mark_blkg_waiting(stats);
848 stats->start_group_wait_time = now;
849 }
850 if (empty) {
851 blkio_mark_blkg_empty(stats);
852 stats->start_empty_time = now;
853 }
854#endif
855 spin_unlock(&blkg->stats_lock);
856
857 /* Reset Per cpu stats which don't take blkg->stats_lock */
858 blkio_reset_stats_cpu(blkg, pol->plid);
859 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700860 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400861
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700862 spin_unlock_irq(&blkcg->lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800863 spin_unlock(&blkio_list_lock);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700864 return 0;
865}
866
Tejun Heo7a4dd282012-03-05 13:15:09 -0800867static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
868 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700869{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800870 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700871 chars_left -= strlen(str);
872 if (chars_left <= 0) {
873 printk(KERN_WARNING
874 "Possibly incorrect cgroup stat display format");
875 return;
876 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200877 if (diskname_only)
878 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700879 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200880 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700881 strlcat(str, " Read", chars_left);
882 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200883 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700884 strlcat(str, " Write", chars_left);
885 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200886 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700887 strlcat(str, " Sync", chars_left);
888 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200889 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700890 strlcat(str, " Async", chars_left);
891 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200892 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700893 strlcat(str, " Total", chars_left);
894 break;
895 default:
896 strlcat(str, " Invalid", chars_left);
897 }
898}
899
Divyesh Shah84c124d2010-04-09 08:31:19 +0200900static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800901 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200902{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800903 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200904 cb->fill(cb, str, val);
905 return val;
906}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700907
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400908
Tejun Heoc1768262012-03-05 13:15:17 -0800909static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400910 enum stat_type_cpu type, enum stat_sub_type sub_type)
911{
Tejun Heoc1768262012-03-05 13:15:17 -0800912 struct blkg_policy_data *pd = blkg->pd[plid];
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400913 int cpu;
914 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400915 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400916
917 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400918 unsigned int start;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800919 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400920
Vivek Goyal575969a2011-05-19 15:38:29 -0400921 do {
922 start = u64_stats_fetch_begin(&stats_cpu->syncp);
923 if (type == BLKIO_STAT_CPU_SECTORS)
924 tval = stats_cpu->sectors;
925 else
926 tval = stats_cpu->stat_arr_cpu[type][sub_type];
927 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
928
929 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400930 }
931
932 return val;
933}
934
Tejun Heoc1768262012-03-05 13:15:17 -0800935static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800936 struct cgroup_map_cb *cb, const char *dname,
937 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400938{
939 uint64_t disk_total, val;
940 char key_str[MAX_KEY_LEN];
941 enum stat_sub_type sub_type;
942
943 if (type == BLKIO_STAT_CPU_SECTORS) {
Tejun Heoc1768262012-03-05 13:15:17 -0800944 val = blkio_read_stat_cpu(blkg, plid, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800945 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
946 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400947 }
948
949 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
950 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800951 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
952 false);
Tejun Heoc1768262012-03-05 13:15:17 -0800953 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400954 cb->fill(cb, key_str, val);
955 }
956
Tejun Heoc1768262012-03-05 13:15:17 -0800957 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
958 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400959
Tejun Heo7a4dd282012-03-05 13:15:09 -0800960 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
961 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400962 cb->fill(cb, key_str, disk_total);
963 return disk_total;
964}
965
Divyesh Shah84c124d2010-04-09 08:31:19 +0200966/* This should be called with blkg->stats_lock held */
Tejun Heoc1768262012-03-05 13:15:17 -0800967static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800968 struct cgroup_map_cb *cb, const char *dname,
969 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700970{
Tejun Heoc1768262012-03-05 13:15:17 -0800971 struct blkg_policy_data *pd = blkg->pd[plid];
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700972 uint64_t disk_total;
973 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200974 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700975
Divyesh Shah84c124d2010-04-09 08:31:19 +0200976 if (type == BLKIO_STAT_TIME)
977 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800978 pd->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100979#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100980 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
981 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800982 pd->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700983 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
Tejun Heo549d3aa2012-03-05 13:15:16 -0800984 uint64_t sum = pd->stats.avg_queue_size_sum;
985 uint64_t samples = pd->stats.avg_queue_size_samples;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700986 if (samples)
987 do_div(sum, samples);
988 else
989 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800990 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
991 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700992 }
Divyesh Shah812df482010-04-08 21:15:35 -0700993 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
994 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800995 pd->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700996 if (type == BLKIO_STAT_IDLE_TIME)
997 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -0800998 pd->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700999 if (type == BLKIO_STAT_EMPTY_TIME)
1000 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001001 pd->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +02001002 if (type == BLKIO_STAT_DEQUEUE)
1003 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo549d3aa2012-03-05 13:15:16 -08001004 pd->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +02001005#endif
1006
1007 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
1008 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -08001009 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
1010 false);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001011 cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001012 }
Tejun Heo549d3aa2012-03-05 13:15:16 -08001013 disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
1014 pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -08001015 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
1016 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001017 cb->fill(cb, key_str, disk_total);
1018 return disk_total;
1019}
1020
Tejun Heo4bfd4822012-03-05 13:15:08 -08001021static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
1022 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001023{
Tejun Heoece84242011-10-19 14:31:15 +02001024 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001025 struct blkio_group *blkg = NULL;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001026 struct blkg_policy_data *pd;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001027 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001028 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +02001029 int i = 0, ret = -EINVAL;
1030 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001031 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001032 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001033
1034 memset(s, 0, sizeof(s));
1035
1036 while ((p = strsep(&buf, " ")) != NULL) {
1037 if (!*p)
1038 continue;
1039
1040 s[i++] = p;
1041
1042 /* Prevent from inputing too many things */
1043 if (i == 3)
1044 break;
1045 }
1046
1047 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +02001048 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001049
1050 p = strsep(&s[0], ":");
1051 if (p != NULL)
1052 major_s = p;
1053 else
Tejun Heoece84242011-10-19 14:31:15 +02001054 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001055
1056 minor_s = s[0];
1057 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +02001058 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001059
Tejun Heoece84242011-10-19 14:31:15 +02001060 if (strict_strtoul(major_s, 10, &major))
1061 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001062
Tejun Heoece84242011-10-19 14:31:15 +02001063 if (strict_strtoul(minor_s, 10, &minor))
1064 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001065
1066 dev = MKDEV(major, minor);
1067
Tejun Heoece84242011-10-19 14:31:15 +02001068 if (strict_strtoull(s[1], 10, &temp))
1069 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001070
Tejun Heoe56da7e2012-03-05 13:15:07 -08001071 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001072 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001073 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001074
1075 rcu_read_lock();
1076
Tejun Heo4bfd4822012-03-05 13:15:08 -08001077 spin_lock_irq(disk->queue->queue_lock);
1078 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1079 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001080
Tejun Heo4bfd4822012-03-05 13:15:08 -08001081 if (IS_ERR(blkg)) {
1082 ret = PTR_ERR(blkg);
1083 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +02001084 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001085
Tejun Heo549d3aa2012-03-05 13:15:16 -08001086 pd = blkg->pd[plid];
1087
Vivek Goyal062a6442010-09-15 17:06:33 -04001088 switch (plid) {
1089 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001090 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1091 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001092 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001093
Tejun Heo549d3aa2012-03-05 13:15:16 -08001094 pd->conf.weight = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001095 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001096 break;
1097 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001098 switch(fileid) {
1099 case BLKIO_THROTL_read_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001100 pd->conf.bps[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001101 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001102 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001103 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001104 pd->conf.bps[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001105 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001106 break;
1107 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -08001108 if (temp > THROTL_IOPS_MAX)
1109 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001110 pd->conf.iops[READ] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001111 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001112 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001113 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +02001114 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -08001115 goto out_unlock;
Tejun Heo549d3aa2012-03-05 13:15:16 -08001116 pd->conf.iops[WRITE] = temp;
Tejun Heoc1768262012-03-05 13:15:17 -08001117 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001118 break;
1119 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001120 break;
1121 default:
1122 BUG();
1123 }
Tejun Heoece84242011-10-19 14:31:15 +02001124 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001125out_unlock:
1126 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +02001127out:
1128 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -08001129
1130 /*
1131 * If queue was bypassing, we should retry. Do so after a short
1132 * msleep(). It isn't strictly necessary but queue can be
1133 * bypassing for some time and it's always nice to avoid busy
1134 * looping.
1135 */
1136 if (ret == -EBUSY) {
1137 msleep(10);
1138 return restart_syscall();
1139 }
Tejun Heoece84242011-10-19 14:31:15 +02001140 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001141}
1142
Vivek Goyal062a6442010-09-15 17:06:33 -04001143static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1144 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001145{
1146 int ret = 0;
1147 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -08001148 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -04001149 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1150 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001151
1152 buf = kstrdup(buffer, GFP_KERNEL);
1153 if (!buf)
1154 return -ENOMEM;
1155
Tejun Heo4bfd4822012-03-05 13:15:08 -08001156 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001157 kfree(buf);
1158 return ret;
1159}
1160
Vivek Goyal92616b52012-03-05 13:15:10 -08001161static const char *blkg_dev_name(struct blkio_group *blkg)
1162{
1163 /* some drivers (floppy) instantiate a queue w/o disk registered */
1164 if (blkg->q->backing_dev_info.dev)
1165 return dev_name(blkg->q->backing_dev_info.dev);
1166 return NULL;
1167}
1168
Tejun Heo4bfd4822012-03-05 13:15:08 -08001169static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1170 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001171{
Tejun Heoc1768262012-03-05 13:15:17 -08001172 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001173 int fileid = BLKIOFILE_ATTR(cft->private);
Tejun Heoc1768262012-03-05 13:15:17 -08001174 struct blkg_policy_data *pd = blkg->pd[plid];
1175 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001176 int rw = WRITE;
1177
Vivek Goyal92616b52012-03-05 13:15:10 -08001178 if (!dname)
1179 return;
1180
Tejun Heoc1768262012-03-05 13:15:17 -08001181 switch (plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -04001182 case BLKIO_POLICY_PROP:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001183 if (pd->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001184 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001185 dname, pd->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001186 break;
1187 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001188 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001189 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001190 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001191 case BLKIO_THROTL_write_bps_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001192 if (pd->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001193 seq_printf(m, "%s\t%llu\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001194 dname, pd->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001195 break;
1196 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001197 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001198 case BLKIO_THROTL_write_iops_device:
Tejun Heo549d3aa2012-03-05 13:15:16 -08001199 if (pd->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -08001200 seq_printf(m, "%s\t%u\n",
Tejun Heo549d3aa2012-03-05 13:15:16 -08001201 dname, pd->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001202 break;
1203 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001204 break;
1205 default:
1206 BUG();
1207 }
1208}
1209
1210/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -08001211static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1212 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001213{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001214 struct blkio_group *blkg;
1215 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001216
Tejun Heo4bfd4822012-03-05 13:15:08 -08001217 spin_lock_irq(&blkcg->lock);
1218 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
Tejun Heoe8989fa2012-03-05 13:15:20 -08001219 blkio_print_group_conf(cft, blkg, m);
Tejun Heo4bfd4822012-03-05 13:15:08 -08001220 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001221}
1222
1223static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1224 struct seq_file *m)
1225{
1226 struct blkio_cgroup *blkcg;
1227 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1228 int name = BLKIOFILE_ATTR(cft->private);
1229
1230 blkcg = cgroup_to_blkio_cgroup(cgrp);
1231
1232 switch(plid) {
1233 case BLKIO_POLICY_PROP:
1234 switch(name) {
1235 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001236 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001237 return 0;
1238 default:
1239 BUG();
1240 }
1241 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001242 case BLKIO_POLICY_THROTL:
1243 switch(name){
1244 case BLKIO_THROTL_read_bps_device:
1245 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001246 case BLKIO_THROTL_read_iops_device:
1247 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001248 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001249 return 0;
1250 default:
1251 BUG();
1252 }
1253 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001254 default:
1255 BUG();
1256 }
1257
1258 return 0;
1259}
1260
1261static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001262 struct cftype *cft, struct cgroup_map_cb *cb,
1263 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001264{
1265 struct blkio_group *blkg;
1266 struct hlist_node *n;
1267 uint64_t cgroup_total = 0;
1268
1269 rcu_read_lock();
1270 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001271 const char *dname = blkg_dev_name(blkg);
Tejun Heoc1768262012-03-05 13:15:17 -08001272 int plid = BLKIOFILE_POLICY(cft->private);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001273
Tejun Heoe8989fa2012-03-05 13:15:20 -08001274 if (!dname)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001275 continue;
Tejun Heoc1768262012-03-05 13:15:17 -08001276 if (pcpu) {
1277 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1278 cb, dname, type);
1279 } else {
Tejun Heo7a4dd282012-03-05 13:15:09 -08001280 spin_lock_irq(&blkg->stats_lock);
Tejun Heoc1768262012-03-05 13:15:17 -08001281 cgroup_total += blkio_get_stat(blkg, plid,
1282 cb, dname, type);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001283 spin_unlock_irq(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001284 }
1285 }
1286 if (show_total)
1287 cb->fill(cb, "Total", cgroup_total);
1288 rcu_read_unlock();
1289 return 0;
1290}
1291
1292/* All map kind of cgroup file get serviced by this function */
1293static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1294 struct cgroup_map_cb *cb)
1295{
1296 struct blkio_cgroup *blkcg;
1297 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1298 int name = BLKIOFILE_ATTR(cft->private);
1299
1300 blkcg = cgroup_to_blkio_cgroup(cgrp);
1301
1302 switch(plid) {
1303 case BLKIO_POLICY_PROP:
1304 switch(name) {
1305 case BLKIO_PROP_time:
1306 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001307 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001308 case BLKIO_PROP_sectors:
1309 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001310 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001311 case BLKIO_PROP_io_service_bytes:
1312 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001313 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001314 case BLKIO_PROP_io_serviced:
1315 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001316 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001317 case BLKIO_PROP_io_service_time:
1318 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001319 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001320 case BLKIO_PROP_io_wait_time:
1321 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001322 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001323 case BLKIO_PROP_io_merged:
1324 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001325 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001326 case BLKIO_PROP_io_queued:
1327 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001328 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001329#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001330 case BLKIO_PROP_unaccounted_time:
1331 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001332 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001333 case BLKIO_PROP_dequeue:
1334 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001335 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001336 case BLKIO_PROP_avg_queue_size:
1337 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001338 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001339 case BLKIO_PROP_group_wait_time:
1340 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001341 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001342 case BLKIO_PROP_idle_time:
1343 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001344 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001345 case BLKIO_PROP_empty_time:
1346 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001347 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001348#endif
1349 default:
1350 BUG();
1351 }
1352 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001353 case BLKIO_POLICY_THROTL:
1354 switch(name){
1355 case BLKIO_THROTL_io_service_bytes:
1356 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001357 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001358 case BLKIO_THROTL_io_serviced:
1359 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001360 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001361 default:
1362 BUG();
1363 }
1364 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001365 default:
1366 BUG();
1367 }
1368
1369 return 0;
1370}
1371
Tejun Heo4bfd4822012-03-05 13:15:08 -08001372static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001373{
1374 struct blkio_group *blkg;
1375 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001376
1377 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1378 return -EINVAL;
1379
1380 spin_lock(&blkio_list_lock);
1381 spin_lock_irq(&blkcg->lock);
1382 blkcg->weight = (unsigned int)val;
1383
Tejun Heo549d3aa2012-03-05 13:15:16 -08001384 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heoe8989fa2012-03-05 13:15:20 -08001385 struct blkg_policy_data *pd = blkg->pd[plid];
Tejun Heo549d3aa2012-03-05 13:15:16 -08001386
Tejun Heoe8989fa2012-03-05 13:15:20 -08001387 if (!pd->conf.weight)
Tejun Heoc1768262012-03-05 13:15:17 -08001388 blkio_update_group_weight(blkg, plid, blkcg->weight);
Tejun Heo549d3aa2012-03-05 13:15:16 -08001389 }
Vivek Goyal062a6442010-09-15 17:06:33 -04001390
Vivek Goyal062a6442010-09-15 17:06:33 -04001391 spin_unlock_irq(&blkcg->lock);
1392 spin_unlock(&blkio_list_lock);
1393 return 0;
1394}
1395
1396static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1397 struct blkio_cgroup *blkcg;
1398 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1399 int name = BLKIOFILE_ATTR(cft->private);
1400
1401 blkcg = cgroup_to_blkio_cgroup(cgrp);
1402
1403 switch(plid) {
1404 case BLKIO_POLICY_PROP:
1405 switch(name) {
1406 case BLKIO_PROP_weight:
1407 return (u64)blkcg->weight;
1408 }
1409 break;
1410 default:
1411 BUG();
1412 }
1413 return 0;
1414}
1415
1416static int
1417blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1418{
1419 struct blkio_cgroup *blkcg;
1420 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1421 int name = BLKIOFILE_ATTR(cft->private);
1422
1423 blkcg = cgroup_to_blkio_cgroup(cgrp);
1424
1425 switch(plid) {
1426 case BLKIO_POLICY_PROP:
1427 switch(name) {
1428 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001429 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001430 }
1431 break;
1432 default:
1433 BUG();
1434 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001435
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001436 return 0;
1437}
1438
Vivek Goyal31e4c282009-12-03 12:59:42 -05001439struct cftype blkio_files[] = {
1440 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001441 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001442 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1443 BLKIO_PROP_weight_device),
1444 .read_seq_string = blkiocg_file_read,
1445 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001446 .max_write_len = 256,
1447 },
1448 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001449 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001450 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1451 BLKIO_PROP_weight),
1452 .read_u64 = blkiocg_file_read_u64,
1453 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001454 },
Vivek Goyal22084192009-12-03 12:59:49 -05001455 {
1456 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001457 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1458 BLKIO_PROP_time),
1459 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001460 },
1461 {
1462 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001463 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1464 BLKIO_PROP_sectors),
1465 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001466 },
1467 {
1468 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001469 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1470 BLKIO_PROP_io_service_bytes),
1471 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001472 },
1473 {
1474 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001475 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1476 BLKIO_PROP_io_serviced),
1477 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001478 },
1479 {
1480 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001481 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1482 BLKIO_PROP_io_service_time),
1483 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001484 },
1485 {
1486 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001487 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1488 BLKIO_PROP_io_wait_time),
1489 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001490 },
1491 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001492 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001493 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1494 BLKIO_PROP_io_merged),
1495 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001496 },
1497 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001498 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001499 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1500 BLKIO_PROP_io_queued),
1501 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001502 },
1503 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001504 .name = "reset_stats",
1505 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001506 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001507#ifdef CONFIG_BLK_DEV_THROTTLING
1508 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001509 .name = "throttle.read_bps_device",
1510 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1511 BLKIO_THROTL_read_bps_device),
1512 .read_seq_string = blkiocg_file_read,
1513 .write_string = blkiocg_file_write,
1514 .max_write_len = 256,
1515 },
1516
1517 {
1518 .name = "throttle.write_bps_device",
1519 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1520 BLKIO_THROTL_write_bps_device),
1521 .read_seq_string = blkiocg_file_read,
1522 .write_string = blkiocg_file_write,
1523 .max_write_len = 256,
1524 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001525
1526 {
1527 .name = "throttle.read_iops_device",
1528 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1529 BLKIO_THROTL_read_iops_device),
1530 .read_seq_string = blkiocg_file_read,
1531 .write_string = blkiocg_file_write,
1532 .max_write_len = 256,
1533 },
1534
1535 {
1536 .name = "throttle.write_iops_device",
1537 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1538 BLKIO_THROTL_write_iops_device),
1539 .read_seq_string = blkiocg_file_read,
1540 .write_string = blkiocg_file_write,
1541 .max_write_len = 256,
1542 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001543 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001544 .name = "throttle.io_service_bytes",
1545 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1546 BLKIO_THROTL_io_service_bytes),
1547 .read_map = blkiocg_file_read_map,
1548 },
1549 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001550 .name = "throttle.io_serviced",
1551 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1552 BLKIO_THROTL_io_serviced),
1553 .read_map = blkiocg_file_read_map,
1554 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001555#endif /* CONFIG_BLK_DEV_THROTTLING */
1556
Vivek Goyal22084192009-12-03 12:59:49 -05001557#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001558 {
1559 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001560 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1561 BLKIO_PROP_avg_queue_size),
1562 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001563 },
1564 {
Divyesh Shah812df482010-04-08 21:15:35 -07001565 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001566 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1567 BLKIO_PROP_group_wait_time),
1568 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001569 },
1570 {
1571 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001572 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1573 BLKIO_PROP_idle_time),
1574 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001575 },
1576 {
1577 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001578 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1579 BLKIO_PROP_empty_time),
1580 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001581 },
1582 {
Vivek Goyal22084192009-12-03 12:59:49 -05001583 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001584 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1585 BLKIO_PROP_dequeue),
1586 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001587 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001588 {
1589 .name = "unaccounted_time",
1590 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1591 BLKIO_PROP_unaccounted_time),
1592 .read_map = blkiocg_file_read_map,
1593 },
Vivek Goyal22084192009-12-03 12:59:49 -05001594#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001595};
1596
1597static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1598{
1599 return cgroup_add_files(cgroup, subsys, blkio_files,
1600 ARRAY_SIZE(blkio_files));
1601}
1602
Tejun Heo7ee9c562012-03-05 13:15:11 -08001603static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1604 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001605{
1606 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001607 unsigned long flags;
1608 struct blkio_group *blkg;
Tejun Heoca32aef2012-03-05 13:15:03 -08001609 struct request_queue *q;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001610
Vivek Goyalb1c35762009-12-03 12:59:47 -05001611 rcu_read_lock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001612
Jens Axboe0f3942a2010-05-03 14:28:55 +02001613 do {
1614 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001615
Jens Axboe0f3942a2010-05-03 14:28:55 +02001616 if (hlist_empty(&blkcg->blkg_list)) {
1617 spin_unlock_irqrestore(&blkcg->lock, flags);
1618 break;
1619 }
1620
1621 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1622 blkcg_node);
Tejun Heoca32aef2012-03-05 13:15:03 -08001623 q = rcu_dereference(blkg->q);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001624 __blkiocg_del_blkio_group(blkg);
1625
Vivek Goyalb1c35762009-12-03 12:59:47 -05001626 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001627
Jens Axboe0f3942a2010-05-03 14:28:55 +02001628 /*
1629 * This blkio_group is being unlinked as associated cgroup is
1630 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001631 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001632 */
1633 spin_lock(&blkio_list_lock);
Tejun Heo03aa2642012-03-05 13:15:19 -08001634 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001635 blkg_destroy(blkg);
Tejun Heo03aa2642012-03-05 13:15:19 -08001636 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001637 spin_unlock(&blkio_list_lock);
1638 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001639
Vivek Goyalb1c35762009-12-03 12:59:47 -05001640 rcu_read_unlock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001641
1642 return 0;
1643}
1644
1645static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1646{
1647 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1648
Ben Blum67523c42010-03-10 15:22:11 -08001649 if (blkcg != &blkio_root_cgroup)
1650 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001651}
1652
1653static struct cgroup_subsys_state *
1654blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1655{
Li Zefan03415092010-05-07 08:57:00 +02001656 struct blkio_cgroup *blkcg;
1657 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001658
Li Zefan03415092010-05-07 08:57:00 +02001659 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001660 blkcg = &blkio_root_cgroup;
1661 goto done;
1662 }
1663
Vivek Goyal31e4c282009-12-03 12:59:42 -05001664 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1665 if (!blkcg)
1666 return ERR_PTR(-ENOMEM);
1667
1668 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1669done:
1670 spin_lock_init(&blkcg->lock);
1671 INIT_HLIST_HEAD(&blkcg->blkg_list);
1672
1673 return &blkcg->css;
1674}
1675
Tejun Heo5efd6112012-03-05 13:15:12 -08001676/**
1677 * blkcg_init_queue - initialize blkcg part of request queue
1678 * @q: request_queue to initialize
1679 *
1680 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1681 * part of new request_queue @q.
1682 *
1683 * RETURNS:
1684 * 0 on success, -errno on failure.
1685 */
1686int blkcg_init_queue(struct request_queue *q)
1687{
Tejun Heo923adde2012-03-05 13:15:13 -08001688 int ret;
1689
Tejun Heo5efd6112012-03-05 13:15:12 -08001690 might_sleep();
1691
Tejun Heo923adde2012-03-05 13:15:13 -08001692 ret = blk_throtl_init(q);
1693 if (ret)
1694 return ret;
1695
1696 mutex_lock(&all_q_mutex);
1697 INIT_LIST_HEAD(&q->all_q_node);
1698 list_add_tail(&q->all_q_node, &all_q_list);
1699 mutex_unlock(&all_q_mutex);
1700
1701 return 0;
Tejun Heo5efd6112012-03-05 13:15:12 -08001702}
1703
1704/**
1705 * blkcg_drain_queue - drain blkcg part of request_queue
1706 * @q: request_queue to drain
1707 *
1708 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1709 */
1710void blkcg_drain_queue(struct request_queue *q)
1711{
1712 lockdep_assert_held(q->queue_lock);
1713
1714 blk_throtl_drain(q);
1715}
1716
1717/**
1718 * blkcg_exit_queue - exit and release blkcg part of request_queue
1719 * @q: request_queue being released
1720 *
1721 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1722 */
1723void blkcg_exit_queue(struct request_queue *q)
1724{
Tejun Heo923adde2012-03-05 13:15:13 -08001725 mutex_lock(&all_q_mutex);
1726 list_del_init(&q->all_q_node);
1727 mutex_unlock(&all_q_mutex);
1728
Tejun Heoe8989fa2012-03-05 13:15:20 -08001729 blkg_destroy_all(q, true);
1730
Tejun Heo5efd6112012-03-05 13:15:12 -08001731 blk_throtl_exit(q);
1732}
1733
Vivek Goyal31e4c282009-12-03 12:59:42 -05001734/*
1735 * We cannot support shared io contexts, as we have no mean to support
1736 * two tasks with the same ioc in two different groups without major rework
1737 * of the main cic data structures. For now we allow a task to change
1738 * its cgroup only if it's the only owner of its ioc.
1739 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001740static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1741 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001742{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001743 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001744 struct io_context *ioc;
1745 int ret = 0;
1746
1747 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001748 cgroup_taskset_for_each(task, cgrp, tset) {
1749 task_lock(task);
1750 ioc = task->io_context;
1751 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1752 ret = -EINVAL;
1753 task_unlock(task);
1754 if (ret)
1755 break;
1756 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001757 return ret;
1758}
1759
Tejun Heobb9d97b2011-12-12 18:12:21 -08001760static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1761 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001762{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001763 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001764 struct io_context *ioc;
1765
Tejun Heobb9d97b2011-12-12 18:12:21 -08001766 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001767 /* we don't lose anything even if ioc allocation fails */
1768 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1769 if (ioc) {
1770 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001771 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001772 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001773 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001774}
1775
Tejun Heo923adde2012-03-05 13:15:13 -08001776static void blkcg_bypass_start(void)
1777 __acquires(&all_q_mutex)
1778{
1779 struct request_queue *q;
1780
1781 mutex_lock(&all_q_mutex);
1782
1783 list_for_each_entry(q, &all_q_list, all_q_node) {
1784 blk_queue_bypass_start(q);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001785 blkg_destroy_all(q, false);
Tejun Heo923adde2012-03-05 13:15:13 -08001786 }
1787}
1788
1789static void blkcg_bypass_end(void)
1790 __releases(&all_q_mutex)
1791{
1792 struct request_queue *q;
1793
1794 list_for_each_entry(q, &all_q_list, all_q_node)
1795 blk_queue_bypass_end(q);
1796
1797 mutex_unlock(&all_q_mutex);
1798}
1799
Vivek Goyal3e252062009-12-04 10:36:42 -05001800void blkio_policy_register(struct blkio_policy_type *blkiop)
1801{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001802 struct request_queue *q;
1803
Tejun Heo923adde2012-03-05 13:15:13 -08001804 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001805 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001806
1807 BUG_ON(blkio_policy[blkiop->plid]);
1808 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001809 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001810
Vivek Goyal3e252062009-12-04 10:36:42 -05001811 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001812 list_for_each_entry(q, &all_q_list, all_q_node)
1813 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001814 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001815}
1816EXPORT_SYMBOL_GPL(blkio_policy_register);
1817
1818void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1819{
Tejun Heoe8989fa2012-03-05 13:15:20 -08001820 struct request_queue *q;
1821
Tejun Heo923adde2012-03-05 13:15:13 -08001822 blkcg_bypass_start();
Vivek Goyal3e252062009-12-04 10:36:42 -05001823 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001824
1825 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1826 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001827 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001828
Vivek Goyal3e252062009-12-04 10:36:42 -05001829 spin_unlock(&blkio_list_lock);
Tejun Heoe8989fa2012-03-05 13:15:20 -08001830 list_for_each_entry(q, &all_q_list, all_q_node)
1831 update_root_blkg_pd(q, blkiop->plid);
Tejun Heo923adde2012-03-05 13:15:13 -08001832 blkcg_bypass_end();
Vivek Goyal3e252062009-12-04 10:36:42 -05001833}
1834EXPORT_SYMBOL_GPL(blkio_policy_unregister);