blob: d42d826ece39497f61ef7b1d189943662fc33ddb [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"
Vivek Goyal3e252062009-12-04 10:36:42 -050023
Divyesh Shah84c124d2010-04-09 08:31:19 +020024#define MAX_KEY_LEN 100
25
Vivek Goyal3e252062009-12-04 10:36:42 -050026static DEFINE_SPINLOCK(blkio_list_lock);
27static LIST_HEAD(blkio_list);
Vivek Goyalb1c35762009-12-03 12:59:47 -050028
Vivek Goyal31e4c282009-12-03 12:59:42 -050029struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050030EXPORT_SYMBOL_GPL(blkio_root_cgroup);
31
Tejun Heo035d10b2012-03-05 13:15:04 -080032static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
33
Ben Blum67523c42010-03-10 15:22:11 -080034static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
35 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080036static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
37 struct cgroup_taskset *);
38static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
39 struct cgroup_taskset *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080040static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080041static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
42static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
43
Vivek Goyal062a6442010-09-15 17:06:33 -040044/* for encoding cft->private value on file */
45#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
46/* What policy owns the file, proportional or throttle */
47#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
48#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
49
Ben Blum67523c42010-03-10 15:22:11 -080050struct cgroup_subsys blkio_subsys = {
51 .name = "blkio",
52 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080053 .can_attach = blkiocg_can_attach,
54 .attach = blkiocg_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080055 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080056 .destroy = blkiocg_destroy,
57 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080058 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080059 .module = THIS_MODULE,
60};
61EXPORT_SYMBOL_GPL(blkio_subsys);
62
Vivek Goyal31e4c282009-12-03 12:59:42 -050063struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
64{
65 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
66 struct blkio_cgroup, css);
67}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050068EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050069
Vivek Goyal70087dc2011-05-16 15:24:08 +020070struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
71{
72 return container_of(task_subsys_state(tsk, blkio_subsys_id),
73 struct blkio_cgroup, css);
74}
75EXPORT_SYMBOL_GPL(task_blkio_cgroup);
76
Vivek Goyal062a6442010-09-15 17:06:33 -040077static inline void
78blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
79{
80 struct blkio_policy_type *blkiop;
81
82 list_for_each_entry(blkiop, &blkio_list, list) {
83 /* If this policy does not own the blkg, do not send updates */
84 if (blkiop->plid != blkg->plid)
85 continue;
86 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080087 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020088 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040089 }
90}
91
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040092static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
93 int fileid)
94{
95 struct blkio_policy_type *blkiop;
96
97 list_for_each_entry(blkiop, &blkio_list, list) {
98
99 /* If this policy does not own the blkg, do not send updates */
100 if (blkiop->plid != blkg->plid)
101 continue;
102
103 if (fileid == BLKIO_THROTL_read_bps_device
104 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800105 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200106 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400107
108 if (fileid == BLKIO_THROTL_write_bps_device
109 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800110 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200111 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400112 }
113}
114
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400115static inline void blkio_update_group_iops(struct blkio_group *blkg,
116 unsigned int iops, int fileid)
117{
118 struct blkio_policy_type *blkiop;
119
120 list_for_each_entry(blkiop, &blkio_list, list) {
121
122 /* If this policy does not own the blkg, do not send updates */
123 if (blkiop->plid != blkg->plid)
124 continue;
125
126 if (fileid == BLKIO_THROTL_read_iops_device
127 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800128 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200129 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400130
131 if (fileid == BLKIO_THROTL_write_iops_device
132 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800133 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200134 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400135 }
136}
137
Divyesh Shah91952912010-04-01 15:01:41 -0700138/*
139 * Add to the appropriate stat variable depending on the request type.
140 * This should be called with the blkg->stats_lock held.
141 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200142static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
143 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700144{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200145 if (direction)
146 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700147 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200148 stat[BLKIO_STAT_READ] += add;
149 if (sync)
150 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700151 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200152 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700153}
154
Divyesh Shahcdc11842010-04-08 21:15:10 -0700155/*
156 * Decrements the appropriate stat variable if non-zero depending on the
157 * request type. Panics on value being zero.
158 * This should be called with the blkg->stats_lock held.
159 */
160static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
161{
162 if (direction) {
163 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
164 stat[BLKIO_STAT_WRITE]--;
165 } else {
166 BUG_ON(stat[BLKIO_STAT_READ] == 0);
167 stat[BLKIO_STAT_READ]--;
168 }
169 if (sync) {
170 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
171 stat[BLKIO_STAT_SYNC]--;
172 } else {
173 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
174 stat[BLKIO_STAT_ASYNC]--;
175 }
176}
177
178#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700179/* This should be called with the blkg->stats_lock held. */
180static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
181 struct blkio_group *curr_blkg)
182{
183 if (blkio_blkg_waiting(&blkg->stats))
184 return;
185 if (blkg == curr_blkg)
186 return;
187 blkg->stats.start_group_wait_time = sched_clock();
188 blkio_mark_blkg_waiting(&blkg->stats);
189}
190
191/* This should be called with the blkg->stats_lock held. */
192static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
193{
194 unsigned long long now;
195
196 if (!blkio_blkg_waiting(stats))
197 return;
198
199 now = sched_clock();
200 if (time_after64(now, stats->start_group_wait_time))
201 stats->group_wait_time += now - stats->start_group_wait_time;
202 blkio_clear_blkg_waiting(stats);
203}
204
205/* This should be called with the blkg->stats_lock held. */
206static void blkio_end_empty_time(struct blkio_group_stats *stats)
207{
208 unsigned long long now;
209
210 if (!blkio_blkg_empty(stats))
211 return;
212
213 now = sched_clock();
214 if (time_after64(now, stats->start_empty_time))
215 stats->empty_time += now - stats->start_empty_time;
216 blkio_clear_blkg_empty(stats);
217}
218
219void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
220{
221 unsigned long flags;
222
223 spin_lock_irqsave(&blkg->stats_lock, flags);
224 BUG_ON(blkio_blkg_idling(&blkg->stats));
225 blkg->stats.start_idle_time = sched_clock();
226 blkio_mark_blkg_idling(&blkg->stats);
227 spin_unlock_irqrestore(&blkg->stats_lock, flags);
228}
229EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
230
231void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
232{
233 unsigned long flags;
234 unsigned long long now;
235 struct blkio_group_stats *stats;
236
237 spin_lock_irqsave(&blkg->stats_lock, flags);
238 stats = &blkg->stats;
239 if (blkio_blkg_idling(stats)) {
240 now = sched_clock();
241 if (time_after64(now, stats->start_idle_time))
242 stats->idle_time += now - stats->start_idle_time;
243 blkio_clear_blkg_idling(stats);
244 }
245 spin_unlock_irqrestore(&blkg->stats_lock, flags);
246}
247EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
248
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200249void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700250{
251 unsigned long flags;
252 struct blkio_group_stats *stats;
253
254 spin_lock_irqsave(&blkg->stats_lock, flags);
255 stats = &blkg->stats;
256 stats->avg_queue_size_sum +=
257 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
258 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
259 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700260 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700261 spin_unlock_irqrestore(&blkg->stats_lock, flags);
262}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200263EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
264
Vivek Goyale5ff0822010-04-26 19:25:11 +0200265void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200266{
267 unsigned long flags;
268 struct blkio_group_stats *stats;
269
270 spin_lock_irqsave(&blkg->stats_lock, flags);
271 stats = &blkg->stats;
272
273 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
274 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
275 spin_unlock_irqrestore(&blkg->stats_lock, flags);
276 return;
277 }
278
279 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200280 * group is already marked empty. This can happen if cfqq got new
281 * request in parent group and moved to this group while being added
282 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200283 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200284 if(blkio_blkg_empty(stats)) {
285 spin_unlock_irqrestore(&blkg->stats_lock, flags);
286 return;
287 }
288
Divyesh Shah28baf442010-04-14 11:22:38 +0200289 stats->start_empty_time = sched_clock();
290 blkio_mark_blkg_empty(stats);
291 spin_unlock_irqrestore(&blkg->stats_lock, flags);
292}
293EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
294
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200295void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
296 unsigned long dequeue)
297{
298 blkg->stats.dequeue += dequeue;
299}
300EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700301#else
302static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
303 struct blkio_group *curr_blkg) {}
304static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700305#endif
306
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200307void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700308 struct blkio_group *curr_blkg, bool direction,
309 bool sync)
310{
311 unsigned long flags;
312
313 spin_lock_irqsave(&blkg->stats_lock, flags);
314 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
315 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700316 blkio_end_empty_time(&blkg->stats);
317 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700318 spin_unlock_irqrestore(&blkg->stats_lock, flags);
319}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200320EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700321
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200322void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700323 bool direction, bool sync)
324{
325 unsigned long flags;
326
327 spin_lock_irqsave(&blkg->stats_lock, flags);
328 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
329 direction, sync);
330 spin_unlock_irqrestore(&blkg->stats_lock, flags);
331}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200332EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700333
Justin TerAvest167400d2011-03-12 16:54:00 +0100334void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
335 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500336{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700337 unsigned long flags;
338
339 spin_lock_irqsave(&blkg->stats_lock, flags);
340 blkg->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400341#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100342 blkg->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400343#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700344 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500345}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700346EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500347
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400348/*
349 * should be called under rcu read lock or queue lock to make sure blkg pointer
350 * is valid.
351 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200352void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
353 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700354{
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400355 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400356 unsigned long flags;
357
358 /*
359 * Disabling interrupts to provide mutual exclusion between two
360 * writes on same cpu. It probably is not needed for 64bit. Not
361 * optimizing that case yet.
362 */
363 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700364
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400365 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
366
Vivek Goyal575969a2011-05-19 15:38:29 -0400367 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400368 stats_cpu->sectors += bytes >> 9;
369 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
370 1, direction, sync);
371 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
372 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400373 u64_stats_update_end(&stats_cpu->syncp);
374 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700375}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200376EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700377
Divyesh Shah84c124d2010-04-09 08:31:19 +0200378void blkiocg_update_completion_stats(struct blkio_group *blkg,
379 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700380{
381 struct blkio_group_stats *stats;
382 unsigned long flags;
383 unsigned long long now = sched_clock();
384
385 spin_lock_irqsave(&blkg->stats_lock, flags);
386 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200387 if (time_after64(now, io_start_time))
388 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
389 now - io_start_time, direction, sync);
390 if (time_after64(io_start_time, start_time))
391 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
392 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700393 spin_unlock_irqrestore(&blkg->stats_lock, flags);
394}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200395EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700396
Vivek Goyal317389a2011-05-23 10:02:19 +0200397/* Merged stats are per cpu. */
Divyesh Shah812d4022010-04-08 21:14:23 -0700398void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
399 bool sync)
400{
Vivek Goyal317389a2011-05-23 10:02:19 +0200401 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700402 unsigned long flags;
403
Vivek Goyal317389a2011-05-23 10:02:19 +0200404 /*
405 * Disabling interrupts to provide mutual exclusion between two
406 * writes on same cpu. It probably is not needed for 64bit. Not
407 * optimizing that case yet.
408 */
409 local_irq_save(flags);
410
411 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
412
413 u64_stats_update_begin(&stats_cpu->syncp);
414 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
415 direction, sync);
416 u64_stats_update_end(&stats_cpu->syncp);
417 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700418}
419EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
420
Tejun Heocd1604f2012-03-05 13:15:06 -0800421struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
422 struct request_queue *q,
423 enum blkio_policy_id plid,
424 bool for_root)
425 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400426{
Tejun Heocd1604f2012-03-05 13:15:06 -0800427 struct blkio_policy_type *pol = blkio_policy[plid];
428 struct blkio_group *blkg, *new_blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400429
Tejun Heocd1604f2012-03-05 13:15:06 -0800430 WARN_ON_ONCE(!rcu_read_lock_held());
431 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500432
Tejun Heocd1604f2012-03-05 13:15:06 -0800433 /*
434 * This could be the first entry point of blkcg implementation and
435 * we shouldn't allow anything to go through for a bypassing queue.
436 * The following can be removed if blkg lookup is guaranteed to
437 * fail on a bypassing queue.
438 */
439 if (unlikely(blk_queue_bypass(q)) && !for_root)
440 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
441
442 blkg = blkg_lookup(blkcg, q, plid);
443 if (blkg)
444 return blkg;
445
Tejun Heo7ee9c562012-03-05 13:15:11 -0800446 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800447 if (!css_tryget(&blkcg->css))
448 return ERR_PTR(-EINVAL);
449
450 /*
451 * Allocate and initialize.
452 *
453 * FIXME: The following is broken. Percpu memory allocation
454 * requires %GFP_KERNEL context and can't be performed from IO
455 * path. Allocation here should inherently be atomic and the
456 * following lock dancing can be removed once the broken percpu
457 * allocation is fixed.
458 */
459 spin_unlock_irq(q->queue_lock);
460 rcu_read_unlock();
461
462 new_blkg = pol->ops.blkio_alloc_group_fn(q, blkcg);
463 if (new_blkg) {
464 new_blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
465
466 spin_lock_init(&new_blkg->stats_lock);
467 rcu_assign_pointer(new_blkg->q, q);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800468 new_blkg->blkcg = blkcg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800469 new_blkg->plid = plid;
470 cgroup_path(blkcg->css.cgroup, new_blkg->path,
471 sizeof(new_blkg->path));
Tejun Heo7ee9c562012-03-05 13:15:11 -0800472 } else {
473 css_put(&blkcg->css);
Tejun Heocd1604f2012-03-05 13:15:06 -0800474 }
475
476 rcu_read_lock();
477 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800478
479 /* did bypass get turned on inbetween? */
480 if (unlikely(blk_queue_bypass(q)) && !for_root) {
481 blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
482 goto out;
483 }
484
485 /* did someone beat us to it? */
486 blkg = blkg_lookup(blkcg, q, plid);
487 if (unlikely(blkg))
488 goto out;
489
490 /* did alloc fail? */
491 if (unlikely(!new_blkg || !new_blkg->stats_cpu)) {
492 blkg = ERR_PTR(-ENOMEM);
493 goto out;
494 }
495
496 /* insert */
497 spin_lock(&blkcg->lock);
498 swap(blkg, new_blkg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500499 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800500 pol->ops.blkio_link_group_fn(q, blkg);
501 spin_unlock(&blkcg->lock);
502out:
503 if (new_blkg) {
504 free_percpu(new_blkg->stats_cpu);
505 kfree(new_blkg);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800506 css_put(&blkcg->css);
Tejun Heocd1604f2012-03-05 13:15:06 -0800507 }
508 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500509}
Tejun Heocd1604f2012-03-05 13:15:06 -0800510EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500511
Vivek Goyalb1c35762009-12-03 12:59:47 -0500512static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
513{
514 hlist_del_init_rcu(&blkg->blkcg_node);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500515}
516
517/*
518 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
519 * indicating that blk_group was unhashed by the time we got to it.
520 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500521int blkiocg_del_blkio_group(struct blkio_group *blkg)
522{
Tejun Heo7ee9c562012-03-05 13:15:11 -0800523 struct blkio_cgroup *blkcg = blkg->blkcg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500524 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500525 int ret = 1;
526
Tejun Heo7ee9c562012-03-05 13:15:11 -0800527 spin_lock_irqsave(&blkcg->lock, flags);
528 if (!hlist_unhashed(&blkg->blkcg_node)) {
529 __blkiocg_del_blkio_group(blkg);
530 ret = 0;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500531 }
Tejun Heo7ee9c562012-03-05 13:15:11 -0800532 spin_unlock_irqrestore(&blkcg->lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200533
Vivek Goyalb1c35762009-12-03 12:59:47 -0500534 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500535}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500536EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500537
538/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800539struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
540 struct request_queue *q,
541 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500542{
543 struct blkio_group *blkg;
544 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500545
Tejun Heoca32aef2012-03-05 13:15:03 -0800546 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
547 if (blkg->q == q && blkg->plid == plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500548 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500549 return NULL;
550}
Tejun Heocd1604f2012-03-05 13:15:06 -0800551EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500552
Tejun Heo72e06c22012-03-05 13:15:00 -0800553void blkg_destroy_all(struct request_queue *q)
554{
555 struct blkio_policy_type *pol;
556
557 while (true) {
558 bool done = true;
559
560 spin_lock(&blkio_list_lock);
561 spin_lock_irq(q->queue_lock);
562
563 /*
564 * clear_queue_fn() might return with non-empty group list
565 * if it raced cgroup removal and lost. cgroup removal is
566 * guaranteed to make forward progress and retrying after a
567 * while is enough. This ugliness is scheduled to be
568 * removed after locking update.
569 */
570 list_for_each_entry(pol, &blkio_list, list)
571 if (!pol->ops.blkio_clear_queue_fn(q))
572 done = false;
573
574 spin_unlock_irq(q->queue_lock);
575 spin_unlock(&blkio_list_lock);
576
577 if (done)
578 break;
579
580 msleep(10); /* just some random duration I like */
581 }
582}
583
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400584static void blkio_reset_stats_cpu(struct blkio_group *blkg)
585{
586 struct blkio_group_stats_cpu *stats_cpu;
587 int i, j, k;
588 /*
589 * Note: On 64 bit arch this should not be an issue. This has the
590 * possibility of returning some inconsistent value on 32bit arch
591 * as 64bit update on 32bit is non atomic. Taking care of this
592 * corner case makes code very complicated, like sending IPIs to
593 * cpus, taking care of stats of offline cpus etc.
594 *
595 * reset stats is anyway more of a debug feature and this sounds a
596 * corner case. So I am not complicating the code yet until and
597 * unless this becomes a real issue.
598 */
599 for_each_possible_cpu(i) {
600 stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
601 stats_cpu->sectors = 0;
602 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
603 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
604 stats_cpu->stat_arr_cpu[j][k] = 0;
605 }
606}
607
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700608static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200609blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700610{
611 struct blkio_cgroup *blkcg;
612 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700613 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700614 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700615 uint64_t queued[BLKIO_STAT_TOTAL];
616 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700617#ifdef CONFIG_DEBUG_BLK_CGROUP
618 bool idling, waiting, empty;
619 unsigned long long now = sched_clock();
620#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700621
622 blkcg = cgroup_to_blkio_cgroup(cgroup);
623 spin_lock_irq(&blkcg->lock);
624 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
625 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700626 stats = &blkg->stats;
627#ifdef CONFIG_DEBUG_BLK_CGROUP
628 idling = blkio_blkg_idling(stats);
629 waiting = blkio_blkg_waiting(stats);
630 empty = blkio_blkg_empty(stats);
631#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700632 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700633 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
634 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700635 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700636 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
637#ifdef CONFIG_DEBUG_BLK_CGROUP
638 if (idling) {
639 blkio_mark_blkg_idling(stats);
640 stats->start_idle_time = now;
641 }
642 if (waiting) {
643 blkio_mark_blkg_waiting(stats);
644 stats->start_group_wait_time = now;
645 }
646 if (empty) {
647 blkio_mark_blkg_empty(stats);
648 stats->start_empty_time = now;
649 }
650#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700651 spin_unlock(&blkg->stats_lock);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400652
653 /* Reset Per cpu stats which don't take blkg->stats_lock */
654 blkio_reset_stats_cpu(blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700655 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400656
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700657 spin_unlock_irq(&blkcg->lock);
658 return 0;
659}
660
Tejun Heo7a4dd282012-03-05 13:15:09 -0800661static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
662 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700663{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800664 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700665 chars_left -= strlen(str);
666 if (chars_left <= 0) {
667 printk(KERN_WARNING
668 "Possibly incorrect cgroup stat display format");
669 return;
670 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200671 if (diskname_only)
672 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700673 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200674 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700675 strlcat(str, " Read", chars_left);
676 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200677 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700678 strlcat(str, " Write", chars_left);
679 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200680 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700681 strlcat(str, " Sync", chars_left);
682 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200683 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700684 strlcat(str, " Async", chars_left);
685 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200686 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700687 strlcat(str, " Total", chars_left);
688 break;
689 default:
690 strlcat(str, " Invalid", chars_left);
691 }
692}
693
Divyesh Shah84c124d2010-04-09 08:31:19 +0200694static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800695 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200696{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800697 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200698 cb->fill(cb, str, val);
699 return val;
700}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700701
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400702
703static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
704 enum stat_type_cpu type, enum stat_sub_type sub_type)
705{
706 int cpu;
707 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400708 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400709
710 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400711 unsigned int start;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400712 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
713
Vivek Goyal575969a2011-05-19 15:38:29 -0400714 do {
715 start = u64_stats_fetch_begin(&stats_cpu->syncp);
716 if (type == BLKIO_STAT_CPU_SECTORS)
717 tval = stats_cpu->sectors;
718 else
719 tval = stats_cpu->stat_arr_cpu[type][sub_type];
720 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
721
722 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400723 }
724
725 return val;
726}
727
728static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800729 struct cgroup_map_cb *cb, const char *dname,
730 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400731{
732 uint64_t disk_total, val;
733 char key_str[MAX_KEY_LEN];
734 enum stat_sub_type sub_type;
735
736 if (type == BLKIO_STAT_CPU_SECTORS) {
737 val = blkio_read_stat_cpu(blkg, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800738 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
739 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400740 }
741
742 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
743 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800744 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
745 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400746 val = blkio_read_stat_cpu(blkg, type, sub_type);
747 cb->fill(cb, key_str, val);
748 }
749
750 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
751 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
752
Tejun Heo7a4dd282012-03-05 13:15:09 -0800753 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
754 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400755 cb->fill(cb, key_str, disk_total);
756 return disk_total;
757}
758
Divyesh Shah84c124d2010-04-09 08:31:19 +0200759/* This should be called with blkg->stats_lock held */
760static uint64_t blkio_get_stat(struct blkio_group *blkg,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800761 struct cgroup_map_cb *cb, const char *dname,
762 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700763{
764 uint64_t disk_total;
765 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200766 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700767
Divyesh Shah84c124d2010-04-09 08:31:19 +0200768 if (type == BLKIO_STAT_TIME)
769 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800770 blkg->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100771#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100772 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
773 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800774 blkg->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700775 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
776 uint64_t sum = blkg->stats.avg_queue_size_sum;
777 uint64_t samples = blkg->stats.avg_queue_size_samples;
778 if (samples)
779 do_div(sum, samples);
780 else
781 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800782 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
783 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700784 }
Divyesh Shah812df482010-04-08 21:15:35 -0700785 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
786 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800787 blkg->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700788 if (type == BLKIO_STAT_IDLE_TIME)
789 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800790 blkg->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700791 if (type == BLKIO_STAT_EMPTY_TIME)
792 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800793 blkg->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200794 if (type == BLKIO_STAT_DEQUEUE)
795 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800796 blkg->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200797#endif
798
799 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
800 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800801 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
802 false);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200803 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700804 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200805 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
806 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800807 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
808 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700809 cb->fill(cb, key_str, disk_total);
810 return disk_total;
811}
812
Tejun Heo4bfd4822012-03-05 13:15:08 -0800813static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
814 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800815{
Tejun Heoece84242011-10-19 14:31:15 +0200816 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800817 struct blkio_group *blkg = NULL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800818 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200819 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200820 int i = 0, ret = -EINVAL;
821 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800822 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200823 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800824
825 memset(s, 0, sizeof(s));
826
827 while ((p = strsep(&buf, " ")) != NULL) {
828 if (!*p)
829 continue;
830
831 s[i++] = p;
832
833 /* Prevent from inputing too many things */
834 if (i == 3)
835 break;
836 }
837
838 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +0200839 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800840
841 p = strsep(&s[0], ":");
842 if (p != NULL)
843 major_s = p;
844 else
Tejun Heoece84242011-10-19 14:31:15 +0200845 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800846
847 minor_s = s[0];
848 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +0200849 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800850
Tejun Heoece84242011-10-19 14:31:15 +0200851 if (strict_strtoul(major_s, 10, &major))
852 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800853
Tejun Heoece84242011-10-19 14:31:15 +0200854 if (strict_strtoul(minor_s, 10, &minor))
855 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800856
857 dev = MKDEV(major, minor);
858
Tejun Heoece84242011-10-19 14:31:15 +0200859 if (strict_strtoull(s[1], 10, &temp))
860 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200861
Tejun Heoe56da7e2012-03-05 13:15:07 -0800862 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800863 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800864 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800865
866 rcu_read_lock();
867
Tejun Heo4bfd4822012-03-05 13:15:08 -0800868 spin_lock_irq(disk->queue->queue_lock);
869 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
870 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800871
Tejun Heo4bfd4822012-03-05 13:15:08 -0800872 if (IS_ERR(blkg)) {
873 ret = PTR_ERR(blkg);
874 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200875 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800876
Vivek Goyal062a6442010-09-15 17:06:33 -0400877 switch (plid) {
878 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200879 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
880 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800881 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800882
Tejun Heo4bfd4822012-03-05 13:15:08 -0800883 blkg->conf.weight = temp;
884 blkio_update_group_weight(blkg, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400885 break;
886 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400887 switch(fileid) {
888 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800889 blkg->conf.bps[READ] = temp;
890 blkio_update_group_bps(blkg, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800891 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400892 case BLKIO_THROTL_write_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800893 blkg->conf.bps[WRITE] = temp;
894 blkio_update_group_bps(blkg, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400895 break;
896 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -0800897 if (temp > THROTL_IOPS_MAX)
898 goto out_unlock;
Tejun Heo4bfd4822012-03-05 13:15:08 -0800899 blkg->conf.iops[READ] = temp;
900 blkio_update_group_iops(blkg, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800901 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400902 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200903 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800904 goto out_unlock;
Tejun Heo4bfd4822012-03-05 13:15:08 -0800905 blkg->conf.iops[WRITE] = temp;
906 blkio_update_group_iops(blkg, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400907 break;
908 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400909 break;
910 default:
911 BUG();
912 }
Tejun Heoece84242011-10-19 14:31:15 +0200913 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800914out_unlock:
915 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +0200916out:
917 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800918
919 /*
920 * If queue was bypassing, we should retry. Do so after a short
921 * msleep(). It isn't strictly necessary but queue can be
922 * bypassing for some time and it's always nice to avoid busy
923 * looping.
924 */
925 if (ret == -EBUSY) {
926 msleep(10);
927 return restart_syscall();
928 }
Tejun Heoece84242011-10-19 14:31:15 +0200929 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800930}
931
Vivek Goyal062a6442010-09-15 17:06:33 -0400932static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
933 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800934{
935 int ret = 0;
936 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800937 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -0400938 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
939 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800940
941 buf = kstrdup(buffer, GFP_KERNEL);
942 if (!buf)
943 return -ENOMEM;
944
Tejun Heo4bfd4822012-03-05 13:15:08 -0800945 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800946 kfree(buf);
947 return ret;
948}
949
Vivek Goyal92616b52012-03-05 13:15:10 -0800950static const char *blkg_dev_name(struct blkio_group *blkg)
951{
952 /* some drivers (floppy) instantiate a queue w/o disk registered */
953 if (blkg->q->backing_dev_info.dev)
954 return dev_name(blkg->q->backing_dev_info.dev);
955 return NULL;
956}
957
Tejun Heo4bfd4822012-03-05 13:15:08 -0800958static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
959 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800960{
Vivek Goyal92616b52012-03-05 13:15:10 -0800961 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800962 int fileid = BLKIOFILE_ATTR(cft->private);
963 int rw = WRITE;
964
Vivek Goyal92616b52012-03-05 13:15:10 -0800965 if (!dname)
966 return;
967
Tejun Heo4bfd4822012-03-05 13:15:08 -0800968 switch (blkg->plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400969 case BLKIO_POLICY_PROP:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800970 if (blkg->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -0800971 seq_printf(m, "%s\t%u\n",
972 dname, blkg->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400973 break;
974 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800975 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400976 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800977 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400978 case BLKIO_THROTL_write_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800979 if (blkg->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -0800980 seq_printf(m, "%s\t%llu\n",
981 dname, blkg->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400982 break;
983 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800984 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400985 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800986 if (blkg->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -0800987 seq_printf(m, "%s\t%u\n",
988 dname, blkg->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400989 break;
990 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400991 break;
992 default:
993 BUG();
994 }
995}
996
997/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -0800998static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
999 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001000{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001001 struct blkio_group *blkg;
1002 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001003
Tejun Heo4bfd4822012-03-05 13:15:08 -08001004 spin_lock_irq(&blkcg->lock);
1005 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
1006 if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
1007 blkio_print_group_conf(cft, blkg, m);
1008 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001009}
1010
1011static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1012 struct seq_file *m)
1013{
1014 struct blkio_cgroup *blkcg;
1015 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1016 int name = BLKIOFILE_ATTR(cft->private);
1017
1018 blkcg = cgroup_to_blkio_cgroup(cgrp);
1019
1020 switch(plid) {
1021 case BLKIO_POLICY_PROP:
1022 switch(name) {
1023 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001024 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001025 return 0;
1026 default:
1027 BUG();
1028 }
1029 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001030 case BLKIO_POLICY_THROTL:
1031 switch(name){
1032 case BLKIO_THROTL_read_bps_device:
1033 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001034 case BLKIO_THROTL_read_iops_device:
1035 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001036 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001037 return 0;
1038 default:
1039 BUG();
1040 }
1041 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001042 default:
1043 BUG();
1044 }
1045
1046 return 0;
1047}
1048
1049static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001050 struct cftype *cft, struct cgroup_map_cb *cb,
1051 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001052{
1053 struct blkio_group *blkg;
1054 struct hlist_node *n;
1055 uint64_t cgroup_total = 0;
1056
1057 rcu_read_lock();
1058 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001059 const char *dname = blkg_dev_name(blkg);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001060
Vivek Goyal92616b52012-03-05 13:15:10 -08001061 if (!dname || BLKIOFILE_POLICY(cft->private) != blkg->plid)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001062 continue;
1063 if (pcpu)
1064 cgroup_total += blkio_get_stat_cpu(blkg, cb, dname,
1065 type);
1066 else {
1067 spin_lock_irq(&blkg->stats_lock);
1068 cgroup_total += blkio_get_stat(blkg, cb, dname, type);
1069 spin_unlock_irq(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001070 }
1071 }
1072 if (show_total)
1073 cb->fill(cb, "Total", cgroup_total);
1074 rcu_read_unlock();
1075 return 0;
1076}
1077
1078/* All map kind of cgroup file get serviced by this function */
1079static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1080 struct cgroup_map_cb *cb)
1081{
1082 struct blkio_cgroup *blkcg;
1083 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1084 int name = BLKIOFILE_ATTR(cft->private);
1085
1086 blkcg = cgroup_to_blkio_cgroup(cgrp);
1087
1088 switch(plid) {
1089 case BLKIO_POLICY_PROP:
1090 switch(name) {
1091 case BLKIO_PROP_time:
1092 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001093 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001094 case BLKIO_PROP_sectors:
1095 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001096 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001097 case BLKIO_PROP_io_service_bytes:
1098 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001099 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001100 case BLKIO_PROP_io_serviced:
1101 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001102 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001103 case BLKIO_PROP_io_service_time:
1104 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001105 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001106 case BLKIO_PROP_io_wait_time:
1107 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001108 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001109 case BLKIO_PROP_io_merged:
1110 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001111 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001112 case BLKIO_PROP_io_queued:
1113 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001114 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001115#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001116 case BLKIO_PROP_unaccounted_time:
1117 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001118 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001119 case BLKIO_PROP_dequeue:
1120 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001121 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001122 case BLKIO_PROP_avg_queue_size:
1123 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001124 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001125 case BLKIO_PROP_group_wait_time:
1126 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001127 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001128 case BLKIO_PROP_idle_time:
1129 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001130 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001131 case BLKIO_PROP_empty_time:
1132 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001133 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001134#endif
1135 default:
1136 BUG();
1137 }
1138 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001139 case BLKIO_POLICY_THROTL:
1140 switch(name){
1141 case BLKIO_THROTL_io_service_bytes:
1142 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001143 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001144 case BLKIO_THROTL_io_serviced:
1145 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001146 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001147 default:
1148 BUG();
1149 }
1150 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001151 default:
1152 BUG();
1153 }
1154
1155 return 0;
1156}
1157
Tejun Heo4bfd4822012-03-05 13:15:08 -08001158static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001159{
1160 struct blkio_group *blkg;
1161 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001162
1163 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1164 return -EINVAL;
1165
1166 spin_lock(&blkio_list_lock);
1167 spin_lock_irq(&blkcg->lock);
1168 blkcg->weight = (unsigned int)val;
1169
Tejun Heo4bfd4822012-03-05 13:15:08 -08001170 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
1171 if (blkg->plid == plid && !blkg->conf.weight)
1172 blkio_update_group_weight(blkg, blkcg->weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001173
Vivek Goyal062a6442010-09-15 17:06:33 -04001174 spin_unlock_irq(&blkcg->lock);
1175 spin_unlock(&blkio_list_lock);
1176 return 0;
1177}
1178
1179static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1180 struct blkio_cgroup *blkcg;
1181 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1182 int name = BLKIOFILE_ATTR(cft->private);
1183
1184 blkcg = cgroup_to_blkio_cgroup(cgrp);
1185
1186 switch(plid) {
1187 case BLKIO_POLICY_PROP:
1188 switch(name) {
1189 case BLKIO_PROP_weight:
1190 return (u64)blkcg->weight;
1191 }
1192 break;
1193 default:
1194 BUG();
1195 }
1196 return 0;
1197}
1198
1199static int
1200blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1201{
1202 struct blkio_cgroup *blkcg;
1203 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1204 int name = BLKIOFILE_ATTR(cft->private);
1205
1206 blkcg = cgroup_to_blkio_cgroup(cgrp);
1207
1208 switch(plid) {
1209 case BLKIO_POLICY_PROP:
1210 switch(name) {
1211 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001212 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001213 }
1214 break;
1215 default:
1216 BUG();
1217 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001218
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001219 return 0;
1220}
1221
Vivek Goyal31e4c282009-12-03 12:59:42 -05001222struct cftype blkio_files[] = {
1223 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001224 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001225 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1226 BLKIO_PROP_weight_device),
1227 .read_seq_string = blkiocg_file_read,
1228 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001229 .max_write_len = 256,
1230 },
1231 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001232 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001233 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1234 BLKIO_PROP_weight),
1235 .read_u64 = blkiocg_file_read_u64,
1236 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001237 },
Vivek Goyal22084192009-12-03 12:59:49 -05001238 {
1239 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001240 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1241 BLKIO_PROP_time),
1242 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001243 },
1244 {
1245 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001246 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1247 BLKIO_PROP_sectors),
1248 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001249 },
1250 {
1251 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001252 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1253 BLKIO_PROP_io_service_bytes),
1254 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001255 },
1256 {
1257 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001258 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1259 BLKIO_PROP_io_serviced),
1260 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001261 },
1262 {
1263 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001264 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1265 BLKIO_PROP_io_service_time),
1266 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001267 },
1268 {
1269 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001270 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1271 BLKIO_PROP_io_wait_time),
1272 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001273 },
1274 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001275 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001276 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1277 BLKIO_PROP_io_merged),
1278 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001279 },
1280 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001281 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001282 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1283 BLKIO_PROP_io_queued),
1284 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001285 },
1286 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001287 .name = "reset_stats",
1288 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001289 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001290#ifdef CONFIG_BLK_DEV_THROTTLING
1291 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001292 .name = "throttle.read_bps_device",
1293 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1294 BLKIO_THROTL_read_bps_device),
1295 .read_seq_string = blkiocg_file_read,
1296 .write_string = blkiocg_file_write,
1297 .max_write_len = 256,
1298 },
1299
1300 {
1301 .name = "throttle.write_bps_device",
1302 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1303 BLKIO_THROTL_write_bps_device),
1304 .read_seq_string = blkiocg_file_read,
1305 .write_string = blkiocg_file_write,
1306 .max_write_len = 256,
1307 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001308
1309 {
1310 .name = "throttle.read_iops_device",
1311 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1312 BLKIO_THROTL_read_iops_device),
1313 .read_seq_string = blkiocg_file_read,
1314 .write_string = blkiocg_file_write,
1315 .max_write_len = 256,
1316 },
1317
1318 {
1319 .name = "throttle.write_iops_device",
1320 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1321 BLKIO_THROTL_write_iops_device),
1322 .read_seq_string = blkiocg_file_read,
1323 .write_string = blkiocg_file_write,
1324 .max_write_len = 256,
1325 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001326 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001327 .name = "throttle.io_service_bytes",
1328 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1329 BLKIO_THROTL_io_service_bytes),
1330 .read_map = blkiocg_file_read_map,
1331 },
1332 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001333 .name = "throttle.io_serviced",
1334 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1335 BLKIO_THROTL_io_serviced),
1336 .read_map = blkiocg_file_read_map,
1337 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001338#endif /* CONFIG_BLK_DEV_THROTTLING */
1339
Vivek Goyal22084192009-12-03 12:59:49 -05001340#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001341 {
1342 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001343 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1344 BLKIO_PROP_avg_queue_size),
1345 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001346 },
1347 {
Divyesh Shah812df482010-04-08 21:15:35 -07001348 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001349 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1350 BLKIO_PROP_group_wait_time),
1351 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001352 },
1353 {
1354 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001355 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1356 BLKIO_PROP_idle_time),
1357 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001358 },
1359 {
1360 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001361 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1362 BLKIO_PROP_empty_time),
1363 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001364 },
1365 {
Vivek Goyal22084192009-12-03 12:59:49 -05001366 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001367 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1368 BLKIO_PROP_dequeue),
1369 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001370 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001371 {
1372 .name = "unaccounted_time",
1373 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1374 BLKIO_PROP_unaccounted_time),
1375 .read_map = blkiocg_file_read_map,
1376 },
Vivek Goyal22084192009-12-03 12:59:49 -05001377#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001378};
1379
1380static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1381{
1382 return cgroup_add_files(cgroup, subsys, blkio_files,
1383 ARRAY_SIZE(blkio_files));
1384}
1385
Tejun Heo7ee9c562012-03-05 13:15:11 -08001386static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1387 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001388{
1389 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001390 unsigned long flags;
1391 struct blkio_group *blkg;
Tejun Heoca32aef2012-03-05 13:15:03 -08001392 struct request_queue *q;
Vivek Goyal3e252062009-12-04 10:36:42 -05001393 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001394
Vivek Goyalb1c35762009-12-03 12:59:47 -05001395 rcu_read_lock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001396
Jens Axboe0f3942a2010-05-03 14:28:55 +02001397 do {
1398 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001399
Jens Axboe0f3942a2010-05-03 14:28:55 +02001400 if (hlist_empty(&blkcg->blkg_list)) {
1401 spin_unlock_irqrestore(&blkcg->lock, flags);
1402 break;
1403 }
1404
1405 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1406 blkcg_node);
Tejun Heoca32aef2012-03-05 13:15:03 -08001407 q = rcu_dereference(blkg->q);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001408 __blkiocg_del_blkio_group(blkg);
1409
Vivek Goyalb1c35762009-12-03 12:59:47 -05001410 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001411
Jens Axboe0f3942a2010-05-03 14:28:55 +02001412 /*
1413 * This blkio_group is being unlinked as associated cgroup is
1414 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001415 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001416 */
1417 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001418 list_for_each_entry(blkiop, &blkio_list, list) {
1419 if (blkiop->plid != blkg->plid)
1420 continue;
Tejun Heoca32aef2012-03-05 13:15:03 -08001421 blkiop->ops.blkio_unlink_group_fn(q, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001422 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001423 spin_unlock(&blkio_list_lock);
1424 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001425
Vivek Goyalb1c35762009-12-03 12:59:47 -05001426 rcu_read_unlock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001427
1428 return 0;
1429}
1430
1431static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1432{
1433 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1434
Ben Blum67523c42010-03-10 15:22:11 -08001435 if (blkcg != &blkio_root_cgroup)
1436 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001437}
1438
1439static struct cgroup_subsys_state *
1440blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1441{
Li Zefan03415092010-05-07 08:57:00 +02001442 struct blkio_cgroup *blkcg;
1443 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001444
Li Zefan03415092010-05-07 08:57:00 +02001445 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001446 blkcg = &blkio_root_cgroup;
1447 goto done;
1448 }
1449
Vivek Goyal31e4c282009-12-03 12:59:42 -05001450 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1451 if (!blkcg)
1452 return ERR_PTR(-ENOMEM);
1453
1454 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1455done:
1456 spin_lock_init(&blkcg->lock);
1457 INIT_HLIST_HEAD(&blkcg->blkg_list);
1458
1459 return &blkcg->css;
1460}
1461
1462/*
1463 * We cannot support shared io contexts, as we have no mean to support
1464 * two tasks with the same ioc in two different groups without major rework
1465 * of the main cic data structures. For now we allow a task to change
1466 * its cgroup only if it's the only owner of its ioc.
1467 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001468static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1469 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001470{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001471 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001472 struct io_context *ioc;
1473 int ret = 0;
1474
1475 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001476 cgroup_taskset_for_each(task, cgrp, tset) {
1477 task_lock(task);
1478 ioc = task->io_context;
1479 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1480 ret = -EINVAL;
1481 task_unlock(task);
1482 if (ret)
1483 break;
1484 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001485 return ret;
1486}
1487
Tejun Heobb9d97b2011-12-12 18:12:21 -08001488static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1489 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001490{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001491 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001492 struct io_context *ioc;
1493
Tejun Heobb9d97b2011-12-12 18:12:21 -08001494 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001495 /* we don't lose anything even if ioc allocation fails */
1496 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1497 if (ioc) {
1498 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001499 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001500 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001501 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001502}
1503
Vivek Goyal3e252062009-12-04 10:36:42 -05001504void blkio_policy_register(struct blkio_policy_type *blkiop)
1505{
1506 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001507
1508 BUG_ON(blkio_policy[blkiop->plid]);
1509 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001510 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001511
Vivek Goyal3e252062009-12-04 10:36:42 -05001512 spin_unlock(&blkio_list_lock);
1513}
1514EXPORT_SYMBOL_GPL(blkio_policy_register);
1515
1516void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1517{
1518 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001519
1520 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1521 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001522 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001523
Vivek Goyal3e252062009-12-04 10:36:42 -05001524 spin_unlock(&blkio_list_lock);
1525}
1526EXPORT_SYMBOL_GPL(blkio_policy_unregister);