blob: b302ce1d662b1fec680e04d670275b0c660c609f [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
Vivek Goyal31e4c282009-12-03 12:59:42 -050030struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
Vivek Goyal9d6a9862009-12-04 10:36:41 -050031EXPORT_SYMBOL_GPL(blkio_root_cgroup);
32
Tejun Heo035d10b2012-03-05 13:15:04 -080033static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
34
Ben Blum67523c42010-03-10 15:22:11 -080035static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
36 struct cgroup *);
Tejun Heobb9d97b2011-12-12 18:12:21 -080037static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
38 struct cgroup_taskset *);
39static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
40 struct cgroup_taskset *);
Tejun Heo7ee9c562012-03-05 13:15:11 -080041static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
Ben Blum67523c42010-03-10 15:22:11 -080042static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
43static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
44
Vivek Goyal062a6442010-09-15 17:06:33 -040045/* for encoding cft->private value on file */
46#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
47/* What policy owns the file, proportional or throttle */
48#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
49#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
50
Ben Blum67523c42010-03-10 15:22:11 -080051struct cgroup_subsys blkio_subsys = {
52 .name = "blkio",
53 .create = blkiocg_create,
Tejun Heobb9d97b2011-12-12 18:12:21 -080054 .can_attach = blkiocg_can_attach,
55 .attach = blkiocg_attach,
Tejun Heo7ee9c562012-03-05 13:15:11 -080056 .pre_destroy = blkiocg_pre_destroy,
Ben Blum67523c42010-03-10 15:22:11 -080057 .destroy = blkiocg_destroy,
58 .populate = blkiocg_populate,
Ben Blum67523c42010-03-10 15:22:11 -080059 .subsys_id = blkio_subsys_id,
Ben Blum67523c42010-03-10 15:22:11 -080060 .module = THIS_MODULE,
61};
62EXPORT_SYMBOL_GPL(blkio_subsys);
63
Vivek Goyal31e4c282009-12-03 12:59:42 -050064struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
65{
66 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
67 struct blkio_cgroup, css);
68}
Vivek Goyal9d6a9862009-12-04 10:36:41 -050069EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
Vivek Goyal31e4c282009-12-03 12:59:42 -050070
Vivek Goyal70087dc2011-05-16 15:24:08 +020071struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
72{
73 return container_of(task_subsys_state(tsk, blkio_subsys_id),
74 struct blkio_cgroup, css);
75}
76EXPORT_SYMBOL_GPL(task_blkio_cgroup);
77
Vivek Goyal062a6442010-09-15 17:06:33 -040078static inline void
79blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
80{
81 struct blkio_policy_type *blkiop;
82
83 list_for_each_entry(blkiop, &blkio_list, list) {
84 /* If this policy does not own the blkg, do not send updates */
85 if (blkiop->plid != blkg->plid)
86 continue;
87 if (blkiop->ops.blkio_update_group_weight_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -080088 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +020089 blkg, weight);
Vivek Goyal062a6442010-09-15 17:06:33 -040090 }
91}
92
Vivek Goyal4c9eefa2010-09-15 17:06:34 -040093static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
94 int fileid)
95{
96 struct blkio_policy_type *blkiop;
97
98 list_for_each_entry(blkiop, &blkio_list, list) {
99
100 /* If this policy does not own the blkg, do not send updates */
101 if (blkiop->plid != blkg->plid)
102 continue;
103
104 if (fileid == BLKIO_THROTL_read_bps_device
105 && blkiop->ops.blkio_update_group_read_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800106 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200107 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400108
109 if (fileid == BLKIO_THROTL_write_bps_device
110 && blkiop->ops.blkio_update_group_write_bps_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800111 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200112 blkg, bps);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400113 }
114}
115
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400116static inline void blkio_update_group_iops(struct blkio_group *blkg,
117 unsigned int iops, int fileid)
118{
119 struct blkio_policy_type *blkiop;
120
121 list_for_each_entry(blkiop, &blkio_list, list) {
122
123 /* If this policy does not own the blkg, do not send updates */
124 if (blkiop->plid != blkg->plid)
125 continue;
126
127 if (fileid == BLKIO_THROTL_read_iops_device
128 && blkiop->ops.blkio_update_group_read_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800129 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200130 blkg, iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400131
132 if (fileid == BLKIO_THROTL_write_iops_device
133 && blkiop->ops.blkio_update_group_write_iops_fn)
Tejun Heoca32aef2012-03-05 13:15:03 -0800134 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200135 blkg,iops);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400136 }
137}
138
Divyesh Shah91952912010-04-01 15:01:41 -0700139/*
140 * Add to the appropriate stat variable depending on the request type.
141 * This should be called with the blkg->stats_lock held.
142 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200143static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
144 bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700145{
Divyesh Shah84c124d2010-04-09 08:31:19 +0200146 if (direction)
147 stat[BLKIO_STAT_WRITE] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700148 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200149 stat[BLKIO_STAT_READ] += add;
150 if (sync)
151 stat[BLKIO_STAT_SYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700152 else
Divyesh Shah84c124d2010-04-09 08:31:19 +0200153 stat[BLKIO_STAT_ASYNC] += add;
Divyesh Shah91952912010-04-01 15:01:41 -0700154}
155
Divyesh Shahcdc11842010-04-08 21:15:10 -0700156/*
157 * Decrements the appropriate stat variable if non-zero depending on the
158 * request type. Panics on value being zero.
159 * This should be called with the blkg->stats_lock held.
160 */
161static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
162{
163 if (direction) {
164 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
165 stat[BLKIO_STAT_WRITE]--;
166 } else {
167 BUG_ON(stat[BLKIO_STAT_READ] == 0);
168 stat[BLKIO_STAT_READ]--;
169 }
170 if (sync) {
171 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
172 stat[BLKIO_STAT_SYNC]--;
173 } else {
174 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
175 stat[BLKIO_STAT_ASYNC]--;
176 }
177}
178
179#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shah812df482010-04-08 21:15:35 -0700180/* This should be called with the blkg->stats_lock held. */
181static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
182 struct blkio_group *curr_blkg)
183{
184 if (blkio_blkg_waiting(&blkg->stats))
185 return;
186 if (blkg == curr_blkg)
187 return;
188 blkg->stats.start_group_wait_time = sched_clock();
189 blkio_mark_blkg_waiting(&blkg->stats);
190}
191
192/* This should be called with the blkg->stats_lock held. */
193static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
194{
195 unsigned long long now;
196
197 if (!blkio_blkg_waiting(stats))
198 return;
199
200 now = sched_clock();
201 if (time_after64(now, stats->start_group_wait_time))
202 stats->group_wait_time += now - stats->start_group_wait_time;
203 blkio_clear_blkg_waiting(stats);
204}
205
206/* This should be called with the blkg->stats_lock held. */
207static void blkio_end_empty_time(struct blkio_group_stats *stats)
208{
209 unsigned long long now;
210
211 if (!blkio_blkg_empty(stats))
212 return;
213
214 now = sched_clock();
215 if (time_after64(now, stats->start_empty_time))
216 stats->empty_time += now - stats->start_empty_time;
217 blkio_clear_blkg_empty(stats);
218}
219
220void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
221{
222 unsigned long flags;
223
224 spin_lock_irqsave(&blkg->stats_lock, flags);
225 BUG_ON(blkio_blkg_idling(&blkg->stats));
226 blkg->stats.start_idle_time = sched_clock();
227 blkio_mark_blkg_idling(&blkg->stats);
228 spin_unlock_irqrestore(&blkg->stats_lock, flags);
229}
230EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
231
232void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
233{
234 unsigned long flags;
235 unsigned long long now;
236 struct blkio_group_stats *stats;
237
238 spin_lock_irqsave(&blkg->stats_lock, flags);
239 stats = &blkg->stats;
240 if (blkio_blkg_idling(stats)) {
241 now = sched_clock();
242 if (time_after64(now, stats->start_idle_time))
243 stats->idle_time += now - stats->start_idle_time;
244 blkio_clear_blkg_idling(stats);
245 }
246 spin_unlock_irqrestore(&blkg->stats_lock, flags);
247}
248EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
249
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200250void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
Divyesh Shahcdc11842010-04-08 21:15:10 -0700251{
252 unsigned long flags;
253 struct blkio_group_stats *stats;
254
255 spin_lock_irqsave(&blkg->stats_lock, flags);
256 stats = &blkg->stats;
257 stats->avg_queue_size_sum +=
258 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
259 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
260 stats->avg_queue_size_samples++;
Divyesh Shah812df482010-04-08 21:15:35 -0700261 blkio_update_group_wait_time(stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700262 spin_unlock_irqrestore(&blkg->stats_lock, flags);
263}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200264EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
265
Vivek Goyale5ff0822010-04-26 19:25:11 +0200266void blkiocg_set_start_empty_time(struct blkio_group *blkg)
Divyesh Shah28baf442010-04-14 11:22:38 +0200267{
268 unsigned long flags;
269 struct blkio_group_stats *stats;
270
271 spin_lock_irqsave(&blkg->stats_lock, flags);
272 stats = &blkg->stats;
273
274 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
275 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
276 spin_unlock_irqrestore(&blkg->stats_lock, flags);
277 return;
278 }
279
280 /*
Vivek Goyale5ff0822010-04-26 19:25:11 +0200281 * group is already marked empty. This can happen if cfqq got new
282 * request in parent group and moved to this group while being added
283 * to service tree. Just ignore the event and move on.
Divyesh Shah28baf442010-04-14 11:22:38 +0200284 */
Vivek Goyale5ff0822010-04-26 19:25:11 +0200285 if(blkio_blkg_empty(stats)) {
286 spin_unlock_irqrestore(&blkg->stats_lock, flags);
287 return;
288 }
289
Divyesh Shah28baf442010-04-14 11:22:38 +0200290 stats->start_empty_time = sched_clock();
291 blkio_mark_blkg_empty(stats);
292 spin_unlock_irqrestore(&blkg->stats_lock, flags);
293}
294EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
295
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200296void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
297 unsigned long dequeue)
298{
299 blkg->stats.dequeue += dequeue;
300}
301EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
Divyesh Shah812df482010-04-08 21:15:35 -0700302#else
303static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
304 struct blkio_group *curr_blkg) {}
305static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700306#endif
307
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200308void blkiocg_update_io_add_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700309 struct blkio_group *curr_blkg, bool direction,
310 bool sync)
311{
312 unsigned long flags;
313
314 spin_lock_irqsave(&blkg->stats_lock, flags);
315 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
316 sync);
Divyesh Shah812df482010-04-08 21:15:35 -0700317 blkio_end_empty_time(&blkg->stats);
318 blkio_set_start_group_wait_time(blkg, curr_blkg);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700319 spin_unlock_irqrestore(&blkg->stats_lock, flags);
320}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200321EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700322
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200323void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -0700324 bool direction, bool sync)
325{
326 unsigned long flags;
327
328 spin_lock_irqsave(&blkg->stats_lock, flags);
329 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
330 direction, sync);
331 spin_unlock_irqrestore(&blkg->stats_lock, flags);
332}
Divyesh Shaha11cdaa2010-04-13 19:59:17 +0200333EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700334
Justin TerAvest167400d2011-03-12 16:54:00 +0100335void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
336 unsigned long unaccounted_time)
Vivek Goyal22084192009-12-03 12:59:49 -0500337{
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700338 unsigned long flags;
339
340 spin_lock_irqsave(&blkg->stats_lock, flags);
341 blkg->stats.time += time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400342#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100343 blkg->stats.unaccounted_time += unaccounted_time;
Vivek Goyala23e6862011-05-19 15:38:20 -0400344#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700345 spin_unlock_irqrestore(&blkg->stats_lock, flags);
Vivek Goyal22084192009-12-03 12:59:49 -0500346}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700347EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
Vivek Goyal22084192009-12-03 12:59:49 -0500348
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400349/*
350 * should be called under rcu read lock or queue lock to make sure blkg pointer
351 * is valid.
352 */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200353void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
354 uint64_t bytes, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700355{
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400356 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400357 unsigned long flags;
358
359 /*
360 * Disabling interrupts to provide mutual exclusion between two
361 * writes on same cpu. It probably is not needed for 64bit. Not
362 * optimizing that case yet.
363 */
364 local_irq_save(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700365
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400366 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
367
Vivek Goyal575969a2011-05-19 15:38:29 -0400368 u64_stats_update_begin(&stats_cpu->syncp);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400369 stats_cpu->sectors += bytes >> 9;
370 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
371 1, direction, sync);
372 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
373 bytes, direction, sync);
Vivek Goyal575969a2011-05-19 15:38:29 -0400374 u64_stats_update_end(&stats_cpu->syncp);
375 local_irq_restore(flags);
Divyesh Shah91952912010-04-01 15:01:41 -0700376}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200377EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700378
Divyesh Shah84c124d2010-04-09 08:31:19 +0200379void blkiocg_update_completion_stats(struct blkio_group *blkg,
380 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
Divyesh Shah91952912010-04-01 15:01:41 -0700381{
382 struct blkio_group_stats *stats;
383 unsigned long flags;
384 unsigned long long now = sched_clock();
385
386 spin_lock_irqsave(&blkg->stats_lock, flags);
387 stats = &blkg->stats;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200388 if (time_after64(now, io_start_time))
389 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
390 now - io_start_time, direction, sync);
391 if (time_after64(io_start_time, start_time))
392 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
393 io_start_time - start_time, direction, sync);
Divyesh Shah91952912010-04-01 15:01:41 -0700394 spin_unlock_irqrestore(&blkg->stats_lock, flags);
395}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200396EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
Divyesh Shah91952912010-04-01 15:01:41 -0700397
Vivek Goyal317389a2011-05-23 10:02:19 +0200398/* Merged stats are per cpu. */
Divyesh Shah812d4022010-04-08 21:14:23 -0700399void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
400 bool sync)
401{
Vivek Goyal317389a2011-05-23 10:02:19 +0200402 struct blkio_group_stats_cpu *stats_cpu;
Divyesh Shah812d4022010-04-08 21:14:23 -0700403 unsigned long flags;
404
Vivek Goyal317389a2011-05-23 10:02:19 +0200405 /*
406 * Disabling interrupts to provide mutual exclusion between two
407 * writes on same cpu. It probably is not needed for 64bit. Not
408 * optimizing that case yet.
409 */
410 local_irq_save(flags);
411
412 stats_cpu = this_cpu_ptr(blkg->stats_cpu);
413
414 u64_stats_update_begin(&stats_cpu->syncp);
415 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
416 direction, sync);
417 u64_stats_update_end(&stats_cpu->syncp);
418 local_irq_restore(flags);
Divyesh Shah812d4022010-04-08 21:14:23 -0700419}
420EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
421
Tejun Heocd1604f2012-03-05 13:15:06 -0800422struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
423 struct request_queue *q,
424 enum blkio_policy_id plid,
425 bool for_root)
426 __releases(q->queue_lock) __acquires(q->queue_lock)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400427{
Tejun Heocd1604f2012-03-05 13:15:06 -0800428 struct blkio_policy_type *pol = blkio_policy[plid];
429 struct blkio_group *blkg, *new_blkg;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400430
Tejun Heocd1604f2012-03-05 13:15:06 -0800431 WARN_ON_ONCE(!rcu_read_lock_held());
432 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500433
Tejun Heocd1604f2012-03-05 13:15:06 -0800434 /*
435 * This could be the first entry point of blkcg implementation and
436 * we shouldn't allow anything to go through for a bypassing queue.
437 * The following can be removed if blkg lookup is guaranteed to
438 * fail on a bypassing queue.
439 */
440 if (unlikely(blk_queue_bypass(q)) && !for_root)
441 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
442
443 blkg = blkg_lookup(blkcg, q, plid);
444 if (blkg)
445 return blkg;
446
Tejun Heo7ee9c562012-03-05 13:15:11 -0800447 /* blkg holds a reference to blkcg */
Tejun Heocd1604f2012-03-05 13:15:06 -0800448 if (!css_tryget(&blkcg->css))
449 return ERR_PTR(-EINVAL);
450
451 /*
452 * Allocate and initialize.
453 *
454 * FIXME: The following is broken. Percpu memory allocation
455 * requires %GFP_KERNEL context and can't be performed from IO
456 * path. Allocation here should inherently be atomic and the
457 * following lock dancing can be removed once the broken percpu
458 * allocation is fixed.
459 */
460 spin_unlock_irq(q->queue_lock);
461 rcu_read_unlock();
462
463 new_blkg = pol->ops.blkio_alloc_group_fn(q, blkcg);
464 if (new_blkg) {
465 new_blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
466
467 spin_lock_init(&new_blkg->stats_lock);
468 rcu_assign_pointer(new_blkg->q, q);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800469 new_blkg->blkcg = blkcg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800470 new_blkg->plid = plid;
471 cgroup_path(blkcg->css.cgroup, new_blkg->path,
472 sizeof(new_blkg->path));
Tejun Heo7ee9c562012-03-05 13:15:11 -0800473 } else {
474 css_put(&blkcg->css);
Tejun Heocd1604f2012-03-05 13:15:06 -0800475 }
476
477 rcu_read_lock();
478 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -0800479
480 /* did bypass get turned on inbetween? */
481 if (unlikely(blk_queue_bypass(q)) && !for_root) {
482 blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
483 goto out;
484 }
485
486 /* did someone beat us to it? */
487 blkg = blkg_lookup(blkcg, q, plid);
488 if (unlikely(blkg))
489 goto out;
490
491 /* did alloc fail? */
492 if (unlikely(!new_blkg || !new_blkg->stats_cpu)) {
493 blkg = ERR_PTR(-ENOMEM);
494 goto out;
495 }
496
497 /* insert */
498 spin_lock(&blkcg->lock);
499 swap(blkg, new_blkg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500500 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
Tejun Heocd1604f2012-03-05 13:15:06 -0800501 pol->ops.blkio_link_group_fn(q, blkg);
502 spin_unlock(&blkcg->lock);
503out:
504 if (new_blkg) {
505 free_percpu(new_blkg->stats_cpu);
506 kfree(new_blkg);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800507 css_put(&blkcg->css);
Tejun Heocd1604f2012-03-05 13:15:06 -0800508 }
509 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500510}
Tejun Heocd1604f2012-03-05 13:15:06 -0800511EXPORT_SYMBOL_GPL(blkg_lookup_create);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500512
Vivek Goyalb1c35762009-12-03 12:59:47 -0500513static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
514{
515 hlist_del_init_rcu(&blkg->blkcg_node);
Vivek Goyalb1c35762009-12-03 12:59:47 -0500516}
517
518/*
519 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
520 * indicating that blk_group was unhashed by the time we got to it.
521 */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500522int blkiocg_del_blkio_group(struct blkio_group *blkg)
523{
Tejun Heo7ee9c562012-03-05 13:15:11 -0800524 struct blkio_cgroup *blkcg = blkg->blkcg;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500525 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500526 int ret = 1;
527
Tejun Heo7ee9c562012-03-05 13:15:11 -0800528 spin_lock_irqsave(&blkcg->lock, flags);
529 if (!hlist_unhashed(&blkg->blkcg_node)) {
530 __blkiocg_del_blkio_group(blkg);
531 ret = 0;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500532 }
Tejun Heo7ee9c562012-03-05 13:15:11 -0800533 spin_unlock_irqrestore(&blkcg->lock, flags);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200534
Vivek Goyalb1c35762009-12-03 12:59:47 -0500535 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500536}
Vivek Goyal9d6a9862009-12-04 10:36:41 -0500537EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500538
539/* called under rcu_read_lock(). */
Tejun Heocd1604f2012-03-05 13:15:06 -0800540struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
541 struct request_queue *q,
542 enum blkio_policy_id plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500543{
544 struct blkio_group *blkg;
545 struct hlist_node *n;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500546
Tejun Heoca32aef2012-03-05 13:15:03 -0800547 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
548 if (blkg->q == q && blkg->plid == plid)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500549 return blkg;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500550 return NULL;
551}
Tejun Heocd1604f2012-03-05 13:15:06 -0800552EXPORT_SYMBOL_GPL(blkg_lookup);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500553
Tejun Heo72e06c22012-03-05 13:15:00 -0800554void blkg_destroy_all(struct request_queue *q)
555{
556 struct blkio_policy_type *pol;
557
558 while (true) {
559 bool done = true;
560
561 spin_lock(&blkio_list_lock);
562 spin_lock_irq(q->queue_lock);
563
564 /*
565 * clear_queue_fn() might return with non-empty group list
566 * if it raced cgroup removal and lost. cgroup removal is
567 * guaranteed to make forward progress and retrying after a
568 * while is enough. This ugliness is scheduled to be
569 * removed after locking update.
570 */
571 list_for_each_entry(pol, &blkio_list, list)
572 if (!pol->ops.blkio_clear_queue_fn(q))
573 done = false;
574
575 spin_unlock_irq(q->queue_lock);
576 spin_unlock(&blkio_list_lock);
577
578 if (done)
579 break;
580
581 msleep(10); /* just some random duration I like */
582 }
583}
584
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400585static void blkio_reset_stats_cpu(struct blkio_group *blkg)
586{
587 struct blkio_group_stats_cpu *stats_cpu;
588 int i, j, k;
589 /*
590 * Note: On 64 bit arch this should not be an issue. This has the
591 * possibility of returning some inconsistent value on 32bit arch
592 * as 64bit update on 32bit is non atomic. Taking care of this
593 * corner case makes code very complicated, like sending IPIs to
594 * cpus, taking care of stats of offline cpus etc.
595 *
596 * reset stats is anyway more of a debug feature and this sounds a
597 * corner case. So I am not complicating the code yet until and
598 * unless this becomes a real issue.
599 */
600 for_each_possible_cpu(i) {
601 stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
602 stats_cpu->sectors = 0;
603 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
604 for (k = 0; k < BLKIO_STAT_TOTAL; k++)
605 stats_cpu->stat_arr_cpu[j][k] = 0;
606 }
607}
608
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700609static int
Divyesh Shah84c124d2010-04-09 08:31:19 +0200610blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700611{
612 struct blkio_cgroup *blkcg;
613 struct blkio_group *blkg;
Divyesh Shah812df482010-04-08 21:15:35 -0700614 struct blkio_group_stats *stats;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700615 struct hlist_node *n;
Divyesh Shahcdc11842010-04-08 21:15:10 -0700616 uint64_t queued[BLKIO_STAT_TOTAL];
617 int i;
Divyesh Shah812df482010-04-08 21:15:35 -0700618#ifdef CONFIG_DEBUG_BLK_CGROUP
619 bool idling, waiting, empty;
620 unsigned long long now = sched_clock();
621#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700622
623 blkcg = cgroup_to_blkio_cgroup(cgroup);
624 spin_lock_irq(&blkcg->lock);
625 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
626 spin_lock(&blkg->stats_lock);
Divyesh Shah812df482010-04-08 21:15:35 -0700627 stats = &blkg->stats;
628#ifdef CONFIG_DEBUG_BLK_CGROUP
629 idling = blkio_blkg_idling(stats);
630 waiting = blkio_blkg_waiting(stats);
631 empty = blkio_blkg_empty(stats);
632#endif
Divyesh Shahcdc11842010-04-08 21:15:10 -0700633 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700634 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
635 memset(stats, 0, sizeof(struct blkio_group_stats));
Divyesh Shahcdc11842010-04-08 21:15:10 -0700636 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
Divyesh Shah812df482010-04-08 21:15:35 -0700637 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
638#ifdef CONFIG_DEBUG_BLK_CGROUP
639 if (idling) {
640 blkio_mark_blkg_idling(stats);
641 stats->start_idle_time = now;
642 }
643 if (waiting) {
644 blkio_mark_blkg_waiting(stats);
645 stats->start_group_wait_time = now;
646 }
647 if (empty) {
648 blkio_mark_blkg_empty(stats);
649 stats->start_empty_time = now;
650 }
651#endif
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700652 spin_unlock(&blkg->stats_lock);
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400653
654 /* Reset Per cpu stats which don't take blkg->stats_lock */
655 blkio_reset_stats_cpu(blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700656 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400657
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700658 spin_unlock_irq(&blkcg->lock);
659 return 0;
660}
661
Tejun Heo7a4dd282012-03-05 13:15:09 -0800662static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
663 char *str, int chars_left, bool diskname_only)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700664{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800665 snprintf(str, chars_left, "%s", dname);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700666 chars_left -= strlen(str);
667 if (chars_left <= 0) {
668 printk(KERN_WARNING
669 "Possibly incorrect cgroup stat display format");
670 return;
671 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200672 if (diskname_only)
673 return;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700674 switch (type) {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200675 case BLKIO_STAT_READ:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700676 strlcat(str, " Read", chars_left);
677 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200678 case BLKIO_STAT_WRITE:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700679 strlcat(str, " Write", chars_left);
680 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200681 case BLKIO_STAT_SYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700682 strlcat(str, " Sync", chars_left);
683 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200684 case BLKIO_STAT_ASYNC:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700685 strlcat(str, " Async", chars_left);
686 break;
Divyesh Shah84c124d2010-04-09 08:31:19 +0200687 case BLKIO_STAT_TOTAL:
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700688 strlcat(str, " Total", chars_left);
689 break;
690 default:
691 strlcat(str, " Invalid", chars_left);
692 }
693}
694
Divyesh Shah84c124d2010-04-09 08:31:19 +0200695static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800696 struct cgroup_map_cb *cb, const char *dname)
Divyesh Shah84c124d2010-04-09 08:31:19 +0200697{
Tejun Heo7a4dd282012-03-05 13:15:09 -0800698 blkio_get_key_name(0, dname, str, chars_left, true);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200699 cb->fill(cb, str, val);
700 return val;
701}
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700702
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400703
704static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
705 enum stat_type_cpu type, enum stat_sub_type sub_type)
706{
707 int cpu;
708 struct blkio_group_stats_cpu *stats_cpu;
Vivek Goyal575969a2011-05-19 15:38:29 -0400709 u64 val = 0, tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400710
711 for_each_possible_cpu(cpu) {
Vivek Goyal575969a2011-05-19 15:38:29 -0400712 unsigned int start;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400713 stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
714
Vivek Goyal575969a2011-05-19 15:38:29 -0400715 do {
716 start = u64_stats_fetch_begin(&stats_cpu->syncp);
717 if (type == BLKIO_STAT_CPU_SECTORS)
718 tval = stats_cpu->sectors;
719 else
720 tval = stats_cpu->stat_arr_cpu[type][sub_type];
721 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
722
723 val += tval;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400724 }
725
726 return val;
727}
728
729static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800730 struct cgroup_map_cb *cb, const char *dname,
731 enum stat_type_cpu type)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400732{
733 uint64_t disk_total, val;
734 char key_str[MAX_KEY_LEN];
735 enum stat_sub_type sub_type;
736
737 if (type == BLKIO_STAT_CPU_SECTORS) {
738 val = blkio_read_stat_cpu(blkg, type, 0);
Tejun Heo7a4dd282012-03-05 13:15:09 -0800739 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
740 dname);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400741 }
742
743 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
744 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800745 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
746 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400747 val = blkio_read_stat_cpu(blkg, type, sub_type);
748 cb->fill(cb, key_str, val);
749 }
750
751 disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
752 blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
753
Tejun Heo7a4dd282012-03-05 13:15:09 -0800754 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
755 false);
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400756 cb->fill(cb, key_str, disk_total);
757 return disk_total;
758}
759
Divyesh Shah84c124d2010-04-09 08:31:19 +0200760/* This should be called with blkg->stats_lock held */
761static uint64_t blkio_get_stat(struct blkio_group *blkg,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800762 struct cgroup_map_cb *cb, const char *dname,
763 enum stat_type type)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700764{
765 uint64_t disk_total;
766 char key_str[MAX_KEY_LEN];
Divyesh Shah84c124d2010-04-09 08:31:19 +0200767 enum stat_sub_type sub_type;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700768
Divyesh Shah84c124d2010-04-09 08:31:19 +0200769 if (type == BLKIO_STAT_TIME)
770 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800771 blkg->stats.time, cb, dname);
Justin TerAvest9026e522011-03-22 21:26:54 +0100772#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest167400d2011-03-12 16:54:00 +0100773 if (type == BLKIO_STAT_UNACCOUNTED_TIME)
774 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800775 blkg->stats.unaccounted_time, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700776 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
777 uint64_t sum = blkg->stats.avg_queue_size_sum;
778 uint64_t samples = blkg->stats.avg_queue_size_samples;
779 if (samples)
780 do_div(sum, samples);
781 else
782 sum = 0;
Tejun Heo7a4dd282012-03-05 13:15:09 -0800783 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
784 sum, cb, dname);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700785 }
Divyesh Shah812df482010-04-08 21:15:35 -0700786 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
787 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800788 blkg->stats.group_wait_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700789 if (type == BLKIO_STAT_IDLE_TIME)
790 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800791 blkg->stats.idle_time, cb, dname);
Divyesh Shah812df482010-04-08 21:15:35 -0700792 if (type == BLKIO_STAT_EMPTY_TIME)
793 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800794 blkg->stats.empty_time, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200795 if (type == BLKIO_STAT_DEQUEUE)
796 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
Tejun Heo7a4dd282012-03-05 13:15:09 -0800797 blkg->stats.dequeue, cb, dname);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200798#endif
799
800 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
801 sub_type++) {
Tejun Heo7a4dd282012-03-05 13:15:09 -0800802 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
803 false);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200804 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700805 }
Divyesh Shah84c124d2010-04-09 08:31:19 +0200806 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
807 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
Tejun Heo7a4dd282012-03-05 13:15:09 -0800808 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
809 false);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700810 cb->fill(cb, key_str, disk_total);
811 return disk_total;
812}
813
Tejun Heo4bfd4822012-03-05 13:15:08 -0800814static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
815 int fileid, struct blkio_cgroup *blkcg)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800816{
Tejun Heoece84242011-10-19 14:31:15 +0200817 struct gendisk *disk = NULL;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800818 struct blkio_group *blkg = NULL;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800819 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200820 unsigned long major, minor;
Tejun Heoece84242011-10-19 14:31:15 +0200821 int i = 0, ret = -EINVAL;
822 int part;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800823 dev_t dev;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200824 u64 temp;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800825
826 memset(s, 0, sizeof(s));
827
828 while ((p = strsep(&buf, " ")) != NULL) {
829 if (!*p)
830 continue;
831
832 s[i++] = p;
833
834 /* Prevent from inputing too many things */
835 if (i == 3)
836 break;
837 }
838
839 if (i != 2)
Tejun Heoece84242011-10-19 14:31:15 +0200840 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800841
842 p = strsep(&s[0], ":");
843 if (p != NULL)
844 major_s = p;
845 else
Tejun Heoece84242011-10-19 14:31:15 +0200846 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800847
848 minor_s = s[0];
849 if (!minor_s)
Tejun Heoece84242011-10-19 14:31:15 +0200850 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800851
Tejun Heoece84242011-10-19 14:31:15 +0200852 if (strict_strtoul(major_s, 10, &major))
853 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800854
Tejun Heoece84242011-10-19 14:31:15 +0200855 if (strict_strtoul(minor_s, 10, &minor))
856 goto out;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800857
858 dev = MKDEV(major, minor);
859
Tejun Heoece84242011-10-19 14:31:15 +0200860 if (strict_strtoull(s[1], 10, &temp))
861 goto out;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200862
Tejun Heoe56da7e2012-03-05 13:15:07 -0800863 disk = get_gendisk(dev, &part);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800864 if (!disk || part)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800865 goto out;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800866
867 rcu_read_lock();
868
Tejun Heo4bfd4822012-03-05 13:15:08 -0800869 spin_lock_irq(disk->queue->queue_lock);
870 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
871 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800872
Tejun Heo4bfd4822012-03-05 13:15:08 -0800873 if (IS_ERR(blkg)) {
874 ret = PTR_ERR(blkg);
875 goto out_unlock;
Wanlong Gaod11bb442011-09-21 10:22:10 +0200876 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800877
Vivek Goyal062a6442010-09-15 17:06:33 -0400878 switch (plid) {
879 case BLKIO_POLICY_PROP:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200880 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
881 temp > BLKIO_WEIGHT_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800882 goto out_unlock;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800883
Tejun Heo4bfd4822012-03-05 13:15:08 -0800884 blkg->conf.weight = temp;
885 blkio_update_group_weight(blkg, temp ?: blkcg->weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400886 break;
887 case BLKIO_POLICY_THROTL:
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400888 switch(fileid) {
889 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800890 blkg->conf.bps[READ] = temp;
891 blkio_update_group_bps(blkg, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800892 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400893 case BLKIO_THROTL_write_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800894 blkg->conf.bps[WRITE] = temp;
895 blkio_update_group_bps(blkg, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400896 break;
897 case BLKIO_THROTL_read_iops_device:
Tejun Heoe56da7e2012-03-05 13:15:07 -0800898 if (temp > THROTL_IOPS_MAX)
899 goto out_unlock;
Tejun Heo4bfd4822012-03-05 13:15:08 -0800900 blkg->conf.iops[READ] = temp;
901 blkio_update_group_iops(blkg, temp ?: -1, fileid);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800902 break;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400903 case BLKIO_THROTL_write_iops_device:
Wanlong Gaod11bb442011-09-21 10:22:10 +0200904 if (temp > THROTL_IOPS_MAX)
Tejun Heoe56da7e2012-03-05 13:15:07 -0800905 goto out_unlock;
Tejun Heo4bfd4822012-03-05 13:15:08 -0800906 blkg->conf.iops[WRITE] = temp;
907 blkio_update_group_iops(blkg, temp ?: -1, fileid);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400908 break;
909 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400910 break;
911 default:
912 BUG();
913 }
Tejun Heoece84242011-10-19 14:31:15 +0200914 ret = 0;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800915out_unlock:
916 rcu_read_unlock();
Tejun Heoece84242011-10-19 14:31:15 +0200917out:
918 put_disk(disk);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800919
920 /*
921 * If queue was bypassing, we should retry. Do so after a short
922 * msleep(). It isn't strictly necessary but queue can be
923 * bypassing for some time and it's always nice to avoid busy
924 * looping.
925 */
926 if (ret == -EBUSY) {
927 msleep(10);
928 return restart_syscall();
929 }
Tejun Heoece84242011-10-19 14:31:15 +0200930 return ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800931}
932
Vivek Goyal062a6442010-09-15 17:06:33 -0400933static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
934 const char *buffer)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800935{
936 int ret = 0;
937 char *buf;
Tejun Heoe56da7e2012-03-05 13:15:07 -0800938 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Vivek Goyal062a6442010-09-15 17:06:33 -0400939 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
940 int fileid = BLKIOFILE_ATTR(cft->private);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800941
942 buf = kstrdup(buffer, GFP_KERNEL);
943 if (!buf)
944 return -ENOMEM;
945
Tejun Heo4bfd4822012-03-05 13:15:08 -0800946 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800947 kfree(buf);
948 return ret;
949}
950
Vivek Goyal92616b52012-03-05 13:15:10 -0800951static const char *blkg_dev_name(struct blkio_group *blkg)
952{
953 /* some drivers (floppy) instantiate a queue w/o disk registered */
954 if (blkg->q->backing_dev_info.dev)
955 return dev_name(blkg->q->backing_dev_info.dev);
956 return NULL;
957}
958
Tejun Heo4bfd4822012-03-05 13:15:08 -0800959static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
960 struct seq_file *m)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800961{
Vivek Goyal92616b52012-03-05 13:15:10 -0800962 const char *dname = blkg_dev_name(blkg);
Tejun Heo4bfd4822012-03-05 13:15:08 -0800963 int fileid = BLKIOFILE_ATTR(cft->private);
964 int rw = WRITE;
965
Vivek Goyal92616b52012-03-05 13:15:10 -0800966 if (!dname)
967 return;
968
Tejun Heo4bfd4822012-03-05 13:15:08 -0800969 switch (blkg->plid) {
Vivek Goyal062a6442010-09-15 17:06:33 -0400970 case BLKIO_POLICY_PROP:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800971 if (blkg->conf.weight)
Tejun Heo7a4dd282012-03-05 13:15:09 -0800972 seq_printf(m, "%s\t%u\n",
973 dname, blkg->conf.weight);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -0400974 break;
975 case BLKIO_POLICY_THROTL:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800976 switch (fileid) {
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400977 case BLKIO_THROTL_read_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800978 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400979 case BLKIO_THROTL_write_bps_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800980 if (blkg->conf.bps[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -0800981 seq_printf(m, "%s\t%llu\n",
982 dname, blkg->conf.bps[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400983 break;
984 case BLKIO_THROTL_read_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800985 rw = READ;
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400986 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -0800987 if (blkg->conf.iops[rw])
Tejun Heo7a4dd282012-03-05 13:15:09 -0800988 seq_printf(m, "%s\t%u\n",
989 dname, blkg->conf.iops[rw]);
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400990 break;
991 }
Vivek Goyal062a6442010-09-15 17:06:33 -0400992 break;
993 default:
994 BUG();
995 }
996}
997
998/* cgroup files which read their data from policy nodes end up here */
Tejun Heo4bfd4822012-03-05 13:15:08 -0800999static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1000 struct seq_file *m)
Vivek Goyal062a6442010-09-15 17:06:33 -04001001{
Tejun Heo4bfd4822012-03-05 13:15:08 -08001002 struct blkio_group *blkg;
1003 struct hlist_node *n;
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001004
Tejun Heo4bfd4822012-03-05 13:15:08 -08001005 spin_lock_irq(&blkcg->lock);
1006 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
1007 if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
1008 blkio_print_group_conf(cft, blkg, m);
1009 spin_unlock_irq(&blkcg->lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001010}
1011
1012static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1013 struct seq_file *m)
1014{
1015 struct blkio_cgroup *blkcg;
1016 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1017 int name = BLKIOFILE_ATTR(cft->private);
1018
1019 blkcg = cgroup_to_blkio_cgroup(cgrp);
1020
1021 switch(plid) {
1022 case BLKIO_POLICY_PROP:
1023 switch(name) {
1024 case BLKIO_PROP_weight_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001025 blkio_read_conf(cft, blkcg, m);
Vivek Goyal062a6442010-09-15 17:06:33 -04001026 return 0;
1027 default:
1028 BUG();
1029 }
1030 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001031 case BLKIO_POLICY_THROTL:
1032 switch(name){
1033 case BLKIO_THROTL_read_bps_device:
1034 case BLKIO_THROTL_write_bps_device:
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001035 case BLKIO_THROTL_read_iops_device:
1036 case BLKIO_THROTL_write_iops_device:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001037 blkio_read_conf(cft, blkcg, m);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001038 return 0;
1039 default:
1040 BUG();
1041 }
1042 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001043 default:
1044 BUG();
1045 }
1046
1047 return 0;
1048}
1049
1050static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001051 struct cftype *cft, struct cgroup_map_cb *cb,
1052 enum stat_type type, bool show_total, bool pcpu)
Vivek Goyal062a6442010-09-15 17:06:33 -04001053{
1054 struct blkio_group *blkg;
1055 struct hlist_node *n;
1056 uint64_t cgroup_total = 0;
1057
1058 rcu_read_lock();
1059 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
Vivek Goyal92616b52012-03-05 13:15:10 -08001060 const char *dname = blkg_dev_name(blkg);
Tejun Heo7a4dd282012-03-05 13:15:09 -08001061
Vivek Goyal92616b52012-03-05 13:15:10 -08001062 if (!dname || BLKIOFILE_POLICY(cft->private) != blkg->plid)
Tejun Heo7a4dd282012-03-05 13:15:09 -08001063 continue;
1064 if (pcpu)
1065 cgroup_total += blkio_get_stat_cpu(blkg, cb, dname,
1066 type);
1067 else {
1068 spin_lock_irq(&blkg->stats_lock);
1069 cgroup_total += blkio_get_stat(blkg, cb, dname, type);
1070 spin_unlock_irq(&blkg->stats_lock);
Vivek Goyal062a6442010-09-15 17:06:33 -04001071 }
1072 }
1073 if (show_total)
1074 cb->fill(cb, "Total", cgroup_total);
1075 rcu_read_unlock();
1076 return 0;
1077}
1078
1079/* All map kind of cgroup file get serviced by this function */
1080static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1081 struct cgroup_map_cb *cb)
1082{
1083 struct blkio_cgroup *blkcg;
1084 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1085 int name = BLKIOFILE_ATTR(cft->private);
1086
1087 blkcg = cgroup_to_blkio_cgroup(cgrp);
1088
1089 switch(plid) {
1090 case BLKIO_POLICY_PROP:
1091 switch(name) {
1092 case BLKIO_PROP_time:
1093 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001094 BLKIO_STAT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001095 case BLKIO_PROP_sectors:
1096 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001097 BLKIO_STAT_CPU_SECTORS, 0, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001098 case BLKIO_PROP_io_service_bytes:
1099 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001100 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001101 case BLKIO_PROP_io_serviced:
1102 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001103 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001104 case BLKIO_PROP_io_service_time:
1105 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001106 BLKIO_STAT_SERVICE_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001107 case BLKIO_PROP_io_wait_time:
1108 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001109 BLKIO_STAT_WAIT_TIME, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001110 case BLKIO_PROP_io_merged:
1111 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal317389a2011-05-23 10:02:19 +02001112 BLKIO_STAT_CPU_MERGED, 1, 1);
Vivek Goyal062a6442010-09-15 17:06:33 -04001113 case BLKIO_PROP_io_queued:
1114 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001115 BLKIO_STAT_QUEUED, 1, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001116#ifdef CONFIG_DEBUG_BLK_CGROUP
Justin TerAvest9026e522011-03-22 21:26:54 +01001117 case BLKIO_PROP_unaccounted_time:
1118 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001119 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001120 case BLKIO_PROP_dequeue:
1121 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001122 BLKIO_STAT_DEQUEUE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001123 case BLKIO_PROP_avg_queue_size:
1124 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001125 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001126 case BLKIO_PROP_group_wait_time:
1127 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001128 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001129 case BLKIO_PROP_idle_time:
1130 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001131 BLKIO_STAT_IDLE_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001132 case BLKIO_PROP_empty_time:
1133 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001134 BLKIO_STAT_EMPTY_TIME, 0, 0);
Vivek Goyal062a6442010-09-15 17:06:33 -04001135#endif
1136 default:
1137 BUG();
1138 }
1139 break;
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001140 case BLKIO_POLICY_THROTL:
1141 switch(name){
1142 case BLKIO_THROTL_io_service_bytes:
1143 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001144 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001145 case BLKIO_THROTL_io_serviced:
1146 return blkio_read_blkg_stats(blkcg, cft, cb,
Vivek Goyal5624a4e2011-05-19 15:38:28 -04001147 BLKIO_STAT_CPU_SERVICED, 1, 1);
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001148 default:
1149 BUG();
1150 }
1151 break;
Vivek Goyal062a6442010-09-15 17:06:33 -04001152 default:
1153 BUG();
1154 }
1155
1156 return 0;
1157}
1158
Tejun Heo4bfd4822012-03-05 13:15:08 -08001159static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
Vivek Goyal062a6442010-09-15 17:06:33 -04001160{
1161 struct blkio_group *blkg;
1162 struct hlist_node *n;
Vivek Goyal062a6442010-09-15 17:06:33 -04001163
1164 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1165 return -EINVAL;
1166
1167 spin_lock(&blkio_list_lock);
1168 spin_lock_irq(&blkcg->lock);
1169 blkcg->weight = (unsigned int)val;
1170
Tejun Heo4bfd4822012-03-05 13:15:08 -08001171 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
1172 if (blkg->plid == plid && !blkg->conf.weight)
1173 blkio_update_group_weight(blkg, blkcg->weight);
Vivek Goyal062a6442010-09-15 17:06:33 -04001174
Vivek Goyal062a6442010-09-15 17:06:33 -04001175 spin_unlock_irq(&blkcg->lock);
1176 spin_unlock(&blkio_list_lock);
1177 return 0;
1178}
1179
1180static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1181 struct blkio_cgroup *blkcg;
1182 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1183 int name = BLKIOFILE_ATTR(cft->private);
1184
1185 blkcg = cgroup_to_blkio_cgroup(cgrp);
1186
1187 switch(plid) {
1188 case BLKIO_POLICY_PROP:
1189 switch(name) {
1190 case BLKIO_PROP_weight:
1191 return (u64)blkcg->weight;
1192 }
1193 break;
1194 default:
1195 BUG();
1196 }
1197 return 0;
1198}
1199
1200static int
1201blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1202{
1203 struct blkio_cgroup *blkcg;
1204 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1205 int name = BLKIOFILE_ATTR(cft->private);
1206
1207 blkcg = cgroup_to_blkio_cgroup(cgrp);
1208
1209 switch(plid) {
1210 case BLKIO_POLICY_PROP:
1211 switch(name) {
1212 case BLKIO_PROP_weight:
Tejun Heo4bfd4822012-03-05 13:15:08 -08001213 return blkio_weight_write(blkcg, plid, val);
Vivek Goyal062a6442010-09-15 17:06:33 -04001214 }
1215 break;
1216 default:
1217 BUG();
1218 }
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001219
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001220 return 0;
1221}
1222
Vivek Goyal31e4c282009-12-03 12:59:42 -05001223struct cftype blkio_files[] = {
1224 {
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001225 .name = "weight_device",
Vivek Goyal062a6442010-09-15 17:06:33 -04001226 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1227 BLKIO_PROP_weight_device),
1228 .read_seq_string = blkiocg_file_read,
1229 .write_string = blkiocg_file_write,
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001230 .max_write_len = 256,
1231 },
1232 {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001233 .name = "weight",
Vivek Goyal062a6442010-09-15 17:06:33 -04001234 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1235 BLKIO_PROP_weight),
1236 .read_u64 = blkiocg_file_read_u64,
1237 .write_u64 = blkiocg_file_write_u64,
Vivek Goyal31e4c282009-12-03 12:59:42 -05001238 },
Vivek Goyal22084192009-12-03 12:59:49 -05001239 {
1240 .name = "time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001241 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1242 BLKIO_PROP_time),
1243 .read_map = blkiocg_file_read_map,
Vivek Goyal22084192009-12-03 12:59:49 -05001244 },
1245 {
1246 .name = "sectors",
Vivek Goyal13f98252010-10-01 14:49:41 +02001247 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1248 BLKIO_PROP_sectors),
1249 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001250 },
1251 {
1252 .name = "io_service_bytes",
Vivek Goyal13f98252010-10-01 14:49:41 +02001253 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1254 BLKIO_PROP_io_service_bytes),
1255 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001256 },
1257 {
1258 .name = "io_serviced",
Vivek Goyal13f98252010-10-01 14:49:41 +02001259 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1260 BLKIO_PROP_io_serviced),
1261 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001262 },
1263 {
1264 .name = "io_service_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001265 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1266 BLKIO_PROP_io_service_time),
1267 .read_map = blkiocg_file_read_map,
Divyesh Shah303a3ac2010-04-01 15:01:24 -07001268 },
1269 {
1270 .name = "io_wait_time",
Vivek Goyal13f98252010-10-01 14:49:41 +02001271 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1272 BLKIO_PROP_io_wait_time),
1273 .read_map = blkiocg_file_read_map,
Divyesh Shah84c124d2010-04-09 08:31:19 +02001274 },
1275 {
Divyesh Shah812d4022010-04-08 21:14:23 -07001276 .name = "io_merged",
Vivek Goyal13f98252010-10-01 14:49:41 +02001277 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1278 BLKIO_PROP_io_merged),
1279 .read_map = blkiocg_file_read_map,
Divyesh Shah812d4022010-04-08 21:14:23 -07001280 },
1281 {
Divyesh Shahcdc11842010-04-08 21:15:10 -07001282 .name = "io_queued",
Vivek Goyal13f98252010-10-01 14:49:41 +02001283 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1284 BLKIO_PROP_io_queued),
1285 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001286 },
1287 {
Divyesh Shah84c124d2010-04-09 08:31:19 +02001288 .name = "reset_stats",
1289 .write_u64 = blkiocg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -05001290 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001291#ifdef CONFIG_BLK_DEV_THROTTLING
1292 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001293 .name = "throttle.read_bps_device",
1294 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1295 BLKIO_THROTL_read_bps_device),
1296 .read_seq_string = blkiocg_file_read,
1297 .write_string = blkiocg_file_write,
1298 .max_write_len = 256,
1299 },
1300
1301 {
1302 .name = "throttle.write_bps_device",
1303 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1304 BLKIO_THROTL_write_bps_device),
1305 .read_seq_string = blkiocg_file_read,
1306 .write_string = blkiocg_file_write,
1307 .max_write_len = 256,
1308 },
Vivek Goyal7702e8f2010-09-15 17:06:36 -04001309
1310 {
1311 .name = "throttle.read_iops_device",
1312 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1313 BLKIO_THROTL_read_iops_device),
1314 .read_seq_string = blkiocg_file_read,
1315 .write_string = blkiocg_file_write,
1316 .max_write_len = 256,
1317 },
1318
1319 {
1320 .name = "throttle.write_iops_device",
1321 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1322 BLKIO_THROTL_write_iops_device),
1323 .read_seq_string = blkiocg_file_read,
1324 .write_string = blkiocg_file_write,
1325 .max_write_len = 256,
1326 },
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001327 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001328 .name = "throttle.io_service_bytes",
1329 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1330 BLKIO_THROTL_io_service_bytes),
1331 .read_map = blkiocg_file_read_map,
1332 },
1333 {
Vivek Goyal4c9eefa2010-09-15 17:06:34 -04001334 .name = "throttle.io_serviced",
1335 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1336 BLKIO_THROTL_io_serviced),
1337 .read_map = blkiocg_file_read_map,
1338 },
Vivek Goyal13f98252010-10-01 14:49:41 +02001339#endif /* CONFIG_BLK_DEV_THROTTLING */
1340
Vivek Goyal22084192009-12-03 12:59:49 -05001341#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -07001342 {
1343 .name = "avg_queue_size",
Vivek Goyal062a6442010-09-15 17:06:33 -04001344 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1345 BLKIO_PROP_avg_queue_size),
1346 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001347 },
1348 {
Divyesh Shah812df482010-04-08 21:15:35 -07001349 .name = "group_wait_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001350 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1351 BLKIO_PROP_group_wait_time),
1352 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001353 },
1354 {
1355 .name = "idle_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001356 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1357 BLKIO_PROP_idle_time),
1358 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001359 },
1360 {
1361 .name = "empty_time",
Vivek Goyal062a6442010-09-15 17:06:33 -04001362 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1363 BLKIO_PROP_empty_time),
1364 .read_map = blkiocg_file_read_map,
Divyesh Shah812df482010-04-08 21:15:35 -07001365 },
1366 {
Vivek Goyal22084192009-12-03 12:59:49 -05001367 .name = "dequeue",
Vivek Goyal062a6442010-09-15 17:06:33 -04001368 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1369 BLKIO_PROP_dequeue),
1370 .read_map = blkiocg_file_read_map,
Divyesh Shahcdc11842010-04-08 21:15:10 -07001371 },
Justin TerAvest9026e522011-03-22 21:26:54 +01001372 {
1373 .name = "unaccounted_time",
1374 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1375 BLKIO_PROP_unaccounted_time),
1376 .read_map = blkiocg_file_read_map,
1377 },
Vivek Goyal22084192009-12-03 12:59:49 -05001378#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -05001379};
1380
1381static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1382{
1383 return cgroup_add_files(cgroup, subsys, blkio_files,
1384 ARRAY_SIZE(blkio_files));
1385}
1386
Tejun Heo7ee9c562012-03-05 13:15:11 -08001387static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1388 struct cgroup *cgroup)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001389{
1390 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001391 unsigned long flags;
1392 struct blkio_group *blkg;
Tejun Heoca32aef2012-03-05 13:15:03 -08001393 struct request_queue *q;
Vivek Goyal3e252062009-12-04 10:36:42 -05001394 struct blkio_policy_type *blkiop;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001395
Vivek Goyalb1c35762009-12-03 12:59:47 -05001396 rcu_read_lock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001397
Jens Axboe0f3942a2010-05-03 14:28:55 +02001398 do {
1399 spin_lock_irqsave(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001400
Jens Axboe0f3942a2010-05-03 14:28:55 +02001401 if (hlist_empty(&blkcg->blkg_list)) {
1402 spin_unlock_irqrestore(&blkcg->lock, flags);
1403 break;
1404 }
1405
1406 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1407 blkcg_node);
Tejun Heoca32aef2012-03-05 13:15:03 -08001408 q = rcu_dereference(blkg->q);
Jens Axboe0f3942a2010-05-03 14:28:55 +02001409 __blkiocg_del_blkio_group(blkg);
1410
Vivek Goyalb1c35762009-12-03 12:59:47 -05001411 spin_unlock_irqrestore(&blkcg->lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001412
Jens Axboe0f3942a2010-05-03 14:28:55 +02001413 /*
1414 * This blkio_group is being unlinked as associated cgroup is
1415 * going away. Let all the IO controlling policies know about
Vivek Goyal61014e92010-10-01 14:49:44 +02001416 * this event.
Jens Axboe0f3942a2010-05-03 14:28:55 +02001417 */
1418 spin_lock(&blkio_list_lock);
Vivek Goyal61014e92010-10-01 14:49:44 +02001419 list_for_each_entry(blkiop, &blkio_list, list) {
1420 if (blkiop->plid != blkg->plid)
1421 continue;
Tejun Heoca32aef2012-03-05 13:15:03 -08001422 blkiop->ops.blkio_unlink_group_fn(q, blkg);
Vivek Goyal61014e92010-10-01 14:49:44 +02001423 }
Jens Axboe0f3942a2010-05-03 14:28:55 +02001424 spin_unlock(&blkio_list_lock);
1425 } while (1);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001426
Vivek Goyalb1c35762009-12-03 12:59:47 -05001427 rcu_read_unlock();
Tejun Heo7ee9c562012-03-05 13:15:11 -08001428
1429 return 0;
1430}
1431
1432static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1433{
1434 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1435
Ben Blum67523c42010-03-10 15:22:11 -08001436 if (blkcg != &blkio_root_cgroup)
1437 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -05001438}
1439
1440static struct cgroup_subsys_state *
1441blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1442{
Li Zefan03415092010-05-07 08:57:00 +02001443 struct blkio_cgroup *blkcg;
1444 struct cgroup *parent = cgroup->parent;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001445
Li Zefan03415092010-05-07 08:57:00 +02001446 if (!parent) {
Vivek Goyal31e4c282009-12-03 12:59:42 -05001447 blkcg = &blkio_root_cgroup;
1448 goto done;
1449 }
1450
Vivek Goyal31e4c282009-12-03 12:59:42 -05001451 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1452 if (!blkcg)
1453 return ERR_PTR(-ENOMEM);
1454
1455 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1456done:
1457 spin_lock_init(&blkcg->lock);
1458 INIT_HLIST_HEAD(&blkcg->blkg_list);
1459
1460 return &blkcg->css;
1461}
1462
Tejun Heo5efd6112012-03-05 13:15:12 -08001463/**
1464 * blkcg_init_queue - initialize blkcg part of request queue
1465 * @q: request_queue to initialize
1466 *
1467 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1468 * part of new request_queue @q.
1469 *
1470 * RETURNS:
1471 * 0 on success, -errno on failure.
1472 */
1473int blkcg_init_queue(struct request_queue *q)
1474{
1475 might_sleep();
1476
1477 return blk_throtl_init(q);
1478}
1479
1480/**
1481 * blkcg_drain_queue - drain blkcg part of request_queue
1482 * @q: request_queue to drain
1483 *
1484 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1485 */
1486void blkcg_drain_queue(struct request_queue *q)
1487{
1488 lockdep_assert_held(q->queue_lock);
1489
1490 blk_throtl_drain(q);
1491}
1492
1493/**
1494 * blkcg_exit_queue - exit and release blkcg part of request_queue
1495 * @q: request_queue being released
1496 *
1497 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1498 */
1499void blkcg_exit_queue(struct request_queue *q)
1500{
1501 blk_throtl_exit(q);
1502}
1503
Vivek Goyal31e4c282009-12-03 12:59:42 -05001504/*
1505 * We cannot support shared io contexts, as we have no mean to support
1506 * two tasks with the same ioc in two different groups without major rework
1507 * of the main cic data structures. For now we allow a task to change
1508 * its cgroup only if it's the only owner of its ioc.
1509 */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001510static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1511 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001512{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001513 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001514 struct io_context *ioc;
1515 int ret = 0;
1516
1517 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heobb9d97b2011-12-12 18:12:21 -08001518 cgroup_taskset_for_each(task, cgrp, tset) {
1519 task_lock(task);
1520 ioc = task->io_context;
1521 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1522 ret = -EINVAL;
1523 task_unlock(task);
1524 if (ret)
1525 break;
1526 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001527 return ret;
1528}
1529
Tejun Heobb9d97b2011-12-12 18:12:21 -08001530static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1531 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -05001532{
Tejun Heobb9d97b2011-12-12 18:12:21 -08001533 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -05001534 struct io_context *ioc;
1535
Tejun Heobb9d97b2011-12-12 18:12:21 -08001536 cgroup_taskset_for_each(task, cgrp, tset) {
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001537 /* we don't lose anything even if ioc allocation fails */
1538 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
1539 if (ioc) {
1540 ioc_cgroup_changed(ioc);
Tejun Heo11a31222012-02-07 07:51:30 +01001541 put_io_context(ioc);
Linus Torvaldsb3c9dd12012-01-15 12:24:45 -08001542 }
Tejun Heobb9d97b2011-12-12 18:12:21 -08001543 }
Vivek Goyal31e4c282009-12-03 12:59:42 -05001544}
1545
Vivek Goyal3e252062009-12-04 10:36:42 -05001546void blkio_policy_register(struct blkio_policy_type *blkiop)
1547{
1548 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001549
1550 BUG_ON(blkio_policy[blkiop->plid]);
1551 blkio_policy[blkiop->plid] = blkiop;
Vivek Goyal3e252062009-12-04 10:36:42 -05001552 list_add_tail(&blkiop->list, &blkio_list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001553
Vivek Goyal3e252062009-12-04 10:36:42 -05001554 spin_unlock(&blkio_list_lock);
1555}
1556EXPORT_SYMBOL_GPL(blkio_policy_register);
1557
1558void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1559{
1560 spin_lock(&blkio_list_lock);
Tejun Heo035d10b2012-03-05 13:15:04 -08001561
1562 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1563 blkio_policy[blkiop->plid] = NULL;
Vivek Goyal3e252062009-12-04 10:36:42 -05001564 list_del_init(&blkiop->list);
Tejun Heo035d10b2012-03-05 13:15:04 -08001565
Vivek Goyal3e252062009-12-04 10:36:42 -05001566 spin_unlock(&blkio_list_lock);
1567}
1568EXPORT_SYMBOL_GPL(blkio_policy_unregister);